diff --git a/crates/ruff_cli/src/args.rs b/crates/ruff_cli/src/args.rs index ba2cca1b16558..8c9acaa67e0a8 100644 --- a/crates/ruff_cli/src/args.rs +++ b/crates/ruff_cli/src/args.rs @@ -507,6 +507,7 @@ impl CheckCommand { extend_exclude: self.extend_exclude, extend_fixable: self.extend_fixable, extend_ignore: self.extend_ignore, + extend_per_file_ignores: self.extend_per_file_ignores, extend_select: self.extend_select, extend_unfixable: self.extend_unfixable, fixable: self.fixable, @@ -627,6 +628,7 @@ pub struct CliOverrides { pub ignore: Option>, pub line_length: Option, pub per_file_ignores: Option>, + pub extend_per_file_ignores: Option>, pub preview: Option, pub respect_gitignore: Option, pub select: Option>, @@ -657,6 +659,12 @@ impl ConfigurationTransformer for CliOverrides { if let Some(extend_exclude) = &self.extend_exclude { config.extend_exclude.extend(extend_exclude.clone()); } + if let Some(extend_per_file_ignores) = &self.extend_per_file_ignores { + config + .lint + .extend_per_file_ignores + .extend(collect_per_file_ignores(extend_per_file_ignores.clone())); + } if let Some(fix) = &self.fix { config.fix = Some(*fix); } diff --git a/crates/ruff_cli/tests/lint.rs b/crates/ruff_cli/tests/lint.rs index f6a7a2a99f60f..5602926b95c4f 100644 --- a/crates/ruff_cli/tests/lint.rs +++ b/crates/ruff_cli/tests/lint.rs @@ -308,3 +308,87 @@ _ = "--------------------------------------------------------------------------- "###); Ok(()) } + +#[test] +fn per_file_ignores_stdin() -> Result<()> { + let tempdir = TempDir::new()?; + let ruff_toml = tempdir.path().join("ruff.toml"); + fs::write( + &ruff_toml, + r#" +extend-select = ["B", "Q"] + +[lint.flake8-quotes] +inline-quotes = "single" +"#, + )?; + + assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) + .current_dir(tempdir.path()) + .arg("check") + .args(STDIN_BASE_OPTIONS) + .args(["--config", &ruff_toml.file_name().unwrap().to_string_lossy()]) + .args(["--stdin-filename", "generated.py"]) + .args(["--per-file-ignores", "generated.py:Q"]) + .arg("-") + .pass_stdin(r#" +import os + +from test import say_hy + +if __name__ == "__main__": + say_hy("dear Ruff contributor") +"#), @r###" + success: false + exit_code: 1 + ----- stdout ----- + generated.py:2:8: F401 [*] `os` imported but unused + Found 1 error. + [*] 1 fixable with the `--fix` option. + + ----- stderr ----- + "###); + Ok(()) +} + +#[test] +fn extend_per_file_ignores_stdin() -> Result<()> { + let tempdir = TempDir::new()?; + let ruff_toml = tempdir.path().join("ruff.toml"); + fs::write( + &ruff_toml, + r#" +extend-select = ["B", "Q"] + +[lint.flake8-quotes] +inline-quotes = "single" +"#, + )?; + + assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) + .current_dir(tempdir.path()) + .arg("check") + .args(STDIN_BASE_OPTIONS) + .args(["--config", &ruff_toml.file_name().unwrap().to_string_lossy()]) + .args(["--stdin-filename", "generated.py"]) + .args(["--extend-per-file-ignores", "generated.py:Q"]) + .arg("-") + .pass_stdin(r#" +import os + +from test import say_hy + +if __name__ == "__main__": + say_hy("dear Ruff contributor") +"#), @r###" + success: false + exit_code: 1 + ----- stdout ----- + generated.py:2:8: F401 [*] `os` imported but unused + Found 1 error. + [*] 1 fixable with the `--fix` option. + + ----- stderr ----- + "###); + Ok(()) +}