xref: /aosp_15_r20/external/pigweed/pw_build/py/gn_target_test.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2023 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""Tests for the pw_build.gn_target module."""
15
16import unittest
17
18from pw_build.gn_target import GnTarget
19
20
21class TestGnTarget(unittest.TestCase):
22    """Tests for gn_target.GnTarget."""
23
24    def test_target_name_and_type(self):
25        """Tests setting and getting the GN target name and type."""
26        target = GnTarget('my_target_type', 'some_target_name')
27        self.assertEqual(target.name(), 'some_target_name')
28        self.assertEqual(target.type(), 'my_target_type')
29
30    def test_target_no_origin(self):
31        target = GnTarget('type', 'name')
32        self.assertEqual(target.origin, None)
33
34    def test_target_no_build_args(self):
35        target = GnTarget('type', 'name')
36        self.assertEqual(list(target.build_args()), [])
37
38    def test_target_with_build_args(self):
39        target = GnTarget('type', 'name')
40        target.attrs = {
41            # build args in paths in these attributes should be included.
42            'public': ['$dir_pw_third_party_foo/include/one-header.h'],
43            'sources': [
44                '$dir_pw_third_party_foo/src/source1.cc',
45                '$dir_pw_third_party_bar/src/source2.cc',
46            ],
47            'inputs': [],
48            'include_dirs': [
49                '$dir_pw_third_party_bar/include/',
50                '$dir_pw_third_party_baz/include/',
51            ],
52            # build args in labels should not be included.
53            'configs': ['$dir_pw_third_party/qux:config'],
54            'deps': ['$dir_pw_third_party/quux:dep'],
55        }
56        build_args = set(target.build_args())
57        self.assertIn('$dir_pw_third_party_foo', build_args)
58        self.assertIn('$dir_pw_third_party_bar', build_args)
59        self.assertIn('$dir_pw_third_party_baz', build_args)
60
61
62if __name__ == '__main__':
63    unittest.main()
64