|
27 | 27 | import java.nio.file.Files;
|
28 | 28 | import java.nio.file.Path;
|
29 | 29 | import java.util.ArrayList;
|
30 |
| -import java.util.Collections; |
31 | 30 | import java.util.HashMap;
|
32 | 31 | import java.util.List;
|
33 | 32 | import java.util.Map;
|
| 33 | +import java.util.Optional; |
| 34 | +import java.util.Set; |
| 35 | +import java.util.concurrent.ExecutionException; |
34 | 36 | import java.util.stream.Stream;
|
| 37 | +import javax.annotation.Nullable; |
35 | 38 | import org.junit.jupiter.api.AfterAll;
|
36 | 39 | import org.junit.jupiter.api.BeforeAll;
|
37 | 40 | import org.junit.jupiter.api.Test;
|
38 | 41 | import org.junit.jupiter.api.io.TempDir;
|
39 | 42 | import org.sonar.api.batch.fs.InputFile;
|
40 |
| -import org.sonarsource.sonarlint.core.StandaloneSonarLintEngineImpl; |
| 43 | +import org.sonarsource.sonarlint.core.analysis.AnalysisEngine; |
| 44 | +import org.sonarsource.sonarlint.core.analysis.api.ActiveRule; |
| 45 | +import org.sonarsource.sonarlint.core.analysis.api.AnalysisConfiguration; |
| 46 | +import org.sonarsource.sonarlint.core.analysis.api.AnalysisEngineConfiguration; |
41 | 47 | import org.sonarsource.sonarlint.core.analysis.api.ClientInputFile;
|
42 | 48 | import org.sonarsource.sonarlint.core.analysis.api.ClientModuleFileSystem;
|
43 | 49 | import org.sonarsource.sonarlint.core.analysis.api.ClientModuleInfo;
|
| 50 | +import org.sonarsource.sonarlint.core.analysis.api.Issue; |
44 | 51 | import org.sonarsource.sonarlint.core.analysis.api.WithTextRange;
|
45 |
| -import org.sonarsource.sonarlint.core.client.api.common.analysis.Issue; |
46 |
| -import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration; |
47 |
| -import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneGlobalConfiguration; |
48 |
| -import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneSonarLintEngine; |
49 |
| -import org.sonarsource.sonarlint.core.commons.IssueSeverity; |
50 |
| -import org.sonarsource.sonarlint.core.commons.Language; |
51 |
| -import org.sonarsource.sonarlint.core.commons.log.ClientLogOutput; |
| 52 | +import org.sonarsource.sonarlint.core.analysis.command.AnalyzeCommand; |
| 53 | +import org.sonarsource.sonarlint.core.analysis.command.RegisterModuleCommand; |
| 54 | +import org.sonarsource.sonarlint.core.commons.api.SonarLanguage; |
| 55 | +import org.sonarsource.sonarlint.core.commons.log.LogOutput; |
| 56 | +import org.sonarsource.sonarlint.core.commons.log.LogOutput.Level; |
| 57 | +import org.sonarsource.sonarlint.core.commons.log.SonarLintLogger; |
| 58 | +import org.sonarsource.sonarlint.core.commons.progress.ProgressMonitor; |
| 59 | +import org.sonarsource.sonarlint.core.plugin.commons.PluginsLoader; |
52 | 60 |
|
53 | 61 | import static org.assertj.core.api.Assertions.assertThat;
|
54 | 62 | import static org.assertj.core.api.Assertions.tuple;
|
55 | 63 |
|
56 |
| -class IPythonTest { |
| 64 | +class SonarLintIPythonTest { |
57 | 65 |
|
58 | 66 | @TempDir
|
59 |
| - public static Path TEMP; |
| 67 | + public static Path temp; |
60 | 68 |
|
61 |
| - private static StandaloneSonarLintEngine sonarlintEngine; |
| 69 | + private static AnalysisEngine sonarlintEngine; |
| 70 | + private final ProgressMonitor progressMonitor = new ProgressMonitor(null); |
62 | 71 |
|
63 | 72 | @BeforeAll
|
64 |
| - static void prepare() throws Exception { |
65 |
| - StandaloneGlobalConfiguration sonarLintConfig = StandaloneGlobalConfiguration.builder() |
66 |
| - .addPlugin(TestsUtils.PLUGIN_LOCATION.getFile().toPath()) |
67 |
| - .setSonarLintUserHome(TEMP) |
68 |
| - .addEnabledLanguage(Language.IPYTHON) |
69 |
| - .setLogOutput((formattedMessage, level) -> { |
70 |
| - /* Don't pollute logs */ }) |
71 |
| - .setModulesProvider(Collections::emptyList) |
| 73 | + static void prepare() { |
| 74 | + var sonarLintConfig = AnalysisEngineConfiguration.builder() |
| 75 | + .setWorkDir(temp) |
72 | 76 | .build();
|
73 |
| - sonarlintEngine = new StandaloneSonarLintEngineImpl(sonarLintConfig); |
| 77 | + |
| 78 | + var logOutput = new LogOutput() { |
| 79 | + @Override |
| 80 | + public void log(String formattedMessage, Level level, @Nullable String stacktrace) { |
| 81 | + /* Don't pollute logs */ |
| 82 | + } |
| 83 | + }; |
| 84 | + SonarLintLogger.setTarget(logOutput); |
| 85 | + var pluginJarLocation = Set.of(TestsUtils.PLUGIN_LOCATION.getFile().toPath()); |
| 86 | + var enabledLanguages = Set.of(SonarLanguage.IPYTHON); |
| 87 | + var pluginConfiguration = new PluginsLoader.Configuration(pluginJarLocation, enabledLanguages, false, Optional.empty()); |
| 88 | + var pluginLoader = new PluginsLoader().load(pluginConfiguration, Set.of()); |
| 89 | + |
| 90 | + sonarlintEngine = new AnalysisEngine(sonarLintConfig, pluginLoader.getLoadedPlugins(), logOutput); |
74 | 91 | }
|
75 | 92 |
|
76 | 93 | @AfterAll
|
77 | 94 | static void stop() {
|
| 95 | + SonarLintLogger.setTarget(null); |
78 | 96 | sonarlintEngine.stop();
|
79 | 97 | }
|
80 | 98 |
|
81 | 99 | @Test
|
82 |
| - void shouldRaiseIssues() { |
| 100 | + void shouldRaiseIssues() throws InterruptedException, ExecutionException { |
83 | 101 | var inputFile = createInputFile(Path.of("projects/ipynb_project/file1.ipynb"), "file1.ipynb", false);
|
84 | 102 | var issues = new ArrayList<Issue>();
|
85 | 103 |
|
86 |
| - var configuration = StandaloneAnalysisConfiguration.builder() |
| 104 | + var configuration = AnalysisConfiguration.builder() |
87 | 105 | .setBaseDir(Path.of("projects/ipynb_project"))
|
88 | 106 | .addInputFile(inputFile)
|
89 |
| - .setModuleKey("myModule") |
| 107 | + .addActiveRules(new ActiveRule("ipython:PrintStatementUsage", SonarLanguage.IPYTHON.name()), |
| 108 | + new ActiveRule("ipython:S1172", SonarLanguage.IPYTHON.name()), |
| 109 | + new ActiveRule("ipython:S930", SonarLanguage.IPYTHON.name()), |
| 110 | + new ActiveRule("ipython:S1542", SonarLanguage.IPYTHON.name()), |
| 111 | + new ActiveRule("ipython:BackticksUsage", SonarLanguage.IPYTHON.name())) |
90 | 112 | .build();
|
91 | 113 |
|
92 |
| - var logsByLevel = new HashMap<ClientLogOutput.Level, List<String>>(); |
| 114 | + var logsByLevel = new HashMap<Level, List<String>>(); |
93 | 115 | var logOutput = createClientLogOutput(logsByLevel);
|
94 | 116 | var clientFileSystem = createClientFileSystem(inputFile);
|
95 |
| - sonarlintEngine.declareModule(new ClientModuleInfo("myModule", clientFileSystem)); |
96 |
| - sonarlintEngine.analyze(configuration, issues::add, logOutput, null); |
97 |
| - |
| 117 | + sonarlintEngine.post(new RegisterModuleCommand(new ClientModuleInfo("myModule", clientFileSystem)), progressMonitor).get(); |
| 118 | + var command = new AnalyzeCommand("myModule", configuration, issues::add, logOutput); |
| 119 | + sonarlintEngine.post(command, progressMonitor).get(); |
98 | 120 | assertThat(issues)
|
99 |
| - .extracting(Issue::getRuleKey, WithTextRange::getStartLine, i -> i.getInputFile().uri(), Issue::getSeverity) |
| 121 | + .extracting(Issue::getRuleKey, WithTextRange::getStartLine, i -> i.getInputFile().uri()) |
100 | 122 | .containsOnly(
|
101 |
| - tuple("ipython:PrintStatementUsage", 32, inputFile.uri(), IssueSeverity.MAJOR), |
102 |
| - tuple("ipython:S1172", 40, inputFile.uri(), IssueSeverity.MAJOR), |
103 |
| - tuple("ipython:S930", 41, inputFile.uri(), IssueSeverity.BLOCKER), |
104 |
| - tuple("ipython:S1172", 42, inputFile.uri(), IssueSeverity.MAJOR), |
105 |
| - tuple("ipython:S1542", 57, inputFile.uri(), IssueSeverity.MAJOR), |
106 |
| - tuple("ipython:BackticksUsage", 58, inputFile.uri(), IssueSeverity.BLOCKER) |
107 |
| - ); |
| 123 | + tuple("ipython:PrintStatementUsage", 32, inputFile.uri()), |
| 124 | + tuple("ipython:S1172", 40, inputFile.uri()), |
| 125 | + tuple("ipython:S930", 41, inputFile.uri()), |
| 126 | + tuple("ipython:S1172", 42, inputFile.uri()), |
| 127 | + tuple("ipython:S1542", 57, inputFile.uri()), |
| 128 | + tuple("ipython:BackticksUsage", 58, inputFile.uri())); |
108 | 129 | }
|
109 | 130 |
|
110 |
| - private static ClientLogOutput createClientLogOutput(Map<ClientLogOutput.Level, List<String>> logsByLevel) { |
111 |
| - return (s, level) -> logsByLevel.computeIfAbsent(level, (k) -> new ArrayList<>()).add(s); |
| 131 | + private static LogOutput createClientLogOutput(Map<Level, List<String>> logsByLevel) { |
| 132 | + return new LogOutput() { |
| 133 | + @Override |
| 134 | + public void log(String formattedMessage, Level level, @Nullable String stacktrace) { |
| 135 | + logsByLevel.computeIfAbsent(level, k -> new ArrayList<>()).add(formattedMessage); |
| 136 | + } |
| 137 | + }; |
112 | 138 | }
|
113 | 139 |
|
114 | 140 | private static ClientModuleFileSystem createClientFileSystem(ClientInputFile... inputFiles) {
|
|
0 commit comments