determinator 0.12.0

Figure out which packages changed between two commits to a workspace.
Documentation
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
// Copyright (c) The cargo-guppy Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Higher-level unit tests for the target determinator.

use cfg_if::cfg_if;
use determinator::{
    rules::{DeterminatorRules, PathMatch, RuleIndex},
    Determinator, Utf8Paths0,
};
use fixtures::json::JsonFixture;
use guppy::{graph::feature::StandardFeatures, CargoMetadata};

#[test]
fn guppy_no_rules() {
    // There are no dependency changes between the old and new fixtures, only file changes.
    let old = JsonFixture::metadata_guppy_869476c();
    let new = JsonFixture::metadata_guppy_c9b4f76();

    let mut determinator = Determinator::new(old.graph(), new.graph());
    // Do not set custom rules -- ensure that default rules are used.

    // README.md is ignored by the default rules.
    determinator.add_changed_paths(vec!["README.md"]);
    let determinator_set = determinator.compute();
    assert!(
        determinator_set.path_changed_set.is_empty(),
        "nothing in workspace changed"
    );
    assert!(
        determinator_set.affected_set.is_empty(),
        "nothing in workspace affected"
    );

    // rust-toolchain causes a full build.
    determinator.add_changed_paths(vec!["rust-toolchain"]);
    let workspace_set = new.graph().resolve_workspace();
    let determinator_set = determinator.compute();
    assert_eq!(
        determinator_set.path_changed_set, workspace_set,
        "everything changed"
    );
    assert_eq!(
        determinator_set.affected_set, workspace_set,
        "everything changed"
    );
}
#[test]
fn guppy_path_rules() {
    // There are no dependency changes between the old and new fixtures, only file changes.
    let old = JsonFixture::metadata_guppy_869476c();
    let new = JsonFixture::metadata_guppy_c9b4f76();
    let opts = read_options(new, "path-rules.toml");

    let mut determinator = Determinator::new(old.graph(), new.graph());
    determinator.set_rules(&opts).expect("rules set correctly");

    let determinator_set = determinator.compute();
    assert!(
        determinator_set.path_changed_set.is_empty(),
        "nothing in workspace changed"
    );
    assert!(
        determinator_set.affected_set.is_empty(),
        "nothing in workspace affected"
    );

    // Try adding some files -- this isn't matched by any rule.
    determinator.add_changed_paths(vec!["fixtures/src/details.rs"]);
    let expected_changed = new
        .graph()
        .resolve_workspace_names(vec!["fixtures"])
        .expect("workspace names resolved");
    let expected_affected = new
        .graph()
        .resolve_workspace_names(vec![
            // fixtures is a test-only dependency of guppy, so guppy's transitive dependencies
            // aren't involved. fixture-manager is not depended on by anyone.
            "fixtures",
            "guppy",
            "fixture-manager",
        ])
        .expect("workspace names resolved");

    {
        let determinator_set = determinator.compute();
        assert_eq!(determinator_set.path_changed_set, expected_changed);
        assert_eq!(determinator_set.affected_set, expected_affected);
    }

    // Add a README, which is ignored by the rules.
    determinator.add_changed_paths(vec!["guppy/README.md", "cargo-guppy/README.tpl"]);
    {
        let determinator_set = determinator.compute();
        assert_eq!(determinator_set.path_changed_set, expected_changed);
        assert_eq!(determinator_set.affected_set, expected_affected);
    }

    // Cargo.lock and .gitignore should be ignored by default and shouldn't cause any changes.
    determinator.add_changed_paths(vec![".gitignore", "Cargo.lock"]);
    {
        let determinator_set = determinator.compute();
        assert_eq!(determinator_set.path_changed_set, expected_changed);
        assert_eq!(determinator_set.affected_set, expected_affected);
    }

    // Check that rules doesn't apply to subdirectories.
    determinator.add_changed_paths(vec!["foo/CODE_OF_CONDUCT.md"]);
    {
        let determinator_set = determinator.compute();
        assert_eq!(determinator_set.path_changed_set, expected_changed);
        assert_eq!(determinator_set.affected_set, expected_affected);
    }

    // Ensure that fallthrough works.

    determinator.add_changed_paths(vec!["CONTRIBUTING.md"]);
    {
        // CONTRIBUTING.md should cause cargo-guppy to be added.
        let new_changed = new
            .graph()
            .resolve_workspace_names(vec!["cargo-guppy", "fixtures"])
            .expect("workspace names resolved");
        let new_affected = expected_affected.union(
            &new.graph()
                .resolve_workspace_names(vec!["cargo-guppy"])
                .expect("workspace names resolved"),
        );
        let determinator_set = determinator.compute();
        assert_eq!(determinator_set.path_changed_set, new_changed);
        assert_eq!(determinator_set.affected_set, new_affected);
    }
    determinator.add_changed_paths(vec!["CODE_OF_CONDUCT.md"]);
    {
        // CODE_OF_CONDUCT.md should cause both guppy and cargo-guppy to be added.
        let new_changed = new
            .graph()
            .resolve_workspace_names(vec!["cargo-guppy", "fixtures", "guppy"])
            .expect("workspace names resolved");
        let new_affected = new_changed.union(
            &new.graph()
                .resolve_workspace_names(
                    // These are all packages that guppy is a dependency of.
                    vec![
                        "guppy-cmdlib",
                        "guppy-benchmarks",
                        "cargo-compare",
                        "fixture-manager",
                    ],
                )
                .expect("workspace names resolved"),
        );
        let determinator_set = determinator.compute();
        assert_eq!(determinator_set.path_changed_set, new_changed);
        assert_eq!(determinator_set.affected_set, new_affected);
    }

    // Ensure that skip-rules works as expected, skipping further rules.
    determinator.add_changed_paths(vec!["internal-tools/benchmarks/foo"]);
    {
        // CODE_OF_CONDUCT.md should cause both guppy and cargo-guppy to be added.
        let new_changed = new
            .graph()
            .resolve_workspace_names(vec![
                "cargo-guppy",
                "fixtures",
                "guppy",
                "guppy-benchmarks",
                "cargo-compare",
            ])
            .expect("workspace paths resolved");
        let new_affected = new_changed.union(
            &new.graph()
                .resolve_workspace_names(
                    // These are all packages that guppy is a dependency of.
                    vec!["guppy-cmdlib", "fixture-manager"],
                )
                .expect("workspace names resolved"),
        );

        let determinator_set = determinator.compute();
        assert_eq!(determinator_set.path_changed_set, new_changed);
        assert_eq!(determinator_set.affected_set, new_affected);
    }
}

