diff options
author | Lukasz Jarocki <lukasz.jarocki@sonarsource.com> | 2023-01-10 17:20:03 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-01-11 20:02:58 +0000 |
commit | edf6bb1b37cd4e09387dd41cea9c13e91e9c4326 (patch) | |
tree | 8cdc51b16001514c8d21073050bf2172c7390a5f /sonar-scanner-engine/src/test/java | |
parent | 54b60b1adac5b703398163ee5edf5a621efc0d24 (diff) | |
download | sonarqube-edf6bb1b37cd4e09387dd41cea9c13e91e9c4326.tar.gz sonarqube-edf6bb1b37cd4e09387dd41cea9c13e91e9c4326.zip |
SONAR-14656 fixed unit tests not passing on Windows
Diffstat (limited to 'sonar-scanner-engine/src/test/java')
-rw-r--r-- | sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java | 17 | ||||
-rw-r--r-- | sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitBlameCommandTest.java | 24 |
2 files changed, 29 insertions, 12 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java index 02470cf13ae..4aa35bce859 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java @@ -32,6 +32,7 @@ import java.util.Random; import java.util.stream.Collectors; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.SystemUtils; import org.junit.Before; import org.junit.Ignore; import org.junit.Rule; @@ -58,6 +59,7 @@ import static java.lang.String.format; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.tuple; +import static org.junit.Assume.assumeFalse; import static org.junit.Assume.assumeTrue; public class FileSystemMediumTest { @@ -442,7 +444,8 @@ public class FileSystemMediumTest { } @Test - public void analysisFailsIfFileDoesNotExist() throws IOException { + public void analysisFailsSymbolicLinkWithoutTargetIsInTheFolder() throws IOException { + assumeFalse(SystemUtils.IS_OS_WINDOWS); File srcDir = new File(baseDir, "src"); srcDir.mkdir(); @@ -1122,7 +1125,8 @@ public class FileSystemMediumTest { // src/srcSubDir2/srcSub2.xoo File srcSubDir2 = createDir(srcDir, "srcSubDir2", true); - writeFile(srcSubDir2, "srcSub2.xoo", "Sample 2 xoo\ncontent").setReadable(false); + boolean fileNotReadable = writeFile(srcSubDir2, "srcSub2.xoo", "Sample 2 xoo\ncontent").setReadable(false); + assumeTrue(fileNotReadable); // src/srcSubDir2/srcSubSubDir2/srcSubSub2.xoo File srcSubSubDir2 = createDir(srcSubDir2, "srcSubSubDir2", false); @@ -1143,7 +1147,8 @@ public class FileSystemMediumTest { public void givenFileWithoutAccessWhenChildrenAreExcludedThenThenScanShouldFail() throws IOException { // src/src.xoo File srcDir = createDir(baseDir, "src", true); - writeFile(srcDir, "src.xoo", "Sample xoo\ncontent").setReadable(false); + boolean fileNotReadable = writeFile(srcDir, "src.xoo", "Sample xoo\ncontent").setReadable(false); + assumeTrue(fileNotReadable); AnalysisBuilder result = tester.newAnalysis() .properties(builder @@ -1189,7 +1194,8 @@ public class FileSystemMediumTest { // src/srcSubDir2/srcSub2.xoo File srcSubDir2 = createDir(srcDir, "srcSubDir2", true); - writeFile(srcSubDir2, "srcSub2.xoo", "Sample 2 xoo\ncontent").setReadable(false); + boolean fileNotReadable = writeFile(srcSubDir2, "srcSub2.xoo", "Sample 2 xoo\ncontent").setReadable(false); + assumeTrue(fileNotReadable); AnalysisResult result = tester.newAnalysis() .properties(builder @@ -1254,7 +1260,8 @@ public class FileSystemMediumTest { private File createDir(File parentDir, String name, boolean isReadable) { File dir = new File(parentDir, name); dir.mkdir(); - dir.setReadable(isReadable); + boolean fileSystemOperationSucceded = dir.setReadable(isReadable); + assumeTrue(fileSystemOperationSucceded); //On windows + java there is no reliable way to play with readable/not readable flag return dir; } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitBlameCommandTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitBlameCommandTest.java index 46b0f57c692..834f02bd837 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitBlameCommandTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitBlameCommandTest.java @@ -237,6 +237,13 @@ public class GitBlameCommandTest { "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); + }); + GitBlameCommand blameCommand = new GitBlameCommand(System2.INSTANCE, mockedCmd); assertThat(blameCommand.checkIfEnabled()).isTrue(); }); @@ -314,11 +321,7 @@ public class GitBlameCommandTest { ProcessWrapperFactory mockFactory = mock(ProcessWrapperFactory.class); ProcessWrapper mockProcess = mock(ProcessWrapper.class); - when(mockFactory.create(isNull(), 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 mockProcess; - }); + mockGitWhereOnWindows(mockFactory); when(mockFactory.create(isNull(), any(), eq("C:\\mockGit.exe"), eq("--version"))).then(invocation -> { var argument = (Consumer<String>) invocation.getArgument(1); @@ -338,8 +341,7 @@ public class GitBlameCommandTest { when(system2.property("PATH")).thenReturn("C:\\some-path;C:\\some-another-path"); ProcessWrapperFactory mockFactory = mock(ProcessWrapperFactory.class); - ProcessWrapper mockProcess = mock(ProcessWrapper.class); - when(mockFactory.create(isNull(), any(), eq("C:\\Windows\\System32\\where.exe"), eq("$PATH:git.exe"))).thenReturn(mockProcess); + mockGitWhereOnWindows(mockFactory); GitBlameCommand blameCommand = new GitBlameCommand(system2, mockFactory); assertThat(blameCommand.checkIfEnabled()).isFalse(); @@ -363,6 +365,14 @@ public class GitBlameCommandTest { return temp.newFolder().toPath().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 -> { + var argument = (Consumer<String>) invocation.getArgument(1); + argument.accept("C:\\mockGit.exe"); + return mock(ProcessWrapper.class); + }); + } + private ProcessWrapperFactory mockGitVersionCommand(String commandOutput) { ProcessWrapperFactory mockFactory = mock(ProcessWrapperFactory.class); ProcessWrapper mockProcess = mock(ProcessWrapper.class); |