Skip to content

Commit 1dd2848

Browse files
committed
Filter for Windows (sob)
1 parent 6bfa639 commit 1dd2848

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

crates/ty/tests/cli/main.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod python_environment;
55
mod rule_selection;
66

77
use anyhow::Context as _;
8+
use insta::Settings;
89
use insta::internals::SettingsBindDropGuard;
910
use insta_cmd::{assert_cmd_snapshot, get_cargo_bin};
1011
use std::{
@@ -760,7 +761,8 @@ fn can_handle_large_binop_expressions() -> anyhow::Result<()> {
760761

761762
pub(crate) struct CliTest {
762763
_temp_dir: TempDir,
763-
_settings_scope: SettingsBindDropGuard,
764+
settings: Settings,
765+
settings_scope: Option<SettingsBindDropGuard>,
764766
project_dir: PathBuf,
765767
ty_binary_path: PathBuf,
766768
}
@@ -795,7 +797,8 @@ impl CliTest {
795797
Ok(Self {
796798
project_dir,
797799
_temp_dir: temp_dir,
798-
_settings_scope: settings_scope,
800+
settings,
801+
settings_scope: Some(settings_scope),
799802
ty_binary_path: get_cargo_bin("ty"),
800803
})
801804
}
@@ -838,6 +841,17 @@ impl CliTest {
838841
Ok(self)
839842
}
840843

844+
/// Add a filter to the settings and rebind them.
845+
pub(crate) fn with_filter(mut self, pattern: &str, replacement: &str) -> Self {
846+
self.settings.add_filter(pattern, replacement);
847+
// Drop the old scope before binding a new one, otherwise the old scope is dropped _after_
848+
// binding and assigning the new one, restoring the settings to their state before the old
849+
// scope was bound.
850+
drop(self.settings_scope.take());
851+
self.settings_scope = Some(self.settings.bind_to_scope());
852+
self
853+
}
854+
841855
fn ensure_parent_directory(path: &Path) -> anyhow::Result<()> {
842856
if let Some(parent) = path.parent() {
843857
std::fs::create_dir_all(parent)
@@ -896,3 +910,11 @@ impl CliTest {
896910
fn tempdir_filter(path: &Path) -> String {
897911
format!(r"{}\\?/?", regex::escape(path.to_str().unwrap()))
898912
}
913+
914+
fn site_packages_filter(python_version: &str) -> String {
915+
if cfg!(windows) {
916+
"Lib/site-packages".to_string()
917+
} else {
918+
format!("lib/python{}/site-packages", regex::escape(python_version))
919+
}
920+
}

crates/ty/tests/cli/python_environment.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use insta_cmd::assert_cmd_snapshot;
22
use ruff_python_ast::PythonVersion;
33

4-
use crate::CliTest;
4+
use crate::{CliTest, site_packages_filter};
55

66
/// Specifying an option on the CLI should take precedence over the same setting in the
77
/// project's configuration. Here, this is tested for the Python version.
@@ -1838,13 +1838,13 @@ fn ty_environment_and_active_environment() -> anyhow::Result<()> {
18381838
",
18391839
),
18401840
])?
1841-
.with_ty_at(ty_executable_path)?;
1841+
.with_ty_at(ty_executable_path)?
1842+
.with_filter(&site_packages_filter("3.13"), "<site-packages>");
18421843

1843-
// `ty_package` should not be found
18441844
assert_cmd_snapshot!(
18451845
case.command()
18461846
.env("VIRTUAL_ENV", case.root().join("active-venv")),
1847-
@r###"
1847+
@r"
18481848
success: false
18491849
exit_code: 1
18501850
----- stdout -----
@@ -1858,14 +1858,14 @@ fn ty_environment_and_active_environment() -> anyhow::Result<()> {
18581858
info: Searched in the following paths during module resolution:
18591859
info: 1. <temp_dir>/ (first-party code)
18601860
info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
1861-
info: 3. <temp_dir>/active-venv/lib/python3.13/site-packages (site-packages)
1861+
info: 3. <temp_dir>/active-venv/<site-packages> (site-packages)
18621862
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
18631863
info: rule `unresolved-import` is enabled by default
18641864
18651865
Found 1 diagnostic
18661866
18671867
----- stderr -----
1868-
"###
1868+
"
18691869
);
18701870

18711871
Ok(())

0 commit comments

Comments
 (0)