Skip to content

Commit

Permalink
Rename more testdata tomls
Browse files Browse the repository at this point in the history
  • Loading branch information
sourcefrog committed Oct 6, 2024
1 parent 6833360 commit 6c9769f
Show file tree
Hide file tree
Showing 22 changed files with 74 additions and 57 deletions.
8 changes: 6 additions & 2 deletions src/build_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}");
Expand All @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion src/mutate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ fn find_path_attribute(attrs: &[Attribute]) -> std::result::Result<Option<Utf8Pa
mod test {
use indoc::indoc;
use itertools::Itertools;
use test_util::copy_of_testdata;

use super::*;
use crate::package::Package;
Expand Down Expand Up @@ -761,7 +762,8 @@ mod test {
fn no_mutants_in_files_with_inner_cfg_test_attribute() {
let options = Options::default();
let console = Console::new();
let workspace = Workspace::open("testdata/cfg_test_inner").unwrap();
let tmp = copy_of_testdata("cfg_test_inner");
let workspace = Workspace::open(tmp.path()).unwrap();
let discovered = workspace
.discover(&PackageFilter::All, &options, &console)
.unwrap();
Expand Down
46 changes: 22 additions & 24 deletions src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ fn locate_project(path: &Utf8Path, workspace: bool) -> Result<Utf8PathBuf> {

#[cfg(test)]
mod test {
use camino::Utf8Path;
use camino::{Utf8Path, Utf8PathBuf};
use itertools::Itertools;

use crate::console::Console;
Expand All @@ -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)
Expand 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(),
Expand All @@ -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();
Expand All @@ -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(),
Expand All @@ -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
Expand All @@ -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())
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ version = "0.0.0"
edition = "2021"
authors = ["Martin Pool"]
publish = false

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ version = "0.0.0"
edition = "2021"
authors = ["Martin Pool"]
publish = false

File renamed without changes.
40 changes: 26 additions & 14 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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| {
Expand All @@ -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| {
Expand All @@ -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| {
Expand All @@ -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| {
Expand Down
5 changes: 3 additions & 2 deletions tests/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 12 additions & 11 deletions tests/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P: AsRef<Path>>(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.
Expand Down

0 comments on commit 6c9769f

Please sign in to comment.