Skip to content

Commit

Permalink
Accept --manifest-path (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
sourcefrog authored Nov 25, 2023
2 parents c9f2210 + e956e8b commit e1155e6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

- Added: Alternative aliases for command line options, so you don't need to remember if it's "regex" or "re": `--regex`, `--examine-re`, `--examine-regex` (all for names to include) and `--exclude-regex`.

- Added: Accept `--manifest-path` as an alternative to `-d`, for consistency with other cargo commands.

## 23.11.1

- New `--in-diff FILE` option tests only mutants that are in the diff from the
Expand Down
20 changes: 17 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use std::fs::read_to_string;
use std::io;
use std::process::exit;

use anyhow::ensure;
use anyhow::Context;
use anyhow::Result;
use camino::Utf8Path;
Expand Down Expand Up @@ -156,6 +157,10 @@ struct Args {
#[arg(long)]
list_files: bool,

/// path to Cargo.toml for the package to mutate.
#[arg(long)]
manifest_path: Option<Utf8PathBuf>,

/// don't read .cargo/mutants.toml.
#[arg(long)]
no_config: bool,
Expand Down Expand Up @@ -240,10 +245,19 @@ fn main() -> Result<()> {
console.setup_global_trace(args.level)?;
interrupt::install_handler();

let start_dir: &Utf8Path = args.dir.as_deref().unwrap_or(Utf8Path::new("."));
let start_dir: &Utf8Path = if let Some(manifest_path) = &args.manifest_path {
ensure!(manifest_path.is_file(), "Manifest path is not a file");
ensure!(
args.dir.is_none(),
"--dir and --manifest-path are mutually exclusive"
);
manifest_path.parent().expect("Manifest path has no parent")
} else if let Some(dir) = &args.dir {
dir
} else {
Utf8Path::new(".")
};
let workspace = Workspace::open(start_dir)?;
// let discovered_workspace = discover_packages(start_dir, false, &args.mutate_packages)?;
// let workspace_dir = &discovered_workspace.workspace_dir;
let config = if args.no_config {
config::Config::default()
} else {
Expand Down
10 changes: 6 additions & 4 deletions src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,18 @@ struct PackageTop {
}

impl Workspace {
/// Open the workspace containing a given directory.
pub fn open<P: AsRef<Utf8Path>>(start_dir: P) -> Result<Self> {
let dir = locate_project(start_dir.as_ref(), true)?;
let cargo_toml_path = dir.join("Cargo.toml");
debug!(?cargo_toml_path, ?dir, "Find root files");
let manifest_path = dir.join("Cargo.toml");
debug!(?manifest_path, ?dir, "Find root files");
check_interrupted()?;
let metadata = cargo_metadata::MetadataCommand::new()
.no_deps()
.manifest_path(&cargo_toml_path)
.manifest_path(&manifest_path)
.exec()
.context("run cargo metadata")?;
.with_context(|| format!("Failed to run cargo metadata on {:?}", manifest_path))?;
debug!(workspace_root = ?metadata.workspace_root, "Found workspace root");
Ok(Workspace { dir, metadata })
}

Expand Down
18 changes: 18 additions & 0 deletions tests/cli/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@ use serde_json::json;

use super::{assert_bytes_eq_json, copy_of_testdata, run};

#[test]
fn open_by_manifest_path() {
run()
.args([
"mutants",
"--list",
"--manifest-path",
"testdata/factorial/Cargo.toml",
])
.assert()
.success()
.stdout(indoc! {"
src/bin/factorial.rs:1: replace main with ()
src/bin/factorial.rs:7: replace factorial -> u32 with 0
src/bin/factorial.rs:7: replace factorial -> u32 with 1
"});
}

#[test]
fn list_warns_about_unmatched_packages() {
run()
Expand Down

0 comments on commit e1155e6

Please sign in to comment.