Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add usecase test interceptor #2486

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clients/cobol-lsp-vscode-extension/dist/note.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
-->

# Dialect support information
Place bundled extension code into this folder
Place bundled extension code into this folder
4 changes: 4 additions & 0 deletions server/engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@
<!--suppress UnresolvedMavenProperty -->
<value>${listingSnap}</value>
</property>
<property>
<name>usecase.test.repo.dir</name>
<value>${usecase.test.repo.dir}</value>
</property>
</systemPropertyVariables>
<excludes>
<exclude>**/org/eclipse/lsp/cobol/dialects/**/*</exclude>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2024 Broadcom.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Broadcom, Inc. - initial API and implementation
*
*/
package org.eclipse.lsp.cobol.usecases.suite;

import lombok.experimental.UtilityClass;
import org.junit.jupiter.api.extension.ExtensionContext;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Utility class for TestWatchers
*/
@UtilityClass
public class TestWatcherHelper {
private static final Pattern UNIT_TEST_ID_PATTERN =
Pattern.compile("\\[.*?\\]/\\[.*?\\]/\\[.*?\\]/\\[.*?:(.*?)\\]");
static final String ROOT_DIRECTORY_PROPERTY = "usecase.test.repo.dir";

Path fetchTargetPath(ExtensionContext context, String rootFolder)
throws IOException {
String className = context.getTestClass().get().getSimpleName();
String methodName = context.getRequiredTestMethod().getName();
Optional<String> parameterInvocation = getParameterInvocation(context);

List<String> subfolder = new ArrayList<>(Arrays.asList(className, methodName));
parameterInvocation.ifPresent(subfolder::add);

Path mainFolderPath = Paths.get(rootFolder, subfolder.toArray(new String[0]));
Files.createDirectories(mainFolderPath);
return mainFolderPath;
}

private static Optional<String> getParameterInvocation(ExtensionContext context) {
Matcher matcher = UNIT_TEST_ID_PATTERN.matcher(context.getUniqueId());
return matcher.find() ? Optional.of(matcher.group(1)) : Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* Copyright (c) 2024 Broadcom.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Broadcom, Inc. - initial API and implementation
*
*/
package org.eclipse.lsp.cobol.usecases.suite;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.lsp.cobol.test.CobolText;
import org.eclipse.lsp.cobol.test.engine.PreprocessedDocument;
import org.eclipse.lsp.cobol.test.engine.TestData;
import org.eclipse.lsp4j.DiagnosticSeverity;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestWatcher;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;

import static org.eclipse.lsp.cobol.usecases.suite.TestWatcherHelper.ROOT_DIRECTORY_PROPERTY;

/**
* This is unit test watcher class and is responsible to write test data if "usecase.test.repo.dir"
* system property is configured
*/
@Slf4j
public class UniqueIdTestWatcher implements TestWatcher {
public static final String SOURCE_CBL = "source.cbl";
public static final String COPYBOOK_EXT = ".cpy";

@Override
public void testSuccessful(ExtensionContext context) {
interceptor(context);
}

@Override
public void testFailed(ExtensionContext context, Throwable cause) {
LOG.info("{} test failed", context.getRequiredTestMethod().toString());
interceptor(context);
}

@Override
public void testAborted(ExtensionContext context, Throwable cause) {
LOG.info("{} test is aborted", context.getRequiredTestMethod().toString());
interceptor(context);
}

@Override
public void testDisabled(ExtensionContext context, Optional<String> reason) {
LOG.info(
"{} test is disabled due to {}",
context.getRequiredTestMethod().toString(),
reason.orElse("unknown reason"));
}

@SneakyThrows
private static void interceptor(ExtensionContext context) {
Optional<String> rootDirectory =
Optional.ofNullable(System.getProperty(ROOT_DIRECTORY_PROPERTY));
if (rootDirectory.isPresent()) {
String rootFolder = rootDirectory.get();
PreprocessedDocument document = getDocumentFromContext(context);

Path mainFolderPath = TestWatcherHelper.fetchTargetPath(context, rootFolder);
writeCobolDocument(document, mainFolderPath);
writeCopybooks(document.getCopybooks(), mainFolderPath);
if (Objects.nonNull(document.getTestData())) {
writeTestData(document.getTestData(), mainFolderPath);
createZeroDiagnosticIndicatorFile(document.getTestData(), mainFolderPath);
} else {
LOG.info("no test data for {}", context.getRequiredTestMethod().toString());
Files.createFile(mainFolderPath.resolve("NoTestData"));
}
}
}

private static PreprocessedDocument getDocumentFromContext(ExtensionContext context) {
return (PreprocessedDocument)
context
.getStore(ExtensionContext.Namespace.create(context.getRequiredTestMethod()))
.get("document");
}

private static void writeCobolDocument(PreprocessedDocument document, Path mainFolderPath)
throws IOException {
Path cobolDocPath = mainFolderPath.resolve(SOURCE_CBL);
writeToFile(cobolDocPath, document.getText());
}

private static void writeCopybooks(List<CobolText> copybooks, Path mainFolderPath) {
copybooks.forEach(
cobolText -> {
try {
Path filePath = mainFolderPath.resolve(cobolText.getFileName() + COPYBOOK_EXT);
writeToFile(filePath, cobolText.getFullText());
System.out.println("Created file: " + filePath);
} catch (IOException e) {
System.err.println("Error writing copybook: " + e.getMessage());
}
});
}

private static void writeTestData(TestData testData, Path mainFolderPath) throws IOException {
Path testDataPath = mainFolderPath.resolve("testData.txt");
Files.deleteIfExists(testDataPath);
slavek-kucera marked this conversation as resolved.
Show resolved Hide resolved
writeToFile(testDataPath, testData.toString());
}

private static void writeToFile(Path filePath, String content) throws IOException {
Files.write(filePath, content.getBytes());
}

private static void createZeroDiagnosticIndicatorFile(TestData testData, Path mainFolderPath)
throws IOException {
boolean hasErrorDiagnostics =
testData.getDiagnostics().values().stream()
.flatMap(List::stream)
.anyMatch(d -> d.getSeverity() == DiagnosticSeverity.Error);

Path diaFlag = mainFolderPath.resolve("ZeroDiagnosticsFlag");
if (!hasErrorDiagnostics) {
if (!Files.exists(diaFlag)) Files.createFile(diaFlag);
} else {
Files.deleteIfExists(diaFlag);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#/*
# * Copyright (c) 2024 Broadcom.
# * The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
# *
# * This program and the accompanying materials are made
# * available under the terms of the Eclipse Public License 2.0
# * which is available at https://www.eclipse.org/legal/epl-2.0/
# *
# * SPDX-License-Identifier: EPL-2.0
# *
# * Contributors:
# * Broadcom, Inc. - initial API and implementation
# *
# */
org.eclipse.lsp.cobol.usecases.suite.UniqueIdTestWatcher
org.eclipse.lsp.cobol.test.engine.ExtensionContextProvider
org.eclipse.lsp.cobol.test.engine.ExtensionContextCleaner
1 change: 1 addition & 0 deletions server/engine/src/test/resources/junit-platform.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
junit.jupiter.extensions.autodetection.enabled=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2024 Broadcom.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Broadcom, Inc. - initial API and implementation
*
*/
package org.eclipse.lsp.cobol.test.engine;

import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

/**
* ExtensionContextCleaner
*/
public class ExtensionContextCleaner implements AfterEachCallback {

/**
*
* @param context
*/
@Override
public void afterEach(ExtensionContext context) {
slavek-kucera marked this conversation as resolved.
Show resolved Hide resolved
ExtensionContextProvider.clearExtensionContext();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2024 Broadcom.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Broadcom, Inc. - initial API and implementation
*
*/

package org.eclipse.lsp.cobol.test.engine;

import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import java.util.Optional;

/**
* BeforeEachCallback for Use case test
*/
public class ExtensionContextProvider implements BeforeEachCallback {

private static final ThreadLocal<ExtensionContext> CONTEXT_HOLDER = new ThreadLocal<>();

@Override
public void beforeEach(ExtensionContext context) {
CONTEXT_HOLDER.set(context);
}

/**
* get junit extensionContext
*
* @return ExtensionContext
*/
public static Optional<ExtensionContext> getExtensionContext() {
return Optional.ofNullable(CONTEXT_HOLDER.get());
}

/**
* clear context
*/
public static void clearExtensionContext() {
CONTEXT_HOLDER.remove();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* to the actual Language Engine, and the testDAta will be used to assert the result.
*/
@Value
class PreprocessedDocument {
public class PreprocessedDocument {
String text;
List<CobolText> copybooks;
TestData testData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/** This data class defines output of use-case text preprocessor */
@Value
@Builder(toBuilder = true)
class TestData {
public class TestData {
String text;
String copybookName;
String dialectType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimap;
import com.google.gson.JsonElement;

import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
Expand Down Expand Up @@ -262,6 +263,7 @@ public AnalysisResult runTest(
PreprocessedDocument document =
AnnotatedDocumentCleaning.prepareDocument(
text, copybooks, subroutineNames, expectedDiagnostics, sqlBackendSetting, analysisConfig.getCompilerOptions());

AnalysisResult actual =
analyze(
UseCase.builder()
Expand All @@ -278,6 +280,7 @@ public AnalysisResult runTest(
.build(),
languageId);
assertResultEquals(actual, document.getTestData());
UseCaseUtils.storeDocumentToUnitTextExtensionContext(document.getText(), document.getCopybooks(), document.getTestData());
slavek-kucera marked this conversation as resolved.
Show resolved Hide resolved
slavek-kucera marked this conversation as resolved.
Show resolved Hide resolved
return actual;
}

Expand Down
Loading
Loading