/
TestFileClassifier.java
82 lines (75 loc) · 3.03 KB
/
TestFileClassifier.java
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
/*
* SonarLint for Eclipse
* Copyright (C) 2015-2024 SonarSource SA
* sonarlint@sonarsource.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* 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 GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonarlint.eclipse.core.internal.jobs;
import java.nio.file.FileSystems;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.sonarlint.eclipse.core.SonarLintLogger;
import org.sonarlint.eclipse.core.analysis.IFileTypeProvider.ISonarLintFileType;
import org.sonarlint.eclipse.core.internal.extension.SonarLintExtensionTracker;
import org.sonarlint.eclipse.core.internal.preferences.SonarLintGlobalConfiguration;
import org.sonarlint.eclipse.core.resource.ISonarLintFile;
public class TestFileClassifier {
private static TestFileClassifier instance;
private List<PathMatcher> pathMatchersForTests;
public TestFileClassifier() {
reload();
}
public static TestFileClassifier get() {
if (instance == null) {
instance = new TestFileClassifier();
}
return instance;
}
/**
* Reload patterns from global preferences.
* Should be called when preferences are changed.
*/
public void reload() {
var allTestPattern = SonarLintGlobalConfiguration.getTestFileGlobPatterns();
var testPatterns = allTestPattern.split(",");
pathMatchersForTests = createMatchersForTests(testPatterns);
}
private static List<PathMatcher> createMatchersForTests(String[] testPatterns) {
var fs = FileSystems.getDefault();
return Stream.of(testPatterns)
.map(pattern -> fs.getPathMatcher("glob:" + pattern))
.collect(Collectors.toList());
}
public boolean isTest(ISonarLintFile file) {
for (var typeProvider : SonarLintExtensionTracker.getInstance().getTypeProviders()) {
if (typeProvider.qualify(file) == ISonarLintFileType.TEST) {
SonarLintLogger.get().debug("File '" + file.getProjectRelativePath() + "' qualified as test by '" + typeProvider.getClass().getSimpleName() + "'");
return true;
}
}
var fileRelativePath = Paths.get(file.getProjectRelativePath());
for (var matcher : pathMatchersForTests) {
if (matcher.matches(fileRelativePath)) {
SonarLintLogger.get().debug("File '" + file.getProjectRelativePath() + "' qualified as test by file pattern");
return true;
}
}
return false;
}
}