Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Sep 26, 2024
1 parent d1b2bda commit c5b46f2
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 100 deletions.
6 changes: 6 additions & 0 deletions crates/ruff/tests/.analyze_graph.rs.pending-snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{"run_id":"1727350954-469459432","line":315,"new":{"module_name":"analyze_graph","snapshot_name":"nested_imports","metadata":{"source":"crates/ruff/tests/analyze_graph.rs","assertion_line":315,"info":{"program":"ruff","args":["analyze","graph","--preview"]}},"snapshot":"success: true\nexit_code: 0\n----- stdout -----\n{\n \"ruff/__init__.py\": [],\n \"ruff/a.py\": [],\n \"ruff/b.py\": [\n \"ruff/c.py\"\n ],\n \"ruff/c.py\": [],\n \"ruff/d.py\": []\n}\n\n----- stderr -----\n"},"old":{"module_name":"analyze_graph","metadata":{},"snapshot":"success: true\nexit_code: 0\n----- stdout -----\n{\n \"ruff/__init__.py\": [],\n \"ruff/a.py\": [\n \"ruff/b.py\"\n ],\n \"ruff/b.py\": [\n \"ruff/c.py\",\n \"ruff/d.py\"\n ],\n \"ruff/c.py\": [],\n \"ruff/d.py\": []\n}\n\n----- stderr -----"}}
{"run_id":"1727351009-933385109","line":255,"new":null,"old":null}
{"run_id":"1727351009-933385109","line":153,"new":null,"old":null}
{"run_id":"1727351009-933385109","line":69,"new":null,"old":null}
{"run_id":"1727351009-933385109","line":314,"new":null,"old":null}
{"run_id":"1727351009-933385109","line":202,"new":null,"old":null}
165 changes: 66 additions & 99 deletions crates/ruff/tests/analyze_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ const INSTA_FILTERS: &[(&str, &str)] = &[
(r"\\", "/"),
];

#[test]
fn dependencies() -> Result<()> {
let tempdir = TempDir::new()?;
let root = ChildPath::new(tempdir.path());

fn setup_sample_project(root: &ChildPath) -> Result<()> {
root.child("ruff").child("__init__.py").write_str("")?;
root.child("ruff")
.child("a.py")
Expand Down Expand Up @@ -57,6 +53,16 @@ fn dependencies() -> Result<()> {
def f(): pass
"#})?;

Ok(())
}

#[test]
fn dependencies() -> Result<()> {
let tempdir = TempDir::new()?;
let root = ChildPath::new(tempdir.path());

setup_sample_project(&root)?;

insta::with_settings!({
filters => INSTA_FILTERS.to_vec(),
}, {
Expand Down Expand Up @@ -121,100 +127,6 @@ fn dependents() -> Result<()> {
def f(): pass
"#})?;

insta::with_settings!({
filters => INSTA_FILTERS.to_vec(),
}, {
assert_cmd_snapshot!(command().arg("--direction").arg("dependents").current_dir(&root), @r###"
success: true
exit_code: 0
----- stdout -----
{
"ruff/__init__.py": [],
"ruff/a.py": [],
"ruff/b.py": [
"ruff/a.py"
],
"ruff/c.py": [
"ruff/b.py"
],
"ruff/d.py": [
"ruff/c.py"
],
"ruff/e.py": [
"ruff/d.py"
]
}
----- stderr -----
"###);
});

Ok(())
}

#[test]
fn string_detection() -> Result<()> {
let tempdir = TempDir::new()?;

let root = ChildPath::new(tempdir.path());

root.child("ruff").child("__init__.py").write_str("")?;
root.child("ruff")
.child("a.py")
.write_str(indoc::indoc! {r#"
import ruff.b
"#})?;
root.child("ruff")
.child("b.py")
.write_str(indoc::indoc! {r#"
import importlib
importlib.import_module("ruff.c")
"#})?;
root.child("ruff").child("c.py").write_str("")?;

insta::with_settings!({
filters => INSTA_FILTERS.to_vec(),
}, {
assert_cmd_snapshot!(command().current_dir(&root), @r###"
success: true
exit_code: 0
----- stdout -----
{
"ruff/__init__.py": [],
"ruff/a.py": [
"ruff/b.py"
],
"ruff/b.py": [],
"ruff/c.py": []
}
----- stderr -----
"###);
});

insta::with_settings!({
filters => INSTA_FILTERS.to_vec(),
}, {
assert_cmd_snapshot!(command().arg("--detect-string-imports").current_dir(&root), @r###"
success: true
exit_code: 0
----- stdout -----
{
"ruff/__init__.py": [],
"ruff/a.py": [
"ruff/b.py"
],
"ruff/b.py": [
"ruff/c.py"
],
"ruff/c.py": []
}
----- stderr -----
"###);
});

Ok(())
}

Expand Down Expand Up @@ -367,3 +279,58 @@ fn wildcard() -> Result<()> {

Ok(())
}

#[test]
fn nested_imports() -> Result<()> {
let tempdir = TempDir::new()?;
let root = ChildPath::new(tempdir.path());

root.child("ruff").child("__init__.py").write_str("")?;
root.child("ruff")
.child("a.py")
.write_str(indoc::indoc! {r#"
match x:
case 1:
import ruff.b
"#})?;
root.child("ruff")
.child("b.py")
.write_str(indoc::indoc! {r#"
try:
import ruff.c
except ImportError as e:
import ruff.d
"#})?;
root.child("ruff")
.child("c.py")
.write_str(indoc::indoc! {r#"def c(): ..."#})?;
root.child("ruff")
.child("d.py")
.write_str(indoc::indoc! {r#"def d(): ..."#})?;

insta::with_settings!({
filters => INSTA_FILTERS.to_vec(),
}, {
assert_cmd_snapshot!(command().current_dir(&root), @r#"
success: true
exit_code: 0
----- stdout -----
{
"ruff/__init__.py": [],
"ruff/a.py": [
"ruff/b.py"
],
"ruff/b.py": [
"ruff/c.py",
"ruff/d.py"
],
"ruff/c.py": [],
"ruff/d.py": []
}
----- stderr -----
"#);
});

Ok(())
}
2 changes: 1 addition & 1 deletion crates/ruff_graph/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl ModuleImports {
}

#[derive(Debug, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ImportMap(BTreeMap<SystemPathBuf, ModuleImports>);

impl ImportMap {
Expand Down

0 comments on commit c5b46f2

Please sign in to comment.