From 1ab6f27ecd8e432967960cfa819e52859884ccd9 Mon Sep 17 00:00:00 2001 From: Belen Pruvost Date: Wed, 21 Apr 2021 11:27:22 +0200 Subject: [PATCH] SONAR-14731 - Fix GitScmProvider log --- .../org/sonar/scm/git/GitScmProvider.java | 2 +- .../org/sonar/scm/git/GitScmProviderTest.java | 33 +++++++++++++------ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java index c7654504d0b..9f3419eee26 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java @@ -254,7 +254,7 @@ public class GitScmProvider extends ScmProvider { } if (targetRef == null) { - LOG.warn(COULD_NOT_FIND_REF, targetBranchName); + LOG.warn(String.format(COULD_NOT_FIND_REF, targetBranchName)); } return targetRef; diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitScmProviderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitScmProviderTest.java index 4efb0ddcb33..f4501a19768 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitScmProviderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitScmProviderTest.java @@ -58,6 +58,8 @@ import org.sonar.api.notifications.AnalysisWarnings; import org.sonar.api.scan.filesystem.PathResolver; import org.sonar.api.utils.MessageException; import org.sonar.api.utils.System2; +import org.sonar.api.utils.log.LogAndArguments; +import org.sonar.api.utils.log.LogTester; import static java.util.Collections.emptySet; import static org.assertj.core.api.Assertions.assertThat; @@ -68,6 +70,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; +import static org.sonar.api.utils.log.LoggerLevel.WARN; import static org.sonar.scm.git.Utils.javaUnzip; public class GitScmProviderTest { @@ -100,13 +103,18 @@ public class GitScmProviderTest { + "Deeper and more profound,\n" + "The door of all subtleties!"; + private static final String BRANCH_NAME = "branch"; + @Rule public TemporaryFolder temp = new TemporaryFolder(); + @Rule + public LogTester logs = new LogTester(); + @Rule public ExpectedException thrown = ExpectedException.none(); - private GitIgnoreCommand gitIgnoreCommand = mock(GitIgnoreCommand.class); + private final GitIgnoreCommand gitIgnoreCommand = mock(GitIgnoreCommand.class); private static final Random random = new Random(); private static final System2 system2 = mock(System2.class); @@ -576,7 +584,7 @@ public class GitScmProviderTest { throw new IOException(); } }; - assertThat(provider.branchChangedFiles("branch", worktree)).isNull(); + assertThat(provider.branchChangedFiles(BRANCH_NAME, worktree)).isNull(); verifyNoInteractions(analysisWarnings); } @@ -585,7 +593,7 @@ public class GitScmProviderTest { Repository repository = mock(Repository.class); RefDatabase refDatabase = mock(RefDatabase.class); when(repository.getRefDatabase()).thenReturn(refDatabase); - when(refDatabase.findRef("branch")).thenReturn(null); + when(refDatabase.findRef(BRANCH_NAME)).thenReturn(null); GitScmProvider provider = new GitScmProvider(mockCommand(), analysisWarnings, gitIgnoreCommand, system2) { @Override @@ -593,10 +601,15 @@ public class GitScmProviderTest { return repository; } }; - assertThat(provider.branchChangedFiles("branch", worktree)).isNull(); + assertThat(provider.branchChangedFiles(BRANCH_NAME, worktree)).isNull(); - String warning = "Could not find ref 'branch' in refs/heads, refs/remotes, refs/remotes/upstream or refs/remotes/origin." - + " You may see unexpected issues and changes. Please make sure to fetch this ref before pull request analysis."; + String refNotFound = "Could not find ref 'branch' in refs/heads, refs/remotes, refs/remotes/upstream or refs/remotes/origin"; + + LogAndArguments warnLog = logs.getLogs(WARN).get(0); + assertThat(warnLog.getRawMsg()).isEqualTo(refNotFound); + + String warning = refNotFound + + ". You may see unexpected issues and changes. Please make sure to fetch this ref before pull request analysis."; verify(analysisWarnings).addUnique(warning); } @@ -669,7 +682,7 @@ public class GitScmProviderTest { throw new IOException(); } }; - assertThat(provider.branchChangedLines("branch", worktree, emptySet())).isNull(); + assertThat(provider.branchChangedLines(BRANCH_NAME, worktree, emptySet())).isNull(); } @Test @@ -710,9 +723,9 @@ public class GitScmProviderTest { createAndCommitFile("project/file1"); String sha1after = provider.revisionId(projectDir); - assertThat(sha1after).hasSize(40); - - assertThat(sha1after).isNotEqualTo(sha1before); + assertThat(sha1after) + .hasSize(40) + .isNotEqualTo(sha1before); assertThat(provider.revisionId(projectDir)).isEqualTo(sha1after); } -- 2.39.5