conv_linker_config_test.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. import io
  19. import os
  20. import shutil
  21. import tempfile
  22. import unittest
  23. import conv_linker_config
  24. from contextlib import redirect_stderr
  25. from linker_config_pb2 import LinkerConfig
  26. class FileArgs:
  27. def __init__(self, files, sep = ':'):
  28. self.files = files
  29. self.sep = sep
  30. class FileArg:
  31. def __init__(self, file):
  32. self.file = file
  33. class TempDirTest(unittest.TestCase):
  34. def setUp(self):
  35. self.tempdir = tempfile.mkdtemp()
  36. def tearDown(self):
  37. shutil.rmtree(self.tempdir)
  38. def write(self, name, contents):
  39. with open(os.path.join(self.tempdir, name), 'wb') as f:
  40. f.write(contents)
  41. def read(self, name):
  42. with open(os.path.join(self.tempdir, name), 'rb') as f:
  43. return f.read()
  44. def resolve_paths(self, args):
  45. for i in range(len(args)):
  46. if isinstance(args[i], FileArgs):
  47. args[i] = args[i].sep.join(os.path.join(self.tempdir, f.file) for f in args[i].files)
  48. elif isinstance(args[i], FileArg):
  49. args[i] = os.path.join(self.tempdir, args[i].file)
  50. return args
  51. class ConvLinkerConfigTest(TempDirTest):
  52. """Unit tests for conv_linker_config."""
  53. def test_Proto_empty_input(self):
  54. self.command(['proto', '-s', '-o', FileArg('out.pb')])
  55. pb = LinkerConfig()
  56. pb.ParseFromString(self.read('out.pb'))
  57. self.assertEqual(pb, LinkerConfig())
  58. def test_Proto_single_input(self):
  59. self.write('foo.json', b'{ "provideLibs": ["libfoo.so"]}')
  60. self.command(['proto', '-s', FileArg('foo.json'), '-o', FileArg('out.pb')])
  61. pb = LinkerConfig()
  62. pb.ParseFromString(self.read('out.pb'))
  63. self.assertSequenceEqual(pb.provideLibs, ['libfoo.so'])
  64. def test_Proto_with_multiple_input(self):
  65. self.write('foo.json', b'{ "provideLibs": ["libfoo.so"]}')
  66. self.write('bar.json', b'{ "provideLibs": ["libbar.so"]}')
  67. self.command(['proto', '-s', FileArgs([FileArg('foo.json'), FileArg('bar.json')]), '-o', FileArg('out.pb')])
  68. pb = LinkerConfig()
  69. pb.ParseFromString(self.read('out.pb'))
  70. self.assertSetEqual(set(pb.provideLibs), set(['libfoo.so', 'libbar.so']))
  71. def test_Proto_with_existing_output(self):
  72. self.write('out.pb', LinkerConfig(provideLibs=['libfoo.so']).SerializeToString())
  73. buf = io.StringIO()
  74. with self.assertRaises(SystemExit) as err:
  75. with redirect_stderr(buf):
  76. self.command(['proto', '-o', FileArg('out.pb')])
  77. self.assertEqual(err.exception.code, 1)
  78. self.assertRegex(buf.getvalue(), r'.*out\.pb exists')
  79. def test_Proto_with_append(self):
  80. self.write('out.pb', LinkerConfig(provideLibs=['libfoo.so']).SerializeToString())
  81. self.write('bar.json', b'{ "provideLibs": ["libbar.so"]}')
  82. self.command(['proto', '-s', FileArg('bar.json'), '-o', FileArg('out.pb'), '-a'])
  83. pb = LinkerConfig()
  84. pb.ParseFromString(self.read('out.pb'))
  85. self.assertSetEqual(set(pb.provideLibs), set(['libfoo.so', 'libbar.so']))
  86. def test_Proto_with_force(self):
  87. self.write('out.pb', LinkerConfig(provideLibs=['libfoo.so']).SerializeToString())
  88. self.write('bar.json', b'{ "provideLibs": ["libbar.so"]}')
  89. self.command(['proto', '-s', FileArg('bar.json'), '-o', FileArg('out.pb'), '-f'])
  90. pb = LinkerConfig()
  91. pb.ParseFromString(self.read('out.pb'))
  92. self.assertSetEqual(set(pb.provideLibs), set(['libbar.so']))
  93. def command(self, args):
  94. parser = conv_linker_config.GetArgParser()
  95. parsed_args = parser.parse_args(self.resolve_paths(args))
  96. parsed_args.func(parsed_args)
  97. if __name__ == '__main__':
  98. unittest.main(verbosity=2)