summaryrefslogtreecommitdiff
path: root/chromium/tools/crates/lib/gen_test.py
blob: cf8e5a0f5c04a2b9b8de781441b2328f454eafe6 (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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
# python3
# Copyright 2021 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from datetime import datetime
import io
import re
import tempfile
import toml
import unittest

from lib import gen
from lib import cargo
from lib import compiler

# An example output from `cargo tree`
# 1. We patch paths into the Cargo.toml files that we feed to `cargo tree` and
#    this demonstrates that for the "quote" crate by including the path there.
# 2. We include features in the output, demonstrated by many deps here. And we
#    also have an example of a crate without any features with the second
#    occurrence of "unicode-xid".
# 3. Some crates get de-duped and that means get a (*) on them, which we can
#    see.
# 4. A proc-macro crate is a different kind of crate and is designated by the
#    (proc-macro) after the version, and we have an example of that with
#    cxxbridge-macro.
# 5. We include direct deps as well as transitive deps, which have more than one
#    piece of ascii art on their line.
CARGO_TREE = """
cxx v1.0.56 (/path/to/chromium/src/third_party/rust/cxx/v1/crate)
├── cxxbridge-macro v1.0.56 (proc-macro)
│   ├── proc-macro2 v1.0.32 default,proc-macro,span-locations
│   │   └── unicode-xid v0.2.2 default
│   ├── quote v1.0.10 (/path/to/chromium/src/third_party/rust/quote/v1/crate) default,proc-macro
│   │   └── proc-macro2 v1.0.32 default,proc-macro,span-locations (*)
│   └── syn v1.0.81 clone-impls,default,derive,full,parsing,printing,proc-macro,quote
│       ├── proc-macro2 v1.0.32 default,proc-macro,span-locations (*)
│       ├── quote v1.0.10 default,proc-macro (*)
│       └── unicode-xid v0.2.2
└── link-cplusplus v1.0.5 default
""".lstrip("\n")


class GenTestCase(unittest.TestCase):
    def assertListSortedEqual(self, a, b, msg=None):
        a.sort()
        b.sort()
        if msg:
            self.assertListEqual(a, b, msg=msg)
        else:
            self.assertListEqual(a, b)

    def matching_archs(self, matching: str) -> set[str]:
        return {
            arch
            for arch in compiler._RUSTC_ARCH_TO_BUILD_CONDITION
            if re.search(matching, arch)
        }

    def all_archs(self):
        return set(compiler._RUSTC_ARCH_TO_BUILD_CONDITION.keys())

    def make_args(self):
        class Args:
            pass

        args = Args
        args.verbose = False
        return args

    def test_parse_cargo_tree_dependency_line(self):
        args = self.make_args()
        lines = CARGO_TREE.split("\n")

        # Here we are simulating `cargo tree` on a third-party Cargo.toml file,
        # not our special third_party.toml file. So we pass False as
        # is_third_party_toml, and we can give an empty parsed toml file as it
        # won't be used then.

        # cxx v1.0.56 (/path/to/chromium/src/third_party/rust/cxx/v1/crate)
        r = gen._parse_cargo_tree_dependency_line(args, dict(), False, lines[0])
        self.assertEqual(r, None)  # not a dependency

        # ├── cxxbridge-macro v1.0.56 (proc-macro)
        r = gen._parse_cargo_tree_dependency_line(args, dict(), False, lines[1])
        expected = gen.CargoTreeDependency(cargo.CrateKey(
            "cxxbridge-macro", "1.0.56"),
                                           full_version="1.0.56")
        self.assertEqual(r, expected)

        # │   ├── proc-macro2 v1.0.32 default,proc-macro,span-locations
        r = gen._parse_cargo_tree_dependency_line(args, dict(), False, lines[2])
        expected = gen.CargoTreeDependency(
            cargo.CrateKey("proc-macro2", "1.0.32"),
            full_version="1.0.32",
            features=["default", "proc-macro", "span-locations"])
        self.assertEqual(r, expected)

        # │   │   └── unicode-xid v0.2.2 default
        r = gen._parse_cargo_tree_dependency_line(args, dict(), False, lines[3])
        expected = gen.CargoTreeDependency(cargo.CrateKey(
            "unicode-xid", "0.2.2"),
                                           full_version="0.2.2",
                                           features=["default"])
        self.assertEqual(r, expected)

        # │   ├── quote v1.0.10 (/path/to/chromium/src/third_party/rust/quote/v1/crate) default,proc-macro
        r = gen._parse_cargo_tree_dependency_line(args, dict(), False, lines[4])
        expected = gen.CargoTreeDependency(
            cargo.CrateKey("quote", "1.0.10"),
            full_version="1.0.10",
            crate_path="/path/to/chromium/src/third_party/rust/quote/v1/crate",
            features=["default", "proc-macro"])
        self.assertEqual(r, expected)

        # │   │   └── proc-macro2 v1.0.32 default,proc-macro,span-locations (*)
        r = gen._parse_cargo_tree_dependency_line(args, dict(), False, lines[5])
        expected = gen.CargoTreeDependency(
            cargo.CrateKey("proc-macro2", "1.0.32"),
            full_version="1.0.32",
            features=["default", "proc-macro", "span-locations"])
        self.assertEqual(r, expected)

        # │   └── syn v1.0.81 clone-impls,default,derive,full,parsing,printing,proc-macro,quote
        r = gen._parse_cargo_tree_dependency_line(args, dict(), False, lines[6])
        expected = gen.CargoTreeDependency(cargo.CrateKey("syn", "1.0.81"),
                                           full_version="1.0.81",
                                           features=[
                                               "clone-impls", "default",
                                               "derive", "full", "parsing",
                                               "printing", "proc-macro", "quote"
                                           ])
        self.assertEqual(r, expected)

        # │       ├── proc-macro2 v1.0.32 default,proc-macro,span-locations (*)
        r = gen._parse_cargo_tree_dependency_line(args, dict(), False, lines[7])
        expected = gen.CargoTreeDependency(
            cargo.CrateKey("proc-macro2", "1.0.32"),
            full_version="1.0.32",
            features=["default", "proc-macro", "span-locations"])
        self.assertEqual(r, expected)

        # │       ├── quote v1.0.10 default,proc-macro (*)
        r = gen._parse_cargo_tree_dependency_line(args, dict(), False, lines[8])
        expected = gen.CargoTreeDependency(cargo.CrateKey("quote", "1.0.10"),
                                           full_version="1.0.10",
                                           features=["default", "proc-macro"])
        self.assertEqual(r, expected)

        # │       └── unicode-xid v0.2.2
        r = gen._parse_cargo_tree_dependency_line(args, dict(), False, lines[9])
        expected = gen.CargoTreeDependency(cargo.CrateKey(
            "unicode-xid", "0.2.2"),
                                           full_version="0.2.2")
        self.assertEqual(r, expected)

        # └── link-cplusplus v1.0.5 default
        r = gen._parse_cargo_tree_dependency_line(args, dict(), False,
                                                  lines[10])
        d = gen.CargoTreeDependency(cargo.CrateKey("link-cplusplus", "1.0.5"),
                                    full_version="1.0.5",
                                    features=["default"])
        self.assertEqual(r, d)

    def test_third_party_toml(self):
        args = self.make_args()
        THIRD_PARTY_TOML = """
# This dependency has no extensions
[dependencies]
cxxbridge-macro = "1"

# This dependency has an extension, and is default-visible to first-party code.
[dependencies.link-cplusplus]
build-script-outputs = [ "src/link.rs", "src/cplusplus.rs" ]

# This dependency is not visible to first-party code thanks to the extension.
[dependencies.syn]
allow-first-party-usage = false
"""

        # Here we are simulating parsing our special third_party.toml file, so
        # we need to present the contents of that file to the
        # _parse_cargo_tree_dependency_line() function, in order for it to look
        # for extensions.
        toml_content = toml.loads(THIRD_PARTY_TOML)

        line = "├── cxxbridge-macro v1.0.56 (proc-macro)"
        r = gen._parse_cargo_tree_dependency_line(args, toml_content, True,
                                                  line)
        d = gen.CargoTreeDependency(
            cargo.CrateKey("cxxbridge-macro", "1.0.56"),
            full_version="1.0.56",
            # Deps from third_party.toml are visible to first-party code by
            # default.
            is_for_first_party_code=True)
        self.assertEqual(r, d)

        line = "├── link-cplusplus v1.0.5 default"
        r = gen._parse_cargo_tree_dependency_line(args, toml_content, True,
                                                  line)
        d = gen.CargoTreeDependency(
            cargo.CrateKey("link-cplusplus", "1.0.5"),
            full_version="1.0.5",
            features=["default"],
            is_for_first_party_code=True,
            # link-cplusplus has build script outputs listed in our
            # third_party.toml.
            build_script_outputs={"src/link.rs", "src/cplusplus.rs"})
        self.assertEqual(r, d)

        line = "└── syn v1.0.81 clone-impls,default"
        r = gen._parse_cargo_tree_dependency_line(args, toml_content, True,
                                                  line)
        d = gen.CargoTreeDependency(
            cargo.CrateKey("syn", "1.0.81"),
            full_version="1.0.81",
            features=["clone-impls", "default"],
            # Deps from third_party.toml are visible to first-party code unless
            # they opt out explicitly, which our syn dependency has done.
            is_for_first_party_code=False)
        self.assertEqual(r, d)

    def test_third_party_toml_dev_deps(self):
        args = self.make_args()
        THIRD_PARTY_TOML = """
[dependencies]
# Nothing. We're testing dev-dependencies here.

[dev-dependencies]
cxxbridge-macro = "1"

# This dependency has an extension, and is default-visible to first-party code.
[dev-dependencies.link-cplusplus]
build-script-outputs = [ "src/link.rs", "src/cplusplus.rs" ]

# This dependency is not visible to first-party code thanks to the extension.
[dev-dependencies.syn]
allow-first-party-usage = false
"""

        # Here we are simulating parsing our special third_party.toml file, so
        # we need to present the contents of that file to the
        # _parse_cargo_tree_dependency_line() function, in order for it to look
        # for extensions.
        toml_content = toml.loads(THIRD_PARTY_TOML)

        line = "├── cxxbridge-macro v1.0.56 (proc-macro)"
        r = gen._parse_cargo_tree_dependency_line(args, toml_content, True,
                                                  line)
        d = gen.CargoTreeDependency(
            cargo.CrateKey("cxxbridge-macro", "1.0.56"),
            full_version="1.0.56",
            # Deps from third_party.toml are visible to first-party code by
            # default.
            is_for_first_party_code=True)
        self.assertEqual(r, d)

        line = "├── link-cplusplus v1.0.5 default"
        r = gen._parse_cargo_tree_dependency_line(args, toml_content, True,
                                                  line)
        d = gen.CargoTreeDependency(
            cargo.CrateKey("link-cplusplus", "1.0.5"),
            full_version="1.0.5",
            features=["default"],
            is_for_first_party_code=True,
            # link-cplusplus has build script outputs listed in our
            # third_party.toml.
            build_script_outputs={"src/link.rs", "src/cplusplus.rs"})
        self.assertEqual(r, d)

        line = "└── syn v1.0.81 clone-impls,default"
        r = gen._parse_cargo_tree_dependency_line(args, toml_content, True,
                                                  line)
        d = gen.CargoTreeDependency(
            cargo.CrateKey("syn", "1.0.81"),
            full_version="1.0.81",
            features=["clone-impls", "default"],
            # Deps from third_party.toml are visible to first-party code unless
            # they opt out explicitly, which our syn dependency has done.
            is_for_first_party_code=False)
        self.assertEqual(r, d)

    def test_get_archs_third_party_toml(self):
        crate_key = None  # For third_party.toml there's none.
        usage_data = None  # For third_party.toml there's none.

        # Test what happens when there is a `target` on a dependency.
        toml_content = toml.loads("""
[dependencies]
all-platform-crate = "1"

[target."cfg(windows)".dependencies]
windows-only-crate = "2"
        """)

        # Test case without a target specified on the command line. Since
        # there's differences per-architecture in the TOML, we will have to test
        # all architectures.
        arch_specific = gen._get_archs_of_interest(toml_content, usage_data,
                                                   None)
        # Not true as there's a arch-specific dependency.
        self.assertFalse(arch_specific.is_single_arch())
        self.assertSetEqual(self.all_archs(), arch_specific.archs_to_test())

        # Test case with a target specified on the command line that matches
        # the target. We only need to test the target the user specified.
        arch_specific = gen._get_archs_of_interest(toml_content, usage_data,
                                                   "x86_64-pc-windows-msvc")
        # We only ever care about one arch, so everything is single-arch.
        self.assertTrue(arch_specific.is_single_arch())
        self.assertSetEqual({"x86_64-pc-windows-msvc"},
                            arch_specific.archs_to_test())

        # Test case with a target specified on the command line that does not
        # match the target. We only need to test the target the user specified,
        # and we're not smart enough to prune it yet..
        arch_specific = gen._get_archs_of_interest(toml_content, usage_data,
                                                   "x86_64-unknown-linux-gnu")
        # We only ever care about one arch, so everything is single-arch.
        self.assertTrue(arch_specific.is_single_arch())
        self.assertSetEqual({"x86_64-unknown-linux-gnu"},
                            arch_specific.archs_to_test())

        # Test what happens when there is no `target` on a dependency.
        toml_content = toml.loads("""
[dependencies]
all-platform-crate = "1"
        """)

        # Test case without a target specified on the command line.
        arch_specific = gen._get_archs_of_interest(toml_content, usage_data,
                                                   None)
        # Is true since we can apply a single architecture's result to other
        # ones.
        self.assertTrue(arch_specific.is_single_arch())
        # We only need to test one architecture and copy its results to the
        # rest.
        self.assertEqual(len(arch_specific.archs_to_test()), 1)

        # We don't care which architecture will be tested, but in the next test
        # case we do, so make sure the arbitrary architecture here isn't doesn't
        # unluckily collide with our next test.
        self.assertFalse({"x86_64-pc-windows-msvc"
                          }.issubset(arch_specific.archs_to_test()))

        # Test case with a target specified on the command line.
        arch_specific = gen._get_archs_of_interest(toml_content, usage_data,
                                                   "x86_64-pc-windows-msvc")
        self.assertTrue(arch_specific.is_single_arch())
        # We should test the architecture specified.
        self.assertSetEqual({"x86_64-pc-windows-msvc"},
                            arch_specific.archs_to_test())

        # Ensure we are OK with a [target] section with no dependencies
        toml_content = toml.loads("""
[dependencies]
all-platform-crate = "1"

[target."cfg(windows)"]
        """)

        arch_specific = gen._get_archs_of_interest(toml_content, usage_data,
                                                   None)
        self.assertTrue(arch_specific.is_single_arch())

    def make_fake_parent(self, child_name: str,
                         archset_where_parent_used: compiler.ArchSet,
                         parent_is_arch_specific: bool,
                         parent_has_arch_specific_deps: bool
                         ) -> gen.ArchSpecific:
        if not parent_has_arch_specific_deps:
            toml_content = toml.loads("""
[dependencies]
{} = "1"
          """.format(child_name))
        else:
            toml_content = toml.loads("""
[target."cfg(windows)".dependencies]
{} = "1"
        """.format(child_name))
        parent_crate_data = gen.PerCrateData()
        parent_crate_data.arch_specific = parent_is_arch_specific
        parent_crate_data.used_on_archs = archset_where_parent_used
        return gen._get_archs_of_interest(toml_content, parent_crate_data, None)

    def make_fake_dep(self, dep_key: cargo.CrateKey,
                      parent_requested_features_of_dep: list[str],
                      dep_for_first_party_code: bool,
                      dep_build_script_outputs: set[str]):
        return gen.CargoTreeDependency(
            dep_key,
            features=parent_requested_features_of_dep,
            is_for_first_party_code=dep_for_first_party_code,
            build_script_outputs=dep_build_script_outputs)

    def add_fake_dependency_edges(self, build_data: gen.BuildData,
                                  new_keys: set[cargo.CrateKey], **kwargs):
        parent_name: str = kwargs["parent_name"]
        parent_arch_specific = self.make_fake_parent(
            kwargs["dep_name"], kwargs["archset_where_parent_used"],
            kwargs["parent_is_arch_specific"],
            kwargs["parent_has_arch_specific_deps"])
        dep_key = cargo.CrateKey(kwargs["dep_name"], "1.2.3")
        dep = self.make_fake_dep(dep_key,
                                 kwargs["parent_requested_features_of_dep"],
                                 kwargs["dep_for_first_party_code"],
                                 kwargs["dep_build_script_outputs"])

        parent_crate_key = cargo.CrateKey(parent_name,
                                          "1.2.3") if parent_name else None

        gen._add_edges_for_dep_on_target_arch(build_data, parent_crate_key,
                                              parent_arch_specific,
                                              kwargs["parent_usage"],
                                              kwargs["parent_output"], dep,
                                              kwargs["computing_arch"],
                                              new_keys)
        return dep_key

    def test_add_edges(self):
        build_data = gen.BuildData()
        new_keys = set()

        for_first_party_code = True
        build_script_outputs = {"src/child-outs.rs"}

        dep_key = self.add_fake_dependency_edges(
            build_data,
            new_keys,
            parent_name=None,  # No parent crate from third_party.toml.
            # Parent used everywhere.
            archset_where_parent_used=compiler.ArchSet.ALL(),
            parent_is_arch_specific=False,
            parent_has_arch_specific_deps=False,
            # computing_arch is chosen arbitrarily since the result will apply
            # to all as parent_has_arch_specific_deps is False
            computing_arch="aarch64-apple-darwin",
            # Parent used for a binary.
            parent_usage=cargo.CrateUsage.FOR_NORMAL,
            # Adding a build-dependency.
            parent_output=cargo.CrateBuildOutput.BUILDRS,
            dep_name="child-name",
            parent_requested_features_of_dep=["feature1", "feature2"],
            dep_for_first_party_code=for_first_party_code,
            dep_build_script_outputs=build_script_outputs)

        # The new crate was added to new_keys
        self.assertSetEqual(new_keys, {dep_key})

        # The child crate should be in the BuildData
        self.assertSetEqual(build_data.for_buildrs.all_crates(), {dep_key})
        self.assertSetEqual(build_data.for_normal.all_crates(), {dep_key})
        self.assertSetEqual(build_data.for_tests.all_crates(), {dep_key})
        # In buildrs usage, (since the parent was building BUILDRS output),
        # the dependency will be used and have data populated.
        with build_data.for_buildrs.per_crate(dep_key) as crate:
            # The requested features will be used on _all_ architectures, even
            # since the ArchSpecific tests one but applies to all.
            features = crate.features.all_archsets()
            self.assertListSortedEqual(features, [
                ("feature1", compiler.ArchSet.ALL()),
                ("feature2", compiler.ArchSet.ALL()),
            ])
            # Similarly, the crate is used everywhere. This should always be a
            # superset of the architectures where features are enabled.
            self.assertEqual(crate.used_on_archs, compiler.ArchSet.ALL())
            # Since the ArchSpecific said the parent is not arch-specific, and
            # there's no arch-specific deps, the dependency will not be arch-
            # specific.
            self.assertFalse(crate.arch_specific)
            self.assertEqual(crate.for_first_party, for_first_party_code)
            self.assertEqual(crate.build_script_outputs, build_script_outputs)
            # There's no edges from the new dependency since we haven't added
            # those.
            for o in cargo.CrateBuildOutput:
                self.assertSetEqual(crate.deps[o].all_deps(),
                                    set(),
                                    msg="For output type {}".format(o))

        # Other usages are not used, and have no features enabled, since the
        # parent crate is only using this dependency for tests so far, as those
        # are the only edges we added.
        with build_data.for_normal.per_crate(dep_key) as crate:
            self.assertListSortedEqual(crate.features.all_archsets(), [])
            self.assertEqual(crate.used_on_archs, compiler.ArchSet.EMPTY())
            self.assertFalse(crate.arch_specific)
            self.assertEqual(crate.for_first_party, for_first_party_code)
            self.assertEqual(crate.build_script_outputs, build_script_outputs)
        with build_data.for_tests.per_crate(dep_key) as crate:
            self.assertListSortedEqual(crate.features.all_archsets(), [])
            self.assertEqual(crate.used_on_archs, compiler.ArchSet.EMPTY())
            self.assertFalse(crate.arch_specific)
            self.assertEqual(crate.for_first_party, for_first_party_code)
            self.assertEqual(crate.build_script_outputs, build_script_outputs)

        # The parent would have edges to the child but this was a dep from the
        # third_party.toml, so there's no parent.

        # Now add a transitive dependency, which will have a parent: the child
        # that we added above. We will only add edges to the grand child on
        # Windows.
        parent_key = dep_key
        for_first_party_code = False
        build_script_outputs = {"src/grand-child-outs.rs"}
        dep_key = self.add_fake_dependency_edges(
            build_data,
            new_keys,
            parent_name="child-name",  # The parent crate of the grand-child.
            # Parent used everywhere.
            archset_where_parent_used=compiler.ArchSet.ALL(),
            parent_is_arch_specific=False,
            parent_has_arch_specific_deps=True,
            # Act as if cargo-tree sees the grand-child on Windows, so we're
            # adding an edge there. Since parent_has_arch_specific_deps is True,
            # the edge won't be added to other architectures automatically.
            computing_arch="x86_64-pc-windows-msvc",
            # Parent was used for buildrs.
            parent_usage=cargo.CrateUsage.FOR_BUILDRS,
            # Adding a build-dependency.
            parent_output=cargo.CrateBuildOutput.BUILDRS,
            dep_name="grand-child-name",
            parent_requested_features_of_dep=["featureA", "featureB"],
            dep_for_first_party_code=False,
            dep_build_script_outputs=build_script_outputs)
        current_archset = compiler.ArchSet(initial={"x86_64-pc-windows-msvc"})

        # The new crate was added to new_keys
        self.assertSetEqual(new_keys, {parent_key, dep_key})

        # The grand-child crate should be in the BuildData
        self.assertSetEqual(build_data.for_buildrs.all_crates(),
                            {parent_key, dep_key})
        self.assertSetEqual(build_data.for_normal.all_crates(),
                            {parent_key, dep_key})
        self.assertSetEqual(build_data.for_tests.all_crates(),
                            {parent_key, dep_key})
        # In buildrs usage, (since its parent was building BUILDRS output),
        # the dependency will be used and have data populated.
        with build_data.for_buildrs.per_crate(dep_key) as crate:
            # The requested features will be used on the architecture we're
            # processing, since they can't be generalized to all architectures.
            features = crate.features.all_archsets()
            self.assertListSortedEqual(features,
                                       [("featureA", current_archset),
                                        ("featureB", current_archset)])
            # Similarly, the crate is used on the architecture we're processing.
            self.assertEqual(crate.used_on_archs, current_archset)
            # Since the dep is in a target rule (parent_has_arch_specific_deps),
            # the crate is considered arch-specific.
            self.assertTrue(crate.arch_specific)
            self.assertEqual(crate.for_first_party, for_first_party_code)
            self.assertEqual(crate.build_script_outputs, build_script_outputs)
            # There's no edges from the new dependency since we haven't added
            # those.
            for o in cargo.CrateBuildOutput:
                self.assertSetEqual(crate.deps[o].all_deps(),
                                    set(),
                                    msg="For output type {}".format(o))

        # Other usages are not used, and have no features enabled, since the
        # parent crate is only using this dependency for tests so far, as those
        # are the only edges we added.
        with build_data.for_normal.per_crate(dep_key) as crate:
            self.assertListSortedEqual(crate.features.all_archsets(), [])
            self.assertEqual(crate.used_on_archs, compiler.ArchSet.EMPTY())
            self.assertFalse(crate.arch_specific)
            self.assertEqual(crate.for_first_party, for_first_party_code)
            self.assertEqual(crate.build_script_outputs, build_script_outputs)
        with build_data.for_tests.per_crate(dep_key) as crate:
            self.assertListSortedEqual(crate.features.all_archsets(), [])
            self.assertEqual(crate.used_on_archs, compiler.ArchSet.EMPTY())
            self.assertFalse(crate.arch_specific)
            self.assertEqual(crate.for_first_party, for_first_party_code)
            self.assertEqual(crate.build_script_outputs, build_script_outputs)

        # The child crate now depends on the grand-child crate on the computed.
        # architecture, only for it's buildrs script.
        with build_data.for_buildrs.per_crate(parent_key) as crate:
            self.assertSetEqual(
                crate.deps[cargo.CrateBuildOutput.BUILDRS].all_deps(),
                {dep_key})
            self.assertSetEqual(
                crate.deps[cargo.CrateBuildOutput.NORMAL].all_deps(), set())
            self.assertSetEqual(
                crate.deps[cargo.CrateBuildOutput.TESTS].all_deps(), set())
        # We haven't considered the parent crate being built into a crate's
        # lib/bin as a normal dependency, or tests as a dev-dependency so
        # there's no edges.
        with build_data.for_normal.per_crate(parent_key) as crate:
            for o in cargo.CrateBuildOutput:
                self.assertSetEqual(crate.deps[o].all_deps(),
                                    set(),
                                    msg="For output type {}".format(o))
        with build_data.for_tests.per_crate(parent_key) as crate:
            for o in cargo.CrateBuildOutput:
                self.assertSetEqual(crate.deps[o].all_deps(),
                                    set(),
                                    msg="For output type {}".format(o))

        new_keys = set()
        # Now we again compute edges between the child and grand-child, for
        # another architecture, and for the child's normal build output. It
        # can use different features of the grand-child there.
        dep_key = self.add_fake_dependency_edges(
            build_data,
            new_keys,
            parent_name="child-name",  # The parent crate of the grand-child.
            # Parent used everywhere.
            archset_where_parent_used=compiler.ArchSet.ALL(),
            parent_is_arch_specific=False,
            parent_has_arch_specific_deps=True,
            # Act as if cargo-tree sees the grand-child on Fuchsia, so we're
            # adding an edge there. Since parent_has_arch_specific_deps is True,
            # the edge won't be added to other architectures automatically.
            computing_arch="x86_64-fuchsia",
            # Parent was used for a lib/bin.
            parent_usage=cargo.CrateUsage.FOR_NORMAL,
            # Adding a build-dependency.
            parent_output=cargo.CrateBuildOutput.BUILDRS,
            dep_name="grand-child-name",
            parent_requested_features_of_dep=["featureB", "featureC"],
            dep_for_first_party_code=False,
            dep_build_script_outputs=build_script_outputs)
        current_archset = compiler.ArchSet(initial={"x86_64-fuchsia"})
        before_archset = compiler.ArchSet(initial={"x86_64-pc-windows-msvc"})
        union_archset = compiler.ArchSet(
            initial={"x86_64-pc-windows-msvc", "x86_64-fuchsia"})

        # The grand-child is being used on a new platform, and is arch-specific,
        # so it will need to compute its deps again.
        self.assertSetEqual(new_keys, {dep_key})

        # In buildrs usage, (since its parent was building BUILDRS output),
        # the dependency will be used and have data populated.
        with build_data.for_buildrs.per_crate(dep_key) as crate:
            # The requested features will be used on the architecture we're
            # processing, since they can't be generalized to all architectures.
            features = crate.features.all_archsets()
            self.assertListSortedEqual(features, [
                ("featureA", before_archset),
                ("featureB", union_archset),
                ("featureC", current_archset),
            ])
            # The crate is used on the union architectures.
            self.assertEqual(crate.used_on_archs, union_archset)
            # There's no edges from the new dependency since we haven't added
            # those.
            for o in cargo.CrateBuildOutput:
                self.assertSetEqual(crate.deps[o].all_deps(),
                                    set(),
                                    msg="For output type {}".format(o))

        # Other usages are not used, and have no features enabled, since the
        # parent crate is only using this dependency for tests so far, as those
        # are the only edges we added.
        with build_data.for_normal.per_crate(dep_key) as crate:
            self.assertListSortedEqual(crate.features.all_archsets(), [])
            self.assertEqual(crate.used_on_archs, compiler.ArchSet.EMPTY())
            self.assertFalse(crate.arch_specific)
            self.assertEqual(crate.for_first_party, for_first_party_code)
            self.assertEqual(crate.build_script_outputs, build_script_outputs)
        with build_data.for_tests.per_crate(dep_key) as crate:
            self.assertListSortedEqual(crate.features.all_archsets(), [])
            self.assertEqual(crate.used_on_archs, compiler.ArchSet.EMPTY())
            self.assertFalse(crate.arch_specific)
            self.assertEqual(crate.for_first_party, for_first_party_code)
            self.assertEqual(crate.build_script_outputs, build_script_outputs)

        # The child crate still depends on the grand-child crate for its buildrs
        # script when building built as a build-dependency.
        with build_data.for_buildrs.per_crate(parent_key) as crate:
            self.assertSetEqual(
                crate.deps[cargo.CrateBuildOutput.BUILDRS].all_deps(),
                {dep_key})
            self.assertSetEqual(
                crate.deps[cargo.CrateBuildOutput.NORMAL].all_deps(), set())
            self.assertSetEqual(
                crate.deps[cargo.CrateBuildOutput.TESTS].all_deps(), set())
        # The child crate now _also_ depends on the grand-child crate for its
        # buildrs script when building built as a normal dependency.
        with build_data.for_normal.per_crate(parent_key) as crate:
            self.assertSetEqual(
                crate.deps[cargo.CrateBuildOutput.BUILDRS].all_deps(),
                {dep_key})
            self.assertSetEqual(
                crate.deps[cargo.CrateBuildOutput.NORMAL].all_deps(), set())
            self.assertSetEqual(
                crate.deps[cargo.CrateBuildOutput.TESTS].all_deps(), set())
        # We haven't considered the parent crate being built into a crate's
        # tests as a dev-dependency so there's no edges.
        with build_data.for_tests.per_crate(parent_key) as crate:
            for o in cargo.CrateBuildOutput:
                self.assertSetEqual(crate.deps[o].all_deps(),
                                    set(),
                                    msg="For output type {}".format(o))

    def test_copyright_year(self):
        modern = b"# Copyright 2001 The Chromium Authors"
        with tempfile.NamedTemporaryFile() as f:
            f.write(modern)
            f.flush()
            self.assertEqual(gen._get_copyright_year(f.name), "2001")

        ancient = b"# Copyright (c) 2001 The Chromium Authors. All rights reserved."
        with tempfile.NamedTemporaryFile() as f:
            f.write(ancient)
            f.flush()
            self.assertEqual(gen._get_copyright_year(f.name), "2001")

        self.assertEqual(gen._get_copyright_year("/file/does/not/exist"),
                         str(datetime.now().year))