[TEST] Move utils for checking all files presented to KtTestUtil

This is needed to remove dependency on :tests-common from module
  with abstract test generators
This commit is contained in:
Dmitriy Novozhilov
2020-12-14 13:26:50 +03:00
parent 64ce307f7f
commit 9e2d691425
19 changed files with 266 additions and 214 deletions
@@ -0,0 +1,36 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@file:JvmName("KtAssert")
package org.jetbrains.kotlin.test
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
/*
* Those functions are needed only in this module because it has no testing framework
* with assertions in it's dependencies
*/
internal fun fail(message: String) {
throw AssertionError(message)
}
@OptIn(ExperimentalContracts::class)
internal fun assertNotNull(message: String, value: Any?) {
contract {
returns() implies (value != null)
}
if (value == null) {
fail(message)
}
}
internal fun assertTrue(message: String, value: Boolean) {
if (!value) {
fail(message)
}
}
@@ -0,0 +1,259 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.test;
import com.google.common.collect.Lists;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.ArrayUtil;
import kotlin.text.StringsKt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.utils.ExceptionUtilsKt;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.*;
public final class InTextDirectivesUtils {
private static final String DIRECTIVES_FILE_NAME = "directives.txt";
public static final String IGNORE_BACKEND_DIRECTIVE_PREFIX = "// IGNORE_BACKEND: ";
private InTextDirectivesUtils() {
}
@Nullable
public static Integer getPrefixedInt(String fileText, String prefix) {
String[] strings = findArrayWithPrefixes(fileText, prefix);
if (strings.length > 0) {
assert strings.length == 1;
return Integer.parseInt(strings[0]);
}
return null;
}
@Nullable
public static Boolean getPrefixedBoolean(String fileText, String prefix) {
String[] strings = findArrayWithPrefixes(fileText, prefix);
if (strings.length > 0) {
assert strings.length == 1;
return Boolean.parseBoolean(strings[0]);
}
return null;
}
@NotNull
public static String[] findArrayWithPrefixes(@NotNull String fileText, @NotNull String... prefixes) {
return ArrayUtil.toStringArray(findListWithPrefixes(fileText, prefixes));
}
@NotNull
public static List<String> findListWithPrefixes(@NotNull String fileText, @NotNull String... prefixes) {
List<String> result = new ArrayList<>();
for (String line : findLinesWithPrefixesRemoved(fileText, prefixes)) {
splitValues(result, line);
}
return result;
}
public static List<String> splitValues(List<String> result, String line) {
String unquoted = StringUtil.unquoteString(line);
if (!unquoted.equals(line)) {
result.add(unquoted);
}
else{
String[] variants = line.split(",");
for (String variant : variants) {
result.add(variant.trim());
}
}
return result;
}
public static boolean isDirectiveDefined(String fileText, String directive) {
return !findListWithPrefixes(fileText, directive).isEmpty();
}
@Nullable
public static String findStringWithPrefixes(String fileText, String... prefixes) {
List<String> strings = findListWithPrefixes(fileText, prefixes);
if (strings.isEmpty()) {
return null;
}
if (strings.size() != 1) {
throw new IllegalStateException("There is more than one string with given prefixes " +
Arrays.toString(prefixes) + ":\n" +
StringUtil.join(strings, "\n") + "\n" +
"Use findListWithPrefixes() instead.");
}
return strings.get(0);
}
@NotNull
public static List<String> findLinesWithPrefixesRemoved(String fileText, String... prefixes) {
return findLinesWithPrefixesRemoved(fileText, true, true, prefixes);
}
@NotNull
public static List<String> findLinesWithPrefixesRemoved(String fileText, boolean trim, boolean strict, String... prefixes) {
if (prefixes.length == 0) {
throw new IllegalArgumentException("Please specify the prefixes to check");
}
List<String> result = new ArrayList<>();
List<String> cleanedPrefixes = cleanDirectivesFromComments(Arrays.asList(prefixes));
for (String line : fileNonEmptyCommentedLines(fileText)) {
for (String prefix : cleanedPrefixes) {
if (line.startsWith(prefix)) {
String noPrefixLine = line.substring(prefix.length());
if (noPrefixLine.isEmpty() ||
Character.isWhitespace(noPrefixLine.charAt(0)) ||
Character.isWhitespace(prefix.charAt(prefix.length() - 1))) {
result.add(trim ? noPrefixLine.trim() : StringUtil.trimTrailing(StringsKt.drop(noPrefixLine, 1)));
break;
} else if (strict) {
throw new AssertionError(
"Line starts with prefix \"" + prefix + "\", but doesn't have space symbol after it: " + line);
}
}
}
}
return result;
}
public static void assertHasUnknownPrefixes(String fileText, Collection<String> knownPrefixes) {
Set<String> prefixes = new HashSet<>();
for (String line : fileNonEmptyCommentedLines(fileText)) {
String prefix = probableDirective(line);
if (prefix != null) {
prefixes.add(prefix);
}
}
prefixes.removeAll(cleanDirectivesFromComments(knownPrefixes));
KtAssert.assertTrue("File contains some unexpected directives" + prefixes, prefixes.isEmpty());
}
private static String probableDirective(String line) {
String[] arr = line.split(" ", 2);
String firstWord = arr[0];
if (firstWord.length() > 1 && StringUtil.toUpperCase(firstWord).equals(firstWord)) {
return firstWord;
}
return null;
}
private static List<String> cleanDirectivesFromComments(Collection<String> prefixes) {
List<String> resultPrefixes = Lists.newArrayList();
for (String prefix : prefixes) {
if (prefix.startsWith("//") || prefix.startsWith("##")) {
resultPrefixes.add(StringUtil.trimLeading(prefix.substring(2)));
}
else {
resultPrefixes.add(prefix);
}
}
return resultPrefixes;
}
@NotNull
private static List<String> fileNonEmptyCommentedLines(String fileText) {
List<String> result = new ArrayList<>();
try {
try (BufferedReader reader = new BufferedReader(new StringReader(fileText))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("//") || line.startsWith("##")) {
String uncommentedLine = line.substring(2).trim();
if (!uncommentedLine.isEmpty()) {
result.add(uncommentedLine);
}
}
}
}
}
catch (IOException e) {
throw ExceptionUtilsKt.rethrow(e);
}
return result;
}
private static String textWithDirectives(File file) {
try {
String fileText;
if (file.isDirectory()) {
File directivesFile = new File(file, DIRECTIVES_FILE_NAME);
if (!directivesFile.exists()) return "";
fileText = FileUtil.loadFile(directivesFile);
}
else {
fileText = FileUtil.loadFile(file);
}
return fileText;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static boolean isCompatibleTarget(TargetBackend targetBackend, File file) {
if (targetBackend == TargetBackend.ANY) return true;
List<String> doNotTarget = findLinesWithPrefixesRemoved(textWithDirectives(file), "// DONT_TARGET_EXACT_BACKEND: ");
if (doNotTarget.contains(targetBackend.name()))
return false;
List<String> backends = findLinesWithPrefixesRemoved(textWithDirectives(file), "// TARGET_BACKEND: ");
return isCompatibleTargetExceptAny(targetBackend, backends);
}
private static boolean isCompatibleTargetExceptAny(TargetBackend targetBackend, List<String> backends) {
if (targetBackend == TargetBackend.ANY) return false;
return backends.isEmpty() || backends.contains(targetBackend.name()) || isCompatibleTargetExceptAny(targetBackend.getCompatibleWith(), backends);
}
public static boolean isIgnoredTarget(TargetBackend targetBackend, File file, String ignoreBackendDirectivePrefix) {
List<String> ignoredBackends = findListWithPrefixes(textWithDirectives(file), ignoreBackendDirectivePrefix);
return ignoredBackends.contains(targetBackend.name());
}
public static boolean isIgnoredTarget(TargetBackend targetBackend, File file) {
return isIgnoredTarget(targetBackend, file, IGNORE_BACKEND_DIRECTIVE_PREFIX);
}
public static boolean dontRunGeneratedCode(TargetBackend targetBackend, File file) {
List<String> backends = findListWithPrefixes(textWithDirectives(file), "// DONT_RUN_GENERATED_CODE: ");
return backends.contains(targetBackend.name());
}
// Whether the target test is supposed to pass successfully on targetBackend
public static boolean isPassingTarget(TargetBackend targetBackend, File file) {
return isCompatibleTarget(targetBackend, file) && !isIgnoredTarget(targetBackend, file);
}
}
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.test
enum class TargetBackend(
val isIR: Boolean,
private val compatibleWithTargetBackend: TargetBackend? = null
) {
ANY(false),
JVM(false),
JVM_OLD(false, JVM),
JVM_IR(true, JVM),
JVM_MULTI_MODULE_IR_AGAINST_OLD(true, JVM_IR),
JVM_MULTI_MODULE_OLD_AGAINST_IR(false, JVM),
JS(false),
JS_IR(true, JS),
JS_IR_ES6(true, JS_IR),
WASM(true),
ANDROID(false, JVM);
val compatibleWith get() = compatibleWithTargetBackend ?: ANY
}
@@ -0,0 +1,28 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface TestMetadata {
String value();
}
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.test.util;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtilRt;
import com.intellij.openapi.vfs.CharsetToolkit;
@@ -13,15 +14,26 @@ import com.intellij.psi.PsiFileFactory;
import com.intellij.psi.impl.PsiFileFactoryImpl;
import com.intellij.testFramework.LightVirtualFile;
import com.intellij.util.PathUtil;
import com.intellij.util.containers.ContainerUtil;
import kotlin.collections.SetsKt;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.idea.KotlinLanguage;
import org.jetbrains.kotlin.psi.KtFile;
import org.jetbrains.kotlin.test.KtAssert;
import org.jetbrains.kotlin.test.TargetBackend;
import org.jetbrains.kotlin.test.TestMetadata;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
import static org.jetbrains.kotlin.test.InTextDirectivesUtils.isCompatibleTarget;
public class KtTestUtil {
private static String homeDir = computeHomeDirectory();
@@ -193,4 +205,196 @@ public class KtTestUtil {
throw new IllegalStateException("Failed to create " + file);
}
}
// ---------------------- assert testdata presented by metadata ----------------------
private static final String PLEASE_REGENERATE_TESTS = "Please regenerate tests (GenerateTests.kt)";
public static void assertAllTestsPresentByMetadataWithExcluded(
@NotNull Class<?> testCaseClass,
@NotNull File testDataDir,
@NotNull Pattern filenamePattern,
@Nullable Pattern excludedPattern,
boolean recursive,
@NotNull String... excludeDirs
) {
assertAllTestsPresentByMetadataWithExcluded(testCaseClass, testDataDir, filenamePattern, excludedPattern, TargetBackend.ANY, recursive, excludeDirs);
}
public static void assertAllTestsPresentByMetadata(
@NotNull Class<?> testCaseClass,
@NotNull File testDataDir,
@NotNull Pattern filenamePattern,
boolean recursive,
@NotNull String... excludeDirs
) {
assertAllTestsPresentByMetadata(
testCaseClass,
testDataDir,
filenamePattern,
TargetBackend.ANY,
recursive,
excludeDirs
);
}
public static void assertAllTestsPresentByMetadataWithExcluded(
@NotNull Class<?> testCaseClass,
@NotNull File testDataDir,
@NotNull Pattern filenamePattern,
@Nullable Pattern excludedPattern,
@NotNull TargetBackend targetBackend,
boolean recursive,
@NotNull String... excludeDirs
) {
File rootFile = new File(getTestsRoot(testCaseClass));
Set<String> filePaths = collectPathsMetadata(testCaseClass);
Set<String> exclude = SetsKt.setOf(excludeDirs);
File[] files = testDataDir.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
if (recursive && containsTestData(file, filenamePattern, excludedPattern) && !exclude.contains(file.getName())) {
assertTestClassPresentByMetadata(testCaseClass, file);
}
}
else {
boolean excluded = excludedPattern != null && excludedPattern.matcher(file.getName()).matches();
if (!excluded && filenamePattern.matcher(file.getName()).matches() && isCompatibleTarget(targetBackend, file)) {
assertFilePathPresent(file, rootFile, filePaths);
}
}
}
}
}
public static void assertAllTestsPresentByMetadata(
@NotNull Class<?> testCaseClass,
@NotNull File testDataDir,
@NotNull Pattern filenamePattern,
@NotNull TargetBackend targetBackend,
boolean recursive,
@NotNull String... excludeDirs
) {
assertAllTestsPresentByMetadataWithExcluded(testCaseClass, testDataDir, filenamePattern, null, targetBackend, recursive, excludeDirs);
}
public static void assertAllTestsPresentInSingleGeneratedClass(
@NotNull Class<?> testCaseClass,
@NotNull File testDataDir,
@NotNull Pattern filenamePattern
) {
assertAllTestsPresentInSingleGeneratedClass(testCaseClass, testDataDir, filenamePattern, TargetBackend.ANY);
}
public static void assertAllTestsPresentInSingleGeneratedClassWithExcluded(
@NotNull Class<?> testCaseClass,
@NotNull File testDataDir,
@NotNull Pattern filenamePattern,
@Nullable Pattern excludePattern
) {
assertAllTestsPresentInSingleGeneratedClass(testCaseClass, testDataDir, filenamePattern, excludePattern, TargetBackend.ANY);
}
public static void assertAllTestsPresentInSingleGeneratedClass(
@NotNull Class<?> testCaseClass,
@NotNull File testDataDir,
@NotNull Pattern filenamePattern,
@NotNull TargetBackend targetBackend
) {
assertAllTestsPresentInSingleGeneratedClass(testCaseClass, testDataDir, filenamePattern, null, targetBackend);
}
public static void assertAllTestsPresentInSingleGeneratedClass(
@NotNull Class<?> testCaseClass,
@NotNull File testDataDir,
@NotNull Pattern filenamePattern,
@Nullable Pattern excludePattern,
@NotNull TargetBackend targetBackend
) {
File rootFile = new File(getTestsRoot(testCaseClass));
Set<String> filePaths = collectPathsMetadata(testCaseClass);
FileUtil.processFilesRecursively(testDataDir, file -> {
boolean excluded = excludePattern != null && excludePattern.matcher(file.getName()).matches();
if (file.isFile() && !excluded && filenamePattern.matcher(file.getName()).matches() && isCompatibleTarget(targetBackend, file)) {
assertFilePathPresent(file, rootFile, filePaths);
}
return true;
});
}
private static void assertFilePathPresent(File file, File rootFile, Set<String> filePaths) {
String path = FileUtil.getRelativePath(rootFile, file);
if (path != null) {
String relativePath = nameToCompare(path);
if (!filePaths.contains(relativePath)) {
KtAssert.fail("Test data file missing from the generated test class: " + file + "\n" + PLEASE_REGENERATE_TESTS);
}
}
}
private static Set<String> collectPathsMetadata(Class<?> testCaseClass) {
return new HashSet<>(ContainerUtil.map(collectMethodsMetadata(testCaseClass), KtTestUtil::nameToCompare));
}
@Nullable
public static String getMethodMetadata(Method method) {
TestMetadata testMetadata = method.getAnnotation(TestMetadata.class);
return (testMetadata != null) ? testMetadata.value() : null;
}
private static Set<String> collectMethodsMetadata(Class<?> testCaseClass) {
Set<String> filePaths = new HashSet<>();
for (Method method : testCaseClass.getDeclaredMethods()) {
String path = getMethodMetadata(method);
if (path != null) {
filePaths.add(path);
}
}
return filePaths;
}
private static boolean containsTestData(File dir, Pattern filenamePattern, @Nullable Pattern excludedPattern) {
File[] files = dir.listFiles();
assert files != null;
for (File file : files) {
if (file.isDirectory()) {
if (containsTestData(file, filenamePattern, excludedPattern)) {
return true;
}
}
else {
boolean excluded = excludedPattern != null && excludedPattern.matcher(file.getName()).matches();
if (! excluded && filenamePattern.matcher(file.getName()).matches()) {
return true;
}
}
}
return false;
}
private static void assertTestClassPresentByMetadata(@NotNull Class<?> outerClass, @NotNull File testDataDir) {
for (Class<?> nestedClass : outerClass.getDeclaredClasses()) {
TestMetadata testMetadata = nestedClass.getAnnotation(TestMetadata.class);
if (testMetadata != null && testMetadata.value().equals(KtTestUtil.getFilePath(testDataDir))) {
return;
}
}
KtAssert.fail("Test data directory missing from the generated test class: " + testDataDir + "\n" + PLEASE_REGENERATE_TESTS);
}
public static String getTestsRoot(@NotNull Class<?> testCaseClass) {
TestMetadata testClassMetadata = testCaseClass.getAnnotation(TestMetadata.class);
KtAssert.assertNotNull("No metadata for class: " + testCaseClass, testClassMetadata);
return testClassMetadata.value();
}
public static String nameToCompare(@NotNull String name) {
return (SystemInfo.isFileSystemCaseSensitive ? name : name.toLowerCase()).replace('\\', '/');
}
}