Skip to content

Commit

Permalink
SLLS-111 Get test/main classification from the client only for Java f…
Browse files Browse the repository at this point in the history
…iles
  • Loading branch information
jblievremont committed Dec 15, 2022
1 parent ef4244d commit d85a4ed
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ private void analyzeSingleModule(AnalysisTask task, Optional<WorkspaceFolderWrap
var excludedByServerConfiguration = connectedEngine.getExcludedFiles(binding.get().getBinding(),
filesToAnalyze.keySet(),
uri -> FileUtils.getFileRelativePath(Paths.get(baseDirUri), uri),
uri -> fileTypeClassifier.isTest(settings, uri, javaConfigCache.getOrFetch(uri)));
uri -> fileTypeClassifier.isTest(settings, uri, filesToAnalyze.get(uri).isJava(), () -> javaConfigCache.getOrFetch(uri)));
excludedByServerConfiguration.forEach(f -> {
lsLogOutput.debug(format("Skip analysis of file '%s' excluded by server configuration", f));
nonExcludedFiles.remove(f);
Expand Down Expand Up @@ -455,7 +455,7 @@ private <G extends AbstractBuilder<G>> G buildCommonAnalysisConfiguration(Worksp
filesToAnalyze.forEach((uri, openFile) -> configurationBuilder
.addInputFiles(
new AnalysisClientInputFile(uri, FileUtils.getFileRelativePath(baseDir, uri), openFile.getContent(),
fileTypeClassifier.isTest(settings, uri, ofNullable(javaConfigs.get(uri))),
fileTypeClassifier.isTest(settings, uri, openFile.isJava(), () -> ofNullable(javaConfigs.get(uri))),
openFile.getLanguageId())));
return configurationBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.net.URI;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.function.Supplier;
import javax.annotation.Nullable;
import org.sonarsource.sonarlint.core.commons.log.SonarLintLogger;
import org.sonarsource.sonarlint.ls.SonarLintExtendedLanguageClient;
Expand All @@ -30,8 +31,9 @@
public class FileTypeClassifier {
private static final SonarLintLogger LOG = SonarLintLogger.get();

public boolean isTest(@Nullable WorkspaceFolderSettings settings, URI fileUri, Optional<SonarLintExtendedLanguageClient.GetJavaConfigResponse> javaConfig) {
if (javaConfig.map(SonarLintExtendedLanguageClient.GetJavaConfigResponse::isTest).orElse(false)) {
public boolean isTest(@Nullable WorkspaceFolderSettings settings, URI fileUri,
boolean isJava, Supplier<Optional<SonarLintExtendedLanguageClient.GetJavaConfigResponse>> javaConfig) {
if (isJava && javaConfig.get().map(SonarLintExtendedLanguageClient.GetJavaConfigResponse::isTest).orElse(false)) {
LOG.debug("Classified as test by vscode-java");
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private InputFile.Type getInputFileType(WorkspaceFolderSettings settings, Path f
}

private boolean isTestFile(WorkspaceFolderSettings settings, URI fileUri) {
return fileTypeClassifier.isTest(settings, fileUri, javaConfigCache.getOrFetch(fileUri));
return fileTypeClassifier.isTest(settings, fileUri, false, () -> javaConfigCache.getOrFetch(fileUri));
}

private static boolean isTestType(InputFile.Type type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ private void processFileEvent(URI fileUri, Type eventType) {

var engineForFile = binding.isPresent() ? binding.get().getEngine() : standaloneEngineManager.getOrCreateStandaloneEngine();

var javaConfig = javaConfigCache.getOrFetch(fileUri);
var inputFile = new InFolderClientInputFile(fileUri, baseDir.relativize(Paths.get(fileUri)).toString(), fileTypeClassifier.isTest(settings, fileUri, javaConfig));
var inputFile = new InFolderClientInputFile(fileUri, baseDir.relativize(Paths.get(fileUri)).toString(),
fileTypeClassifier.isTest(settings, fileUri, false, () -> javaConfigCache.getOrFetch(fileUri)));

engineForFile.fireModuleFileEvent(WorkspaceFoldersProvider.key(folder), ClientModuleFileEvent.of(inputFile, eventType));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.assertj.core.api.Assertions.tuple;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -56,7 +57,7 @@ static void setUp() throws IOException {
@Test
void should_provide_main_files_of_requested_suffix() {
var fileTypeClassifier = mock(FileTypeClassifier.class);
when(fileTypeClassifier.isTest(any(), any(), any())).thenReturn(false);
when(fileTypeClassifier.isTest(any(), any(), anyBoolean(), any())).thenReturn(false);
var folderWrapper = new WorkspaceFolderWrapper(tempFolder.toUri(), new WorkspaceFolder(tempFolder.toString(), "My Folder"));
folderWrapper.setSettings(EMPTY_SETTINGS);
var folderFileSystem = new FolderFileSystem(folderWrapper, mock(JavaConfigCache.class), fileTypeClassifier);
Expand All @@ -72,7 +73,7 @@ void should_provide_main_files_of_requested_suffix() {
@Test
void should_provide_test_files_of_requested_suffix() {
var fileTypeClassifier = mock(FileTypeClassifier.class);
when(fileTypeClassifier.isTest(any(), any(), any())).thenReturn(true);
when(fileTypeClassifier.isTest(any(), any(), anyBoolean(), any())).thenReturn(true);
var folderWrapper = new WorkspaceFolderWrapper(tempFolder.toUri(), new WorkspaceFolder(tempFolder.toString(), "My Folder"));
folderWrapper.setSettings(EMPTY_SETTINGS);
var folderFileSystem = new FolderFileSystem(folderWrapper, mock(JavaConfigCache.class), fileTypeClassifier);
Expand All @@ -88,7 +89,7 @@ void should_provide_test_files_of_requested_suffix() {
@Test
void should_provide_all_main_files() {
var fileTypeClassifier = mock(FileTypeClassifier.class);
when(fileTypeClassifier.isTest(any(), any(), any())).thenReturn(false);
when(fileTypeClassifier.isTest(any(), any(), anyBoolean(), any())).thenReturn(false);
var folderWrapper = new WorkspaceFolderWrapper(tempFolder.toUri(), new WorkspaceFolder(tempFolder.toString(), "My Folder"));
folderWrapper.setSettings(EMPTY_SETTINGS);
var folderFileSystem = new FolderFileSystem(folderWrapper, mock(JavaConfigCache.class), fileTypeClassifier);
Expand All @@ -104,7 +105,7 @@ void should_provide_all_main_files() {
@Test
void should_provide_all_test_files() {
var fileTypeClassifier = mock(FileTypeClassifier.class);
when(fileTypeClassifier.isTest(any(), any(), any())).thenReturn(true);
when(fileTypeClassifier.isTest(any(), any(), anyBoolean(), any())).thenReturn(true);
var folderWrapper = new WorkspaceFolderWrapper(tempFolder.toUri(), new WorkspaceFolder(tempFolder.toString(), "My Folder"));
folderWrapper.setSettings(EMPTY_SETTINGS);
var folderFileSystem = new FolderFileSystem(folderWrapper, mock(JavaConfigCache.class), fileTypeClassifier);
Expand All @@ -120,7 +121,7 @@ void should_provide_all_test_files() {
@Test
void should_throw_an_exception_when_folder_does_not_exist() {
var fileTypeClassifier = mock(FileTypeClassifier.class);
when(fileTypeClassifier.isTest(any(), any(), any())).thenReturn(false);
when(fileTypeClassifier.isTest(any(), any(), anyBoolean(), any())).thenReturn(false);
var folderWrapper = new WorkspaceFolderWrapper(URI.create("file:///wrong_path"), new WorkspaceFolder(tempFolder.toString(), "My Folder"));
folderWrapper.setSettings(EMPTY_SETTINGS);
var folderFileSystem = new FolderFileSystem(folderWrapper, mock(JavaConfigCache.class), fileTypeClassifier);
Expand Down

0 comments on commit d85a4ed

Please sign in to comment.