diff options
author | Eric Giffon <eric.giffon@sonarsource.com> | 2023-03-30 11:09:11 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-03-30 20:03:07 +0000 |
commit | e36fcb5a436559c4b2fa1ae4ed5618a882002ba1 (patch) | |
tree | cf8c70793acac802b834c680e1ef8dcb383d9df2 /sonar-scanner-engine | |
parent | 2628add44f0d29a5a1bc15f2bcb5da4264822114 (diff) | |
download | sonarqube-e36fcb5a436559c4b2fa1ae4ed5618a882002ba1.tar.gz sonarqube-e36fcb5a436559c4b2fa1ae4ed5618a882002ba1.zip |
SONAR-18828 Specify dotnet scanner version in warning
Diffstat (limited to 'sonar-scanner-engine')
2 files changed, 55 insertions, 6 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DeprecatedPropertiesWarningGenerator.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DeprecatedPropertiesWarningGenerator.java index 8dfb0eccf3e..0a2b46d7a57 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DeprecatedPropertiesWarningGenerator.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DeprecatedPropertiesWarningGenerator.java @@ -21,11 +21,13 @@ package org.sonar.scanner.scan; import com.google.common.annotations.VisibleForTesting; import java.util.Optional; +import org.apache.commons.lang.StringUtils; import org.sonar.api.CoreProperties; import org.sonar.api.config.Configuration; import org.sonar.api.notifications.AnalysisWarnings; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; +import org.sonar.batch.bootstrapper.EnvironmentInformation; import org.sonar.scanner.bootstrap.ScannerWsClientProvider; public class DeprecatedPropertiesWarningGenerator { @@ -38,24 +40,43 @@ public class DeprecatedPropertiesWarningGenerator { @VisibleForTesting static final String LOGIN_WARN_MESSAGE = String.format("The property '%s' is deprecated and will be removed in the future. " + "Please use the '%s' property instead when passing a token.", CoreProperties.LOGIN, ScannerWsClientProvider.TOKEN_PROPERTY); + @VisibleForTesting + static final String SCANNER_DOTNET_WARN_MESSAGE = String.format(" The '%s' property is available from SonarScanner " + + "for .NET version 5.13.", ScannerWsClientProvider.TOKEN_PROPERTY); + private static final String ENV_KEY_SCANNER_DOTNET = "ScannerMSBuild"; private final Configuration configuration; private final AnalysisWarnings analysisWarnings; + private final EnvironmentInformation environmentInformation; - public DeprecatedPropertiesWarningGenerator(Configuration configuration, AnalysisWarnings analysisWarnings) { + public DeprecatedPropertiesWarningGenerator(Configuration configuration, AnalysisWarnings analysisWarnings, + EnvironmentInformation environmentInformation) { this.configuration = configuration; this.analysisWarnings = analysisWarnings; + this.environmentInformation = environmentInformation; } public void execute() { Optional<String> login = configuration.get(CoreProperties.LOGIN); Optional<String> password = configuration.get(CoreProperties.PASSWORD); + + String warningMessage = null; if (password.isPresent()) { - LOG.warn(PASSWORD_WARN_MESSAGE); - analysisWarnings.addUnique(PASSWORD_WARN_MESSAGE); + warningMessage = PASSWORD_WARN_MESSAGE; } else if (login.isPresent()) { - LOG.warn(LOGIN_WARN_MESSAGE); - analysisWarnings.addUnique(LOGIN_WARN_MESSAGE); + warningMessage = LOGIN_WARN_MESSAGE; + } + + if (warningMessage != null) { + if (isScannerDotNet()) { + warningMessage += SCANNER_DOTNET_WARN_MESSAGE; + } + LOG.warn(warningMessage); + analysisWarnings.addUnique(warningMessage); } } + + private boolean isScannerDotNet() { + return StringUtils.containsIgnoreCase(environmentInformation.getKey(), ENV_KEY_SCANNER_DOTNET); + } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/DeprecatedPropertiesWarningGeneratorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/DeprecatedPropertiesWarningGeneratorTest.java index c6eb658e6f5..f584add964d 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/DeprecatedPropertiesWarningGeneratorTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/DeprecatedPropertiesWarningGeneratorTest.java @@ -29,12 +29,15 @@ import org.sonar.api.config.internal.MapSettings; import org.sonar.api.notifications.AnalysisWarnings; import org.sonar.api.utils.log.LogTester; import org.sonar.api.utils.log.LoggerLevel; +import org.sonar.batch.bootstrapper.EnvironmentInformation; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.when; import static org.sonar.scanner.scan.DeprecatedPropertiesWarningGenerator.LOGIN_WARN_MESSAGE; import static org.sonar.scanner.scan.DeprecatedPropertiesWarningGenerator.PASSWORD_WARN_MESSAGE; +import static org.sonar.scanner.scan.DeprecatedPropertiesWarningGenerator.SCANNER_DOTNET_WARN_MESSAGE; public class DeprecatedPropertiesWarningGeneratorTest { @@ -44,13 +47,15 @@ public class DeprecatedPropertiesWarningGeneratorTest { private final MapSettings settings = new MapSettings(); private final AnalysisWarnings analysisWarnings = Mockito.spy(AnalysisWarnings.class); + private final EnvironmentInformation environmentInformation = Mockito.mock(EnvironmentInformation.class); private final DeprecatedPropertiesWarningGenerator underTest = new DeprecatedPropertiesWarningGenerator(settings.asConfig(), - analysisWarnings); + analysisWarnings, environmentInformation); @Before public void setUp() throws Exception { settings.removeProperty(CoreProperties.LOGIN); settings.removeProperty(CoreProperties.PASSWORD); + when(environmentInformation.getKey()).thenReturn("ScannerCLI"); } @Test @@ -75,6 +80,29 @@ public class DeprecatedPropertiesWarningGeneratorTest { } @Test + public void execute_whenUsingLoginAndDotNetScanner_shouldAddWarning() { + settings.setProperty(CoreProperties.LOGIN, "test"); + when(environmentInformation.getKey()).thenReturn("ScannerMSBuild"); + + underTest.execute(); + + verify(analysisWarnings, times(1)).addUnique(LOGIN_WARN_MESSAGE + SCANNER_DOTNET_WARN_MESSAGE); + Assertions.assertThat(logger.logs(LoggerLevel.WARN)).contains(LOGIN_WARN_MESSAGE + SCANNER_DOTNET_WARN_MESSAGE); + } + + @Test + public void execute_whenUsingPasswordAndDotNetScanner_shouldAddWarning() { + settings.setProperty(CoreProperties.LOGIN, "test"); + settings.setProperty(CoreProperties.PASSWORD, "winner winner chicken dinner"); + when(environmentInformation.getKey()).thenReturn("ScannerMSBuild"); + + underTest.execute(); + + verify(analysisWarnings, times(1)).addUnique(PASSWORD_WARN_MESSAGE + SCANNER_DOTNET_WARN_MESSAGE); + Assertions.assertThat(logger.logs(LoggerLevel.WARN)).contains(PASSWORD_WARN_MESSAGE + SCANNER_DOTNET_WARN_MESSAGE); + } + + @Test public void execute_whenNotUsingLoginOrPassword_shouldNotAddWarning() { underTest.execute(); |