Skip to content

Commit

Permalink
Add --cap-lints option
Browse files Browse the repository at this point in the history
  • Loading branch information
sourcefrog committed Jul 13, 2024
1 parent cdd7b93 commit 07d4984
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
41 changes: 34 additions & 7 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn run_cargo(
let start = Instant::now();
let argv = cargo_argv(build_dir.path(), packages, phase, options);
let env = vec![
("CARGO_ENCODED_RUSTFLAGS".to_owned(), rustflags()),
("CARGO_ENCODED_RUSTFLAGS".to_owned(), rustflags(options)),
// The tests might use Insta <https://insta.rs>, and we don't want it to write
// updates to the source tree, and we *certainly* don't want it to write
// updates and then let the test pass.
Expand Down Expand Up @@ -140,8 +140,9 @@ fn cargo_argv(
///
/// See <https://doc.rust-lang.org/cargo/reference/environment-variables.html>
/// <https://doc.rust-lang.org/rustc/lints/levels.html#capping-lints>
fn rustflags() -> String {
let rustflags: Vec<String> = if let Some(rustflags) = env::var_os("CARGO_ENCODED_RUSTFLAGS") {
fn rustflags(options: &Options) -> String {
let mut rustflags: Vec<String> = if let Some(rustflags) = env::var_os("CARGO_ENCODED_RUSTFLAGS")
{
rustflags
.to_str()
.expect("CARGO_ENCODED_RUSTFLAGS is not valid UTF-8")
Expand All @@ -162,7 +163,9 @@ fn rustflags() -> String {
// TODO: build.rustflags config value.
Vec::new()
};
// rustflags.push("--cap-lints=allow".to_owned());
if options.cap_lints {
rustflags.push("--cap-lints=warn".to_owned());
}
// debug!("adjusted rustflags: {:?}", rustflags);
rustflags.join("\x1f")
}
Expand Down Expand Up @@ -290,6 +293,17 @@ mod test {
);
}

#[test]
fn cap_lints_passed_to_cargo() {
let args = Args::try_parse_from(["mutants", "--cap-lints=true"].as_slice()).unwrap();
let options = Options::from_args(&args).unwrap();
let build_dir = Utf8Path::new("/tmp/buildXYZ");
assert_eq!(
cargo_argv(build_dir, None, Phase::Check, &options)[1..],
["check", "--tests", "--workspace",]
);
}

#[test]
fn feature_args_passed_to_cargo() {
let args = Args::try_parse_from(
Expand All @@ -315,21 +329,34 @@ mod test {
fn rustflags_with_no_environment_variables() {
env::remove_var("RUSTFLAGS");
env::remove_var("CARGO_ENCODED_RUSTFLAGS");
assert_eq!(rustflags(), "");
assert_eq!(
rustflags(&Options {
cap_lints: true,
..Default::default()
}),
"--cap-lints=warn"
);
}

#[test]
fn rustflags_added_to_existing_encoded_rustflags() {
env::set_var("RUSTFLAGS", "--something\x1f--else");
env::remove_var("CARGO_ENCODED_RUSTFLAGS");
assert_eq!(rustflags(), "--something\x1f--else");
let options = Options {
cap_lints: true,
..Default::default()
};
assert_eq!(rustflags(&options), "--something\x1f--else\x1f--cap-lints=warn");
}

#[test]
fn rustflags_added_to_existing_rustflags() {
env::set_var("RUSTFLAGS", "-Dwarnings");
env::remove_var("CARGO_ENCODED_RUSTFLAGS");
assert_eq!(rustflags(), "-Dwarnings");
assert_eq!(rustflags(&Options {
cap_lints: true,
..Default::default()
}), "-Dwarnings\x1f--cap-lints=warn");
}
}
}
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ pub struct Args {
#[arg(long, value_enum, default_value_t = BaselineStrategy::Run, help_heading = "Execution")]
baseline: BaselineStrategy,

/// Turn off all rustc lints, so that denied warnings won't make mutants unviable.
#[arg(long, action = ArgAction::Set, default_value_t = false, help_heading = "Build")]
cap_lints: bool,

/// Print mutants that were caught by tests.
#[arg(long, short = 'v', help_heading = "Output")]
caught: bool,
Expand Down
4 changes: 4 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub struct Options {
/// Run tests in an unmutated tree?
pub baseline: BaselineStrategy,

/// Turn off all lints.
pub cap_lints: bool,

/// Don't run the tests, just see if each mutant builds.
pub check_only: bool,

Expand Down Expand Up @@ -196,6 +199,7 @@ impl Options {
&config.additional_cargo_test_args,
),
baseline: args.baseline,
cap_lints: args.cap_lints,
check_only: args.check,
colors: args.colors,
emit_json: args.json,
Expand Down

0 comments on commit 07d4984

Please sign in to comment.