probe_code/language/
test_detection.rs

1use std::path::Path;
2
3/// Function to determine if a file is a test file based on common naming conventions and directory patterns
4pub fn is_test_file(path: &Path) -> bool {
5    let _debug_mode = std::env::var("DEBUG").unwrap_or_default() == "1";
6
7    // Check file name patterns
8    if let Some(file_name) = path.file_name().and_then(|f| f.to_str()) {
9        // Rust: *_test.rs, *_tests.rs, test_*.rs, tests.rs
10        if file_name.ends_with("_test.rs")
11            || file_name.ends_with("_tests.rs")
12            || file_name.starts_with("test_")
13            || file_name == "tests.rs"
14        {
15            if _debug_mode {
16                println!("DEBUG: Test file detected (Rust pattern): {file_name}");
17            }
18            return true;
19        }
20
21        // JavaScript/TypeScript: *.test.js, *.spec.js, *.test.ts, *.spec.ts
22        if file_name.ends_with(".test.js")
23            || file_name.ends_with(".spec.js")
24            || file_name.ends_with(".test.jsx")
25            || file_name.ends_with(".spec.jsx")
26            || file_name.ends_with(".test.ts")
27            || file_name.ends_with(".spec.ts")
28            || file_name.ends_with(".test.tsx")
29            || file_name.ends_with(".spec.tsx")
30        {
31            if _debug_mode {
32                println!("DEBUG: Test file detected (JS/TS pattern): {file_name}");
33            }
34            return true;
35        }
36
37        // Python: test_*.py, *_test.py
38        if file_name.starts_with("test_") && file_name.ends_with(".py")
39            || file_name.ends_with("_test.py")
40        {
41            if _debug_mode {
42                println!("DEBUG: Test file detected (Python pattern): {file_name}");
43            }
44            return true;
45        }
46
47        // Go: *_test.go
48        if file_name.ends_with("_test.go") {
49            if _debug_mode {
50                println!("DEBUG: Test file detected (Go pattern): {file_name}");
51            }
52            return true;
53        }
54
55        // C/C++: test_*.c, *_test.c, *_tests.c, test_*.cpp, *_test.cpp, *_tests.cpp
56        if (file_name.starts_with("test_")
57            || file_name.ends_with("_test.c")
58            || file_name.ends_with("_tests.c"))
59            && (file_name.ends_with(".c") || file_name.ends_with(".h"))
60            || (file_name.starts_with("test_")
61                || file_name.ends_with("_test.cpp")
62                || file_name.ends_with("_tests.cpp"))
63                && (file_name.ends_with(".cpp")
64                    || file_name.ends_with(".hpp")
65                    || file_name.ends_with(".cc")
66                    || file_name.ends_with(".hxx")
67                    || file_name.ends_with(".cxx"))
68        {
69            if _debug_mode {
70                println!("DEBUG: Test file detected (C/C++ pattern): {file_name}");
71            }
72            return true;
73        }
74
75        // Java: *Test.java, Test*.java
76        if file_name.ends_with("Test.java")
77            || file_name.starts_with("Test") && file_name.ends_with(".java")
78        {
79            if _debug_mode {
80                println!("DEBUG: Test file detected (Java pattern): {file_name}");
81            }
82            return true;
83        }
84
85        // Ruby: test_*.rb, *_test.rb, *_spec.rb
86        if file_name.starts_with("test_") && file_name.ends_with(".rb")
87            || file_name.ends_with("_test.rb")
88            || file_name.ends_with("_spec.rb")
89        {
90            if _debug_mode {
91                println!("DEBUG: Test file detected (Ruby pattern): {file_name}");
92            }
93            return true;
94        }
95
96        // PHP: *Test.php, Test*.php
97        if file_name.ends_with("Test.php")
98            || file_name.starts_with("Test") && file_name.ends_with(".php")
99        {
100            if _debug_mode {
101                println!("DEBUG: Test file detected (PHP pattern): {file_name}");
102            }
103            return true;
104        }
105    }
106
107    // Check directory patterns
108    let path_str = path.to_string_lossy();
109
110    // Common test directory patterns across languages
111    if path_str.contains("/test/")
112        || path_str.contains("/tests/")
113        || path_str.contains("/spec/")
114        || path_str.contains("/specs/")
115        || path_str.contains("/__tests__/")
116        || path_str.contains("/__test__/")
117    {
118        if _debug_mode {
119            println!("DEBUG: Test file detected (in test directory): {path_str}");
120        }
121        return true;
122    }
123
124    false
125}