|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#[cfg(test)] |
| 9 | +mod tests { |
| 10 | + use ahash::AHashSet; |
| 11 | + use lifeguard::config::AnalysisConfig; |
| 12 | + use lifeguard::imports::ImportGraph; |
| 13 | + use lifeguard::project; |
| 14 | + use lifeguard::project::CachingMode; |
| 15 | + use lifeguard::pyrefly::module_name::ModuleName; |
| 16 | + use lifeguard::pyrefly::sys_info::PythonVersion; |
| 17 | + use lifeguard::test_lib::TestSources; |
| 18 | + |
| 19 | + fn py315() -> PythonVersion { |
| 20 | + PythonVersion::new(3, 15, 0) |
| 21 | + } |
| 22 | + |
| 23 | + fn side_effect_imports(modules: Vec<(&str, &str)>) -> Vec<(ModuleName, AHashSet<ModuleName>)> { |
| 24 | + let sources = TestSources::new_with_version(&modules, py315()); |
| 25 | + let config = AnalysisConfig::with_python_version(py315(), None); |
| 26 | + let (import_graph, exports) = ImportGraph::make_with_exports(&sources, &config); |
| 27 | + let output = project::run_analysis( |
| 28 | + &sources, |
| 29 | + &exports, |
| 30 | + &import_graph, |
| 31 | + &config, |
| 32 | + CachingMode::Disabled, |
| 33 | + ); |
| 34 | + |
| 35 | + output |
| 36 | + .side_effect_imports |
| 37 | + .into_iter() |
| 38 | + .filter(|(_, v)| !v.is_empty()) |
| 39 | + .collect() |
| 40 | + } |
| 41 | + |
| 42 | + fn side_effects_for(modules: Vec<(&str, &str)>, module_name: &str) -> AHashSet<ModuleName> { |
| 43 | + let all = side_effect_imports(modules); |
| 44 | + let name = ModuleName::from_str(module_name); |
| 45 | + all.into_iter() |
| 46 | + .find(|(k, _)| *k == name) |
| 47 | + .map(|(_, v)| v) |
| 48 | + .unwrap_or_default() |
| 49 | + } |
| 50 | + |
| 51 | + fn has_side_effect(modules: Vec<(&str, &str)>, module_name: &str, import: &str) -> bool { |
| 52 | + side_effects_for(modules, module_name).contains(&ModuleName::from_str(import)) |
| 53 | + } |
| 54 | + |
| 55 | + // --- Basic lazy import behavior --- |
| 56 | + |
| 57 | + #[test] |
| 58 | + fn test_eager_import_unused_is_side_effect() { |
| 59 | + assert!(has_side_effect( |
| 60 | + vec![("main", "import a"), ("a", "x = 1")], |
| 61 | + "main", |
| 62 | + "a", |
| 63 | + )); |
| 64 | + } |
| 65 | + |
| 66 | + #[test] |
| 67 | + fn test_lazy_import_unused_is_not_side_effect() { |
| 68 | + assert!(!has_side_effect( |
| 69 | + vec![("main", "lazy import a"), ("a", "x = 1")], |
| 70 | + "main", |
| 71 | + "a", |
| 72 | + )); |
| 73 | + } |
| 74 | + |
| 75 | + #[test] |
| 76 | + fn test_lazy_import_used_is_not_side_effect() { |
| 77 | + let code = "lazy import a\nx = a.foo"; |
| 78 | + assert!(!has_side_effect( |
| 79 | + vec![("main", code), ("a", "foo = 1")], |
| 80 | + "main", |
| 81 | + "a", |
| 82 | + )); |
| 83 | + } |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn test_eager_import_used_is_not_side_effect() { |
| 87 | + let code = "import a\nx = a.foo"; |
| 88 | + assert!(!has_side_effect( |
| 89 | + vec![("main", code), ("a", "foo = 1")], |
| 90 | + "main", |
| 91 | + "a", |
| 92 | + )); |
| 93 | + } |
| 94 | + |
| 95 | + // --- Lazy + eager interaction (order independent) --- |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn test_lazy_then_eager_import_unused_is_side_effect() { |
| 99 | + let code = "lazy import a\nimport a"; |
| 100 | + assert!(has_side_effect( |
| 101 | + vec![("main", code), ("a", "x = 1")], |
| 102 | + "main", |
| 103 | + "a", |
| 104 | + )); |
| 105 | + } |
| 106 | + |
| 107 | + #[test] |
| 108 | + fn test_eager_then_lazy_import_unused_is_side_effect() { |
| 109 | + let code = "import a\nlazy import a"; |
| 110 | + assert!(has_side_effect( |
| 111 | + vec![("main", code), ("a", "x = 1")], |
| 112 | + "main", |
| 113 | + "a", |
| 114 | + )); |
| 115 | + } |
| 116 | + |
| 117 | + // --- from-import variants --- |
| 118 | + |
| 119 | + #[test] |
| 120 | + fn test_lazy_from_import_unused_is_not_side_effect() { |
| 121 | + assert!(!has_side_effect( |
| 122 | + vec![("main", "lazy from a import b"), ("a", "b = 1")], |
| 123 | + "main", |
| 124 | + "a", |
| 125 | + )); |
| 126 | + } |
| 127 | + |
| 128 | + #[test] |
| 129 | + fn test_eager_from_import_unused_is_side_effect() { |
| 130 | + assert!(has_side_effect( |
| 131 | + vec![("main", "from a import b"), ("a", "b = 1")], |
| 132 | + "main", |
| 133 | + "a", |
| 134 | + )); |
| 135 | + } |
| 136 | + |
| 137 | + // --- Submodule interaction: lazy import a; import a.b --- |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn test_lazy_parent_with_eager_submodule_import() { |
| 141 | + let code = "lazy import a\nimport a.b"; |
| 142 | + let se = side_effects_for( |
| 143 | + vec![("main", code), ("a", "x = 1"), ("a.b", "y = 2")], |
| 144 | + "main", |
| 145 | + ); |
| 146 | + // `a` is only imported lazily at module level, so not a side-effect import. |
| 147 | + // `a.b` is imported eagerly and unused, so it IS a side-effect import. |
| 148 | + // (a's side effects flow through the a.b import chain in the import graph) |
| 149 | + assert!(!se.contains(&ModuleName::from_str("a"))); |
| 150 | + assert!(se.contains(&ModuleName::from_str("a.b"))); |
| 151 | + } |
| 152 | + |
| 153 | + // --- Multiple lazy imports --- |
| 154 | + |
| 155 | + #[test] |
| 156 | + fn test_all_lazy_imports_unused_no_side_effects() { |
| 157 | + let code = "lazy import a\nlazy import b"; |
| 158 | + let se = side_effects_for(vec![("main", code), ("a", "x = 1"), ("b", "y = 2")], "main"); |
| 159 | + assert!(se.is_empty()); |
| 160 | + } |
| 161 | + |
| 162 | + #[test] |
| 163 | + fn test_mixed_lazy_and_eager_unused() { |
| 164 | + let code = "lazy import a\nimport b"; |
| 165 | + let se = side_effects_for(vec![("main", code), ("a", "x = 1"), ("b", "y = 2")], "main"); |
| 166 | + assert!(!se.contains(&ModuleName::from_str("a"))); |
| 167 | + assert!(se.contains(&ModuleName::from_str("b"))); |
| 168 | + } |
| 169 | +} |
0 commit comments