Skip to content

Commit

Permalink
More tests for packages and add --workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
sourcefrog committed Nov 8, 2023
1 parent 8e665ec commit c69d1e1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
9 changes: 5 additions & 4 deletions book/src/workspaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

cargo-mutants supports testing Cargo workspaces that contain multiple packages. The entire workspace tree is copied.

By default, all source files in all packages in the workspace are tested.
By default, cargo-mutants has [the same behavior as Cargo](https://doc.rust-lang.org/cargo/reference/workspaces.html):

**NOTE: This behavior is likely to change in future: see <https://github.com/sourcefrog/cargo-mutants/issues/156>.**

With the `--package` option, only mutants from the package with the given name are testeg. The effect can be seen in `--list`, etc. This option can be repeated.
* If `--workspace` is given, all packages in the workspace are tested.
* If `--package` is given, the named packages are tested.
* If the starting directory (or `-d` directory) is in a package, that package is tested.
* Otherwise, the starting directory must be in a virtual workspace. If it specifies default members, they are tested. Otherwise, all packages are tested.

You can use the `--file` options to restrict cargo-mutants to testing only files
from some subdirectory, e.g. with `-f "utils/**/*.rs"`. (Remember to quote globs
Expand Down
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ struct Args {
#[arg(long, action = clap::ArgAction::SetTrue)]
version: bool,

/// test every package in the workspace.
#[arg(long)]
workspace: bool,

/// additional args for all cargo invocations.
#[arg(long, short = 'C', allow_hyphen_values = true)]
cargo_arg: Vec<String>,
Expand Down Expand Up @@ -237,8 +241,9 @@ fn main() -> Result<()> {
debug!(?options);
let package_filter = if !args.mutate_packages.is_empty() {
PackageFilter::explicit(&args.mutate_packages)
} else if args.workspace {
PackageFilter::All
} else {
// TODO: --workspace to ::All
PackageFilter::Auto(start_dir.to_owned())
};
let discovered = workspace.discover(&package_filter, &options, &console)?;
Expand Down
32 changes: 30 additions & 2 deletions src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,38 @@ mod test {
);
}

#[test]
fn package_filter_all_from_subdir_gets_everything() {
let subdir_path = Utf8Path::new("testdata/tree/workspace/main");
let workspace = Workspace::open(subdir_path).expect("Find workspace root");
let packages = workspace.packages(&PackageFilter::All).unwrap();
assert_eq!(
packages.iter().map(|p| &p.name).collect_vec(),
["cargo_mutants_testdata_workspace_utils", "main", "main2"]
);
}

#[test]
fn auto_packages_in_workspace_subdir_finds_single_package() {
let workspace =
Workspace::open("testdata/tree/workspace/main").expect("Find workspace root");
let subdir_path = Utf8Path::new("testdata/tree/workspace/main");
let workspace = Workspace::open(subdir_path).expect("Find workspace root");
let packages = workspace
.packages(&PackageFilter::Auto(subdir_path.to_owned()))
.unwrap();
assert_eq!(packages.iter().map(|p| &p.name).collect_vec(), ["main"]);
}

#[test]
fn auto_packages_in_virtual_workspace_gets_everything() {
let path = Utf8Path::new("testdata/tree/workspace");
let workspace = Workspace::open(path).expect("Find workspace root");
let packages = workspace
.packages(&PackageFilter::Auto(path.to_owned()))
.unwrap();
assert_eq!(
packages.iter().map(|p| &p.name).collect_vec(),
["cargo_mutants_testdata_workspace_utils", "main", "main2"]
);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion tests/cli/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn list_files_json_workspace() {
#[test]
fn list_files_as_json_in_workspace_subdir() {
run()
.args(["mutants", "--list-files", "--json"])
.args(["mutants", "--list-files", "--json", "--workspace"])
.current_dir("testdata/tree/workspace/main2")
.assert()
.stdout(indoc! {r#"
Expand Down

0 comments on commit c69d1e1

Please sign in to comment.