diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2017-09-19 17:25:57 +0200 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2017-09-28 09:14:43 +0200 |
commit | e19f3f56fe2d8bc2101086b166ec4de92a5c3c8f (patch) | |
tree | 9091cd9ea21ae09da8666488724a9edc6a24f2a2 /sonar-scanner-engine/src/test/java/org/sonar | |
parent | 22c8ebe741fb7ff7c6f18f95c7b74aa0104f4620 (diff) | |
download | sonarqube-e19f3f56fe2d8bc2101086b166ec4de92a5c3c8f.tar.gz sonarqube-e19f3f56fe2d8bc2101086b166ec4de92a5c3c8f.zip |
SONAR-9837 Detect files changed in the current branch with SCM
Diffstat (limited to 'sonar-scanner-engine/src/test/java/org/sonar')
5 files changed, 217 insertions, 10 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/MetadataGeneratorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/MetadataGeneratorTest.java index 27fa5362f54..2dd1961963c 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/MetadataGeneratorTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/MetadataGeneratorTest.java @@ -140,11 +140,10 @@ public class MetadataGeneratorTest { FileUtils.write(srcFile.toFile(), "single line"); // status - when(statusDetection.status("foo", "src/main/java/foo/Bar.java", "6c1d64c0b3555892fe7273e954f6fb5a")) + DefaultInputFile inputFile = createInputFileWithMetadata(baseDir, "src/main/java/foo/Bar.java"); + when(statusDetection.status("foo", inputFile, "6c1d64c0b3555892fe7273e954f6fb5a")) .thenReturn(InputFile.Status.ADDED); - InputFile inputFile = createInputFileWithMetadata(baseDir, "src/main/java/foo/Bar.java"); - assertThat(inputFile.type()).isEqualTo(InputFile.Type.MAIN); assertThat(inputFile.file()).isEqualTo(srcFile.toFile()); assertThat(inputFile.absolutePath()).isEqualTo(PathUtils.sanitize(srcFile.toAbsolutePath().toString())); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/StatusDetectionFactoryTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/StatusDetectionFactoryTest.java index 160580763f0..259b5e5a653 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/StatusDetectionFactoryTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/StatusDetectionFactoryTest.java @@ -21,6 +21,7 @@ package org.sonar.scanner.scan.filesystem; import org.junit.Test; 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; @@ -28,7 +29,7 @@ import static org.mockito.Mockito.mock; public class StatusDetectionFactoryTest { @Test public void testCreate() throws Exception { - StatusDetectionFactory factory = new StatusDetectionFactory(mock(ProjectRepositories.class)); + StatusDetectionFactory factory = new StatusDetectionFactory(mock(ProjectRepositories.class), mock(ScmChangedFiles.class)); StatusDetection detection = factory.create(); assertThat(detection).isNotNull(); } 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 a295fdc08be..bcf70eb65d7 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 @@ -22,23 +22,47 @@ package org.sonar.scanner.scan.filesystem; import com.google.common.collect.HashBasedTable; import com.google.common.collect.ImmutableTable; import com.google.common.collect.Table; +import java.nio.file.Paths; +import java.util.Collections; import org.junit.Test; import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.fs.internal.DefaultInputFile; +import org.sonar.api.batch.fs.internal.TestInputFileBuilder; import org.sonar.scanner.repository.FileData; import org.sonar.scanner.repository.ProjectRepositories; +import org.sonar.scanner.scm.ScmChangedFiles; import static org.assertj.core.api.Assertions.assertThat; public class StatusDetectionTest { @Test public void detect_status() { - Table<String, String, String> t = ImmutableTable.of(); - ProjectRepositories ref = new ProjectRepositories(t, createTable(), null); - StatusDetection statusDetection = new StatusDetection(ref); + ProjectRepositories ref = new ProjectRepositories(ImmutableTable.of(), createTable(), null); + ScmChangedFiles changedFiles = new ScmChangedFiles(null); + StatusDetection statusDetection = new StatusDetection(ref, changedFiles); - assertThat(statusDetection.status("foo", "src/Foo.java", "ABCDE")).isEqualTo(InputFile.Status.SAME); - assertThat(statusDetection.status("foo", "src/Foo.java", "XXXXX")).isEqualTo(InputFile.Status.CHANGED); - assertThat(statusDetection.status("foo", "src/Other.java", "QWERT")).isEqualTo(InputFile.Status.ADDED); + assertThat(statusDetection.status("foo", createFile("src/Foo.java"), "ABCDE")).isEqualTo(InputFile.Status.SAME); + assertThat(statusDetection.status("foo", createFile("src/Foo.java"), "XXXXX")).isEqualTo(InputFile.Status.CHANGED); + assertThat(statusDetection.status("foo", createFile("src/Other.java"), "QWERT")).isEqualTo(InputFile.Status.ADDED); + } + + @Test + public void detect_status_branches_exclude() { + ProjectRepositories ref = new ProjectRepositories(ImmutableTable.of(), createTable(), null); + ScmChangedFiles changedFiles = new ScmChangedFiles(Collections.emptyList()); + StatusDetection statusDetection = new StatusDetection(ref, changedFiles); + + // normally changed + assertThat(statusDetection.status("foo", createFile("src/Foo.java"), "XXXXX")).isEqualTo(InputFile.Status.SAME); + } + + @Test + public void detect_status_branches_confirm() { + 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.status("foo", createFile("src/Foo.java"), "XXXXX")).isEqualTo(InputFile.Status.CHANGED); } private static Table<String, String, FileData> createTable() { @@ -49,4 +73,8 @@ public class StatusDetectionTest { return t; } + + private static DefaultInputFile createFile(String relativePath) { + return new TestInputFileBuilder("module", relativePath).build(); + } } 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 new file mode 100644 index 00000000000..6777aaa7333 --- /dev/null +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/ScmChangedFilesProviderTest.java @@ -0,0 +1,125 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.scanner.scm; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Collections; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.sonar.api.batch.fs.internal.DefaultInputModule; +import org.sonar.api.batch.fs.internal.InputModuleHierarchy; +import org.sonar.api.batch.scm.ScmBranchProvider; +import org.sonar.api.batch.scm.ScmProvider; +import org.sonar.scanner.scan.branch.BranchConfiguration; + +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.verifyZeroInteractions; +import static org.mockito.Mockito.when; + +public class ScmChangedFilesProviderTest { + @Mock + private ScmConfiguration scmConfiguration; + @Mock + private BranchConfiguration branchConfiguration; + @Mock + private InputModuleHierarchy inputModuleHierarchy; + @Mock + private ScmBranchProvider scmProvider; + + private Path rootBaseDir = Paths.get("root"); + private ScmChangedFilesProvider provider; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + DefaultInputModule root = mock(DefaultInputModule.class); + when(root.getBaseDir()).thenReturn(rootBaseDir); + when(inputModuleHierarchy.root()).thenReturn(root); + provider = new ScmChangedFilesProvider(); + } + + @Test + public void testNoScmProvider() { + when(branchConfiguration.isShortLivingBranch()).thenReturn(true); + ScmChangedFiles scmChangedFiles = provider.provide(scmConfiguration, branchConfiguration, inputModuleHierarchy); + + assertThat(scmChangedFiles.get()).isNull(); + verify(scmConfiguration).provider(); + } + + @Test + public void testProviderDoesntSupport() { + when(branchConfiguration.branchTarget()).thenReturn("target"); + when(branchConfiguration.isShortLivingBranch()).thenReturn(true); + when(scmConfiguration.provider()).thenReturn(scmProvider); + when(scmProvider.branchChangedFiles("target", rootBaseDir)).thenReturn(null); + ScmChangedFiles scmChangedFiles = provider.provide(scmConfiguration, branchConfiguration, inputModuleHierarchy); + + assertThat(scmChangedFiles.get()).isNull(); + verify(scmProvider).branchChangedFiles("target", rootBaseDir); + } + + @Test + public void testNoOpInNonShortLivedBranch() { + when(branchConfiguration.isShortLivingBranch()).thenReturn(false); + ScmChangedFiles scmChangedFiles = provider.provide(scmConfiguration, branchConfiguration, inputModuleHierarchy); + + assertThat(scmChangedFiles.get()).isNull(); + verifyZeroInteractions(scmConfiguration); + } + + @Test + public void testLegacyScmProvider() { + ScmProvider legacy = mock(ScmProvider.class); + when(scmConfiguration.provider()).thenReturn(legacy); + when(branchConfiguration.isShortLivingBranch()).thenReturn(true); + + ScmChangedFiles scmChangedFiles = provider.provide(scmConfiguration, branchConfiguration, inputModuleHierarchy); + + assertThat(scmChangedFiles.get()).isNull(); + verify(scmConfiguration).provider(); + verifyZeroInteractions(legacy); + } + + @Test + public void testReturnChangedFiles() { + 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"))); + ScmChangedFiles scmChangedFiles = provider.provide(scmConfiguration, branchConfiguration, inputModuleHierarchy); + + assertThat(scmChangedFiles.get()).containsOnly(Paths.get("changedFile")); + verify(scmProvider).branchChangedFiles("target", rootBaseDir); + } + + @Test + public void testCacheObject() { + provider.provide(scmConfiguration, branchConfiguration, inputModuleHierarchy); + provider.provide(scmConfiguration, branchConfiguration, inputModuleHierarchy); + verify(branchConfiguration).isShortLivingBranch(); + } + +} diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/ScmChangedFilesTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/ScmChangedFilesTest.java new file mode 100644 index 00000000000..ec1ce40a091 --- /dev/null +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/ScmChangedFilesTest.java @@ -0,0 +1,54 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.scanner.scm; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Collection; +import java.util.Collections; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ScmChangedFilesTest { + private ScmChangedFiles scmChangedFiles; + + @Test + public void testGetter() { + Collection<Path> files = Collections.singletonList(Paths.get("files")); + scmChangedFiles = new ScmChangedFiles(files); + assertThat(scmChangedFiles.get()).containsOnly(Paths.get("files")); + } + + @Test + public void testNullable() { + scmChangedFiles = new ScmChangedFiles(null); + assertThat(scmChangedFiles.get()).isNull(); + assertThat(scmChangedFiles.verifyChanged(Paths.get("files2"))).isTrue(); + } + + @Test + public void testConfirm() { + Collection<Path> files = Collections.singletonList(Paths.get("files")); + scmChangedFiles = new ScmChangedFiles(files); + assertThat(scmChangedFiles.verifyChanged(Paths.get("files"))).isTrue(); + assertThat(scmChangedFiles.verifyChanged(Paths.get("files2"))).isFalse(); + } +} |