|
| 1 | +use std::collections::{BTreeSet, HashSet}; |
| 2 | +use std::fs; |
| 3 | +use std::path::Path; |
| 4 | +use std::sync::{Arc, Mutex}; |
| 5 | + |
| 6 | +use crate::Build; |
| 7 | +use crate::core::builder::cli_paths::match_paths_to_steps_and_run; |
| 8 | +use crate::core::builder::{Builder, StepDescription}; |
| 9 | +use crate::utils::tests::TestCtx; |
| 10 | + |
| 11 | +fn render_steps_for_cli_args(args_str: &str) -> String { |
| 12 | + // Split a single string into a step kind and subsequent arguments. |
| 13 | + // E.g. "test ui" => ("test", &["ui"]) |
| 14 | + let args = args_str.split_ascii_whitespace().collect::<Vec<_>>(); |
| 15 | + let (kind, args) = args.split_first().unwrap(); |
| 16 | + |
| 17 | + // Arbitrary tuple to represent the host system. |
| 18 | + let hosts = &["x86_64-unknown-linux-gnu"]; |
| 19 | + // Arbitrary tuple to represent the target system, which might not be the host. |
| 20 | + let targets = &["aarch64-unknown-linux-gnu"]; |
| 21 | + |
| 22 | + let config = |
| 23 | + TestCtx::new().config(kind).args(args).hosts(hosts).targets(targets).create_config(); |
| 24 | + let build = Build::new(config); |
| 25 | + let mut builder = Builder::new(&build); |
| 26 | + |
| 27 | + // Tell the builder to log steps that it would run, instead of running them. |
| 28 | + let mut buf = Arc::new(Mutex::new(String::new())); |
| 29 | + let buf2 = Arc::clone(&buf); |
| 30 | + builder.log_cli_step_for_tests = Some(Box::new(move |step_desc, pathsets, targets| { |
| 31 | + use std::fmt::Write; |
| 32 | + let mut buf = buf2.lock().unwrap(); |
| 33 | + |
| 34 | + let StepDescription { name, kind, .. } = step_desc; |
| 35 | + // Strip boilerplate to make step names easier to read. |
| 36 | + let name = name.strip_prefix("bootstrap::core::build_steps::").unwrap_or(name); |
| 37 | + |
| 38 | + writeln!(buf, "[{kind:?}] {name}").unwrap(); |
| 39 | + writeln!(buf, " targets: {targets:?}").unwrap(); |
| 40 | + for pathset in pathsets { |
| 41 | + writeln!(buf, " - {pathset:?}").unwrap(); |
| 42 | + } |
| 43 | + })); |
| 44 | + |
| 45 | + builder.execute_cli(); |
| 46 | + |
| 47 | + String::clone(&buf.lock().unwrap()) |
| 48 | +} |
| 49 | + |
| 50 | +fn snapshot_test_inner(name: &str, args_str: &str) { |
| 51 | + let mut settings = insta::Settings::clone_current(); |
| 52 | + // Use the test name as the snapshot filename, not its whole fully-qualified name. |
| 53 | + settings.set_prepend_module_to_snapshot(false); |
| 54 | + settings.bind(|| { |
| 55 | + insta::assert_snapshot!(name, render_steps_for_cli_args(args_str), args_str); |
| 56 | + }); |
| 57 | +} |
| 58 | + |
| 59 | +/// Keep the snapshots directory tidy by forbidding `.snap` files that don't |
| 60 | +/// correspond to a test name. |
| 61 | +fn no_unused_snapshots_inner(known_test_names: &[&str]) { |
| 62 | + let known_test_names = known_test_names.iter().copied().collect::<HashSet<&str>>(); |
| 63 | + |
| 64 | + let mut unexpected_file_names = BTreeSet::new(); |
| 65 | + |
| 66 | + // FIXME(Zalathar): Is there a better way to locate the snapshots dir? |
| 67 | + for entry in walkdir::WalkDir::new("src/core/builder/cli_paths/snapshots") |
| 68 | + .into_iter() |
| 69 | + .map(Result::unwrap) |
| 70 | + { |
| 71 | + let meta = entry.metadata().unwrap(); |
| 72 | + if !meta.is_file() { |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + let name = entry.file_name().to_str().unwrap(); |
| 77 | + if let Some(name_stub) = name.strip_suffix(".snap") |
| 78 | + && !known_test_names.contains(name_stub) |
| 79 | + { |
| 80 | + unexpected_file_names.insert(name.to_owned()); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + assert!( |
| 85 | + unexpected_file_names.is_empty(), |
| 86 | + "Found snapshot files that don't correspond to a test name: {unexpected_file_names:#?}", |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +macro_rules! declare_tests { |
| 91 | + ( |
| 92 | + $( ($name:ident, $args:literal) ),* $(,)? |
| 93 | + ) => { |
| 94 | + $( |
| 95 | + #[test] |
| 96 | + fn $name() { |
| 97 | + snapshot_test_inner(stringify!($name), $args); |
| 98 | + } |
| 99 | + )* |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn no_unused_snapshots() { |
| 103 | + let known_test_names = &[ $( stringify!($name), )* ]; |
| 104 | + no_unused_snapshots_inner(known_test_names); |
| 105 | + } |
| 106 | + }; |
| 107 | +} |
| 108 | + |
| 109 | +// Snapshot tests for bootstrap's command-line path-to-step handling. |
| 110 | +// |
| 111 | +// To bless these tests as necessary, choose one: |
| 112 | +// - Run `INSTA_UPDATE=always ./x test bootstrap` |
| 113 | +// - Run `./x test bootstrap --bless` |
| 114 | +// - Follow the instructions for `cargo-insta` in bootstrap's README.md |
| 115 | +// |
| 116 | +// These snapshot tests capture _current_ behavior, to prevent unintended |
| 117 | +// changes or regressions. If the current behavior is wrong or undersirable, |
| 118 | +// then any fix will necessarily have to re-bless the affected tests! |
| 119 | +declare_tests!( |
| 120 | + // tidy-alphabetical-start |
| 121 | + (x_build, "build"), |
| 122 | + // tidy-alphabetical-end |
| 123 | +); |
0 commit comments