Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fast rerun #156

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub struct Config {
pub filter_files: Vec<String>,
/// Override the number of threads to use.
pub threads: Option<NonZeroUsize>,
/// Rerun tests even if they passed previously
pub force_rerun: bool,
}

impl Config {
Expand Down Expand Up @@ -94,6 +96,7 @@ impl Config {
skip_files: Vec::new(),
filter_files: Vec::new(),
threads: None,
force_rerun: false,
}
}

Expand All @@ -120,6 +123,7 @@ impl Config {
let Args {
ref filters,
quiet: _,
force_rerun,
check,
bless,
threads,
Expand All @@ -130,6 +134,7 @@ impl Config {

self.filter_files.extend_from_slice(filters);
self.skip_files.extend_from_slice(skip);
self.force_rerun |= force_rerun;

let bless = match (bless, check) {
(_, true) => false,
Expand Down
6 changes: 6 additions & 0 deletions src/config/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ pub struct Args {
/// output.
pub check: bool,

/// Rerun all tests, even if their test files are unchanged and they passed
/// in a previous run.
pub force_rerun: bool,

/// Whether to overwrite `.stderr` files on mismtach with the actual
/// output.
pub bless: bool,
Expand Down Expand Up @@ -48,6 +52,8 @@ impl Args {
self.check = true;
} else if arg == "--bless" {
self.bless = true;
} else if arg == "--force-rerun" {
self.force_rerun = true;
} else if let Some(skip) = parse_value("--skip", &arg, &mut iter)? {
self.skip.push(skip.into_owned());
} else if arg == "--help" {
Expand Down
38 changes: 35 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,35 @@ pub fn default_file_filter(path: &Path, config: &Config) -> bool {
///
/// To only include rust files see [`default_file_filter`].
pub fn default_any_file_filter(path: &Path, config: &Config) -> bool {
let path = path.display().to_string();
let contains_path = |files: &[String]| files.iter().any(|f| path.contains(f));
let path_str = path.display().to_string();
let contains_path = |files: &[String]| files.iter().any(|f| path_str.contains(f));

if contains_path(&config.skip_files) {
return false;
}

config.filter_files.is_empty() || contains_path(&config.filter_files)
if !config.filter_files.is_empty() && !contains_path(&config.filter_files) {
return false;
}
config.force_rerun || modified_since_last_successful_run(path, config)
}

/// Returns `true` if the file was modified since the last time it was tested and passed successfully.
pub fn modified_since_last_successful_run(path: &Path, config: &Config) -> bool {
macro_rules! modified {
($path:expr) => {
if let Ok(time) = std::fs::metadata(&$path).and_then(|m| m.modified()) {
time
} else {
// If we can't get a time, assume the file has been modified.
return true;
}
};
}
let file_modified = modified!(path);
let test_suite = modified!(std::env::current_exe().unwrap());
let check_file = modified!(config.out_dir.join(path.with_extension("timestamp")));
(check_file < test_suite) || (check_file < file_modified)
}

/// The default per-file config used by `run_tests`.
Expand Down Expand Up @@ -271,6 +292,10 @@ pub fn run_tests_generic(
todo.push_back((entry, config));
}
} else if file_filter(&path, config) {
// If we start a test, remove the marker that it was cached.
let check_file = config.out_dir.join(path.with_extension("timestamp"));
// The file may not exist, in this case we don't need to do anything.
let _ = std::fs::remove_file(check_file);
let status = status_emitter.register_test(path);
// Forward .rs files to the test workers.
submit.send((status, config)).unwrap();
Expand Down Expand Up @@ -636,6 +661,13 @@ impl dyn TestStatus {
run_rustfix(
&stderr, &stdout, path, comments, revision, config, *mode, extra_args,
)?;

// Make sure the `default_file_filter` recognizes that the test has already been run successfully
// and avoid running it again.
let check_file = config.out_dir.join(path.with_extension("timestamp"));
std::fs::create_dir_all(check_file.parent().unwrap()).unwrap();
std::fs::File::create(check_file).unwrap();
Copy link
Owner Author

@oli-obk oli-obk Sep 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a test failed last time, then rustc compiletest always reruns it.

This also happens here, but I realize now that we have an early success path for run tests 🙃 so run tests are always run again


Ok(TestOk::Ok)
}

Expand Down
1 change: 1 addition & 0 deletions tests/integrations/basic-bin/tests/ui_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fn main() -> ui_test::color_eyre::Result<()> {
} else {
OutputConflictHandling::Error("cargo test".to_string())
},
force_rerun: true,
..Config::rustc("tests/actual_tests")
};
config.stderr_filter("in ([0-9]m )?[0-9\\.]+s", "");
Expand Down
1 change: 1 addition & 0 deletions tests/integrations/basic-fail-mode/tests/ui_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fn main() -> ui_test::color_eyre::Result<()> {
} else {
OutputConflictHandling::Error("cargo test".to_string())
},
force_rerun: true,
..Config::rustc("tests/actual_tests")
};
config.stderr_filter("in ([0-9]m )?[0-9\\.]+s", "");
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/basic-fail/Cargo.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Caused by:
thread 'main' panicked at 'invalid mode/result combo: yolo: Err(tests failed

Location:
$DIR/src/lib.rs:LL:CC)', tests/ui_tests_bless.rs:52:18
$DIR/src/lib.rs:LL:CC)', tests/ui_tests_bless.rs:53:18
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: test failed, to rerun pass `--test ui_tests_bless`
Error: failed to parse rustc version info: invalid_foobarlaksdfalsdfj
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/basic-fail/tests/ui_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() -> ui_test::color_eyre::Result<()> {
output_conflict_handling: OutputConflictHandling::Error(
"DO NOT BLESS. These are meant to fail".into(),
),

force_rerun: true,
..Config::rustc("tests/actual_tests")
};

Expand Down
1 change: 1 addition & 0 deletions tests/integrations/basic-fail/tests/ui_tests_bless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ fn main() -> ui_test::color_eyre::Result<()> {
OutputConflictHandling::Error("cargo test".to_string())
},
mode,
force_rerun: true,
..Config::rustc(root_dir)
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ fn main() -> ui_test::color_eyre::Result<()> {
output_conflict_handling: OutputConflictHandling::Error(
"DO NOT BLESS. These are meant to fail".into(),
),
force_rerun: true,
..Config::rustc("tests/actual_tests")
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ fn main() -> ui_test::color_eyre::Result<()> {
"DO NOT BLESS. These are meant to fail".into(),
),
host: Some("foo".into()),
force_rerun: true,
..Config::rustc("tests/actual_tests")
};

Expand Down
1 change: 1 addition & 0 deletions tests/integrations/basic/tests/ui_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fn main() -> ui_test::color_eyre::Result<()> {
} else {
OutputConflictHandling::Error("cargo test".to_string())
},
force_rerun: true,
..Config::rustc("tests/actual_tests")
};
config.stderr_filter("in ([0-9]m )?[0-9\\.]+s", "");
Expand Down