Skip to content

Commit 24783f7

Browse files
committed
Improve coverage for #35.
1 parent 6e801c9 commit 24783f7

File tree

2 files changed

+69
-26
lines changed

2 files changed

+69
-26
lines changed

src/main/java/fr/cnes/sonar/plugins/icode/check/ICodeSensor.java

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -100,31 +100,10 @@ public void execute(SensorContext sensorContext) {
100100
final Configuration config = sensorContext.config();
101101
// Represent the active rules used for the analysis.
102102
final ActiveRules activeRules = sensorContext.activeRules();
103+
103104
// run i-Code CNES execution
104105
if(config.getBoolean(ICodePluginProperties.AUTOLAUNCH_PROP_KEY).orElse(Boolean.getBoolean(ICodePluginProperties.AUTOLAUNCH_PROP_DEFAULT))) {
105-
LOGGER.info("i-Code CNES auto-launch enabled.");
106-
final String executable = config.get(ICodePluginProperties.ICODE_PATH_KEY).orElse(ICodePluginProperties.ICODE_PATH_DEFAULT);
107-
final String[] files = sensorContext.fileSystem().baseDir().list();
108-
final String outputFile = config.get(ICodePluginProperties.REPORT_PATH_KEY).orElse(ICodePluginProperties.REPORT_PATH_DEFAULT);
109-
final String outputPath = Paths.get(sensorContext.fileSystem().baseDir().toString(),outputFile).toString();
110-
final String outputOption = "-o";
111-
final String command = String.join(" ", executable, String.join(" ",files), outputOption, outputPath);
112-
113-
LOGGER.info("Running i-Code CNES and generating results to "+ outputPath);
114-
try {
115-
final Process icode = Runtime.getRuntime().exec(command);
116-
int success = icode.waitFor();
117-
if(0!=success){
118-
final String message = String.format("i-Code CNES auto-launch analysis failed with exit code %d.",success);
119-
throw new ICodeException(message);
120-
}
121-
LOGGER.info("Auto-launch successfully executed i-Code CNES.");
122-
} catch (InterruptedException | IOException | ICodeException e) {
123-
LOGGER.error(e.getMessage(), e);
124-
sensorContext.newAnalysisError().message(e.getMessage()).save();
125-
}
126-
127-
106+
executeICode(sensorContext);
128107
}
129108

130109
// Report files found in file system and corresponding to SQ property.
@@ -162,6 +141,48 @@ public void execute(SensorContext sensorContext) {
162141

163142
}
164143

144+
/**
145+
* Execute i-Code through a system process.
146+
*
147+
* @param sensorContext Context of the sensor.
148+
*/
149+
protected void executeICode(final SensorContext sensorContext) {
150+
LOGGER.info("i-Code CNES auto-launch enabled.");
151+
final Configuration config = sensorContext.config();
152+
final String executable = config.get(ICodePluginProperties.ICODE_PATH_KEY).orElse(ICodePluginProperties.ICODE_PATH_DEFAULT);
153+
final String[] files = sensorContext.fileSystem().baseDir().list();
154+
final String outputFile = config.get(ICodePluginProperties.REPORT_PATH_KEY).orElse(ICodePluginProperties.REPORT_PATH_DEFAULT);
155+
final String outputPath = Paths.get(sensorContext.fileSystem().baseDir().toString(),outputFile).toString();
156+
final String outputOption = "-o";
157+
final String command = String.join(" ", executable, String.join(" ",files), outputOption, outputPath);
158+
159+
LOGGER.info("Running i-Code CNES and generating results to "+ outputPath);
160+
try {
161+
int success = runICode(command);
162+
if(0!=success){
163+
final String message = String.format("i-Code CNES auto-launch analysis failed with exit code %d.",success);
164+
throw new ICodeException(message);
165+
}
166+
LOGGER.info("Auto-launch successfully executed i-Code CNES.");
167+
} catch (InterruptedException | IOException | ICodeException e) {
168+
LOGGER.error(e.getMessage(), e);
169+
sensorContext.newAnalysisError().message(e.getMessage()).save();
170+
}
171+
}
172+
173+
/**
174+
* Run the i-Code command.
175+
*
176+
* @param command The i-Code Command to execute.
177+
* @return 0 if all was fine.
178+
* @throws IOException On possible unknown file.
179+
* @throws InterruptedException On possible process problem.
180+
*/
181+
protected int runICode(final String command) throws IOException, InterruptedException {
182+
final Process icode = Runtime.getRuntime().exec(command);
183+
return icode.waitFor();
184+
}
185+
165186
/**
166187
* This method save an issue into the SonarQube service.
167188
*

src/test/java/fr/cnes/sonar/plugins/icode/check/ICodeSensorTest.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.junit.Before;
2525
import org.junit.Test;
2626
import org.mockito.Mockito;
27+
import org.mockito.internal.matchers.Null;
2728
import org.sonar.api.batch.fs.FilePredicate;
2829
import org.sonar.api.batch.fs.InputFile;
2930
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
@@ -111,15 +112,36 @@ public void test_normal_work() {
111112
Assert.assertTrue(context.config().hasKey("sonar.icode.reports.path"));
112113
}
113114

114-
@Test(expected = NullPointerException.class)
115+
@Test
115116
public void test_normal_work_with_icode_launch_failed() {
116117
final ICodeSensor sensor = new ICodeSensor();
117118

118119
final MapSettings settings = new MapSettings();
119120
settings.setProperty("sonar.icode.launch",true);
120121
context.setSettings(settings);
121122

123+
try {
124+
sensor.execute(context);
125+
} catch(Exception e) {
126+
assert(true);
127+
}
128+
}
129+
130+
@Test
131+
public void test_normal_work_with_icode_launch_success() {
132+
final ICodeSensor sensor = new ICodeSensor() {
133+
@Override
134+
protected int runICode(final String command) {
135+
return 0;
136+
}
137+
};
138+
139+
final MapSettings settings = new MapSettings();
140+
settings.setProperty("sonar.icode.launch",true);
141+
context.setSettings(settings);
142+
122143
sensor.execute(context);
144+
assert(true);
123145
}
124146

125147
@Test
@@ -130,8 +152,8 @@ public void test_get_scanned_files() {
130152
final AnalysisFile file = new AnalysisFile();
131153
final AnalysisFile file2 = new AnalysisFile();
132154

133-
file.fileName = "badaboum.sh";
134-
file2.fileName = "bash.sh";
155+
file.fileName = "badaboum.sh";file.language="shell";
156+
file2.fileName = "bash.sh";file2.language="shell";
135157

136158
project.analysisFile = new AnalysisFile[]{file, file2};
137159

0 commit comments

Comments
 (0)