#[test]
fn guppy_package_rules() {
    // There are no dependency changes between the old and new fixtures, only file changes.
    let old = JsonFixture::metadata_guppy_869476c();
    let new = JsonFixture::metadata_guppy_c9b4f76();
    let opts = read_options(new, "package-rules.toml");

    let mut determinator = Determinator::new(old.graph(), new.graph());
    determinator.set_rules(&opts).expect("rules set correctly");

    // Nothing changed means empty set.
    let determinator_set = determinator.compute();
    assert!(
        determinator_set.path_changed_set.is_empty(),
        "no path changes"
    );
    assert!(
        determinator_set.summary_changed_set.is_empty(),
        "no summary changes"
    );
    assert!(determinator_set.affected_set.is_empty(), "no changes");

    {
        // This ruleset disables default rules, so Cargo.lock changing should cause everything to be
        // built.
        let mut determinator = determinator.clone();
        determinator.add_changed_paths(vec!["Cargo.lock"]);
        let determinator_set = determinator.compute();
        let workspace_set = new.graph().resolve_workspace();
        assert_eq!(
            determinator_set.path_changed_set, workspace_set,
            "everything changed"
        );
        assert_eq!(
            determinator_set.affected_set, workspace_set,
            "everything changed"
        );
    }

    // Add a file that doesn't match any of the rules.
    determinator.add_changed_paths(vec!["cargo-guppy/foo.rs"]);
    let determinator_set = determinator.compute();
    let expected_path_changed = new
        .graph()
        .resolve_workspace_names(vec!["cargo-guppy"])
        .expect("valid workspace names");

    assert_eq!(
        determinator_set.path_changed_set, expected_path_changed,
        "cargo-guppy in path changes"
    );
    assert!(
        determinator_set.summary_changed_set.is_empty(),
        "no summary changes"
    );
    assert_eq!(
        determinator_set.affected_set, expected_path_changed,
        "cargo-guppy in affected set"
    );

    // Add a file which matches fixtures (and triggers guppy-cmdlib).
    determinator.add_changed_paths(vec!["fixtures/src/main.rs"]);
    let determinator_set = determinator.compute();
    let expected_path_changed = new
        .graph()
        .resolve_workspace_names(vec!["cargo-guppy", "fixtures"])
        .expect("valid workspace names");
    let expected_affected = expected_path_changed.union(
        &new.graph()
            .resolve_workspace_names(vec![
                // fixtures is a *dev dependency* of guppy, so guppy itself is affected but
                // packages that depend on it, such as guppy-benchmarks, are *not*.
                "guppy",
                // guppy-cmdlib is added through a package rule.
                "guppy-cmdlib",
                // cargo-compare depends on guppy-cmdlib.
                "cargo-compare",
                // fixture-manager depends on guppy-cmdlib.
                "fixture-manager",
            ])
            .expect("valid workspace names"),
    );

    assert_eq!(
        determinator_set.path_changed_set, expected_path_changed,
        "cargo-guppy + fixtures in path changes"
    );
    assert!(
        determinator_set.summary_changed_set.is_empty(),
        "no summary changes"
    );
    assert_eq!(
        determinator_set.affected_set, expected_affected,
        "most but not all packages affected"
    );
}

