Skip to content

Commit 4c796c8

Browse files
SCANMAVEN-276 ScannerEngineBootstrapper.isSuccessful() should be verified before calling getEngineFacade() (#286)
1 parent a09d151 commit 4c796c8

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

.cirrus.yml

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ build_task:
3939
env:
4040
SONAR_TOKEN: VAULT[development/kv/data/next data.token]
4141
SONAR_HOST_URL: https://next.sonarqube.com/sonarqube
42+
SCANNER_VERSION: LATEST
4243
SIGN_KEY: VAULT[development/kv/data/sign data.key]
4344
PGP_PASSPHRASE: VAULT[development/kv/data/sign data.passphrase]
4445
maven_cache:

sonar-maven-plugin/src/main/java/org/sonarsource/scanner/maven/SonarQubeMojo.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,9 @@ public void execute() throws MojoExecutionException, MojoFailureException {
105105
return;
106106
}
107107

108-
ScannerEngineBootstrapper bootstrapper = bootstrapperFactory.create();
109-
boolean success = new ScannerBootstrapper(getLog(), session, bootstrapper, mavenProjectConverter, propertyDecryptor).execute();
110-
if (!success) {
111-
throw new MojoFailureException("Analysis failed");
112-
}
108+
ScannerEngineBootstrapper engineBootstrapper = bootstrapperFactory.create();
109+
ScannerBootstrapper scannerBootstrapper = new ScannerBootstrapper(getLog(), session, engineBootstrapper, mavenProjectConverter, propertyDecryptor);
110+
scannerBootstrapper.execute();
113111
}
114112

115113
private void warnAboutUnspecifiedSonarPluginVersion() {

sonar-maven-plugin/src/main/java/org/sonarsource/scanner/maven/bootstrap/ScannerBootstrapper.java

+14-7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.apache.maven.artifact.versioning.ComparableVersion;
3535
import org.apache.maven.execution.MavenSession;
3636
import org.apache.maven.plugin.MojoExecutionException;
37+
import org.apache.maven.plugin.MojoFailureException;
3738
import org.apache.maven.plugin.logging.Log;
3839
import org.apache.maven.project.MavenProject;
3940
import org.sonarsource.scanner.lib.AnalysisProperties;
@@ -65,15 +66,21 @@ public ScannerBootstrapper(Log log, MavenSession session, ScannerEngineBootstrap
6566
this.propertyDecryptor = propertyDecryptor;
6667
}
6768

68-
public boolean execute() throws MojoExecutionException {
69+
public void execute() throws MojoExecutionException {
6970
logEnvironmentInformation();
70-
try (ScannerEngineBootstrapResult bootstrapResult = bootstrapper.bootstrap();
71-
ScannerEngineFacade engineFacade = bootstrapResult.getEngineFacade()) {
72-
if (!engineFacade.isSonarCloud()) {
73-
serverVersion = engineFacade.getServerVersion();
74-
checkSQVersion();
71+
try (ScannerEngineBootstrapResult bootstrapResult = bootstrapper.bootstrap()) {
72+
if (!bootstrapResult.isSuccessful()) {
73+
throw new MojoFailureException("The scanner boostrapping has failed! See the logs for more details.");
74+
}
75+
try (ScannerEngineFacade engineFacade = bootstrapResult.getEngineFacade()) {
76+
if (!engineFacade.isSonarCloud()) {
77+
serverVersion = engineFacade.getServerVersion();
78+
checkSQVersion();
79+
}
80+
if (!engineFacade.analyze(collectProperties())) {
81+
throw new MojoFailureException("The scanner analysis has failed! See the logs for more details.");
82+
}
7583
}
76-
return engineFacade.analyze(collectProperties());
7784
} catch (Exception e) {
7885
throw new MojoExecutionException(e.getMessage(), e);
7986
}

sonar-maven-plugin/src/test/java/org/sonarsource/scanner/maven/bootstrap/ScannerBootstrapperTest.java

+26
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
5151

5252
import static org.assertj.core.api.Assertions.assertThat;
53+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
5354
import static org.junit.Assert.assertThrows;
5455
import static org.mockito.ArgumentMatchers.any;
5556
import static org.mockito.ArgumentMatchers.contains;
@@ -123,6 +124,8 @@ void setUp()
123124

124125
when(scannerEngineBootstrapResult.getEngineFacade()).thenReturn(scannerEngineFacade);
125126
when(scannerEngineBootstrapper.bootstrap()).thenReturn(scannerEngineBootstrapResult);
127+
when(scannerEngineBootstrapResult.isSuccessful()).thenReturn(true);
128+
when(scannerEngineFacade.analyze(any())).thenReturn(true);
126129
scannerBootstrapper = new ScannerBootstrapper(log, session, scannerEngineBootstrapper, mavenProjectConverter, new PropertyDecryptor(log, securityDispatcher));
127130
}
128131

@@ -148,6 +151,29 @@ void testSQ56() throws MojoExecutionException {
148151
verifyCommonCalls();
149152
}
150153

154+
@Test
155+
void when_ScannerEngineBootstrapper_is_not_successful_getEngineFacade_should_not_be_called() {
156+
when(scannerEngineBootstrapResult.isSuccessful()).thenReturn(false);
157+
when(scannerEngineBootstrapResult.getEngineFacade()).thenThrow(new IllegalAccessError("Should not be called"));
158+
when(scannerEngineFacade.isSonarCloud()).thenReturn(false);
159+
when(scannerEngineFacade.getServerVersion()).thenReturn("5.6");
160+
161+
assertThatThrownBy( () -> scannerBootstrapper.execute())
162+
.isInstanceOf(MojoExecutionException.class)
163+
.hasMessage("The scanner boostrapping has failed! See the logs for more details.");
164+
}
165+
166+
@Test
167+
void throw_an_exception_when_analyze_fail() {
168+
when(scannerEngineFacade.analyze(any())).thenReturn(false);
169+
when(scannerEngineFacade.isSonarCloud()).thenReturn(false);
170+
when(scannerEngineFacade.getServerVersion()).thenReturn("5.6");
171+
172+
assertThatThrownBy( () -> scannerBootstrapper.execute())
173+
.isInstanceOf(MojoExecutionException.class)
174+
.hasMessage("The scanner analysis has failed! See the logs for more details.");
175+
}
176+
151177
@Test
152178
void testVersionComparisonWithBuildNumber() throws MojoExecutionException {
153179
when(scannerEngineFacade.isSonarCloud()).thenReturn(false);

0 commit comments

Comments
 (0)