Jooyung Han | f6fd4c2 | 2023-03-09 14:50:35 +0900 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2023 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
| 17 | """Unit tests for conv_linker_config.py.""" |
| 18 | |
| 19 | import os |
| 20 | import shutil |
| 21 | import tempfile |
| 22 | import unittest |
| 23 | |
| 24 | import conv_linker_config |
| 25 | from linker_config_pb2 import LinkerConfig |
| 26 | |
| 27 | class FileArgs: |
| 28 | def __init__(self, files, sep = ':'): |
| 29 | self.files = files |
| 30 | self.sep = sep |
| 31 | |
| 32 | |
| 33 | class FileArg: |
| 34 | def __init__(self, file): |
| 35 | self.file = file |
| 36 | |
| 37 | |
| 38 | class TempDirTest(unittest.TestCase): |
| 39 | |
| 40 | def setUp(self): |
| 41 | self.tempdir = tempfile.mkdtemp() |
| 42 | |
| 43 | |
| 44 | def tearDown(self): |
| 45 | shutil.rmtree(self.tempdir) |
| 46 | |
| 47 | |
| 48 | def write(self, name, contents): |
| 49 | with open(os.path.join(self.tempdir, name), 'wb') as f: |
| 50 | f.write(contents) |
| 51 | |
| 52 | |
| 53 | def read(self, name): |
| 54 | with open(os.path.join(self.tempdir, name), 'rb') as f: |
| 55 | return f.read() |
| 56 | |
| 57 | |
| 58 | def resolve_paths(self, args): |
| 59 | for i in range(len(args)): |
| 60 | if isinstance(args[i], FileArgs): |
| 61 | args[i] = args[i].sep.join(os.path.join(self.tempdir, f.file) for f in args[i].files) |
| 62 | elif isinstance(args[i], FileArg): |
| 63 | args[i] = os.path.join(self.tempdir, args[i].file) |
| 64 | return args |
| 65 | |
| 66 | |
| 67 | class ConvLinkerConfigTest(TempDirTest): |
| 68 | """Unit tests for conv_linker_config.""" |
| 69 | |
| 70 | def test_Proto_with_multiple_input(self): |
| 71 | self.write('foo.json', b'{ "provideLibs": ["libfoo.so"]}') |
| 72 | self.write('bar.json', b'{ "provideLibs": ["libbar.so"]}') |
| 73 | self.command(['proto', '-s', FileArgs([FileArg('foo.json'), FileArg('bar.json')]), '-o', FileArg('out.pb')]) |
| 74 | pb = LinkerConfig() |
| 75 | pb.ParseFromString(self.read('out.pb')) |
| 76 | self.assertSetEqual(set(pb.provideLibs), set(['libfoo.so', 'libbar.so'])) |
| 77 | |
| 78 | |
| 79 | def command(self, args): |
| 80 | parser = conv_linker_config.GetArgParser() |
| 81 | parsed_args = parser.parse_args(self.resolve_paths(args)) |
| 82 | parsed_args.func(parsed_args) |
| 83 | |
| 84 | |
| 85 | if __name__ == '__main__': |
| 86 | unittest.main(verbosity=2) |