#[test]
fn guppy_package_rules_2() {
    // There are no dependency changes between the old and new fixtures, only file changes.
    let old = JsonFixture::metadata_guppy_869476c();
    let new = JsonFixture::metadata_guppy_c9b4f76();
    let opts = read_options(new, "package-rules.toml");

    let mut determinator = Determinator::new(old.graph(), new.graph());
    determinator.set_rules(&opts).expect("rules set correctly");

    // Changing a "fake-trigger" file means "proptest-ext" changes, which causes "guppy-benchmarks"
    // to change, which according to a package rule means everything gets rebuilt.
    determinator.add_changed_paths(vec!["foo/fake-trigger"]);
    let determinator_set = determinator.compute();
    let expected_path_changed = new
        .graph()
        .resolve_workspace_names(vec!["proptest-ext"])
        .expect("valid workspace names");

    assert_eq!(
        determinator_set.path_changed_set, expected_path_changed,
        "cargo-guppy + fixtures in path changes"
    );
    assert!(
        determinator_set.summary_changed_set.is_empty(),
        "no summary changes"
    );
    assert_eq!(
        determinator_set.affected_set,
        new.graph().resolve_workspace(),
        "all packages affected"
    );
}

#[test]
fn guppy_deps() {
    // new updates the version of toml, which should cause most things to change.
    let old = JsonFixture::metadata_guppy_78cb7e8();
    let new = JsonFixture::metadata_guppy_869476c();
    let opts = read_options(new, "path-rules.toml");

    let mut determinator = Determinator::new(old.graph(), new.graph());
    determinator.set_rules(&opts).expect("rules set correctly");

    // This changes the "toml" dependency, so many packages should be marked changed.
    let determinator_set = determinator.compute();
    let expected = new
        .graph()
        .resolve_workspace_names(vec![
            "cargo-guppy",
            "fixture-manager",
            "guppy",
            "guppy-summaries",
            "cargo-compare",
            // toml is only a dev-dependency for target-spec. Ensure that it's marked as changed.
            "target-spec",
            // Packages not marked changed include "fixtures", "guppy-cmdlib" and
            // "guppy-benchmarks". While these packages depend on guppy, they don't enable the
            // summaries feature so they aren't influenced by the toml dependency.
        ])
        .expect("workspace names resolved");

    assert!(
        determinator_set.path_changed_set.is_empty(),
        "no path changes"
    );
    assert_eq!(
        determinator_set.summary_changed_set, expected,
        "some summary changes"
    );
    assert_eq!(
        determinator_set.affected_set, expected,
        "some packages affected"
    );

    // Try setting fixture-manager as features-only. This should cause guppy's summaries feature to
    // always be enabled, which means that fixtures, guppy-cmdlib and guppy-benchmarks should be
    // added to the expected set.
    determinator
        .set_features_only(
            ["fixture-manager"].iter().copied(),
            StandardFeatures::Default,
        )
        .expect("fixture-manager is a valid package name");

    let determinator_set = determinator.compute();
    let features_only_expected = expected.union(
        &new.graph()
            .resolve_workspace_names(vec!["fixtures", "guppy-cmdlib", "guppy-benchmarks"])
            .expect("workspace names resolved"),
    );
    assert!(
        determinator_set.path_changed_set.is_empty(),
        "no path changes"
    );
    assert_eq!(
        determinator_set.summary_changed_set, features_only_expected,
        "some summary changes"
    );
    assert_eq!(
        determinator_set.affected_set, features_only_expected,
        "some packages affected"
    );
}

