Skip to content

Commit 2b78e91

Browse files
author
Gilles Grousset
committed
Fixed regressions on DartAnalyzerSensor
- Unix file path not supported anymore (separator issue) - Reworked external process management - Fixed issue with default options file creation / restoration
1 parent 6f1eff8 commit 2b78e91

File tree

2 files changed

+41
-50
lines changed

2 files changed

+41
-50
lines changed

CHANGELOG.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@
1212

1313
#### Enhancements
1414

15-
- Add Flutter 1.20.0 test report compatibility (thanks to [Peter Leibiger](https://github.com/kuhnroyal])), fixes [#19](https://github.com/insideapp-oss/sonar-flutter/issues/19)
16-
- Improve test report parsing (thanks to [Peter Leibiger](https://github.com/kuhnroyal])), fixes [#13](https://github.com/insideapp-oss/sonar-flutter/issues/13)
15+
- Add Flutter 1.20.0 test report compatibility (thanks to [Peter Leibiger](https://github.com/kuhnroyal)), fixes [#19](https://github.com/insideapp-oss/sonar-flutter/issues/19)
16+
- Improve test report parsing (thanks to [Peter Leibiger](https://github.com/kuhnroyal)), fixes [#13](https://github.com/insideapp-oss/sonar-flutter/issues/13)
1717
- Ability to use existing analysis options (configurable behavior) (thanks to [Stephane Janicaud](https://github.com/stephanecodes])), fixes [#23](https://github.com/insideapp-oss/sonar-flutter/issues/23), [#18](https://github.com/insideapp-oss/sonar-flutter/issues/18)
18+
- Paginated analysis with dartanalyzer (thanks to [victorgilc](https://github.com/victorgilc))
1819

1920
#### Bug Fixes
2021

21-
- Report the correct test case count (thanks to [Peter Leibiger](https://github.com/kuhnroyal]))
22-
- Restore Java 8 compatibility required by the sonar-scanner (thanks to [Peter Leibiger](https://github.com/kuhnroyal]))
22+
- Report the correct test case count (thanks to [Peter Leibiger](https://github.com/kuhnroyal))
23+
- Better test report parsing (thanks to [victorgilc](https://github.com/victorgilc))
24+
- Restore Java 8 compatibility required by the sonar-scanner (thanks to [Peter Leibiger](https://github.com/kuhnroyal))
2325
- Restore the original analysis options file when analysis crashes (thanks to [amond](https://github.com/amondnet))
26+
- Windows support (thanks to [victorgilc](https://github.com/victorgilc)), fixes [#12](https://github.com/insideapp-oss/sonar-flutter/issues/12), [#24](https://github.com/insideapp-oss/sonar-flutter/issues/24)
2427

2528
## 0.2.1
2629

dart-lang/src/main/java/fr/insideapp/sonarqube/dart/lang/issues/dartanalyzer/DartAnalyzerSensor.java

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,10 @@
1919
*/
2020
package fr.insideapp.sonarqube.dart.lang.issues.dartanalyzer;
2121

22-
import java.io.File;
23-
import java.io.IOException;
24-
import java.net.URL;
25-
import java.nio.charset.Charset;
26-
import java.util.ArrayList;
27-
import java.util.List;
28-
import java.util.stream.Collectors;
29-
30-
import org.apache.commons.io.IOUtils;
22+
import com.google.common.io.Files;
23+
import com.google.common.io.Resources;
24+
import fr.insideapp.sonarqube.dart.lang.Dart;
25+
import fr.insideapp.sonarqube.dart.lang.DartSensor;
3126
import org.buildobjects.process.ProcBuilder;
3227
import org.slf4j.Logger;
3328
import org.slf4j.LoggerFactory;
@@ -42,17 +37,19 @@
4237
import org.sonar.api.batch.sensor.issue.internal.DefaultIssueLocation;
4338
import org.sonar.api.rule.RuleKey;
4439

45-
import com.google.common.io.Files;
46-
import com.google.common.io.Resources;
47-
48-
import fr.insideapp.sonarqube.dart.lang.Dart;
49-
import fr.insideapp.sonarqube.dart.lang.DartSensor;
40+
import java.io.File;
41+
import java.io.IOException;
42+
import java.net.URL;
43+
import java.util.ArrayList;
44+
import java.util.List;
45+
import java.util.stream.Collectors;
5046

5147
public class DartAnalyzerSensor implements Sensor {
5248
private static final Logger LOGGER = LoggerFactory.getLogger(DartAnalyzerSensor.class);
5349
private static final String ANALYZER_COMMAND = System.getProperty("os.name").toUpperCase().contains("WINDOWS")
5450
? "dartanalyzer.bat"
5551
: "dartanalyzer";
52+
private static final int ANALYZER_TIMEOUT = 10 * 60 * 1000;
5653
private static final String ANALYSIS_OPTIONS_FILENAME = "analysis_options.yaml";
5754
private static final String ANALYSIS_OPTIONS_FILE = "/fr/insideapp/sonarqube/dart/dartanalyzer/analysis_options.yaml";
5855
private static final Integer PAGE_SIZE = 10;
@@ -70,8 +67,6 @@ public void execute(SensorContext sensorContext) {
7067

7168
selectOptionFileToUse(sensorContext);
7269

73-
saveCurrentAnalysisOptionsFile(sensorContext);
74-
7570
recordIssues(sensorContext, buildIssues(getFilesWithAbsolutePath(sensorContext)));
7671

7772
} catch (Exception e) {
@@ -84,19 +79,21 @@ public void execute(SensorContext sensorContext) {
8479
}
8580

8681
private void selectOptionFileToUse(SensorContext sensorContext) throws IOException {
87-
boolean useExistingAnalysisOptions = getUseExistingAnalysisOptions(sensorContext);
82+
useExistingAnalysisOptions = getUseExistingAnalysisOptions(sensorContext);
8883

89-
if (useExistingAnalysisOptions && !this.existsAnalysisOptionsFile(sensorContext)) {
90-
LOGGER.warn("File {} does not exists", ANALYSIS_OPTIONS_FILENAME);
84+
// Usage of existing option is required but file is missing
85+
// Or usage of existing option is not required
86+
if ((useExistingAnalysisOptions && !this.existsAnalysisOptionsFile(sensorContext)) || !useExistingAnalysisOptions) {
9187
useDefaultAnalysisOptionsFile(sensorContext);
92-
useExistingAnalysisOptions = false;
9388
}
9489
}
9590

9691
private void useDefaultAnalysisOptionsFile(SensorContext sensorContext) throws IOException {
9792
LOGGER.debug("Either {} option is not set to true or {} file does not exists, use default analysis options instead",
9893
DartSensor.DART_ANALYSIS_USE_EXISTING_OPTIONS_KEY, ANALYSIS_OPTIONS_FILENAME);
9994

95+
useExistingAnalysisOptions = false;
96+
10097
this.saveCurrentAnalysisOptionsFile(sensorContext);
10198
this.createAnalysisOptionsFile(sensorContext);
10299
}
@@ -106,37 +103,28 @@ private Boolean getUseExistingAnalysisOptions(SensorContext sensorContext) {
106103
}
107104

108105
private List<DartAnalyzerReportIssue> buildIssues(List<String> filesWithAbsolutePath)
109-
throws IOException, InterruptedException {
110-
List<DartAnalyzerReportIssue> issues = new ArrayList<DartAnalyzerReportIssue>();
106+
throws IOException {
107+
List<DartAnalyzerReportIssue> issues = new ArrayList<>();
111108

112109
for (String paginatedFileBatch : getPaginatedFilesPaths(filesWithAbsolutePath)) {
113110

114111
LOGGER.debug("Current file batch: {}", paginatedFileBatch);
115-
Process process = null;
116-
try {
117-
String command = ANALYZER_COMMAND + " " + paginatedFileBatch.toString();
118-
process = new ProcessBuilder(ANALYZER_COMMAND, " " + paginatedFileBatch.toString()).start();
119-
String output = null;
120-
int exitCode = process.waitFor();
121-
122-
LOGGER.debug("ExitCode: {}", exitCode);
123-
124-
if (exitCode == 0 || exitCode == 1) {
125-
output = IOUtils.toString(process.getErrorStream(), Charset.defaultCharset());
126-
LOGGER.debug("Error output: {}", output);
127-
throw new RuntimeException("Error while executing the command: ".concat(command));
128-
}
129-
130-
output = IOUtils.toString(process.getInputStream(), Charset.defaultCharset());
131-
132-
LOGGER.debug("Output: {}", output);
133112

113+
try {
114+
String output = new ProcBuilder(ANALYZER_COMMAND)
115+
.withArgs(paginatedFileBatch.split(" "))
116+
.withTimeoutMillis(ANALYZER_TIMEOUT)
117+
//.withExpectedExitStatuses(0, 1, 2, 3)
118+
.ignoreExitStatus()
119+
.run()
120+
.getOutputString();
121+
134122
issues.addAll(new DartAnalyzerReportParser().parse(output));
135-
} finally {
136-
if (process != null) {
137-
process.destroyForcibly();
138-
}
123+
} catch (Exception e) {
124+
throw new IOException(e);
139125
}
126+
127+
140128
}
141129
LOGGER.debug("Found issues: {}", issues.size());
142130
return issues;
@@ -211,12 +199,12 @@ private List<String> getFilesWithAbsolutePath(SensorContext sensorContext) {
211199

212200
String absolutePath = fileSystem.baseDir().getAbsolutePath();
213201

214-
LOGGER.debug("Files absolut path: {}", absolutePath);
202+
LOGGER.debug("Files absolute path: {}", absolutePath);
215203

216204
fileSystem.inputFiles(mainFilePredicate).forEach(s -> {
217205
LOGGER.debug("Input file path: {}", s.toString());
218206

219-
String fullPath = new StringBuilder(absolutePath).append("\\").append(s.toString().replace("/", "\\"))
207+
String fullPath = new StringBuilder(absolutePath).append(File.separator).append(s.toString().replace("/", File.separator))
220208
.toString();
221209

222210
LOGGER.debug("Current file full path: {}", fullPath);

0 commit comments

Comments
 (0)