Skip to content

Commit d8ff742

Browse files
committed
Fix analyze graph tests on windows
1 parent 665f680 commit d8ff742

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

crates/ruff/tests/cli/analyze_graph.rs

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
use std::process::Command;
2+
13
use insta_cmd::assert_cmd_snapshot;
24

35
use crate::CliTest;
46

57
#[test]
68
fn type_checking_imports() -> anyhow::Result<()> {
7-
let test = CliTest::with_files([
9+
let test = AnalyzeTest::with_files([
810
("ruff/__init__.py", ""),
911
(
1012
"ruff/a.py",
@@ -27,7 +29,7 @@ fn type_checking_imports() -> anyhow::Result<()> {
2729
("ruff/c.py", ""),
2830
])?;
2931

30-
assert_cmd_snapshot!(test.analyze_graph_command(), @r###"
32+
assert_cmd_snapshot!(test.command(), @r###"
3133
success: true
3234
exit_code: 0
3335
----- stdout -----
@@ -47,7 +49,7 @@ fn type_checking_imports() -> anyhow::Result<()> {
4749
"###);
4850

4951
assert_cmd_snapshot!(
50-
test.analyze_graph_command()
52+
test.command()
5153
.arg("--no-type-checking-imports"),
5254
@r###"
5355
success: true
@@ -71,7 +73,7 @@ fn type_checking_imports() -> anyhow::Result<()> {
7173

7274
#[test]
7375
fn type_checking_imports_from_config() -> anyhow::Result<()> {
74-
let test = CliTest::with_files([
76+
let test = AnalyzeTest::with_files([
7577
("ruff/__init__.py", ""),
7678
(
7779
"ruff/a.py",
@@ -101,7 +103,7 @@ fn type_checking_imports_from_config() -> anyhow::Result<()> {
101103
),
102104
])?;
103105

104-
assert_cmd_snapshot!(test.analyze_graph_command(), @r###"
106+
assert_cmd_snapshot!(test.command(), @r###"
105107
success: true
106108
exit_code: 0
107109
----- stdout -----
@@ -125,7 +127,7 @@ fn type_checking_imports_from_config() -> anyhow::Result<()> {
125127
"#,
126128
)?;
127129

128-
assert_cmd_snapshot!(test.analyze_graph_command(), @r###"
130+
assert_cmd_snapshot!(test.command(), @r###"
129131
success: true
130132
exit_code: 0
131133
----- stdout -----
@@ -148,10 +150,44 @@ fn type_checking_imports_from_config() -> anyhow::Result<()> {
148150
Ok(())
149151
}
150152

151-
impl CliTest {
152-
fn analyze_graph_command(&self) -> std::process::Command {
153-
let mut command = self.command();
153+
struct AnalyzeTest {
154+
cli_test: CliTest,
155+
}
156+
157+
impl AnalyzeTest {
158+
pub(crate) fn new() -> anyhow::Result<Self> {
159+
Ok(Self {
160+
cli_test: CliTest::with_settings(|_, mut settings| {
161+
settings.add_filter(r#"\\"#, "/");
162+
settings
163+
})?,
164+
})
165+
}
166+
167+
fn with_files<'a>(files: impl IntoIterator<Item = (&'a str, &'a str)>) -> anyhow::Result<Self> {
168+
let case = Self::new()?;
169+
case.write_files(files)?;
170+
Ok(case)
171+
}
172+
173+
#[expect(unused)]
174+
fn with_file(path: impl AsRef<std::path::Path>, content: &str) -> anyhow::Result<Self> {
175+
let fixture = Self::new()?;
176+
fixture.write_file(path, content)?;
177+
Ok(fixture)
178+
}
179+
180+
fn command(&self) -> Command {
181+
let mut command = self.cli_test.command();
154182
command.arg("analyze").arg("graph").arg("--preview");
155183
command
156184
}
157185
}
186+
187+
impl std::ops::Deref for AnalyzeTest {
188+
type Target = CliTest;
189+
190+
fn deref(&self) -> &Self::Target {
191+
&self.cli_test
192+
}
193+
}

crates/ruff/tests/cli/main.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ impl CliTest {
6363
files: impl IntoIterator<Item = (&'a str, &'a str)>,
6464
) -> anyhow::Result<Self> {
6565
let case = Self::new()?;
66-
for file in files {
67-
case.write_file(file.0, file.1)?;
68-
}
66+
case.write_files(files)?;
6967
Ok(case)
7068
}
7169

@@ -154,6 +152,16 @@ impl CliTest {
154152
Ok(())
155153
}
156154

155+
pub(crate) fn write_files<'a>(
156+
&self,
157+
files: impl IntoIterator<Item = (&'a str, &'a str)>,
158+
) -> Result<()> {
159+
for file in files {
160+
self.write_file(file.0, file.1)?;
161+
}
162+
Ok(())
163+
}
164+
157165
/// Returns the path to the test directory root.
158166
pub(crate) fn root(&self) -> &Path {
159167
&self.project_dir

0 commit comments

Comments
 (0)