#[test]
fn guppy_match_paths() {
    let old = JsonFixture::metadata_guppy_869476c();
    let new = JsonFixture::metadata_guppy_c9b4f76();
    let opts = read_options(new, "path-rules.toml");

    let mut determinator = Determinator::new(old.graph(), new.graph());
    determinator
        .set_rules(&opts)
        .expect("options set correctly");

    // These expected outputs were figured out by manually matching path-rules.toml and
    // default-rules.toml.
    let expected = vec![
        ("Cargo.toml", PathMatch::RuleMatchedAll),
        (
            "README.md",
            PathMatch::RuleMatched(RuleIndex::CustomPath(0)),
        ),
        (
            "foo/README.tpl",
            PathMatch::RuleMatched(RuleIndex::CustomPath(0)),
        ),
        (
            "CONTRIBUTING.md",
            PathMatch::RuleMatched(RuleIndex::DefaultPath(4)),
        ),
        (
            "CODE_OF_CONDUCT.md",
            PathMatch::RuleMatched(RuleIndex::CustomPath(2)),
        ),
        ("guppy/src/foo", PathMatch::AncestorMatched),
        ("guppy/src/lib.rs", PathMatch::AncestorMatched),
        (
            "internal-tools/proptest-ext/src/lib.rs",
            PathMatch::AncestorMatched,
        ),
        (
            "Cargo.lock",
            PathMatch::RuleMatched(RuleIndex::DefaultPath(3)),
        ),
    ];

    for (path, m) in expected {
        assert_eq!(
            determinator.match_path(path, |_| {}),
            m,
            "expected rule match for {}",
            path
        );
    }
}

static GIT_MATCH_PATHS_DIFF: &str =
    include_str!("../../../fixtures/determinator-paths/git-diff.out");

// Test matching paths against this repository.
#[test]
fn git_match_paths() {
    let paths = Utf8Paths0::new(GIT_MATCH_PATHS_DIFF);
    git_match_paths_impl(paths)
}

#[cfg(windows)]
#[test]
fn git_match_paths_backslashes() {
    // This will convert the forward slashes to backslashes on Windows, but keep them the same on
    // Unix platforms.
    let paths = Utf8Paths0::new_forward_slashes(GIT_MATCH_PATHS_DIFF);
    git_match_paths_impl(paths)
}

fn git_match_paths_impl(paths: Utf8Paths0) {
    cfg_if! {
        if #[cfg(windows)] {
            let json = include_str!("../../../fixtures/determinator-paths/guppy-win.json");
        } else {
            let json = include_str!("../../../fixtures/determinator-paths/guppy-linux.json");
        }
    };

    let package_graph = CargoMetadata::parse_json(json)
        .expect("metadata json parsed correct")
        .build_graph()
        .expect("PackageGraph built");
    let mut determinator = Determinator::new(&package_graph, &package_graph);

    determinator.add_changed_paths(paths.iter());

    let determinator_set = determinator.compute();
    // The path changed set should contain several packages -- if paths weren't correctly matched
    // on the platform then they may be missing.
    let expected_path_changed = package_graph
        .resolve_workspace_names(vec![
            "cargo-guppy",
            "determinator",
            "guppy",
            "target-spec",
            "hakari",
        ])
        .expect("workspace names resolved");
    assert_eq!(
        determinator_set.path_changed_set, expected_path_changed,
        "correct path changed set"
    );

    let expected_affected = package_graph
        .resolve_workspace_names(vec![
            "cargo-guppy",
            "guppy",
            "fixture-manager",
            "cargo-compare",
            "target-spec",
            "determinator",
            "hakari",
            "fixtures",
            "guppy-cmdlib",
            "guppy-benchmarks",
        ])
        .expect("workspace names resolved");
    assert_eq!(
        determinator_set.affected_set, expected_affected,
        "correct affected set"
    );
}

fn read_options(fixture: &JsonFixture, toml_name: &str) -> DeterminatorRules {
    // Path to the determinator.toml file.
    let mut toml_path = fixture.abs_path().to_path_buf();
    toml_path.pop();
    toml_path.push(toml_name);

    let opts =
        std::fs::read_to_string(&toml_path).expect("determinator.toml was successfully read");
    DeterminatorRules::parse(&opts).expect("determinator.toml parsed")
}