diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2017-09-20 17:11:21 +0200 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2017-09-28 09:14:43 +0200 |
commit | 5abe7f209af3621d1bd94ef15c9d43e93a056200 (patch) | |
tree | bceacae53e61a91e3fab19a5a2de91b44900bc4d /sonar-scanner-engine/src/test/java/org | |
parent | 0a201a057cac9263b641614d5491d2061ab3ab80 (diff) | |
download | sonarqube-5abe7f209af3621d1bd94ef15c9d43e93a056200.tar.gz sonarqube-5abe7f209af3621d1bd94ef15c9d43e93a056200.zip |
SONAR-9837 Detect files changed in the current branch with SCM
Diffstat (limited to 'sonar-scanner-engine/src/test/java/org')
2 files changed, 43 insertions, 2 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/StatusDetectionTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/StatusDetectionTest.java index bcf70eb65d7..8a8470f135e 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/StatusDetectionTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/StatusDetectionTest.java @@ -33,6 +33,10 @@ import org.sonar.scanner.repository.ProjectRepositories; import org.sonar.scanner.scm.ScmChangedFiles; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; public class StatusDetectionTest { @Test @@ -54,6 +58,26 @@ public class StatusDetectionTest { // normally changed assertThat(statusDetection.status("foo", createFile("src/Foo.java"), "XXXXX")).isEqualTo(InputFile.Status.SAME); + + // normally added + assertThat(statusDetection.status("foo", createFile("src/Other.java"), "QWERT")).isEqualTo(InputFile.Status.SAME); + } + + @Test + public void detect_status_without_metadata() { + DefaultInputFile mockedFile = mock(DefaultInputFile.class); + when(mockedFile.relativePath()).thenReturn("module/src/Foo.java"); + when(mockedFile.path()).thenReturn(Paths.get("module", "src", "Foo.java")); + + ProjectRepositories ref = new ProjectRepositories(ImmutableTable.of(), createTable(), null); + ScmChangedFiles changedFiles = new ScmChangedFiles(Collections.singletonList(Paths.get("module", "src", "Foo.java"))); + StatusDetection statusDetection = new StatusDetection(ref, changedFiles); + + assertThat(statusDetection.getStatusWithoutMetadata("foo", mockedFile)).isEqualTo(InputFile.Status.ADDED); + + verify(mockedFile).path(); + verify(mockedFile).relativePath(); + verifyNoMoreInteractions(mockedFile); } @Test diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/ScmChangedFilesProviderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/ScmChangedFilesProviderTest.java index 6777aaa7333..bf278fee6ce 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/ScmChangedFilesProviderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/ScmChangedFilesProviderTest.java @@ -23,7 +23,9 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Collections; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.sonar.api.batch.fs.internal.DefaultInputModule; @@ -48,6 +50,9 @@ public class ScmChangedFilesProviderTest { @Mock private ScmBranchProvider scmProvider; + @Rule + public ExpectedException exception = ExpectedException.none(); + private Path rootBaseDir = Paths.get("root"); private ScmChangedFilesProvider provider; @@ -70,6 +75,18 @@ public class ScmChangedFilesProviderTest { } @Test + public void testFailIfRelativePath() { + when(branchConfiguration.branchTarget()).thenReturn("target"); + when(branchConfiguration.isShortLivingBranch()).thenReturn(true); + when(scmConfiguration.provider()).thenReturn(scmProvider); + when(scmProvider.branchChangedFiles("target", rootBaseDir)).thenReturn(Collections.singleton(Paths.get("changedFile"))); + + exception.expect(IllegalStateException.class); + exception.expectMessage("changed file with a relative path"); + provider.provide(scmConfiguration, branchConfiguration, inputModuleHierarchy); + } + + @Test public void testProviderDoesntSupport() { when(branchConfiguration.branchTarget()).thenReturn("target"); when(branchConfiguration.isShortLivingBranch()).thenReturn(true); @@ -108,10 +125,10 @@ public class ScmChangedFilesProviderTest { when(branchConfiguration.branchTarget()).thenReturn("target"); when(branchConfiguration.isShortLivingBranch()).thenReturn(true); when(scmConfiguration.provider()).thenReturn(scmProvider); - when(scmProvider.branchChangedFiles("target", rootBaseDir)).thenReturn(Collections.singletonList(Paths.get("changedFile"))); + when(scmProvider.branchChangedFiles("target", rootBaseDir)).thenReturn(Collections.singleton(Paths.get("changedFile").toAbsolutePath())); ScmChangedFiles scmChangedFiles = provider.provide(scmConfiguration, branchConfiguration, inputModuleHierarchy); - assertThat(scmChangedFiles.get()).containsOnly(Paths.get("changedFile")); + assertThat(scmChangedFiles.get()).containsOnly(Paths.get("changedFile").toAbsolutePath()); verify(scmProvider).branchChangedFiles("target", rootBaseDir); } |