pytest_language_server/fixtures/
imports.rs

1//! Fixture import resolution.
2//!
3//! This module handles tracking and resolving fixtures that are imported
4//! into conftest.py or test files via `from X import *` or explicit imports.
5//!
6//! When a conftest.py has `from .pytest_fixtures import *`, all fixtures
7//! defined in that module become available as if they were defined in the
8//! conftest.py itself.
9
10use super::FixtureDatabase;
11use once_cell::sync::Lazy;
12use rustpython_parser::ast::Stmt;
13use std::collections::HashSet;
14use std::path::{Path, PathBuf};
15use std::sync::Arc;
16use tracing::{debug, info};
17
18/// Static HashSet of standard library module names for O(1) lookup.
19static STDLIB_MODULES: Lazy<HashSet<&'static str>> = Lazy::new(|| {
20    [
21        "os",
22        "sys",
23        "re",
24        "json",
25        "typing",
26        "collections",
27        "functools",
28        "itertools",
29        "pathlib",
30        "datetime",
31        "time",
32        "math",
33        "random",
34        "copy",
35        "io",
36        "abc",
37        "contextlib",
38        "dataclasses",
39        "enum",
40        "logging",
41        "unittest",
42        "asyncio",
43        "concurrent",
44        "multiprocessing",
45        "threading",
46        "subprocess",
47        "shutil",
48        "tempfile",
49        "glob",
50        "fnmatch",
51        "pickle",
52        "sqlite3",
53        "urllib",
54        "http",
55        "email",
56        "html",
57        "xml",
58        "socket",
59        "ssl",
60        "select",
61        "signal",
62        "struct",
63        "codecs",
64        "textwrap",
65        "string",
66        "difflib",
67        "inspect",
68        "dis",
69        "traceback",
70        "warnings",
71        "weakref",
72        "types",
73        "importlib",
74        "pkgutil",
75        "pprint",
76        "reprlib",
77        "numbers",
78        "decimal",
79        "fractions",
80        "statistics",
81        "hashlib",
82        "hmac",
83        "secrets",
84        "base64",
85        "binascii",
86        "zlib",
87        "gzip",
88        "bz2",
89        "lzma",
90        "zipfile",
91        "tarfile",
92        "csv",
93        "configparser",
94        "argparse",
95        "getopt",
96        "getpass",
97        "platform",
98        "errno",
99        "ctypes",
100        "__future__",
101    ]
102    .into_iter()
103    .collect()
104});
105
106/// Represents a fixture import in a Python file.
107#[derive(Debug, Clone)]
108#[allow(dead_code)] // Fields used for debugging and potential future features
109pub struct FixtureImport {
110    /// The module path being imported from (e.g., ".pytest_fixtures" or "pytest_fixtures")
111    pub module_path: String,
112    /// Whether this is a star import (`from X import *`)
113    pub is_star_import: bool,
114    /// Specific names imported (empty for star imports)
115    pub imported_names: Vec<String>,
116    /// The file that contains this import
117    pub importing_file: PathBuf,
118    /// Line number of the import statement
119    pub line: usize,
120}
121
122impl FixtureDatabase {
123    /// Extract fixture imports from a module's statements.
124    /// Returns a list of imports that could potentially bring in fixtures.
125    pub(crate) fn extract_fixture_imports(
126        &self,
127        stmts: &[Stmt],
128        file_path: &Path,
129        line_index: &[usize],
130    ) -> Vec<FixtureImport> {
131        let mut imports = Vec::new();
132
133        for stmt in stmts {
134            if let Stmt::ImportFrom(import_from) = stmt {
135                // Skip imports from standard library or well-known non-fixture modules
136                let mut module = import_from
137                    .module
138                    .as_ref()
139                    .map(|m| m.to_string())
140                    .unwrap_or_default();
141
142                // Add leading dots for relative imports
143                if let Some(_level) = import_from.level {
144                    // For now, assume level is 1 (one dot) - TODO: find proper way to extract Int value
145                    module = ".".to_string() + &module;
146                }
147
148                // Skip obvious non-fixture imports
149                if self.is_standard_library_module(&module) {
150                    continue;
151                }
152
153                let line =
154                    self.get_line_from_offset(import_from.range.start().to_usize(), line_index);
155
156                // Check if this is a star import
157                let is_star = import_from
158                    .names
159                    .iter()
160                    .any(|alias| alias.name.as_str() == "*");
161
162                if is_star {
163                    imports.push(FixtureImport {
164                        module_path: module,
165                        is_star_import: true,
166                        imported_names: Vec::new(),
167                        importing_file: file_path.to_path_buf(),
168                        line,
169                    });
170                } else {
171                    // Collect specific imported names
172                    let names: Vec<String> = import_from
173                        .names
174                        .iter()
175                        .map(|alias| alias.asname.as_ref().unwrap_or(&alias.name).to_string())
176                        .collect();
177
178                    if !names.is_empty() {
179                        imports.push(FixtureImport {
180                            module_path: module,
181                            is_star_import: false,
182                            imported_names: names,
183                            importing_file: file_path.to_path_buf(),
184                            line,
185                        });
186                    }
187                }
188            }
189        }
190
191        imports
192    }
193
194    /// Check if a module is a standard library module that can't contain fixtures.
195    /// Uses a static HashSet for O(1) lookup instead of linear array search.
196    fn is_standard_library_module(&self, module: &str) -> bool {
197        let first_part = module.split('.').next().unwrap_or(module);
198        STDLIB_MODULES.contains(first_part)
199    }
200
201    /// Resolve a module path to a file path.
202    /// Handles both relative imports (starting with .) and absolute imports.
203    pub(crate) fn resolve_module_to_file(
204        &self,
205        module_path: &str,
206        importing_file: &Path,
207    ) -> Option<PathBuf> {
208        debug!(
209            "Resolving module '{}' from file {:?}",
210            module_path, importing_file
211        );
212
213        let parent_dir = importing_file.parent()?;
214
215        if module_path.starts_with('.') {
216            // Relative import
217            self.resolve_relative_import(module_path, parent_dir)
218        } else {
219            // Absolute import - search in the same directory tree
220            self.resolve_absolute_import(module_path, parent_dir)
221        }
222    }
223
224    /// Resolve a relative import like `.pytest_fixtures` or `..utils`.
225    fn resolve_relative_import(&self, module_path: &str, base_dir: &Path) -> Option<PathBuf> {
226        let mut current_dir = base_dir.to_path_buf();
227        let mut chars = module_path.chars().peekable();
228
229        // Count leading dots to determine how many directories to go up
230        while chars.peek() == Some(&'.') {
231            chars.next();
232            if chars.peek() != Some(&'.') {
233                // Single dot - stay in current directory
234                break;
235            }
236            // Additional dots - go up one directory
237            current_dir = current_dir.parent()?.to_path_buf();
238        }
239
240        let remaining: String = chars.collect();
241        if remaining.is_empty() {
242            // Import from __init__.py of current/parent package
243            let init_path = current_dir.join("__init__.py");
244            if init_path.exists() {
245                return Some(init_path);
246            }
247            return None;
248        }
249
250        self.find_module_file(&remaining, &current_dir)
251    }
252
253    /// Resolve an absolute import by searching up the directory tree.
254    fn resolve_absolute_import(&self, module_path: &str, start_dir: &Path) -> Option<PathBuf> {
255        let mut current_dir = start_dir.to_path_buf();
256
257        loop {
258            if let Some(path) = self.find_module_file(module_path, &current_dir) {
259                return Some(path);
260            }
261
262            // Go up one directory
263            match current_dir.parent() {
264                Some(parent) => current_dir = parent.to_path_buf(),
265                None => break,
266            }
267        }
268
269        None
270    }
271
272    /// Find a module file given a dotted path and base directory.
273    fn find_module_file(&self, module_path: &str, base_dir: &Path) -> Option<PathBuf> {
274        let parts: Vec<&str> = module_path.split('.').collect();
275        let mut current_path = base_dir.to_path_buf();
276
277        for (i, part) in parts.iter().enumerate() {
278            let is_last = i == parts.len() - 1;
279
280            if is_last {
281                // Last part - could be a module file or a package
282                let py_file = current_path.join(format!("{}.py", part));
283                if py_file.exists() {
284                    return Some(py_file);
285                }
286
287                // Also check if the file is in the cache (for test files that don't exist on disk)
288                let canonical_py_file = self.get_canonical_path(py_file.clone());
289                if self.file_cache.contains_key(&canonical_py_file) {
290                    return Some(py_file);
291                }
292
293                // Check if it's a package with __init__.py
294                let package_init = current_path.join(part).join("__init__.py");
295                if package_init.exists() {
296                    return Some(package_init);
297                }
298
299                // Also check if the package __init__.py is in the cache
300                let canonical_package_init = self.get_canonical_path(package_init.clone());
301                if self.file_cache.contains_key(&canonical_package_init) {
302                    return Some(package_init);
303                }
304            } else {
305                // Not the last part - must be a directory
306                current_path = current_path.join(part);
307                if !current_path.is_dir() {
308                    return None;
309                }
310            }
311        }
312
313        None
314    }
315
316    /// Get fixtures that are re-exported from a file via imports.
317    /// This handles `from .module import *` patterns that bring fixtures into scope.
318    ///
319    /// Results are cached with content-hash and definitions-version based invalidation.
320    /// Returns fixture names that are available in `file_path` via imports.
321    pub fn get_imported_fixtures(
322        &self,
323        file_path: &Path,
324        visited: &mut HashSet<PathBuf>,
325    ) -> HashSet<String> {
326        let canonical_path = self.get_canonical_path(file_path.to_path_buf());
327
328        // Prevent circular imports
329        if visited.contains(&canonical_path) {
330            debug!("Circular import detected for {:?}, skipping", file_path);
331            return HashSet::new();
332        }
333        visited.insert(canonical_path.clone());
334
335        // Get the file content first (needed for cache validation)
336        let Some(content) = self.get_file_content(&canonical_path) else {
337            return HashSet::new();
338        };
339
340        let content_hash = Self::hash_content(&content);
341        let current_version = self
342            .definitions_version
343            .load(std::sync::atomic::Ordering::SeqCst);
344
345        // Check cache - valid if both content hash and definitions version match
346        if let Some(cached) = self.imported_fixtures_cache.get(&canonical_path) {
347            let (cached_content_hash, cached_version, cached_fixtures) = cached.value();
348            if *cached_content_hash == content_hash && *cached_version == current_version {
349                debug!("Cache hit for imported fixtures in {:?}", canonical_path);
350                return cached_fixtures.as_ref().clone();
351            }
352        }
353
354        // Compute imported fixtures
355        let imported_fixtures = self.compute_imported_fixtures(&canonical_path, &content, visited);
356
357        // Store in cache
358        self.imported_fixtures_cache.insert(
359            canonical_path.clone(),
360            (
361                content_hash,
362                current_version,
363                Arc::new(imported_fixtures.clone()),
364            ),
365        );
366
367        info!(
368            "Found {} imported fixtures for {:?}: {:?}",
369            imported_fixtures.len(),
370            file_path,
371            imported_fixtures
372        );
373
374        imported_fixtures
375    }
376
377    /// Internal method to compute imported fixtures without caching.
378    fn compute_imported_fixtures(
379        &self,
380        canonical_path: &Path,
381        content: &str,
382        visited: &mut HashSet<PathBuf>,
383    ) -> HashSet<String> {
384        let mut imported_fixtures = HashSet::new();
385
386        let Some(parsed) = self.get_parsed_ast(canonical_path, content) else {
387            return imported_fixtures;
388        };
389
390        let line_index = self.get_line_index(canonical_path, content);
391
392        if let rustpython_parser::ast::Mod::Module(module) = parsed.as_ref() {
393            let imports = self.extract_fixture_imports(&module.body, canonical_path, &line_index);
394
395            for import in imports {
396                // Resolve the import to a file path
397                let Some(resolved_path) =
398                    self.resolve_module_to_file(&import.module_path, canonical_path)
399                else {
400                    debug!(
401                        "Could not resolve module '{}' from {:?}",
402                        import.module_path, canonical_path
403                    );
404                    continue;
405                };
406
407                let resolved_canonical = self.get_canonical_path(resolved_path);
408
409                debug!(
410                    "Resolved import '{}' to {:?}",
411                    import.module_path, resolved_canonical
412                );
413
414                if import.is_star_import {
415                    // Star import: get all fixtures from the resolved file
416                    // First, get fixtures defined directly in that file
417                    if let Some(file_fixtures) = self.file_definitions.get(&resolved_canonical) {
418                        for fixture_name in file_fixtures.iter() {
419                            imported_fixtures.insert(fixture_name.clone());
420                        }
421                    }
422
423                    // Also recursively get fixtures imported into that file
424                    let transitive = self.get_imported_fixtures(&resolved_canonical, visited);
425                    imported_fixtures.extend(transitive);
426                } else {
427                    // Explicit import: only include the specified names if they are fixtures
428                    for name in &import.imported_names {
429                        if self.definitions.contains_key(name) {
430                            imported_fixtures.insert(name.clone());
431                        }
432                    }
433                }
434            }
435        }
436
437        imported_fixtures
438    }
439
440    /// Check if a fixture is available in a file via imports.
441    /// This is used in resolution to check conftest.py files that import fixtures.
442    pub fn is_fixture_imported_in_file(&self, fixture_name: &str, file_path: &Path) -> bool {
443        let mut visited = HashSet::new();
444        let imported = self.get_imported_fixtures(file_path, &mut visited);
445        imported.contains(fixture_name)
446    }
447}