From 6c9769f10b3956b589fcca03af270e8a8a9cf117 Mon Sep 17 00:00:00 2001 From: Martin Pool Date: Sun, 6 Oct 2024 08:32:22 -0700 Subject: [PATCH] Rename more testdata tomls --- src/build_dir.rs | 8 +++- src/mutate.rs | 3 +- src/visit.rs | 4 +- src/workspace.rs | 46 +++++++++---------- .../{Cargo.toml => Cargo_test.toml} | 0 .../{Cargo.toml => Cargo_test.toml} | 0 .../{Cargo.toml => Cargo_test.toml} | 0 .../cdylib/{Cargo.toml => Cargo_test.toml} | 0 .../{Cargo.toml => Cargo_test.toml} | 0 .../diff0/{Cargo.toml => Cargo_test.toml} | 0 .../diff1/{Cargo.toml => Cargo_test.toml} | 0 .../{Cargo.toml => Cargo_test.toml} | 0 .../{Cargo.toml => Cargo_test.toml} | 0 .../{Cargo.toml => Cargo_test.toml} | 0 .../{Cargo.toml => Cargo_test.toml} | 0 .../mut_ref/{Cargo.toml => Cargo_test.toml} | 1 - .../{Cargo.toml => Cargo_test.toml} | 0 .../{Cargo.toml => Cargo_test.toml} | 1 - .../{Cargo.toml => Cargo_test.toml} | 0 tests/main.rs | 40 ++++++++++------ tests/trace.rs | 5 +- tests/util/mod.rs | 23 +++++----- 22 files changed, 74 insertions(+), 57 deletions(-) rename testdata/already_failing_doctests/{Cargo.toml => Cargo_test.toml} (100%) rename testdata/already_failing_tests/{Cargo.toml => Cargo_test.toml} (100%) rename testdata/already_hangs/{Cargo.toml => Cargo_test.toml} (100%) rename testdata/cdylib/{Cargo.toml => Cargo_test.toml} (100%) rename testdata/dependency/{Cargo.toml => Cargo_test.toml} (100%) rename testdata/diff0/{Cargo.toml => Cargo_test.toml} (100%) rename testdata/diff1/{Cargo.toml => Cargo_test.toml} (100%) rename testdata/error_value/{Cargo.toml => Cargo_test.toml} (100%) rename testdata/everything_skipped/{Cargo.toml => Cargo_test.toml} (100%) rename testdata/missing_test/{Cargo.toml => Cargo_test.toml} (100%) rename testdata/missing_test_fixed/{Cargo.toml => Cargo_test.toml} (100%) rename testdata/mut_ref/{Cargo.toml => Cargo_test.toml} (99%) rename testdata/nested_mod/{Cargo.toml => Cargo_test.toml} (100%) rename testdata/never_type/{Cargo.toml => Cargo_test.toml} (99%) rename testdata/nightly_only/{Cargo.toml => Cargo_test.toml} (100%) diff --git a/src/build_dir.rs b/src/build_dir.rs index 9f2a27a0..e6aa7141 100644 --- a/src/build_dir.rs +++ b/src/build_dir.rs @@ -90,11 +90,14 @@ impl BuildDir { #[cfg(test)] mod test { + use test_util::copy_of_testdata; + use super::*; #[test] fn build_dir_copy_from() { - let workspace = Workspace::open("testdata/factorial").unwrap(); + let tmp = copy_of_testdata("factorial"); + let workspace = Workspace::open(tmp.path()).unwrap(); let build_dir = BuildDir::copy_from(workspace.root(), true, false, &Console::new()).unwrap(); let debug_form = format!("{build_dir:?}"); @@ -107,7 +110,8 @@ mod test { #[test] fn build_dir_in_place() -> Result<()> { - let workspace = Workspace::open("testdata/factorial")?; + let tmp = copy_of_testdata("factorial"); + let workspace = Workspace::open(tmp.path())?; let build_dir = BuildDir::in_place(workspace.root())?; // On Windows e.g. the paths might not have the same form, but they // should point to the same place. diff --git a/src/mutate.rs b/src/mutate.rs index d08dd52c..f9a5d532 100644 --- a/src/mutate.rs +++ b/src/mutate.rs @@ -316,7 +316,8 @@ mod test { #[test] fn filter_by_attributes() { - let mutants = Workspace::open(Utf8Path::new("testdata/hang_avoided_by_attr")) + let tmp = copy_of_testdata("hang_avoided_by_attr"); + let mutants = Workspace::open(tmp.path()) .unwrap() .mutants(&PackageFilter::All, &Options::default(), &Console::new()) .unwrap(); diff --git a/src/visit.rs b/src/visit.rs index adfdb385..013788b9 100644 --- a/src/visit.rs +++ b/src/visit.rs @@ -726,6 +726,7 @@ fn find_path_attribute(attrs: &[Attribute]) -> std::result::Result Result { #[cfg(test)] mod test { - use camino::Utf8Path; + use camino::{Utf8Path, Utf8PathBuf}; use itertools::Itertools; use crate::console::Console; @@ -340,15 +340,16 @@ mod test { #[test] fn find_root_from_subdirectory_of_workspace_finds_the_workspace_root() { - let workspace = Workspace::open("testdata/workspace/main") - .expect("Find root from within workspace/main"); + let tmp = copy_of_testdata("workspace"); + let workspace = Workspace::open(tmp.path()).expect("Find root from within workspace/main"); let root = workspace.root(); - assert_eq!(root.file_name(), Some("workspace"), "Wrong root: {root:?}"); + assert_eq!(root, tmp.path(), "Wrong root"); } #[test] fn find_top_source_files_from_subdirectory_of_workspace() { - let workspace = Workspace::open("testdata/workspace/main").expect("Find workspace root"); + let tmp = copy_of_testdata("workspace"); + let workspace = Workspace::open(tmp.path()).expect("Find workspace root"); assert_eq!( workspace .packages(&PackageFilter::All) @@ -372,8 +373,8 @@ mod test { #[test] fn package_filter_all_from_subdir_gets_everything() { - let subdir_path = Utf8Path::new("testdata/workspace/main"); - let workspace = Workspace::open(subdir_path).expect("Find workspace root"); + let tmp = copy_of_testdata("workspace"); + let workspace = Workspace::open(tmp.path().join("main")).expect("Find workspace root"); let packages = workspace.packages(&PackageFilter::All).unwrap(); assert_eq!( packages.iter().map(|p| &p.name).collect_vec(), @@ -383,8 +384,9 @@ mod test { #[test] fn auto_packages_in_workspace_subdir_finds_single_package() { - let subdir_path = Utf8Path::new("testdata/workspace/main"); - let workspace = Workspace::open(subdir_path).expect("Find workspace root"); + let tmp = copy_of_testdata("workspace"); + let subdir_path = Utf8PathBuf::try_from(tmp.path().join("main")).unwrap(); + let workspace = Workspace::open(&subdir_path).expect("Find workspace root"); let packages = workspace .packages(&PackageFilter::Auto(subdir_path.to_owned())) .unwrap(); @@ -393,10 +395,12 @@ mod test { #[test] fn auto_packages_in_virtual_workspace_gets_everything() { - let path = Utf8Path::new("testdata/workspace"); - let workspace = Workspace::open(path).expect("Find workspace root"); + let tmp = copy_of_testdata("workspace"); + let workspace = Workspace::open(tmp.path()).expect("Find workspace root"); let packages = workspace - .packages(&PackageFilter::Auto(path.to_owned())) + .packages(&PackageFilter::Auto( + tmp.path().to_owned().try_into().unwrap(), + )) .unwrap(); assert_eq!( packages.iter().map(|p| &p.name).collect_vec(), @@ -406,13 +410,10 @@ mod test { #[test] fn filter_by_single_package() { - let workspace = Workspace::open("testdata/workspace/main").expect("Find workspace root"); + let tmp = copy_of_testdata("workspace"); + let workspace = Workspace::open(tmp.path().join("main")).expect("Find workspace root"); let root_dir = workspace.root(); - assert_eq!( - root_dir.file_name(), - Some("workspace"), - "found the workspace root" - ); + assert_eq!(root_dir, tmp.path()); let filter = PackageFilter::explicit(["main"]); assert_eq!( workspace @@ -436,12 +437,9 @@ mod test { #[test] fn filter_by_multiple_packages() { - let workspace = Workspace::open("testdata/workspace/main").unwrap(); - assert_eq!( - workspace.root().file_name(), - Some("workspace"), - "found the workspace root" - ); + let tmp = copy_of_testdata("workspace"); + let workspace = Workspace::open(tmp.path().join("main")).expect("Find workspace root"); + assert_eq!(workspace.root(), tmp.path(), "found the workspace root"); let selection = PackageFilter::explicit(["main", "main2"]); let discovered = workspace .discover(&selection, &Options::default(), &Console::new()) diff --git a/testdata/already_failing_doctests/Cargo.toml b/testdata/already_failing_doctests/Cargo_test.toml similarity index 100% rename from testdata/already_failing_doctests/Cargo.toml rename to testdata/already_failing_doctests/Cargo_test.toml diff --git a/testdata/already_failing_tests/Cargo.toml b/testdata/already_failing_tests/Cargo_test.toml similarity index 100% rename from testdata/already_failing_tests/Cargo.toml rename to testdata/already_failing_tests/Cargo_test.toml diff --git a/testdata/already_hangs/Cargo.toml b/testdata/already_hangs/Cargo_test.toml similarity index 100% rename from testdata/already_hangs/Cargo.toml rename to testdata/already_hangs/Cargo_test.toml diff --git a/testdata/cdylib/Cargo.toml b/testdata/cdylib/Cargo_test.toml similarity index 100% rename from testdata/cdylib/Cargo.toml rename to testdata/cdylib/Cargo_test.toml diff --git a/testdata/dependency/Cargo.toml b/testdata/dependency/Cargo_test.toml similarity index 100% rename from testdata/dependency/Cargo.toml rename to testdata/dependency/Cargo_test.toml diff --git a/testdata/diff0/Cargo.toml b/testdata/diff0/Cargo_test.toml similarity index 100% rename from testdata/diff0/Cargo.toml rename to testdata/diff0/Cargo_test.toml diff --git a/testdata/diff1/Cargo.toml b/testdata/diff1/Cargo_test.toml similarity index 100% rename from testdata/diff1/Cargo.toml rename to testdata/diff1/Cargo_test.toml diff --git a/testdata/error_value/Cargo.toml b/testdata/error_value/Cargo_test.toml similarity index 100% rename from testdata/error_value/Cargo.toml rename to testdata/error_value/Cargo_test.toml diff --git a/testdata/everything_skipped/Cargo.toml b/testdata/everything_skipped/Cargo_test.toml similarity index 100% rename from testdata/everything_skipped/Cargo.toml rename to testdata/everything_skipped/Cargo_test.toml diff --git a/testdata/missing_test/Cargo.toml b/testdata/missing_test/Cargo_test.toml similarity index 100% rename from testdata/missing_test/Cargo.toml rename to testdata/missing_test/Cargo_test.toml diff --git a/testdata/missing_test_fixed/Cargo.toml b/testdata/missing_test_fixed/Cargo_test.toml similarity index 100% rename from testdata/missing_test_fixed/Cargo.toml rename to testdata/missing_test_fixed/Cargo_test.toml diff --git a/testdata/mut_ref/Cargo.toml b/testdata/mut_ref/Cargo_test.toml similarity index 99% rename from testdata/mut_ref/Cargo.toml rename to testdata/mut_ref/Cargo_test.toml index ce092de8..65bc7d56 100644 --- a/testdata/mut_ref/Cargo.toml +++ b/testdata/mut_ref/Cargo_test.toml @@ -4,4 +4,3 @@ version = "0.0.0" edition = "2021" authors = ["Martin Pool"] publish = false - diff --git a/testdata/nested_mod/Cargo.toml b/testdata/nested_mod/Cargo_test.toml similarity index 100% rename from testdata/nested_mod/Cargo.toml rename to testdata/nested_mod/Cargo_test.toml diff --git a/testdata/never_type/Cargo.toml b/testdata/never_type/Cargo_test.toml similarity index 99% rename from testdata/never_type/Cargo.toml rename to testdata/never_type/Cargo_test.toml index 5c81a8a5..12978a78 100644 --- a/testdata/never_type/Cargo.toml +++ b/testdata/never_type/Cargo_test.toml @@ -4,4 +4,3 @@ version = "0.0.0" edition = "2021" authors = ["Martin Pool"] publish = false - diff --git a/testdata/nightly_only/Cargo.toml b/testdata/nightly_only/Cargo_test.toml similarity index 100% rename from testdata/nightly_only/Cargo.toml rename to testdata/nightly_only/Cargo_test.toml diff --git a/tests/main.rs b/tests/main.rs index 782216bb..aea86385 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -4,7 +4,7 @@ use std::collections::HashSet; use std::env; -use std::fs::{self, read_dir, read_to_string}; +use std::fs::{self, create_dir, read_dir, read_to_string}; use std::path::Path; use std::thread::sleep; use std::time::Duration; @@ -18,7 +18,7 @@ use pretty_assertions::assert_eq; use tempfile::TempDir; mod util; -use util::{copy_of_testdata, run, MAIN_BINARY, OUTER_TIMEOUT}; +use util::{copy_of_testdata, copy_testdata_to, run, MAIN_BINARY, OUTER_TIMEOUT}; #[test] fn incorrect_cargo_subcommand() { @@ -899,16 +899,28 @@ fn log_file_names_are_short_and_dont_collide() { ); } +fn setup_relative_dependency(tree_name: &str) -> TempDir { + let tmp = TempDir::new().unwrap(); + let tmp_path = tmp.path(); + let tmp_testdata = tmp_path.join("testdata"); + create_dir(&tmp_testdata).unwrap(); + copy_testdata_to(tree_name, tmp_testdata.join(tree_name)); + cp_r::CopyOptions::new() + .copy_tree("mutants_attrs", tmp_path.join("mutants_attrs")) + .unwrap(); + tmp +} + #[test] fn cargo_mutants_in_override_dependency_tree_passes() { - // Run against the testdata directory directly, without copying it, so that the - // relative dependency `../dependency` is still used. + let tree_name = "override_dependency"; + let tmp = setup_relative_dependency(tree_name); run() .arg("mutants") .arg("--no-times") .arg("--no-shuffle") .arg("-d") - .arg("testdata/override_dependency") + .arg(tmp.path().join("testdata").join(tree_name)) .assert() .success() .stdout(predicate::function(|stdout: &str| { @@ -919,14 +931,15 @@ fn cargo_mutants_in_override_dependency_tree_passes() { #[test] fn cargo_mutants_in_relative_dependency_tree_passes() { - // Run against the testdata directory directly, without copying it, so that the - // relative dependency `../dependency` is still used. + let tree_name = "relative_dependency"; + let tmp = setup_relative_dependency(tree_name); + copy_testdata_to("dependency", tmp.path().join("testdata").join("dependency")); run() .arg("mutants") .arg("--no-times") .arg("--no-shuffle") .arg("-d") - .arg("testdata/relative_dependency") + .arg(tmp.path().join("testdata").join(tree_name)) .assert() .success() .stdout(predicate::function(|stdout: &str| { @@ -937,14 +950,14 @@ fn cargo_mutants_in_relative_dependency_tree_passes() { #[test] fn cargo_mutants_in_replace_dependency_tree_passes() { - // Run against the testdata directory directly, without copying it, so that the - // relative dependency `../dependency` is still used. + let tree_name = "replace_dependency"; + let tmp = setup_relative_dependency(tree_name); run() .arg("mutants") .arg("--no-times") .arg("--no-shuffle") .arg("-d") - .arg("testdata/replace_dependency") + .arg(tmp.path().join("testdata").join(tree_name)) .assert() .success() .stdout(predicate::function(|stdout: &str| { @@ -955,14 +968,13 @@ fn cargo_mutants_in_replace_dependency_tree_passes() { #[test] fn cargo_mutants_in_patch_dependency_tree_passes() { - // Run against the testdata directory directly, without copying it, so that the - // relative dependency `../dependency` is still used. + let tmp = setup_relative_dependency("patch_dependency"); run() .arg("mutants") .arg("--no-times") .arg("--no-shuffle") .arg("-d") - .arg("testdata/patch_dependency") + .arg(tmp.path().join("testdata").join("patch_dependency")) .assert() .success() .stdout(predicate::function(|stdout: &str| { diff --git a/tests/trace.rs b/tests/trace.rs index 4d16ce82..1ea45071 100644 --- a/tests/trace.rs +++ b/tests/trace.rs @@ -5,15 +5,16 @@ use predicates::prelude::*; mod util; -use util::run; +use util::{copy_of_testdata, run}; #[test] fn env_var_controls_trace() { + let tmp = copy_of_testdata("never_type"); run() .env("CARGO_MUTANTS_TRACE_LEVEL", "trace") .args(["mutants", "--list"]) .arg("-d") - .arg("testdata/never_type") + .arg(tmp.path()) .assert() // This is a debug!() message; it should only be seen if the trace var // was wired correctly to stderr. diff --git a/tests/util/mod.rs b/tests/util/mod.rs index b96736e0..ac39cf91 100644 --- a/tests/util/mod.rs +++ b/tests/util/mod.rs @@ -67,30 +67,31 @@ impl CommandInstaExt for assert_cmd::Command { } } -// Copy the source because output is written into mutants.out. +// Copy the source for one testdata tree. pub fn copy_of_testdata(tree_name: &str) -> TempDir { assert!( !tree_name.contains("/"), "testdata tree name {tree_name:?} should be just the directory name" ); - let tmp_src_dir = TempDir::with_prefix(format!("cargo-mutants-testdata-{tree_name}-")).unwrap(); - let tmp_path = tmp_src_dir.path(); + let tmp = TempDir::with_prefix(format!("cargo-mutants-testdata-{tree_name}-")).unwrap(); + copy_testdata_to(tree_name, tmp.path()); + tmp +} + +pub fn copy_testdata_to>(tree_name: &str, dest: P) { + let dest = dest.as_ref(); cp_r::CopyOptions::new() .filter(|path, _stat| { Ok(["target", "mutants.out", "mutants.out.old"] .iter() .all(|p| !path.starts_with(p))) }) - .copy_tree(Path::new("testdata").join(tree_name), &tmp_path) - .unwrap(); - if tmp_path.join("Cargo_test.toml").is_file() { - rename( - tmp_path.join("Cargo_test.toml"), - tmp_path.join("Cargo.toml"), - ) + .copy_tree(Path::new("testdata").join(tree_name), dest) .unwrap(); + if dest.join("Cargo_test.toml").is_file() { + rename(dest.join("Cargo_test.toml"), dest.join("Cargo.toml")) + .expect("rename Cargo_test.toml"); } - tmp_src_dir } /// Assert that some bytes, when parsed as json, equal a json value.