-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathTestFileClassifier.java
More file actions
152 lines (136 loc) · 5.58 KB
/
Copy pathTestFileClassifier.java
File metadata and controls
152 lines (136 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* SonarQube Python Plugin
* Copyright (C) SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* You can redistribute and/or modify this program under the terms of
* the Sonar Source-Available License Version 1, as published by SonarSource Sàrl.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonar.plugins.python;
import java.util.Arrays;
import java.util.Locale;
import java.util.Set;
import javax.annotation.Nullable;
import org.sonar.api.config.Configuration;
import org.sonar.plugins.python.api.tree.AssertStatement;
import org.sonar.plugins.python.api.tree.BaseTreeVisitor;
import org.sonar.plugins.python.api.tree.FileInput;
import org.sonar.plugins.python.api.tree.FunctionDef;
import org.sonar.plugins.python.api.tree.ImportFrom;
import org.sonar.plugins.python.api.tree.ImportName;
import org.sonar.plugins.python.api.tree.Statement;
import static org.sonarsource.analyzer.commons.appsec.TestFileClassifier.HEURISTIC_DISABLED_KEY;
import org.sonar.plugins.python.api.tree.StatementList;
/**
* Heuristic classifier used when {@code sonar.tests} is not configured.
* Determines whether a file should be treated as a test file for rule-execution
* purposes, without affecting metric computation (which always uses the platform
* {@code InputFile#type()}).
*/
public class TestFileClassifier {
private static final Set<String> TEST_FRAMEWORK_MODULES = Set.of("unittest", "pytest");
private TestFileClassifier() {
}
/**
* Returns {@code true} when the project configuration explicitly controls which files are test
* files, making the path-based heuristic unnecessary. Both {@link PythonScanner} and
* {@link org.sonar.plugins.python.indexer.SonarQubePythonIndexer} use this to decide whether
* to activate (and cache) the heuristic.
*/
public static boolean isTestSourceConfigured(Configuration config) {
return isPropertyConfigured(config, "sonar.tests")
|| config.getBoolean("sonar.python.testFileHeuristic.disabled").orElse(false)
|| config.getBoolean(HEURISTIC_DISABLED_KEY).orElse(false);
}
private static boolean isPropertyConfigured(Configuration config, String key) {
return config.get(key).filter(v -> !v.isBlank()).isPresent();
}
/**
* Path-only check — safe to call when no parsed tree is available (e.g. cache hits).
* Matches files whose directory contains {@code test} or {@code tests}, or whose
* filename follows the standard pytest discovery patterns ({@code test_*.py} / {@code *_test.py}).
*/
static boolean looksLikeTestFileByPath(String filePath) {
if (filePath.isEmpty()) {
return false;
}
String normalizedPath = filePath.replace('\\', '/');
String[] components = normalizedPath.split("/");
// Check directory components (all but the last)
boolean dirMatch = Arrays.stream(components, 0, components.length - 1)
.map(c -> c.toLowerCase(Locale.ROOT))
.anyMatch(dir -> "test".equals(dir) || "tests".equals(dir));
if (dirMatch) {
return true;
}
// Check filename
String filename = components[components.length - 1].toLowerCase(Locale.ROOT);
return filename.startsWith("test_") || filename.endsWith("_test.py");
}
/**
* Full check — uses the parsed tree in addition to path heuristics.
* Applies when a {@link FileInput} is available (i.e. during a full parse).
*/
static boolean looksLikeTestFile(String filePath, @Nullable FileInput tree) {
if (looksLikeTestFileByPath(filePath)) {
return true;
}
if (tree == null) {
return false;
}
return isImportBasedTestFile(tree) || isPytestPatternFile(tree);
}
private static boolean isImportBasedTestFile(FileInput tree) {
StatementList statements = tree.statements();
if (statements == null) {
return false;
}
return statements.statements().stream().anyMatch(TestFileClassifier::isTestFrameworkImport);
}
private static boolean isTestFrameworkImport(Statement statement) {
if (statement instanceof ImportName importName) {
return importName.modules().stream()
.map(aliasedName -> aliasedName.dottedName().names())
.filter(names -> !names.isEmpty())
.anyMatch(names -> TEST_FRAMEWORK_MODULES.contains(names.get(0).name()));
}
if (statement instanceof ImportFrom importFrom) {
var module = importFrom.module();
if (module != null) {
var names = module.names();
return !names.isEmpty() && TEST_FRAMEWORK_MODULES.contains(names.get(0).name());
}
}
return false;
}
private static boolean isPytestPatternFile(FileInput tree) {
StatementList statements = tree.statements();
if (statements == null) {
return false;
}
return statements.statements().stream()
.filter(FunctionDef.class::isInstance)
.map(FunctionDef.class::cast)
.anyMatch(f -> f.name().name().startsWith("test_") && containsAssert(f));
}
private static boolean containsAssert(FunctionDef functionDef) {
var visitor = new AssertVisitor();
functionDef.body().accept(visitor);
return visitor.hasAssert;
}
private static class AssertVisitor extends BaseTreeVisitor {
boolean hasAssert = false;
@Override
public void visitAssertStatement(AssertStatement assertStatement) {
hasAssert = true;
}
}
}