-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SLLS-268 refactoring for better coverage
- Loading branch information
1 parent
c28b19b
commit dd5eb5c
Showing
5 changed files
with
199 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
src/test/java/org/sonarsource/sonarlint/ls/folders/WorkspaceFolderBranchManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* SonarLint Language Server | ||
* Copyright (C) 2009-2024 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.sonarsource.sonarlint.ls.folders; | ||
|
||
import java.io.File; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.concurrent.CancellationException; | ||
import org.eclipse.lsp4j.jsonrpc.CancelChecker; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
import org.sonarsource.sonarlint.core.rpc.client.SonarLintCancelChecker; | ||
import org.sonarsource.sonarlint.ls.backend.BackendServiceFacade; | ||
import org.sonarsource.sonarlint.ls.log.LanguageClientLogger; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static testutils.JavaUnzip.javaUnzip; | ||
|
||
class WorkspaceFolderBranchManagerTest { | ||
private static WorkspaceFolderBranchManager underTest; | ||
|
||
@BeforeAll | ||
static void setUp() { | ||
var fakeClientLogger = mock(LanguageClientLogger.class); | ||
var backendServiceFacade = mock(BackendServiceFacade.class); | ||
underTest = new WorkspaceFolderBranchManager(backendServiceFacade, fakeClientLogger); | ||
} | ||
|
||
@Test | ||
void matchProjectBranch_shouldReturnTrueWhenCurrentBranch(@TempDir File projectDir) { | ||
javaUnzip("closest-branch.zip", projectDir); | ||
Path path = Paths.get(projectDir.getPath(), "closest-branch"); | ||
|
||
var cancelChecker = new SonarLintCancelChecker(DummyCancelChecker::new); | ||
|
||
var matchProjectBranch = underTest.matchProjectBranch(path.toUri().toString(), "current_branch", cancelChecker); | ||
assertThat(matchProjectBranch).isTrue(); | ||
} | ||
|
||
@Test | ||
void matchProjectBranch_shouldReturnFalseWhenCanceled(@TempDir File projectDir) { | ||
javaUnzip("closest-branch.zip", projectDir); | ||
Path path = Paths.get(projectDir.getPath(), "closest-branch"); | ||
|
||
var cancelChecker = new SonarLintCancelChecker(new CanceledDummyCancelChecker()); | ||
|
||
var matchProjectBranch = underTest.matchProjectBranch(path.toUri().toString(), "current_branch", cancelChecker); | ||
assertThat(matchProjectBranch).isFalse(); | ||
} | ||
|
||
static class DummyCancelChecker implements CancelChecker { | ||
private final boolean canceled = false; | ||
|
||
@Override | ||
public void checkCanceled() { | ||
if (canceled) { | ||
throw new RuntimeException("Canceled"); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isCanceled() { | ||
return canceled; | ||
} | ||
} | ||
|
||
static class CanceledDummyCancelChecker implements CancelChecker { | ||
@Override | ||
public void checkCanceled() { | ||
throw new CancellationException("Canceled"); | ||
} | ||
|
||
@Override | ||
public boolean isCanceled() { | ||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* SonarLint Language Server | ||
* Copyright (C) 2009-2024 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 testutils; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.Enumeration; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipFile; | ||
|
||
import static java.lang.String.format; | ||
|
||
public class JavaUnzip { | ||
public static void javaUnzip(String zipFileName, File toDir) { | ||
File testRepos = new File("src/test/resources/test-repos"); | ||
File zipFile = new File(testRepos, zipFileName); | ||
javaUnzip(zipFile, toDir); | ||
} | ||
|
||
private static void javaUnzip(File zip, File toDir) { | ||
try { | ||
try (ZipFile zipFile = new ZipFile(zip)) { | ||
Enumeration<? extends ZipEntry> entries = zipFile.entries(); | ||
while (entries.hasMoreElements()) { | ||
ZipEntry entry = entries.nextElement(); | ||
File to = new File(toDir, entry.getName()); | ||
if (entry.isDirectory()) { | ||
forceMkdir(to); | ||
} else { | ||
File parent = to.getParentFile(); | ||
forceMkdir(parent); | ||
|
||
Files.copy(zipFile.getInputStream(entry), to.toPath()); | ||
} | ||
} | ||
} | ||
} catch (Exception e) { | ||
throw new IllegalStateException(format("Fail to unzip %s to %s", zip, toDir), e); | ||
} | ||
} | ||
|
||
private static void forceMkdir(final File directory) throws IOException { | ||
if ((directory != null) && (!directory.mkdirs() && !directory.isDirectory())) { | ||
throw new IOException("Cannot create directory '" + directory + "'."); | ||
} | ||
} | ||
} |