Skip to content

Commit

Permalink
Оптимизация механизма запуска тестов.
Browse files Browse the repository at this point in the history
Добавлен параметр конфигурации для расчета списка тестов силами BSL Language Server. По умолчанию ищутся все методы с аннотацией "Тест". Механизм включен по умолчанию.
Добавлен параметр конфигурации для задания списка каталогов, в которых располагаются тесты.
  • Loading branch information
nixel2007 committed Jan 19, 2025
1 parent df086dc commit 1f90151
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,29 @@
*/
package com.github._1c_syntax.bsl.languageserver.codelenses;

import com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration;
import com.github._1c_syntax.bsl.languageserver.configuration.codelens.TestRunnerAdapterOptions;
import com.github._1c_syntax.bsl.languageserver.context.DocumentContext;
import com.github._1c_syntax.bsl.languageserver.context.FileType;
import com.github._1c_syntax.bsl.languageserver.events.LanguageServerInitializeRequestReceivedEvent;
import com.github._1c_syntax.utils.Absolute;
import lombok.RequiredArgsConstructor;
import org.eclipse.lsp4j.ClientInfo;
import org.eclipse.lsp4j.InitializeParams;
import org.springframework.context.event.EventListener;

import java.net.URI;
import java.nio.file.Path;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

@RequiredArgsConstructor
public abstract class AbstractRunTestsCodeLensSupplier<T extends CodeLensData>
implements CodeLensSupplier<T> {

protected final LanguageServerConfiguration configuration;

private boolean clientIsSupported;

/**
Expand All @@ -57,6 +68,23 @@ public void handleEvent(LanguageServerInitializeRequestReceivedEvent event) {
*/
@Override
public boolean isApplicable(DocumentContext documentContext) {
return documentContext.getFileType() == FileType.OS && clientIsSupported;
var configurationRoot = Optional.ofNullable(documentContext.getServerContext().getConfigurationRoot())
.map(Path::toString)
.orElse("");
var uri = documentContext.getUri();

var testSources = configuration.getCodeLensOptions().getTestRunnerAdapterOptions().getTestSources()
.stream()
.map(testDir -> Path.of(configurationRoot, testDir))
.map(path -> Absolute.path(path).toUri())
.collect(Collectors.toSet());

return documentContext.getFileType() == FileType.OS
&& testSources.stream().anyMatch(testSource -> isInside(uri, testSource))
&& clientIsSupported;
}

private static boolean isInside(URI childURI, URI parentURI) {
return !parentURI.relativize(childURI).isAbsolute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,25 @@
* Поставщик линзы для запуска всех тестов в текущем файле.
*/
@Component
@RequiredArgsConstructor
@Slf4j
public class RunAllTestsCodeLensSupplier
extends AbstractRunTestsCodeLensSupplier<DefaultCodeLensData> {

private static final String COMMAND_ID = "language-1c-bsl.languageServer.runAllTests";

private final TestRunnerAdapter testRunnerAdapter;
private final LanguageServerConfiguration configuration;
private final Resources resources;

public RunAllTestsCodeLensSupplier(
LanguageServerConfiguration configuration,
TestRunnerAdapter testRunnerAdapter,
Resources resources
) {
super(configuration);
this.testRunnerAdapter = testRunnerAdapter;
this.resources = resources;
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,25 @@
* Поставщик линз для запуска теста по конкретному тестовому методу.
*/
@Component
@RequiredArgsConstructor
@Slf4j
public class RunTestCodeLensSupplier
extends AbstractRunTestsCodeLensSupplier<RunTestCodeLensSupplier.RunTestCodeLensData> {

private static final String COMMAND_ID = "language-1c-bsl.languageServer.runTest";

private final TestRunnerAdapter testRunnerAdapter;
private final LanguageServerConfiguration configuration;
private final Resources resources;

public RunTestCodeLensSupplier(
LanguageServerConfiguration configuration,
TestRunnerAdapter testRunnerAdapter,
Resources resources
) {
super(configuration);
this.testRunnerAdapter = testRunnerAdapter;
this.resources = resources;
}

/**
* {@inheritDoc}
*/
Expand All @@ -77,7 +85,7 @@ public List<CodeLens> getCodeLenses(DocumentContext documentContext) {
.map(symbolTree::getMethodSymbol)
.flatMap(Optional::stream)
.map(methodSymbol -> toCodeLens(methodSymbol, documentContext))
.collect(Collectors.toList());
.toList();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration;
import com.github._1c_syntax.bsl.languageserver.context.DocumentContext;
import com.github._1c_syntax.bsl.languageserver.context.symbol.MethodSymbol;
import com.github._1c_syntax.bsl.languageserver.context.symbol.annotations.Annotation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.exec.CommandLine;
Expand All @@ -46,7 +48,6 @@
import java.util.WeakHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* Расчетчик списка тестов в документе.
Expand Down Expand Up @@ -77,6 +78,16 @@ public List<String> getTestIds(DocumentContext documentContext) {
private List<String> computeTestIds(DocumentContext documentContext) {
var options = configuration.getCodeLensOptions().getTestRunnerAdapterOptions();

if (options.isGetTestsByTestRunner()) {
return computeTestIdsByTestRunner(documentContext);
}

return computeTestIdsByLanguageServer(documentContext);
}

private List<String> computeTestIdsByTestRunner(DocumentContext documentContext) {
var options = configuration.getCodeLensOptions().getTestRunnerAdapterOptions();

var executable = SystemUtils.IS_OS_WINDOWS ? options.getExecutableWin() : options.getExecutable();
var path = Paths.get(documentContext.getUri()).toString();
var arguments = String.format(options.getGetTestsArguments(), path);
Expand Down Expand Up @@ -123,7 +134,17 @@ private List<String> computeTestIds(DocumentContext documentContext) {
.map(getTestsRegex::matcher)
.filter(Matcher::matches)
.map(matcher -> matcher.group(1))
.collect(Collectors.toList());
.toList();
}

private List<String> computeTestIdsByLanguageServer(DocumentContext documentContext) {
return documentContext.getSymbolTree()
.getMethods()
.stream()
.filter(methodSymbol -> methodSymbol.getAnnotations().stream()
.map(Annotation::getName)
.anyMatch("Тест"::equalsIgnoreCase))
.map(MethodSymbol::getName)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.SystemUtils;

import java.util.List;
import java.util.Set;

/**
* Параметры запускателя тестового фреймворка.
*/
Expand All @@ -37,6 +40,11 @@
@JsonIgnoreProperties(ignoreUnknown = true)
public class TestRunnerAdapterOptions {

/**
* Каталоги с исходными файлами тестов.
*/
private Set<String> testSources = Set.of("tests");

/**
* Имя исполняемого файла тестового фреймворка (linux и macOS).
*/
Expand All @@ -45,6 +53,10 @@ public class TestRunnerAdapterOptions {
* Имя исполняемого файла тестового фреймворка (windows).
*/
private String executableWin = "1testrunner.bat";
/**
* Флаг, указывающий на необходимость получения списка тестов через исполняемый файл тестового фреймворка.
*/
private boolean getTestsByTestRunner;
/**
* Аргументы для получения списка тестов.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.github._1c_syntax.utils.Absolute;
import com.github._1c_syntax.utils.Lazy;
import edu.umd.cs.findbugs.annotations.Nullable;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -67,6 +68,7 @@ public class ServerContext {
private final Lazy<CF> configurationMetadata = new Lazy<>(this::computeConfigurationMetadata);
@Nullable
@Setter
@Getter
private Path configurationRoot;
private final Map<URI, String> mdoRefs = Collections.synchronizedMap(new HashMap<>());
private final Map<String, Map<ModuleType, DocumentContext>> documentsByMDORef
Expand Down

0 comments on commit 1f90151

Please sign in to comment.