aboutsummaryrefslogtreecommitdiff
path: root/pw_build/py/gn_target_test.py
blob: 204defe971a633e62ab99efa92f77c3a9f48b580 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# Copyright 2023 The Pigweed Authors
#
# 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
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed 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.
"""Tests for the pw_build.gn_target module."""

import unittest

from pw_build.bazel_query import BazelRule
from pw_build.gn_target import GnTarget
from pw_build.gn_utils import GnLabel, MalformedGnError


class TestGnTarget(unittest.TestCase):
    """Tests for gn_target.GnTarget."""

    def setUp(self):
        self.rule = BazelRule('//my-package:my-target', 'cc_library')

    def test_from_bazel_rule_label(self):
        """Tests the GN target name and package derived from a Bazel rule."""
        target = GnTarget('$build', '$src', bazel=self.rule)
        self.assertEqual(target.name(), 'my-target')
        self.assertEqual(target.package(), 'my-package')

    def test_from_bazel_rule_type_source_set(self):
        """Tests creating a `pw_source_set` from a Bazel rule."""
        target = GnTarget('$build', '$src', bazel=self.rule)
        self.assertEqual(target.type(), 'pw_source_set')

    def test_from_bazel_rule_type_static_lib(self):
        """Tests creating a `pw_static_library` from a Bazel rule."""
        self.rule.set_attr('linkstatic', True)
        target = GnTarget('$build', '$src', bazel=self.rule)
        self.assertEqual(target.type(), 'pw_static_library')

    def test_from_bazel_rule_type_invalid(self):
        """Tests creating an invalid type from a Bazel rule."""
        with self.assertRaises(MalformedGnError):
            rule = BazelRule('//my-package:my-target', 'custom_type')
            GnTarget('$build', '$src', bazel=rule)

    def test_from_bazel_rule_visibility(self):
        """Tests getting the GN visibility from a Bazel rule."""
        self.rule.set_attr(
            'visibility',
            [
                '//foo:__subpackages__',
                '//foo/bar:__pkg__',
                '//baz:__pkg__',
            ],
        )
        target = GnTarget('$build', '$src', bazel=self.rule)
        self.assertEqual(
            {str(scope) for scope in target.visibility},
            {
                '$build/my-package:*',
                '$build/foo/*',
                '$build/baz:*',
            },
        )

    def test_from_bazel_rule_visibility_public(self):
        """Tests that 'public' overrides any other visibility"""
        self.rule.set_attr(
            'visibility',
            [
                '//visibility:private',
                '//visibility:public',
            ],
        )
        target = GnTarget('$build', '$src', bazel=self.rule)
        self.assertEqual({str(scope) for scope in target.visibility}, {'//*'})

    def test_from_bazel_rule_visibility_invalid(self):
        """Tests that and invalid visibility raises an error."""
        self.rule.set_attr('visibility', ['invalid'])
        with self.assertRaises(MalformedGnError):
            GnTarget('$build', '$src', bazel=self.rule)

    def test_from_bazel_rule_testonly_unset(self):
        """Tests that omitting `testonly` defaults it to False."""
        target = GnTarget('$build', '$src', bazel=self.rule)
        self.assertFalse(target.testonly)

    def test_from_bazel_rule_testonly_false(self):
        """Tests setting `testonly` to False."""
        self.rule.set_attr('testonly', False)
        target = GnTarget('$build', '$src', bazel=self.rule)
        self.assertFalse(target.testonly)

    def test_from_bazel_rule_testonly_true(self):
        """Tests setting `testonly` to True."""
        self.rule.set_attr('testonly', True)
        target = GnTarget('$build', '$src', bazel=self.rule)
        self.assertTrue(target.testonly)

    def test_from_bazel_rule_public(self):
        """Tests getting the GN public headers from a Bazel rule."""
        self.rule.set_attr('hdrs', ['//foo:bar.h', '//:baz.h'])
        target = GnTarget('$build', '$src', bazel=self.rule)
        self.assertEqual(
            {str(path) for path in target.public},
            {
                '$src/foo/bar.h',
                '$src/baz.h',
            },
        )

    def test_from_bazel_rule_sources(self):
        """Tests getting the GN source files from a Bazel rule."""
        self.rule.set_attr('srcs', ['//foo:bar.cc', '//:baz.cc'])
        target = GnTarget('$build', '$src', bazel=self.rule)
        self.assertEqual(
            {str(path) for path in target.sources},
            {
                '$src/foo/bar.cc',
                '$src/baz.cc',
            },
        )

    def test_from_bazel_rule_inputs(self):
        """Tests getting the GN input files from a Bazel rule."""
        self.rule.set_attr(
            'additional_linker_inputs', ['//foo:bar.data', '//:baz.data']
        )
        target = GnTarget('$build', '$src', bazel=self.rule)
        self.assertEqual(
            {str(path) for path in target.inputs},
            {
                '$src/foo/bar.data',
                '$src/baz.data',
            },
        )

    def test_from_bazel_rule_include_dirs(self):
        """Tests getting the GN include directories from a Bazel rule."""
        self.rule.set_attr('includes', ['//foo'])
        target = GnTarget('$build', '$src', bazel=self.rule)
        self.assertEqual(
            set(target.config.get('include_dirs')),
            {
                '$src',
                '$src/foo',
            },
        )

    def test_from_bazel_rule_configs(self):
        """Tests getting the GN config flags from a Bazel rule."""
        self.rule.set_attr('defines', ['KEY1=VAL1'])
        self.rule.set_attr('copts', ['-frobinator'])
        self.rule.set_attr('linkopts', ['-fizzbuzzer'])
        self.rule.set_attr('local_defines', ['KEY2=VAL2', 'KEY3=VAL3'])
        target = GnTarget('$build', '$src', bazel=self.rule)
        self.assertEqual(
            set(target.config.get('public_defines')), {'KEY1=VAL1'}
        )
        self.assertEqual(set(target.config.get('cflags')), {'-frobinator'})
        self.assertEqual(set(target.config.get('ldflags')), {'-fizzbuzzer'})
        self.assertEqual(
            set(target.config.get('defines')), {'KEY2=VAL2', 'KEY3=VAL3'}
        )

    def test_from_bazel_rule_deps(self):
        """Tests getting the GN dependencies from a Bazel rule."""
        self.rule.set_attr(
            'deps', ['//my-package:foo', '@com_corp_project//bar']
        )
        self.rule.set_attr(
            'implementation_deps',
            [
                '//other-pkg/subdir',
                '@com_corp_project//:top-level',
            ],
        )
        target = GnTarget('$build', '$src', bazel=self.rule)
        self.assertEqual(
            {str(label) for label in target.public_deps},
            {
                '$build/my-package:foo',
                '$repo/bar',
            },
        )
        self.assertEqual(
            {str(label) for label in target.deps},
            {
                '$build/other-pkg/subdir',
                '$repo:top-level',
            },
        )

    def test_make_relative_adjacent(self):
        """Tests rebasing labels for a target."""
        target = GnTarget('$build/pkg', '$src')
        label = target.make_relative(GnLabel('$build/pkg:adjacent-config'))
        self.assertEqual(label, ":adjacent-config")

    def test_make_relative_absolute(self):
        """Tests rebasing labels for a target."""
        target = GnTarget('$build/pkg', '$src')
        label = target.make_relative(GnLabel('//absolute:config'))
        self.assertEqual(label, "//absolute:config")

    def test_make_relative_descend(self):
        """Tests rebasing labels for a target."""
        target = GnTarget('$build/pkg', '$src')
        label = target.make_relative(GnLabel('$build/pkg/relative:dep'))
        self.assertEqual(label, "relative:dep")

    def test_make_relative_ascend(self):
        """Tests rebasing labels for a target."""
        target = GnTarget('$build/pkg', '$src')
        label = target.make_relative(GnLabel('$build/dotdot/relative'))
        self.assertEqual(label, "../dotdot/relative")


if __name__ == '__main__':
    unittest.main()