Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SCANCLI-168 Update Scanner Java Library dependency to 3.2 #210

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions it/src/test/java/com/sonarsource/scanner/it/ScannerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public void should_use_environment_prop() {

assertThat(buildResult.isSuccess()).isFalse();
assertThat(buildResult.getLogs())
.contains("Error status returned by url [http://www.google.com/404/api/v2/analysis/version]: 404");
.contains("Failed to query server version: Not Found");
}

@Test
Expand All @@ -216,7 +216,7 @@ public void should_fail_if_unable_to_connect() {
assertThat(result.isSuccess()).isFalse();
// with the following message
assertThat(result.getLogs())
.contains("Error status returned by url [http://www.google.com/404/api/v2/analysis/version]: 404");
.contains("Failed to query server version: Not Found");
}

// SONARPLUGINS-3574
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
<dependency>
<groupId>org.sonarsource.scanner.lib</groupId>
<artifactId>sonar-scanner-java-library</artifactId>
<version>3.1.1.261</version>
<version>3.2.0.370</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down Expand Up @@ -240,8 +240,8 @@
<configuration>
<rules>
<requireFilesSize>
<minsize>11100000</minsize>
<maxsize>11200000</maxsize>
<minsize>11200000</minsize>
<maxsize>11300000</maxsize>
<files>
<file>${project.build.directory}/sonar-scanner-${project.version}.zip</file>
</files>
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/org/sonarsource/scanner/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
*/
public class Main {
private static final Logger LOG = LoggerFactory.getLogger(Main.class);
private static final String FAILURE = "FAILURE";
private static final String SUCCESS = "SUCCESS";

private final Exit exit;
private final Cli cli;
Expand Down Expand Up @@ -71,18 +73,25 @@ void analyze() {
checkSkip(p);
configureLogging(p);
init(p);
try (var engine = scannerEngineBootstrapper.bootstrap()) {
var success = engine.analyze((Map) p);
if (success) {
displayExecutionResult(stats, "SUCCESS");
status = Exit.SUCCESS;
try (var result = scannerEngineBootstrapper.bootstrap()) {
if (result.isSuccessful()) {
var engine = result.getEngineFacade();
var success = engine.analyze((Map) p);
if (success) {
displayExecutionResult(stats, SUCCESS);
status = Exit.SUCCESS;
} else {
displayExecutionResult(stats, FAILURE);
status = Exit.SCANNER_ENGINE_ERROR;
}
} else {
displayExecutionResult(stats, "FAILURE");
status = Exit.SCANNER_ENGINE_ERROR;
LOG.debug("Scanner engine bootstrapping failed");
displayExecutionResult(stats, FAILURE);
status = Exit.INTERNAL_ERROR;
henryju marked this conversation as resolved.
Show resolved Hide resolved
}
}
} catch (Throwable e) {
displayExecutionResult(stats, "FAILURE");
displayExecutionResult(stats, FAILURE);
showError(e, cli.isDebugEnabled());
status = isUserError(e) ? Exit.USER_ERROR : Exit.INTERNAL_ERROR;
} finally {
Expand Down
28 changes: 22 additions & 6 deletions src/test/java/org/sonarsource/scanner/cli/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.slf4j.LoggerFactory;
import org.slf4j.event.Level;
import org.sonar.api.utils.MessageException;
import org.sonarsource.scanner.lib.ScannerEngineBootstrapResult;
import org.sonarsource.scanner.lib.ScannerEngineBootstrapper;
import org.sonarsource.scanner.lib.ScannerEngineFacade;
import org.sonarsource.scanner.lib.ScannerProperties;
Expand All @@ -56,11 +57,14 @@ class MainTest {
private final ScannerEngineBootstrapperFactory scannerEngineBootstrapperFactory = mock();
private final ScannerEngineBootstrapper bootstrapper = mock();
private final ScannerEngineFacade engine = mock();
private final ScannerEngineBootstrapResult result = mock();

@BeforeEach
void setUp() {
when(scannerEngineBootstrapperFactory.create(any(Properties.class), any(String.class))).thenReturn(bootstrapper);
when(bootstrapper.bootstrap()).thenReturn(engine);
when(result.isSuccessful()).thenReturn(true);
when(result.getEngineFacade()).thenReturn(engine);
when(bootstrapper.bootstrap()).thenReturn(result);
when(engine.analyze(any())).thenReturn(true);
when(conf.properties()).thenReturn(properties);
}
Expand All @@ -79,7 +83,7 @@ void should_execute_scanner_engine() {
}

@Test
void should_exit_with_error_on_error_during_analysis() {
void should_exit_with_error_on_exception_during_analysis() {
Exception e = new NullPointerException("NPE");
e = new IllegalStateException("Error", e);
doThrow(e).when(engine).analyze(any());
Expand All @@ -94,9 +98,7 @@ void should_exit_with_error_on_error_during_analysis() {

@Test
void should_exit_with_error_on_error_during_bootstrap() {
Exception e = new NullPointerException("NPE");
e = new IllegalStateException("Error", e);
doThrow(e).when(bootstrapper).bootstrap();
when(result.isSuccessful()).thenReturn(false);
when(cli.getInvokedFrom()).thenReturn("");
when(cli.isDebugEnabled()).thenReturn(true);

Expand All @@ -106,7 +108,21 @@ void should_exit_with_error_on_error_during_bootstrap() {
verify(bootstrapper).bootstrap();
verify(engine, never()).analyze(any());
verify(exit).exit(Exit.INTERNAL_ERROR);
assertThat(logTester.logs(Level.ERROR)).contains("Error during SonarScanner CLI execution");
assertThat(logTester.logs(Level.INFO)).contains("EXECUTION FAILURE");
}

@Test
void should_exit_with_error_on_error_during_analysis() {
when(engine.analyze(any())).thenReturn(false);
when(cli.getInvokedFrom()).thenReturn("");
when(cli.isDebugEnabled()).thenReturn(true);

Main main = new Main(exit, cli, conf, scannerEngineBootstrapperFactory);
main.analyze();

verify(bootstrapper).bootstrap();
verify(exit).exit(Exit.SCANNER_ENGINE_ERROR);
assertThat(logTester.logs(Level.INFO)).contains("EXECUTION FAILURE");
}

@Test
Expand Down
Loading