From a736d49942ba5e17ea5315d77ef3f1acdc2d0b2b Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 6 Feb 2024 16:39:53 -0500 Subject: [PATCH] Make show-settings filters directory-agnostic --- crates/ruff/tests/show_settings.rs | 42 ++++++++++++++++++------------ 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/crates/ruff/tests/show_settings.rs b/crates/ruff/tests/show_settings.rs index e2016e378542b3..3a4396700eeef0 100644 --- a/crates/ruff/tests/show_settings.rs +++ b/crates/ruff/tests/show_settings.rs @@ -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"))); });