| #!/usr/bin/env python |
| # |
| # Copyright (C) 2022 The Android Open Source Project |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # ibuted under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| """Unit tests for build_file_generator.py""" |
| |
| import unittest |
| import tempfile |
| |
| from build_file_generator import ConfigAxis, \ |
| AndroidBpModule, AndroidBpComment, AndroidBpFile |
| |
| |
| class TestAndroidBpModule(unittest.TestCase): |
| |
| def test_required_properties(self): |
| with self.assertRaises(ValueError): |
| AndroidBpModule(name="", module_type="module_type") |
| with self.assertRaises(ValueError): |
| AndroidBpModule(name="name", module_type="") |
| |
| def test_primitive_properties(self): |
| module = AndroidBpModule(name="libfoo", module_type="cc_api_library") |
| module.add_property(prop="string_property", val="libfoo.map.txt") |
| module.add_property(prop="int_property", val=1) |
| module.add_property(prop="bool_property", val=True) |
| self.assertEqual( |
| module.string(), """cc_api_library { |
| name: "libfoo", |
| string_property: "libfoo.map.txt", |
| int_property: 1, |
| bool_property: true, |
| }""") |
| |
| def test_list_like_properties(self): |
| module = AndroidBpModule(name="libfoo", module_type="cc_api_library") |
| module.add_property(prop="single_element_list", val=["word1"]) |
| module.add_property(prop="multi_element_list", val=["word1", "word2"]) |
| self.assertEqual( |
| module.string(), """cc_api_library { |
| name: "libfoo", |
| single_element_list: ["word1"], |
| multi_element_list: [ |
| "word1", |
| "word2" |
| ], |
| }""") |
| |
| def test_arch_properties(self): |
| module = AndroidBpModule(name="libfoo", module_type="cc_api_library") |
| module.add_property("export_include_dirs", ["include"]) |
| module.add_property("export_include_dirs", ["include_arm"], |
| ConfigAxis.ArmConfig) |
| module.add_property("export_include_dirs", |
| ["include_x86", "include_x86_other"], |
| ConfigAxis.X86Config) |
| self.assertEqual( |
| module.string(), """cc_api_library { |
| name: "libfoo", |
| export_include_dirs: ["include"], |
| arch: { |
| arm: { |
| export_include_dirs: ["include_arm"], |
| }, |
| x86: { |
| export_include_dirs: [ |
| "include_x86", |
| "include_x86_other" |
| ], |
| }, |
| }, |
| }""") |
| |
| def test_extend_properties(self): |
| module = AndroidBpModule(name="libfoo", module_type="cc_api_library") |
| module.extend_property(prop="export_include_dirs", val="dir1") |
| module.extend_property(prop="export_include_dirs", |
| val="dir2") # value will be converted to list |
| module.add_property(prop="export_include_dirs", |
| val=["include_x86"], |
| axis=ConfigAxis.X86Config) |
| self.assertEqual( |
| module.string(), """cc_api_library { |
| name: "libfoo", |
| export_include_dirs: [ |
| "dir1", |
| "dir2" |
| ], |
| arch: { |
| x86: { |
| export_include_dirs: ["include_x86"], |
| }, |
| }, |
| }""") |
| |
| |
| class TestAndroidBpComment(unittest.TestCase): |
| |
| def test_single_line_comment(self): |
| comment = AndroidBpComment("This is a comment") |
| self.assertEqual(comment.string(), "// This is a comment") |
| comment = AndroidBpComment("// Do not double-add comment token") |
| self.assertEqual(comment.string(), |
| "// Do not double-add comment token") |
| |
| def test_line_separator_in_comment(self): |
| comment = AndroidBpComment("Comment string\nMore comment string") |
| self.assertEqual(comment.string(), """// Comment string |
| // More comment string""") |
| |
| |
| class TestAndroidBpFile(unittest.TestCase): |
| |
| def test_additions(self): |
| bp_file = AndroidBpFile("mydir") |
| bp_file.add_comment_string("WARNING: This is autogenerated") |
| bp_file.add_module( |
| AndroidBpModule(name="libfoo", module_type="cc_api_library")) |
| self.assertEqual( |
| bp_file.string(), """// WARNING: This is autogenerated |
| cc_api_library { |
| name: "libfoo", |
| }""") |
| |
| def test_stale_file(self): |
| with tempfile.TemporaryDirectory() as tmpdir: |
| bp_file = AndroidBpFile(tmpdir) |
| bp_file.add_comment_string("This is a comment") |
| # bp_file is stale since it has not been created yet |
| self.assertTrue(bp_file.is_stale()) |
| # Flush contents to Android.bp file |
| bp_file.write() |
| self.assertFalse(bp_file.is_stale()) |
| # Add more comments |
| bp_file.add_comment_string( |
| "This comment should force creation of a new Android.bp") |
| self.assertTrue(bp_file.is_stale()) |
| |
| |
| if __name__ == "__main__": |
| unittest.main() |