From 8e480e275b12cb0feb6c364602ee9a42187428c9 Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Mon, 7 Nov 2016 16:11:55 +0300 Subject: [PATCH] Ignore files with incompatible target when check that all test are presented --- .../kotlin/test/InTextDirectivesUtils.java | 36 +++++++++++++++++ .../kotlin/test/KotlinTestUtils.java | 3 +- .../tests/generator/SimpleTestClassModel.java | 4 +- .../generator/SimpleTestMethodModel.java | 39 +++---------------- 4 files changed, 45 insertions(+), 37 deletions(-) diff --git a/compiler/tests-common/org/jetbrains/kotlin/test/InTextDirectivesUtils.java b/compiler/tests-common/org/jetbrains/kotlin/test/InTextDirectivesUtils.java index 276bee40376..bc7dc88bb88 100644 --- a/compiler/tests-common/org/jetbrains/kotlin/test/InTextDirectivesUtils.java +++ b/compiler/tests-common/org/jetbrains/kotlin/test/InTextDirectivesUtils.java @@ -18,6 +18,7 @@ package org.jetbrains.kotlin.test; import com.google.common.collect.Lists; import com.google.common.collect.Sets; +import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.util.ArrayUtil; import kotlin.text.StringsKt; @@ -26,12 +27,15 @@ import org.jetbrains.annotations.Nullable; import org.junit.Assert; 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"; + private InTextDirectivesUtils() { } @@ -203,4 +207,36 @@ public final class InTextDirectivesUtils { 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 backends = findLinesWithPrefixesRemoved(textWithDirectives(file), "// TARGET_BACKEND: "); + return backends.isEmpty() || backends.contains(targetBackend.name()); + } + + public static boolean isIgnoredTarget(TargetBackend targetBackend, File file) { + if (targetBackend == TargetBackend.ANY) return false; + + List ignoredBackends = findLinesWithPrefixesRemoved(textWithDirectives(file), "// IGNORE_BACKEND: "); + return ignoredBackends.contains(targetBackend.name()); + } } diff --git a/compiler/tests-common/org/jetbrains/kotlin/test/KotlinTestUtils.java b/compiler/tests-common/org/jetbrains/kotlin/test/KotlinTestUtils.java index 808ce999e94..858457fbb11 100644 --- a/compiler/tests-common/org/jetbrains/kotlin/test/KotlinTestUtils.java +++ b/compiler/tests-common/org/jetbrains/kotlin/test/KotlinTestUtils.java @@ -830,6 +830,7 @@ public class KotlinTestUtils { @NotNull Class testCaseClass, @NotNull File testDataDir, @NotNull Pattern filenamePattern, + @NotNull TargetBackend targetBackend, boolean recursive, @NotNull String... excludeDirs ) { @@ -846,7 +847,7 @@ public class KotlinTestUtils { assertTestClassPresentByMetadata(testCaseClass, file); } } - else if (filenamePattern.matcher(file.getName()).matches()) { + else if (filenamePattern.matcher(file.getName()).matches() && InTextDirectivesUtils.isCompatibleTarget(targetBackend, file)) { assertFilePathPresent(file, rootFile, filePaths); } } diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/generator/SimpleTestClassModel.java b/generators/src/org/jetbrains/kotlin/generators/tests/generator/SimpleTestClassModel.java index 58b75dd5310..33328d0ac78 100644 --- a/generators/src/org/jetbrains/kotlin/generators/tests/generator/SimpleTestClassModel.java +++ b/generators/src/org/jetbrains/kotlin/generators/tests/generator/SimpleTestClassModel.java @@ -218,8 +218,8 @@ public class SimpleTestClassModel implements TestClassModel { exclude.append("\""); } String assertTestsPresentStr = String.format( - "KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s%s);", - KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()), recursive, exclude + "KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s.%s, %s%s);", + KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()), TargetBackend.class.getSimpleName(), targetBackend.toString(), recursive, exclude ); p.println(assertTestsPresentStr); } diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/generator/SimpleTestMethodModel.java b/generators/src/org/jetbrains/kotlin/generators/tests/generator/SimpleTestMethodModel.java index f00008a5001..4502f8347ee 100644 --- a/generators/src/org/jetbrains/kotlin/generators/tests/generator/SimpleTestMethodModel.java +++ b/generators/src/org/jetbrains/kotlin/generators/tests/generator/SimpleTestMethodModel.java @@ -26,13 +26,12 @@ import org.jetbrains.kotlin.test.TargetBackend; import org.jetbrains.kotlin.utils.Printer; import java.io.File; -import java.io.IOException; -import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import static org.jetbrains.kotlin.test.InTextDirectivesUtils.isIgnoredTarget; + public class SimpleTestMethodModel implements TestMethodModel { - private static final String DIRECTIVES_FILE_NAME = "directives.txt"; @NotNull private final File rootDir; @@ -75,14 +74,14 @@ public class SimpleTestMethodModel implements TestMethodModel { String filePath = KotlinTestUtils.getFilePath(file) + (file.isDirectory() ? "/" : ""); p.println("String fileName = KotlinTestUtils.navigationMetadata(\"", filePath, "\");"); - if (isIgnored()) { + if (isIgnoredTarget(targetBackend, file)) { p.println("try {"); p.pushIndent(); } p.println(doTestMethodName, "(fileName);"); - if (isIgnored()) { + if (isIgnoredTarget(targetBackend, file)) { p.println("throw new AssertionError(\"Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that.\");"); p.popIndent(); p.println("}"); @@ -98,37 +97,9 @@ public class SimpleTestMethodModel implements TestMethodModel { return KotlinTestUtils.getFilePath(new File(path)); } - private String textWithDirectives() { - 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); - } - } - @Override public boolean shouldBeGenerated() { - if (targetBackend == TargetBackend.ANY) return true; - - List backends = InTextDirectivesUtils.findLinesWithPrefixesRemoved(textWithDirectives(), "// TARGET_BACKEND: "); - return backends.isEmpty() || backends.contains(targetBackend.name()); - } - - private boolean isIgnored() { - if (targetBackend == TargetBackend.ANY) return false; - - List ignoredBackends = InTextDirectivesUtils.findLinesWithPrefixesRemoved(textWithDirectives(), "// IGNORE_BACKEND: "); - return ignoredBackends.contains(targetBackend.name()); + return InTextDirectivesUtils.isCompatibleTarget(targetBackend, file); } @NotNull