Skip to content

Commit

Permalink
Add ArgGroup for --diff, --fix-only, --fix
Browse files Browse the repository at this point in the history
  • Loading branch information
evanrittenhouse committed Aug 14, 2023
1 parent 21e51ad commit d1626a4
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 5 deletions.
6 changes: 5 additions & 1 deletion crates/ruff_cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ pub enum Command {
// The `Parser` derive is for ruff_dev, for ruff_cli `Args` would be sufficient
#[derive(Clone, Debug, clap::Parser)]
#[allow(clippy::struct_excessive_bools, clippy::module_name_repetitions)]
#[clap(group = clap::ArgGroup::new("fix_args")
.required(false)
.multiple(false)
.args(&["fix", "diff", "fix_only"]))]
pub struct CheckArgs {
/// List of files or directories to check.
pub files: Vec<PathBuf>,
Expand Down Expand Up @@ -193,7 +197,7 @@ pub struct CheckArgs {
pub extend_exclude: Option<Vec<FilePattern>>,
/// Attempt to automatically fix both safe and unsafe lint violations. Only applicable when
/// autofix itself is enabled (e.g., via `--fix`).
#[arg(long, requires("fix"))]
#[arg(long, requires("fix_args"))]
pub r#unsafe: bool,
/// List of rule codes to treat as eligible for autofix. Only applicable
/// when autofix itself is enabled (e.g., via `--fix`).
Expand Down
125 changes: 121 additions & 4 deletions crates/ruff_cli/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ fn unreadable_dir() -> Result<()> {
}

#[test]
fn different_safety_levels() -> Result<()> {
fn display_different_safety_levels() -> Result<()> {
let mut cmd = Command::cargo_bin(BIN_NAME)?;

// `--fix` should only apply safe fixes, but should tell the user about `--fix --unsafe` if
Expand Down Expand Up @@ -381,7 +381,7 @@ Found 2 errors.
}

#[test]
fn only_unsafe_fixes_remain() -> Result<()> {
fn display_unsafe_fixes_remain() -> Result<()> {
let mut cmd = Command::cargo_bin(BIN_NAME)?;

let output = cmd
Expand All @@ -401,7 +401,7 @@ Found 1 error.
}

#[test]
fn applies_safe_fixes_only() -> Result<()> {
fn fix_applies_safe_fixes_only() -> Result<()> {
let mut cmd = Command::cargo_bin(BIN_NAME)?;

// `--fix` should only apply safe fixes. Since we're runnnig in `stdin` mode, output shouldn't
Expand All @@ -428,7 +428,7 @@ fn applies_safe_fixes_only() -> Result<()> {
}

#[test]
fn applies_all_fixes() -> Result<()> {
fn fix_applies_all_fixes() -> Result<()> {
let mut cmd = Command::cargo_bin(BIN_NAME)?;

// `--fix --unsafe` should apply both safe and unsafe fixes.
Expand All @@ -453,3 +453,120 @@ fn applies_all_fixes() -> Result<()> {

Ok(())
}

#[test]
fn diff_diffs_all_fixes() -> Result<()> {
let mut cmd = Command::cargo_bin(BIN_NAME)?;

// `--fix --unsafe` should apply both safe and unsafe fixes.
let output = cmd
.args([
"-",
"--format",
"text",
"--isolated",
"--select",
"F601,UP034",
"--diff",
"--unsafe",
])
.write_stdin("x = {'a': 1, 'a': 1}\nprint(('foo'))\n")
.assert()
.failure();
assert_eq!(
str::from_utf8(&output.get_output().stdout)?,
r#"@@ -1,2 +1,2 @@
-x = {'a': 1, 'a': 1}
-print(('foo'))
+x = {'a': 1}
+print('foo')
"#
);

Ok(())
}

#[test]
fn diff_diffs_safe_fixes_only() -> Result<()> {
let mut cmd = Command::cargo_bin(BIN_NAME)?;

// `--fix --unsafe` should apply both safe and unsafe fixes.
let output = cmd
.args([
"-",
"--format",
"text",
"--isolated",
"--select",
"F601,UP034",
"--diff",
])
.write_stdin("x = {'a': 1, 'a': 1}\nprint(('foo'))\n")
.assert()
.failure();
assert_eq!(
str::from_utf8(&output.get_output().stdout)?,
r#"@@ -1,2 +1,2 @@
x = {'a': 1, 'a': 1}
-print(('foo'))
+print('foo')
"#
);

Ok(())
}

#[test]
fn fix_only_applies_all_fixes() -> Result<()> {
let mut cmd = Command::cargo_bin(BIN_NAME)?;

// `--fix --unsafe` should apply both safe and unsafe fixes.
let output = cmd
.args([
"-",
"--format",
"text",
"--isolated",
"--select",
"F601,UP034",
"--fix-only",
"--unsafe",
])
.write_stdin("x = {'a': 1, 'a': 1}\nprint(('foo'))\n")
.assert()
.success();
assert_eq!(
str::from_utf8(&output.get_output().stdout)?,
"x = {'a': 1}\nprint('foo')\n"
);

Ok(())
}

#[test]
fn fix_only_applies_safe_fixes_only() -> Result<()> {
let mut cmd = Command::cargo_bin(BIN_NAME)?;

// `--fix --unsafe` should apply both safe and unsafe fixes.
let output = cmd
.args([
"-",
"--format",
"text",
"--isolated",
"--select",
"F601,UP034",
"--fix-only",
])
.write_stdin("x = {'a': 1, 'a': 1}\nprint(('foo'))\n")
.assert()
.success();
assert_eq!(
str::from_utf8(&output.get_output().stdout)?,
"x = {'a': 1, 'a': 1}\nprint('foo')\n"
);

Ok(())
}

0 comments on commit d1626a4

Please sign in to comment.