diff options
Diffstat (limited to 'sonar-scanner-engine/src/test/java/org/sonar/scm/git/NativeGitBlameCommandTest.java')
-rw-r--r-- | sonar-scanner-engine/src/test/java/org/sonar/scm/git/NativeGitBlameCommandTest.java | 129 |
1 files changed, 52 insertions, 77 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scm/git/NativeGitBlameCommandTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scm/git/NativeGitBlameCommandTest.java index df3d257c287..23d5e6ca5f0 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scm/git/NativeGitBlameCommandTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scm/git/NativeGitBlameCommandTest.java @@ -19,7 +19,6 @@ */ package org.sonar.scm.git; -import com.tngtech.java.junit.dataprovider.DataProviderRunner; import java.io.File; import java.io.IOException; import java.nio.file.Files; @@ -32,21 +31,21 @@ import java.util.function.Consumer; import java.util.stream.Stream; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.errors.GitAPIException; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.io.TempDir; import org.slf4j.event.Level; import org.sonar.api.batch.scm.BlameLine; +import org.sonar.api.testfixtures.log.LogTesterJUnit5; import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.System2; -import org.sonar.api.testfixtures.log.LogTester; -import org.sonar.scm.git.ProcessWrapperFactory.ProcessWrapper; +import org.sonar.core.util.ProcessWrapperFactory; +import org.sonar.core.util.ProcessWrapperFactory.ProcessWrapper; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assume.assumeTrue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; @@ -54,32 +53,31 @@ import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import static org.sonar.scm.git.GitUtils.createFile; +import static org.sonar.scm.git.GitUtils.createRepository; import static org.sonar.scm.git.NativeGitBlameCommand.BLAME_COMMAND; import static org.sonar.scm.git.NativeGitBlameCommand.GIT_DIR_ARGUMENT; import static org.sonar.scm.git.NativeGitBlameCommand.GIT_DIR_FLAG; import static org.sonar.scm.git.NativeGitBlameCommand.GIT_DIR_FORCE_FLAG; -import static org.sonar.scm.git.GitUtils.createFile; -import static org.sonar.scm.git.GitUtils.createRepository; import static org.sonar.scm.git.Utils.javaUnzip; -@RunWith(DataProviderRunner.class) -public class NativeGitBlameCommandTest { +class NativeGitBlameCommandTest { private static final String DUMMY_JAVA = "src/main/java/org/dummy/Dummy.java"; - @Rule - public TemporaryFolder temp = new TemporaryFolder(); - @Rule - public LogTester logTester = new LogTester(); + @TempDir + private Path tempDir; + @RegisterExtension + private final LogTesterJUnit5 logTester = new LogTesterJUnit5(); private final ProcessWrapperFactory processWrapperFactory = new ProcessWrapperFactory(); private final NativeGitBlameCommand blameCommand = new NativeGitBlameCommand(System2.INSTANCE, processWrapperFactory); - @Before - public void skipTestsIfNoGitFound() { + @BeforeEach + void skipTestsIfNoGitFound() { assumeTrue(blameCommand.checkIfEnabled()); } @Test - public void should_read_lines_only_based_on_new_line() throws Exception { + void should_read_lines_only_based_on_new_line() throws Exception { Path baseDir = createNewTempFolder().toPath(); String filePath = "file.txt"; createFile(filePath, "test1\rtest2\r\ttest3", baseDir); @@ -92,7 +90,7 @@ public class NativeGitBlameCommandTest { } @Test - public void blame_collects_all_lines() throws Exception { + void blame_collects_all_lines() throws Exception { File projectDir = createNewTempFolder(); javaUnzip("dummy-git.zip", projectDir); File baseDir = new File(projectDir, "dummy-git"); @@ -123,7 +121,7 @@ public class NativeGitBlameCommandTest { } @Test - public void blame_different_author_and_committer() throws Exception { + void blame_different_author_and_committer() throws Exception { File projectDir = createNewTempFolder(); javaUnzip("dummy-git-different-committer.zip", projectDir); File baseDir = new File(projectDir, "dummy-git"); @@ -154,21 +152,21 @@ public class NativeGitBlameCommandTest { } @Test - public void git_blame_uses_safe_local_repository() throws Exception { + void git_blame_uses_safe_local_repository() throws Exception { File projectDir = createNewTempFolder(); File baseDir = new File(projectDir, "dummy-git"); ProcessWrapperFactory mockFactory = mock(ProcessWrapperFactory.class); ProcessWrapper mockProcess = mock(ProcessWrapper.class); String gitCommand = "git"; - when(mockFactory.create(any(), any(), anyString(), anyString(), anyString(), anyString(), + when(mockFactory.create(any(), any(), any(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString())) - .then(invocation -> mockProcess); + .then(invocation -> mockProcess); NativeGitBlameCommand blameCommand = new NativeGitBlameCommand(gitCommand, System2.INSTANCE, mockFactory); blameCommand.blame(baseDir.toPath(), DUMMY_JAVA); - verify(mockFactory).create(any(), any(), eq(gitCommand), + verify(mockFactory).create(any(), any(), any(), eq(gitCommand), eq(GIT_DIR_FLAG), eq(String.format(GIT_DIR_ARGUMENT, baseDir.toPath())), eq(GIT_DIR_FORCE_FLAG), @@ -178,7 +176,7 @@ public class NativeGitBlameCommandTest { } @Test - public void modified_file_returns_no_blame() throws Exception { + void modified_file_returns_no_blame() throws Exception { File projectDir = createNewTempFolder(); javaUnzip("dummy-git.zip", projectDir); @@ -191,9 +189,8 @@ public class NativeGitBlameCommandTest { } @Test - public void throw_exception_if_symlink_found() throws Exception { + void throw_exception_if_symlink_found(@TempDir File projectDir) throws Exception { assumeTrue(!System2.INSTANCE.isOsWindows()); - File projectDir = temp.newFolder(); javaUnzip("dummy-git.zip", projectDir); Path baseDir = projectDir.toPath().resolve("dummy-git"); @@ -207,69 +204,66 @@ public class NativeGitBlameCommandTest { } @Test - public void git_should_be_detected() { + void git_should_be_detected() { NativeGitBlameCommand blameCommand = new NativeGitBlameCommand(System2.INSTANCE, processWrapperFactory); assertThat(blameCommand.checkIfEnabled()).isTrue(); } @Test - public void git_should_not_be_detected() { + void git_should_not_be_detected() { NativeGitBlameCommand blameCommand = new NativeGitBlameCommand("randomcmdthatwillneverbefound", System2.INSTANCE, processWrapperFactory); assertThat(blameCommand.checkIfEnabled()).isFalse(); } @Test - public void git_should_not_be_enabled_if_version_command_is_not_found() { + void git_should_not_be_enabled_if_version_command_is_not_found() { ProcessWrapperFactory mockedCmd = mockGitVersionCommand("error: unknown option `version'"); NativeGitBlameCommand blameCommand = new NativeGitBlameCommand(System2.INSTANCE, mockedCmd); assertThat(blameCommand.checkIfEnabled()).isFalse(); } @Test - public void git_should_not_be_enabled_if_version_command_does_not_return_string_output() { + void git_should_not_be_enabled_if_version_command_does_not_return_string_output() { ProcessWrapperFactory mockedCmd = mockGitVersionCommand(null); NativeGitBlameCommand blameCommand = new NativeGitBlameCommand(System2.INSTANCE, mockedCmd); assertThat(blameCommand.checkIfEnabled()).isFalse(); } @Test - public void git_should_be_enabled_if_version_is_equal_or_greater_than_required_minimum() { + void git_should_be_enabled_if_version_is_equal_or_greater_than_required_minimum() { Stream.of( "git version 2.24.0", "git version 2.25.2.1", "git version 2.24.1.1.windows.2", - "git version 2.25.1.msysgit.2" - ).forEach(output -> { - ProcessWrapperFactory mockedCmd = mockGitVersionCommand(output); - mockGitWhereOnWindows(mockedCmd); - when(mockedCmd.create(isNull(), any(), eq("C:\\mockGit.exe"), eq("--version"))).then(invocation -> { - var argument = (Consumer<String>) invocation.getArgument(1); - argument.accept(output); - return mock(ProcessWrapper.class); + "git version 2.25.1.msysgit.2").forEach(output -> { + ProcessWrapperFactory mockedCmd = mockGitVersionCommand(output); + mockGitWhereOnWindows(mockedCmd); + when(mockedCmd.create(isNull(), any(), any(), eq("C:\\mockGit.exe"), eq("--version"))).then(invocation -> { + var argument = (Consumer<String>) invocation.getArgument(1); + argument.accept(output); + return mock(ProcessWrapper.class); + }); + + NativeGitBlameCommand blameCommand = new NativeGitBlameCommand(System2.INSTANCE, mockedCmd); + assertThat(blameCommand.checkIfEnabled()).isTrue(); }); - - NativeGitBlameCommand blameCommand = new NativeGitBlameCommand(System2.INSTANCE, mockedCmd); - assertThat(blameCommand.checkIfEnabled()).isTrue(); - }); } @Test - public void git_should_not_be_enabled_if_version_is_less_than_required_minimum() { + void git_should_not_be_enabled_if_version_is_less_than_required_minimum() { ProcessWrapperFactory mockFactory = mockGitVersionCommand("git version 1.9.0"); NativeGitBlameCommand blameCommand = new NativeGitBlameCommand(System2.INSTANCE, mockFactory); assertThat(blameCommand.checkIfEnabled()).isFalse(); } @Test - public void throw_exception_if_command_fails() throws Exception { - Path baseDir = temp.newFolder().toPath(); + void throw_exception_if_command_fails(@TempDir Path baseDir) { NativeGitBlameCommand blameCommand = new NativeGitBlameCommand("randomcmdthatwillneverbefound", System2.INSTANCE, processWrapperFactory); assertThatThrownBy(() -> blameCommand.blame(baseDir, "file")).isInstanceOf(IOException.class); } @Test - public void blame_without_email_doesnt_fail() throws Exception { - Path baseDir = temp.newFolder().toPath(); + void blame_without_email_doesnt_fail(@TempDir Path baseDir) throws Exception { Git git = createRepository(baseDir); String filePath = "file.txt"; createFile(filePath, "line", baseDir); @@ -286,8 +280,7 @@ public class NativeGitBlameCommandTest { } @Test - public void blame_mail_with_spaces_doesnt_fail() throws Exception { - Path baseDir = temp.newFolder().toPath(); + void blame_mail_with_spaces_doesnt_fail(@TempDir Path baseDir) throws Exception { Git git = createRepository(baseDir); String filePath = "file.txt"; createFile(filePath, "line", baseDir); @@ -301,25 +294,7 @@ public class NativeGitBlameCommandTest { } @Test - public void do_not_execute() throws Exception { - Path baseDir = temp.newFolder().toPath(); - Git git = createRepository(baseDir); - String filePath = "file.txt"; - createFile(filePath, "line", baseDir); - commitWithNoEmail(git, filePath); - - NativeGitBlameCommand blameCommand = new NativeGitBlameCommand(System2.INSTANCE, processWrapperFactory); - assertThat(blameCommand.checkIfEnabled()).isTrue(); - List<BlameLine> blame = blameCommand.blame(baseDir, filePath); - assertThat(blame).hasSize(1); - BlameLine blameLine = blame.get(0); - assertThat(blameLine.author()).isNull(); - assertThat(blameLine.revision()).isNotNull(); - assertThat(blameLine.date()).isNotNull(); - } - - @Test - public void execution_on_windows_should_fallback_to_full_path() { + void execution_on_windows_should_fallback_to_full_path() { logTester.setLevel(Level.DEBUG); System2 system2 = mock(System2.class); when(system2.isOsWindows()).thenReturn(true); @@ -328,7 +303,7 @@ public class NativeGitBlameCommandTest { ProcessWrapper mockProcess = mock(ProcessWrapper.class); mockGitWhereOnWindows(mockFactory); - when(mockFactory.create(isNull(), any(), eq("C:\\mockGit.exe"), eq("--version"))).then(invocation -> { + when(mockFactory.create(isNull(), any(), any(), eq("C:\\mockGit.exe"), eq("--version"))).then(invocation -> { var argument = (Consumer<String>) invocation.getArgument(1); argument.accept("git version 2.30.1"); return mockProcess; @@ -340,7 +315,7 @@ public class NativeGitBlameCommandTest { } @Test - public void execution_on_windows_is_disabled_if_git_not_on_path() { + void execution_on_windows_is_disabled_if_git_not_on_path() { System2 system2 = mock(System2.class); when(system2.isOsWindows()).thenReturn(true); when(system2.property("PATH")).thenReturn("C:\\some-path;C:\\some-another-path"); @@ -367,11 +342,11 @@ public class NativeGitBlameCommandTest { private File createNewTempFolder() throws IOException { // This is needed for Windows, otherwise the created File point to invalid (shortened by Windows) temp folder path - return temp.newFolder().toPath().toRealPath(LinkOption.NOFOLLOW_LINKS).toFile(); + return tempDir.toRealPath(LinkOption.NOFOLLOW_LINKS).toFile(); } private void mockGitWhereOnWindows(ProcessWrapperFactory processWrapperFactory) { - when(processWrapperFactory.create(isNull(), any(), eq("C:\\Windows\\System32\\where.exe"), eq("$PATH:git.exe"))).then(invocation -> { + when(processWrapperFactory.create(isNull(), any(), any(), eq("C:\\Windows\\System32\\where.exe"), eq("$PATH:git.exe"))).then(invocation -> { var argument = (Consumer<String>) invocation.getArgument(1); argument.accept("C:\\mockGit.exe"); return mock(ProcessWrapper.class); @@ -382,7 +357,7 @@ public class NativeGitBlameCommandTest { ProcessWrapperFactory mockFactory = mock(ProcessWrapperFactory.class); ProcessWrapper mockProcess = mock(ProcessWrapper.class); - when(mockFactory.create(isNull(), any(), eq("git"), eq("--version"))).then(invocation -> { + when(mockFactory.create(isNull(), any(), any(), eq("git"), eq("--version"))).then(invocation -> { var argument = (Consumer<String>) invocation.getArgument(1); argument.accept(commandOutput); return mockProcess; |