diff --git a/crates/ruff/tests/.analyze_graph.rs.pending-snap b/crates/ruff/tests/.analyze_graph.rs.pending-snap new file mode 100644 index 0000000000000..c1eecc7b3269a --- /dev/null +++ b/crates/ruff/tests/.analyze_graph.rs.pending-snap @@ -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} diff --git a/crates/ruff/tests/analyze_graph.rs b/crates/ruff/tests/analyze_graph.rs index 28cfba740aed4..3133476636465 100644 --- a/crates/ruff/tests/analyze_graph.rs +++ b/crates/ruff/tests/analyze_graph.rs @@ -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") @@ -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(), }, { @@ -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(()) } @@ -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(()) +} diff --git a/crates/ruff_graph/src/lib.rs b/crates/ruff_graph/src/lib.rs index 260855fb31313..0d6a6669bafb6 100644 --- a/crates/ruff_graph/src/lib.rs +++ b/crates/ruff_graph/src/lib.rs @@ -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); impl ImportMap {