Add tests for conv_linker_config

to cover the recent features.
- conv_linker_config proto with empty input
- conv_linker_config proto with existing output
- conv_linker_config proto with --append
- conv_linker_config proto with --force

Bug: n/a
Test: conv_linker_config_test
Change-Id: I0de79b6e05c2608e0e2f30dfbf04d8289672f362
This commit is contained in:
Jooyung Han 2023-03-09 14:54:31 +09:00
parent f6fd4c2823
commit fba49bbf56

View file

@ -16,12 +16,14 @@
# #
"""Unit tests for conv_linker_config.py.""" """Unit tests for conv_linker_config.py."""
import io
import os import os
import shutil import shutil
import tempfile import tempfile
import unittest import unittest
import conv_linker_config import conv_linker_config
from contextlib import redirect_stderr
from linker_config_pb2 import LinkerConfig from linker_config_pb2 import LinkerConfig
class FileArgs: class FileArgs:
@ -67,6 +69,22 @@ class TempDirTest(unittest.TestCase):
class ConvLinkerConfigTest(TempDirTest): class ConvLinkerConfigTest(TempDirTest):
"""Unit tests for conv_linker_config.""" """Unit tests for conv_linker_config."""
def test_Proto_empty_input(self):
self.command(['proto', '-s', '-o', FileArg('out.pb')])
pb = LinkerConfig()
pb.ParseFromString(self.read('out.pb'))
self.assertEqual(pb, LinkerConfig())
def test_Proto_single_input(self):
self.write('foo.json', b'{ "provideLibs": ["libfoo.so"]}')
self.command(['proto', '-s', FileArg('foo.json'), '-o', FileArg('out.pb')])
pb = LinkerConfig()
pb.ParseFromString(self.read('out.pb'))
self.assertSequenceEqual(pb.provideLibs, ['libfoo.so'])
def test_Proto_with_multiple_input(self): def test_Proto_with_multiple_input(self):
self.write('foo.json', b'{ "provideLibs": ["libfoo.so"]}') self.write('foo.json', b'{ "provideLibs": ["libfoo.so"]}')
self.write('bar.json', b'{ "provideLibs": ["libbar.so"]}') self.write('bar.json', b'{ "provideLibs": ["libbar.so"]}')
@ -76,6 +94,34 @@ class ConvLinkerConfigTest(TempDirTest):
self.assertSetEqual(set(pb.provideLibs), set(['libfoo.so', 'libbar.so'])) self.assertSetEqual(set(pb.provideLibs), set(['libfoo.so', 'libbar.so']))
def test_Proto_with_existing_output(self):
self.write('out.pb', LinkerConfig(provideLibs=['libfoo.so']).SerializeToString())
buf = io.StringIO()
with self.assertRaises(SystemExit) as err:
with redirect_stderr(buf):
self.command(['proto', '-o', FileArg('out.pb')])
self.assertEqual(err.exception.code, 1)
self.assertRegex(buf.getvalue(), r'.*out\.pb exists')
def test_Proto_with_append(self):
self.write('out.pb', LinkerConfig(provideLibs=['libfoo.so']).SerializeToString())
self.write('bar.json', b'{ "provideLibs": ["libbar.so"]}')
self.command(['proto', '-s', FileArg('bar.json'), '-o', FileArg('out.pb'), '-a'])
pb = LinkerConfig()
pb.ParseFromString(self.read('out.pb'))
self.assertSetEqual(set(pb.provideLibs), set(['libfoo.so', 'libbar.so']))
def test_Proto_with_force(self):
self.write('out.pb', LinkerConfig(provideLibs=['libfoo.so']).SerializeToString())
self.write('bar.json', b'{ "provideLibs": ["libbar.so"]}')
self.command(['proto', '-s', FileArg('bar.json'), '-o', FileArg('out.pb'), '-f'])
pb = LinkerConfig()
pb.ParseFromString(self.read('out.pb'))
self.assertSetEqual(set(pb.provideLibs), set(['libbar.so']))
def command(self, args): def command(self, args):
parser = conv_linker_config.GetArgParser() parser = conv_linker_config.GetArgParser()
parsed_args = parser.parse_args(self.resolve_paths(args)) parsed_args = parser.parse_args(self.resolve_paths(args))