Make show-settings filters directory-agnostic (#9866)

Closes https://github.com/astral-sh/ruff/issues/9864.
This commit is contained in:
Charlie Marsh 2024-02-06 19:20:27 -08:00 committed by GitHub
parent fdb5eefb33
commit e9ddd4819a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 47 additions and 26 deletions

View file

@ -31,9 +31,9 @@ pub(crate) fn show_settings(
let settings = resolver.resolve(&path); let settings = resolver.resolve(&path);
writeln!(writer, "Resolved settings for: {path:?}")?; writeln!(writer, "Resolved settings for: \"{}\"", path.display())?;
if let Some(settings_path) = pyproject_config.path.as_ref() { if let Some(settings_path) = pyproject_config.path.as_ref() {
writeln!(writer, "Settings path: {settings_path:?}")?; writeln!(writer, "Settings path: \"{}\"", settings_path.display())?;
} }
write!(writer, "{settings}")?; write!(writer, "{settings}")?;

View file

@ -4,25 +4,29 @@ use std::process::Command;
const BIN_NAME: &str = "ruff"; const BIN_NAME: &str = "ruff";
#[cfg(not(target_os = "windows"))]
const TEST_FILTERS: &[(&str, &str)] = &[
("\"[^\\*\"]*/pyproject.toml", "\"[BASEPATH]/pyproject.toml"),
("\".*/crates", "\"[BASEPATH]/crates"),
("\".*/\\.ruff_cache", "\"[BASEPATH]/.ruff_cache"),
("\".*/ruff\"", "\"[BASEPATH]\""),
];
#[cfg(target_os = "windows")]
const TEST_FILTERS: &[(&str, &str)] = &[
(r#""[^\*"]*\\pyproject.toml"#, "\"[BASEPATH]/pyproject.toml"),
(r#"".*\\crates"#, "\"[BASEPATH]/crates"),
(r#"".*\\\.ruff_cache"#, "\"[BASEPATH]/.ruff_cache"),
(r#"".*\\ruff""#, "\"[BASEPATH]\""),
(r#"\\+(\w\w|\s|")"#, "/$1"),
];
#[test] #[test]
fn display_default_settings() { fn display_default_settings() {
insta::with_settings!({ filters => TEST_FILTERS.to_vec() }, { // Navigate from the crate directory to the workspace root.
let base_path = Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.parent()
.unwrap();
let base_path = base_path.to_string_lossy();
// Escape the backslashes for the regex.
let base_path = regex::escape(&base_path);
#[cfg(not(target_os = "windows"))]
let test_filters = &[(base_path.as_ref(), "[BASEPATH]")];
#[cfg(target_os = "windows")]
let test_filters = &[
(base_path.as_ref(), "[BASEPATH]"),
(r#"\\+(\w\w|\s|\.|")"#, "/$1"),
];
insta::with_settings!({ filters => test_filters.to_vec() }, {
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
.args(["check", "--show-settings", "unformatted.py"]).current_dir(Path::new("./resources/test/fixtures"))); .args(["check", "--show-settings", "unformatted.py"]).current_dir(Path::new("./resources/test/fixtures")));
}); });

View file

@ -205,7 +205,9 @@ linter.external = []
linter.ignore_init_module_imports = false linter.ignore_init_module_imports = false
linter.logger_objects = [] linter.logger_objects = []
linter.namespace_packages = [] linter.namespace_packages = []
linter.src = ["[BASEPATH]"] linter.src = [
"[BASEPATH]",
]
linter.tab_size = 4 linter.tab_size = 4
linter.line_length = 88 linter.line_length = 88
linter.task_tags = [ linter.task_tags = [

View file

@ -123,6 +123,9 @@ macro_rules! display_settings {
(@field $fmt:ident, $prefix:ident, $settings:ident.$field:ident | debug) => { (@field $fmt:ident, $prefix:ident, $settings:ident.$field:ident | debug) => {
writeln!($fmt, "{}{} = {:?}", $prefix, stringify!($field), $settings.$field)?; writeln!($fmt, "{}{} = {:?}", $prefix, stringify!($field), $settings.$field)?;
}; };
(@field $fmt:ident, $prefix:ident, $settings:ident.$field:ident | path) => {
writeln!($fmt, "{}{} = \"{}\"", $prefix, stringify!($field), $settings.$field.display())?;
};
(@field $fmt:ident, $prefix:ident, $settings:ident.$field:ident | quoted) => { (@field $fmt:ident, $prefix:ident, $settings:ident.$field:ident | quoted) => {
writeln!($fmt, "{}{} = \"{}\"", $prefix, stringify!($field), $settings.$field)?; writeln!($fmt, "{}{} = \"{}\"", $prefix, stringify!($field), $settings.$field)?;
}; };
@ -152,6 +155,20 @@ macro_rules! display_settings {
} }
} }
}; };
(@field $fmt:ident, $prefix:ident, $settings:ident.$field:ident | paths) => {
{
write!($fmt, "{}{} = ", $prefix, stringify!($field))?;
if $settings.$field.is_empty() {
writeln!($fmt, "[]")?;
} else {
writeln!($fmt, "[")?;
for elem in &$settings.$field {
writeln!($fmt, "\t\"{}\",", elem.display())?;
}
writeln!($fmt, "]")?;
}
}
};
(@field $fmt:ident, $prefix:ident, $settings:ident.$field:ident) => { (@field $fmt:ident, $prefix:ident, $settings:ident.$field:ident) => {
writeln!($fmt, "{}{} = {}", $prefix, stringify!($field), $settings.$field)?; writeln!($fmt, "{}{} = {}", $prefix, stringify!($field), $settings.$field)?;
}; };
@ -220,7 +237,7 @@ impl Display for LinterSettings {
namespace = "linter", namespace = "linter",
fields = [ fields = [
self.exclude, self.exclude,
self.project_root | debug, self.project_root | path,
self.rules | nested, self.rules | nested,
self.per_file_ignores, self.per_file_ignores,
@ -238,7 +255,7 @@ impl Display for LinterSettings {
self.ignore_init_module_imports, self.ignore_init_module_imports,
self.logger_objects | array, self.logger_objects | array,
self.namespace_packages | debug, self.namespace_packages | debug,
self.src | debug, self.src | paths,
self.tab_size, self.tab_size,
self.line_length, self.line_length,
self.task_tags | array, self.task_tags | array,

View file

@ -60,9 +60,7 @@ impl fmt::Display for Settings {
display_settings! { display_settings! {
formatter = f, formatter = f,
fields = [ fields = [
// We want the quotes and lossy UTF8 conversion for this path, so self.cache_dir | path,
// using PathBuf's `Debug` formatter suffices.
self.cache_dir | debug,
self.fix, self.fix,
self.fix_only, self.fix_only,
self.output_format, self.output_format,
@ -101,7 +99,7 @@ impl fmt::Display for FileResolverSettings {
self.include, self.include,
self.extend_include, self.extend_include,
self.respect_gitignore, self.respect_gitignore,
self.project_root | debug, self.project_root | path,
] ]
} }
Ok(()) Ok(())