aboutsummaryrefslogtreecommitdiff
path: root/pw_build/py/gn_utils_test.py
blob: ee7f599799a8165950567e133fd9d635b014610f (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# 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_utils module."""

import unittest

from pw_build.gn_utils import GnLabel, GnPath, GnVisibility


class TestGnPath(unittest.TestCase):
    """Tests for gn_utils.GnPath."""

    def test_from_bazel(self):
        """Tests creating a GN path from a Bazel string."""
        path = GnPath('$dir_3p_test', bazel='//foo:bar/baz.txt')
        self.assertEqual(path.file(), 'baz.txt')
        self.assertEqual(path.name(), 'baz')
        self.assertEqual(path.extension(), 'txt')
        self.assertEqual(path.dir(), '$dir_3p_test/foo/bar')

    def test_from_gn(self):
        """Tests creating a GN path from a GN string."""
        path = GnPath('$dir_3p_test', gn='foo/bar/baz.txt')
        self.assertEqual(path.file(), 'baz.txt')
        self.assertEqual(path.name(), 'baz')
        self.assertEqual(path.extension(), 'txt')
        self.assertEqual(path.dir(), '$dir_3p_test/foo/bar')

    def test_from_str(self):
        """Tests creating a GN path from a raw string."""
        path = GnPath('$dir_3p_test/foo/bar/baz.txt')
        self.assertEqual(path.file(), 'baz.txt')
        self.assertEqual(path.name(), 'baz')
        self.assertEqual(path.extension(), 'txt')
        self.assertEqual(path.dir(), '$dir_3p_test/foo/bar')


class TestGnLabel(unittest.TestCase):
    """Tests for gn_utils.GnLabel."""

    def test_from_bazel_with_name(self):
        """Tests creating a GN label from a Bazel string including a name."""
        label = GnLabel('$dir_3p/test', bazel='//foo/bar:baz')
        self.assertEqual(label.name(), 'baz')
        self.assertEqual(label.dir(), '$dir_3p/test/foo/bar')
        self.assertEqual(label.no_toolchain(), '$dir_3p/test/foo/bar:baz')
        self.assertEqual(
            label.with_toolchain(),
            '$dir_3p/test/foo/bar:baz(default_toolchain)',
        )
        self.assertFalse(label.repo())

    def test_from_bazel_without_name(self):
        """Tests creating a GN label from a Bazel string without a name."""
        label = GnLabel('$dir_3p/test', bazel='//foo/bar')
        self.assertEqual(label.name(), 'bar')
        self.assertEqual(label.dir(), '$dir_3p/test/foo/bar')
        self.assertEqual(label.no_toolchain(), '$dir_3p/test/foo/bar')
        self.assertEqual(
            label.with_toolchain(), '$dir_3p/test/foo/bar(default_toolchain)'
        )
        self.assertFalse(label.repo())

    def test_from_bazel_with_external_repo(self):
        """Tests creating a GN label from a Bazel string with a repo."""
        label = GnLabel('$dir_3p/test', bazel='@com_corp_project//foo/bar:baz')
        self.assertEqual(label.name(), 'baz')
        self.assertEqual(label.dir(), '$repo/foo/bar')
        self.assertEqual(label.no_toolchain(), '$repo/foo/bar:baz')
        self.assertEqual(
            label.with_toolchain(),
            '$repo/foo/bar:baz(default_toolchain)',
        )
        self.assertEqual(label.repo(), 'com_corp_project')

    def test_from_gn_absolute(self):
        """Tests creating a GN label from an absolute GN label string."""
        label = GnLabel('$dir_3p/test', gn='//foo/bar:baz')
        self.assertEqual(label.name(), 'baz')
        self.assertEqual(label.dir(), '//foo/bar')
        self.assertEqual(label.no_toolchain(), '//foo/bar:baz')
        self.assertEqual(
            label.with_toolchain(), '//foo/bar:baz(default_toolchain)'
        )

    def test_from_gn_with_variable(self):
        """Tests creating a GN label from a GN string with a variable."""
        label = GnLabel('$dir_3p/test', gn='$dir_pw_build/foo/bar')
        self.assertEqual(label.name(), 'bar')
        self.assertEqual(label.dir(), '$dir_pw_build/foo/bar')
        self.assertEqual(label.no_toolchain(), '$dir_pw_build/foo/bar')
        self.assertEqual(
            label.with_toolchain(), '$dir_pw_build/foo/bar(default_toolchain)'
        )

    def test_from_gn_relative(self):
        """Tests creating a GN label from a relative GN label string."""
        label = GnLabel('$dir_3p/test', gn='foo/bar')
        self.assertEqual(label.name(), 'bar')
        self.assertEqual(label.dir(), '$dir_3p/test/foo/bar')
        self.assertEqual(label.no_toolchain(), '$dir_3p/test/foo/bar')
        self.assertEqual(
            label.with_toolchain(), '$dir_3p/test/foo/bar(default_toolchain)'
        )

    def test_from_gn_with_dotdot(self):
        """Tests creating a GN label from a GN string that ascends the tree."""
        label = GnLabel('$dir_3p/test', gn='../../../foo/../bar')
        self.assertEqual(label.name(), 'bar')
        self.assertEqual(label.dir(), '../bar')
        self.assertEqual(label.no_toolchain(), '../bar')
        self.assertEqual(label.with_toolchain(), '../bar(default_toolchain)')

    def test_from_str_with_name(self):
        """Tests creating a GN label from a raw string with a target name."""
        label = GnLabel('$dir_3p/test/foo/bar:baz')
        self.assertEqual(label.name(), 'baz')
        self.assertEqual(label.dir(), '$dir_3p/test/foo/bar')
        self.assertEqual(label.no_toolchain(), '$dir_3p/test/foo/bar:baz')
        self.assertEqual(
            label.with_toolchain(),
            '$dir_3p/test/foo/bar:baz(default_toolchain)',
        )

    def test_from_str_without_name(self):
        """Tests creating a GN label from a raw string without a name."""
        label = GnLabel('$dir_3p/test/foo/bar')
        self.assertEqual(label.name(), 'bar')
        self.assertEqual(label.dir(), '$dir_3p/test/foo/bar')
        self.assertEqual(label.no_toolchain(), '$dir_3p/test/foo/bar')
        self.assertEqual(
            label.with_toolchain(), '$dir_3p/test/foo/bar(default_toolchain)'
        )

    def test_relative_to_with_name(self):
        """Tests creating a relative label from a GN label with a name."""
        label = GnLabel('$dir_3p/foo/bar:baz')
        self.assertEqual(label.relative_to('$dir_3p'), 'foo/bar:baz')
        self.assertEqual(label.relative_to('$dir_3p/foo'), 'bar:baz')
        self.assertEqual(label.relative_to('$dir_3p/foo/bar'), ':baz')
        self.assertEqual(label.relative_to('$dir_3p/bar'), '../foo/bar:baz')
        self.assertEqual(label.relative_to('//'), '$dir_3p/foo/bar:baz')
        self.assertEqual(label.relative_to('$other'), '$dir_3p/foo/bar:baz')

    def test_relative_to_without_name(self):
        """Tests creating a relative label from a GN label without a name."""
        label = GnLabel('$dir_3p/foo/bar')
        self.assertEqual(label.relative_to('$dir_3p/foo/bar'), ':bar')

    def test_relative_to_absolute_with_name(self):
        """Tests creating a relative label from a named absolute GN label."""
        label = GnLabel('//foo/bar:baz')
        self.assertEqual(label.relative_to('//'), 'foo/bar:baz')
        self.assertEqual(label.relative_to('//bar/baz'), '../../foo/bar:baz')

    def test_relative_to_absolute_without_name(self):
        """Tests creating a relative label from an unnamed absolute GN label."""
        label = GnLabel('//foo/bar')
        self.assertEqual(label.relative_to('//foo/bar'), ':bar')

    def test_resolve_repo_without_repo(self):
        """Tests trying to set the repo placeholder for a local label."""
        label = GnLabel('$dir_3p/test', bazel='//foo/bar:baz')
        self.assertEqual(str(label), '$dir_3p/test/foo/bar:baz')
        self.assertFalse(label.repo())
        label.resolve_repo('my-external_repo')
        self.assertEqual(str(label), '$dir_3p/test/foo/bar:baz')
        self.assertFalse(label.repo())

    def test_resolve_repowith_repo(self):
        """Tests setting the repo placeholder."""
        label = GnLabel('$dir_3p/test', bazel='@com_corp_project//foo/bar:baz')
        self.assertEqual(str(label), '$repo/foo/bar:baz')
        self.assertEqual(label.repo(), 'com_corp_project')
        label.resolve_repo('my-external_repo')
        self.assertEqual(
            str(label), '$dir_pw_third_party/my-external_repo/foo/bar:baz'
        )
        self.assertEqual(label.repo(), 'com_corp_project')


class TestGnVisibility(unittest.TestCase):
    """Tests for gn_utils.GnVisibility."""

    def test_from_bazel_public(self):
        """Tests creating a public visibility scope from a Bazel string."""
        scope = GnVisibility(
            '$dir_3p/test', '$dir_3p/test/foo', bazel='//visibility:public'
        )
        self.assertEqual(str(scope), '//*')

    def test_from_bazel_private(self):
        """Tests creating a private visibility scope from a Bazel string."""
        scope = GnVisibility(
            '$dir_3p/test', '$dir_3p/test/foo', bazel='//visibility:private'
        )
        self.assertEqual(str(scope), '$dir_3p/test/foo:*')

    def test_from_bazel_same_subpackage(self):
        """Tests creating a visibility for the same subpackage."""
        scope = GnVisibility(
            '$dir_3p/test', '$dir_3p/test/foo', bazel='//foo:__subpackages__'
        )
        self.assertEqual(str(scope), '$dir_3p/test/foo/*')

    def test_from_bazel_same_package(self):
        """Tests creating a visibility for the same package."""
        scope = GnVisibility(
            '$dir_3p/test', '$dir_3p/test/foo', bazel='//foo:__pkg__'
        )
        self.assertEqual(str(scope), '$dir_3p/test/foo:*')

    def test_from_bazel_other_subpackages(self):
        """Tests creating a visibility for a different subpackage."""
        scope = GnVisibility(
            '$dir_3p/test', '$dir_3p/test/foo', bazel='//bar:__subpackages__'
        )
        self.assertEqual(str(scope), '$dir_3p/test/bar/*')

    def test_from_bazel_other_package(self):
        """Tests creating a visibility for a different package."""
        scope = GnVisibility(
            '$dir_3p/test', '$dir_3p/test/foo', bazel='//bar:__pkg__'
        )
        self.assertEqual(str(scope), '$dir_3p/test/bar:*')

    def test_from_gn_relative(self):
        """Tests creating a visibility from a relative GN string."""
        scope = GnVisibility('$dir_3p/test', '$dir_3p/test/foo', gn=':*')
        self.assertEqual(str(scope), '$dir_3p/test/foo:*')

    def test_from_gn_with_dotdot(self):
        """Tests creating a visibility from a string that ascends the tree."""
        scope = GnVisibility('$dir_3p/test', '$dir_3p/test/foo', gn='../*')
        self.assertEqual(str(scope), '$dir_3p/test/*')

    def test_from_gn_absolute(self):
        """Tests creating a visibility from an absolute GN string."""
        scope = GnVisibility('$dir_3p/test', '$dir_3p/test/foo', gn='$dir_3p/*')
        self.assertEqual(str(scope), '$dir_3p/*')

    def test_from_str(self):
        """Tests creating a visibility from a raw string."""
        scope = GnVisibility('$dir_3p/test', '$dir_3p/test/foo/bar:*')
        self.assertEqual(str(scope), '$dir_3p/test/foo/bar:*')

    def test_within_equal_discrete(self):
        """Tests that two equal, discrete visibilities are within each other."""
        scope1 = GnVisibility('$dir_3p/test', '$dir_3p/test/foo/bar:baz')
        scope2 = GnVisibility('$dir_3p/test', '$dir_3p/test/foo/bar:baz')
        self.assertTrue(scope1.within(scope2))
        self.assertTrue(scope2.within(scope1))

    def test_within_equal_globbed(self):
        """Tests that two equal, globbed visibilities are within each other."""
        scope1 = GnVisibility('$dir_3p/test', '$dir_3p/test/foo/bar:*')
        scope2 = GnVisibility('$dir_3p/test', '$dir_3p/test/foo/bar:*')
        self.assertTrue(scope1.within(scope2))
        self.assertTrue(scope2.within(scope1))

    def test_within_equal_subtree(self):
        """Tests that two equal, subtree visibilities are within each other."""
        scope1 = GnVisibility('$dir_3p/test', '$dir_3p/test/foo/bar/*')
        scope2 = GnVisibility('$dir_3p/test', '$dir_3p/test/foo/bar/*')
        self.assertTrue(scope1.within(scope2))
        self.assertTrue(scope2.within(scope1))

    def test_within_not_equal_both_discrete(self):
        """Tests that two unrelated visibilities are not within each other."""
        scope1 = GnVisibility('$dir_3p/test', '$dir_3p/test/foo/bar:baz')
        scope2 = GnVisibility('$dir_3p/test', '$dir_3p/test/foo/bar:qux')
        self.assertFalse(scope1.within(scope2))
        self.assertFalse(scope2.within(scope1))

    def test_within_not_equal_both_globbed(self):
        """Tests that two unrelated visibilities are not within each other."""
        scope1 = GnVisibility('$dir_3p/test', '$dir_3p/test/foo/bar:*')
        scope2 = GnVisibility('$dir_3p/test', '$dir_3p/test/foo/baz:*')
        self.assertFalse(scope1.within(scope2))
        self.assertFalse(scope2.within(scope1))

    def test_within_not_equal_both_subtree(self):
        """Tests that two unrelated visibilities are not within each other."""
        scope1 = GnVisibility('$dir_3p/test', '$dir_3p/test/foo/bar/*')
        scope2 = GnVisibility('$dir_3p/test', '$dir_3p/test/foo/baz/*')
        self.assertFalse(scope1.within(scope2))
        self.assertFalse(scope2.within(scope1))

    def test_within_subset_discrete_in_globbed(self):
        """Tests a discrete visibility that is within a globbed one."""
        scope1 = GnVisibility('$dir_3p/test', '$dir_3p/test/foo/bar:baz')
        scope2 = GnVisibility('$dir_3p/test', '$dir_3p/test/foo/bar:*')
        self.assertTrue(scope1.within(scope2))
        self.assertFalse(scope2.within(scope1))

    def test_within_subset_discrete_in_subtree(self):
        """Tests a discrete visibility that is within a subtree."""
        scope1 = GnVisibility('$dir_3p/test', '$dir_3p/test/foo/bar:baz')
        scope2 = GnVisibility('$dir_3p/test', '$dir_3p/test/foo/*')
        public_scope = GnVisibility('$dir_3p/test', '//*')
        self.assertTrue(scope1.within(scope2))
        self.assertTrue(scope1.within(public_scope))
        self.assertFalse(scope2.within(scope1))
        self.assertFalse(public_scope.within(scope1))

    def test_within_subset_globbed_in_subtree(self):
        """Tests a globbed visibility that is within a subtree."""
        scope1 = GnVisibility('$dir_3p/test', '$dir_3p/test/foo/bar:*')
        scope2 = GnVisibility('$dir_3p/test', '$dir_3p/test/foo/*')
        public_scope = GnVisibility('$dir_3p/test', '//*')
        self.assertTrue(scope1.within(scope2))
        self.assertTrue(scope1.within(public_scope))
        self.assertFalse(scope2.within(scope1))
        self.assertFalse(public_scope.within(scope1))

    def test_within_subset_subtree_in_subtree(self):
        """Tests a subtree visibility that is within a subtree."""
        scope1 = GnVisibility('$dir_3p/test', '$dir_3p/test/foo/bar/*')
        scope2 = GnVisibility('$dir_3p/test', '$dir_3p/test/foo/*')
        public_scope = GnVisibility('$dir_3p/test', '//*')
        self.assertTrue(scope1.within(scope2))
        self.assertTrue(scope1.within(public_scope))
        self.assertFalse(scope2.within(scope1))
        self.assertFalse(public_scope.within(scope1))

    def test_within_disjoint(self):
        """Tests that disjoint visibilities are not within each other."""
        scope1 = GnVisibility('$dir_3p/test', '$dir_3p/test/foo:bar')
        scope2 = GnVisibility('$dir_3p/test', '$dir_3p/test/foo/baz:*')
        scope3 = GnVisibility('$dir_3p/test', '$dir_3p/test/foo/qux/*')
        self.assertFalse(scope1.within(scope2))
        self.assertFalse(scope1.within(scope3))
        self.assertFalse(scope2.within(scope1))
        self.assertFalse(scope2.within(scope3))
        self.assertFalse(scope3.within(scope1))
        self.assertFalse(scope3.within(scope2))


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