Skip to content

Commit

Permalink
Make show-settings filters directory-agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Feb 6, 2024
1 parent daae28e commit a736d49
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions crates/ruff/tests/show_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,33 @@ use std::process::Command;

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]
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();
println!("base_path: {:?}", base_path);
let base_path = base_path.to_string_lossy();

#[cfg(not(target_os = "windows"))]
let test_filters = &[(base_path.as_ref(), "[BASEPATH]")];

#[cfg(target_os = "windows")]
// Escape the backslashes for the regex.
let foo = base_path.replace("\\", "\\\\");
println!("base_path: {:?}", base_path);

#[cfg(target_os = "windows")]
let test_filters = &[
(base_path.as_ref(), "[BASEPATH]"),
(foo.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))
.args(["check", "--show-settings", "unformatted.py"]).current_dir(Path::new("./resources/test/fixtures")));
});
Expand Down

0 comments on commit a736d49

Please sign in to comment.