Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 45 additions & 9 deletions crates/ruff/tests/cli/analyze_graph.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::process::Command;

use insta_cmd::assert_cmd_snapshot;

use crate::CliTest;

#[test]
fn type_checking_imports() -> anyhow::Result<()> {
let test = CliTest::with_files([
let test = AnalyzeTest::with_files([
("ruff/__init__.py", ""),
(
"ruff/a.py",
Expand All @@ -27,7 +29,7 @@ fn type_checking_imports() -> anyhow::Result<()> {
("ruff/c.py", ""),
])?;

assert_cmd_snapshot!(test.analyze_graph_command(), @r###"
assert_cmd_snapshot!(test.command(), @r###"
success: true
exit_code: 0
----- stdout -----
Expand All @@ -47,7 +49,7 @@ fn type_checking_imports() -> anyhow::Result<()> {
"###);

assert_cmd_snapshot!(
test.analyze_graph_command()
test.command()
.arg("--no-type-checking-imports"),
@r###"
success: true
Expand All @@ -71,7 +73,7 @@ fn type_checking_imports() -> anyhow::Result<()> {

#[test]
fn type_checking_imports_from_config() -> anyhow::Result<()> {
let test = CliTest::with_files([
let test = AnalyzeTest::with_files([
("ruff/__init__.py", ""),
(
"ruff/a.py",
Expand Down Expand Up @@ -101,7 +103,7 @@ fn type_checking_imports_from_config() -> anyhow::Result<()> {
),
])?;

assert_cmd_snapshot!(test.analyze_graph_command(), @r###"
assert_cmd_snapshot!(test.command(), @r###"
success: true
exit_code: 0
----- stdout -----
Expand All @@ -125,7 +127,7 @@ fn type_checking_imports_from_config() -> anyhow::Result<()> {
"#,
)?;

assert_cmd_snapshot!(test.analyze_graph_command(), @r###"
assert_cmd_snapshot!(test.command(), @r###"
success: true
exit_code: 0
----- stdout -----
Expand All @@ -148,10 +150,44 @@ fn type_checking_imports_from_config() -> anyhow::Result<()> {
Ok(())
}

impl CliTest {
fn analyze_graph_command(&self) -> std::process::Command {
let mut command = self.command();
struct AnalyzeTest {
cli_test: CliTest,
}

impl AnalyzeTest {
pub(crate) fn new() -> anyhow::Result<Self> {
Ok(Self {
cli_test: CliTest::with_settings(|_, mut settings| {
settings.add_filter(r#"\\\\"#, "/");
settings
})?,
})
}

fn with_files<'a>(files: impl IntoIterator<Item = (&'a str, &'a str)>) -> anyhow::Result<Self> {
let case = Self::new()?;
case.write_files(files)?;
Ok(case)
}

#[expect(unused)]
fn with_file(path: impl AsRef<std::path::Path>, content: &str) -> anyhow::Result<Self> {
let fixture = Self::new()?;
fixture.write_file(path, content)?;
Ok(fixture)
}

fn command(&self) -> Command {
let mut command = self.cli_test.command();
command.arg("analyze").arg("graph").arg("--preview");
command
}
}

impl std::ops::Deref for AnalyzeTest {
type Target = CliTest;

fn deref(&self) -> &Self::Target {
&self.cli_test
}
}
14 changes: 11 additions & 3 deletions crates/ruff/tests/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ impl CliTest {
files: impl IntoIterator<Item = (&'a str, &'a str)>,
) -> anyhow::Result<Self> {
let case = Self::new()?;
for file in files {
case.write_file(file.0, file.1)?;
}
case.write_files(files)?;
Ok(case)
}

Expand Down Expand Up @@ -154,6 +152,16 @@ impl CliTest {
Ok(())
}

pub(crate) fn write_files<'a>(
&self,
files: impl IntoIterator<Item = (&'a str, &'a str)>,
) -> Result<()> {
for file in files {
self.write_file(file.0, file.1)?;
}
Ok(())
}

/// Returns the path to the test directory root.
pub(crate) fn root(&self) -> &Path {
&self.project_dir
Expand Down