Refactoring: extract mutation with file logic to separate file
This commit is contained in:
@@ -31,9 +31,7 @@ import junit.framework.TestCase;
|
|||||||
import kotlin.Unit;
|
import kotlin.Unit;
|
||||||
import kotlin.collections.CollectionsKt;
|
import kotlin.collections.CollectionsKt;
|
||||||
import kotlin.collections.SetsKt;
|
import kotlin.collections.SetsKt;
|
||||||
import kotlin.io.FilesKt;
|
|
||||||
import kotlin.jvm.functions.Function1;
|
import kotlin.jvm.functions.Function1;
|
||||||
import kotlin.text.Charsets;
|
|
||||||
import kotlin.text.StringsKt;
|
import kotlin.text.StringsKt;
|
||||||
import org.jetbrains.annotations.NonNls;
|
import org.jetbrains.annotations.NonNls;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@@ -112,7 +110,6 @@ public class KotlinTestUtils {
|
|||||||
|
|
||||||
private static final boolean AUTOMATICALLY_UNMUTE_PASSED_TESTS = false;
|
private static final boolean AUTOMATICALLY_UNMUTE_PASSED_TESTS = false;
|
||||||
private static final boolean AUTOMATICALLY_MUTE_FAILED_TESTS = false;
|
private static final boolean AUTOMATICALLY_MUTE_FAILED_TESTS = false;
|
||||||
private static final String AUTOMATICALLY_MUTE_FAILED_TESTS_WITH_CONTENT = null;
|
|
||||||
|
|
||||||
private static final List<File> filesToDelete = new ArrayList<>();
|
private static final List<File> filesToDelete = new ArrayList<>();
|
||||||
|
|
||||||
@@ -1048,7 +1045,7 @@ public class KotlinTestUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public interface DoTest {
|
public interface DoTest {
|
||||||
void invoke(String filePath) throws Exception;
|
void invoke(@NotNull String filePath) throws Exception;
|
||||||
}
|
}
|
||||||
|
|
||||||
// In this test runner version the `testDataFile` parameter is annotated by `TestDataFile`.
|
// In this test runner version the `testDataFile` parameter is annotated by `TestDataFile`.
|
||||||
@@ -1074,38 +1071,7 @@ public class KotlinTestUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void runTest(DoTest test, String testDataFilePath) throws Exception {
|
private static void runTest(DoTest test, String testDataFilePath) throws Exception {
|
||||||
testWithMuteInFile(test, testDataFilePath);
|
MuteWithFileKt.testWithMuteInFile(test).invoke(testDataFilePath);
|
||||||
}
|
|
||||||
|
|
||||||
private static void testWithMuteInFile(DoTest test, String testDataFilePath) throws Exception {
|
|
||||||
File testDataFile = new File(testDataFilePath);
|
|
||||||
|
|
||||||
boolean isMutedWithFile = isMutedWithFile(testDataFile);
|
|
||||||
if (isMutedWithFile && !RUN_IGNORED_TESTS_AS_REGULAR) {
|
|
||||||
System.err.println("IGNORED TEST: " + testDataFilePath);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
test.invoke(testDataFilePath);
|
|
||||||
}
|
|
||||||
catch (Throwable e) {
|
|
||||||
boolean hasFailFile = hasFailFile(testDataFile);
|
|
||||||
if (hasFailFile) {
|
|
||||||
if (checkFailFile(e, testDataFile)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//noinspection ConstantConditions
|
|
||||||
if (!isMutedWithFile && !hasFailFile && AUTOMATICALLY_MUTE_FAILED_TESTS_WITH_CONTENT != null) {
|
|
||||||
createMuteFile(testDataFile, AUTOMATICALLY_MUTE_FAILED_TESTS_WITH_CONTENT);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
|
|
||||||
Assert.assertNull("Test is good but there is a fail file", failFile(testDataFile));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static DoTest testWithCustomIgnoreDirective(DoTest test, TargetBackend targetBackend, String ignoreDirective) throws Exception {
|
private static DoTest testWithCustomIgnoreDirective(DoTest test, TargetBackend targetBackend, String ignoreDirective) throws Exception {
|
||||||
@@ -1183,58 +1149,6 @@ public class KotlinTestUtils {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isMutedWithFile(@NotNull File testDataFile) {
|
|
||||||
if (!testDataFile.isFile()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
File muteFile = new File(testDataFile.getPath() + ".mute");
|
|
||||||
return muteFile.exists() && muteFile.isFile();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void createMuteFile(@NotNull File testDataFile, @NotNull String text) throws IOException {
|
|
||||||
if (text.isEmpty()) {
|
|
||||||
throw new IllegalArgumentException("Mute text must not be empty");
|
|
||||||
}
|
|
||||||
|
|
||||||
FilesKt.writeText(new File(testDataFile.getPath() + ".mute"), text, Charsets.UTF_8);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private static File failFile(@NotNull File testDataFile) {
|
|
||||||
if (!testDataFile.isFile()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
File failFile = new File(testDataFile.getPath() + ".fail");
|
|
||||||
if (!failFile.exists() || !failFile.isFile()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return failFile;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean hasFailFile(@NotNull File testDataFile) {
|
|
||||||
return failFile(testDataFile) != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean checkFailFile(@NotNull Throwable failure, @NotNull File testDataFile) {
|
|
||||||
File failFile = failFile(testDataFile);
|
|
||||||
if (failFile == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
String muteMessage = failure.getMessage();
|
|
||||||
|
|
||||||
Throwable cause = failure.getCause();
|
|
||||||
if (cause != null) {
|
|
||||||
muteMessage = muteMessage + "\n" + cause.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEqualsToFile(failFile, muteMessage);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getTestsRoot(@NotNull Class<?> testCaseClass) {
|
public static String getTestsRoot(@NotNull Class<?> testCaseClass) {
|
||||||
TestMetadata testClassMetadata = testCaseClass.getAnnotation(TestMetadata.class);
|
TestMetadata testClassMetadata = testCaseClass.getAnnotation(TestMetadata.class);
|
||||||
Assert.assertNotNull("No metadata for class: " + testCaseClass, testClassMetadata);
|
Assert.assertNotNull("No metadata for class: " + testCaseClass, testClassMetadata);
|
||||||
|
|||||||
@@ -0,0 +1,86 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2019 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 org.jetbrains.kotlin.test.KotlinTestUtils.DoTest
|
||||||
|
import org.junit.Assert
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
private val RUN_MUTED_TESTS = java.lang.Boolean.getBoolean("org.jetbrains.kotlin.run.muted.tests")
|
||||||
|
private val AUTOMATICALLY_MUTE_FAILED_TESTS_WITH_CONTENT: String? = null
|
||||||
|
|
||||||
|
@Throws(Exception::class)
|
||||||
|
fun testWithMuteInFile(test: DoTest): DoTest {
|
||||||
|
return object : DoTest {
|
||||||
|
override fun invoke(filePath: String) {
|
||||||
|
val testDataFile = File(filePath)
|
||||||
|
|
||||||
|
val isMutedWithFile = isMutedWithFile(testDataFile)
|
||||||
|
if (isMutedWithFile && !RUN_MUTED_TESTS) {
|
||||||
|
System.err.println("IGNORED TEST: $filePath")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val failFile = failFile(testDataFile)
|
||||||
|
val hasFailFile = failFile != null
|
||||||
|
|
||||||
|
try {
|
||||||
|
test.invoke(filePath)
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
if (checkFailFile(e, testDataFile)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isMutedWithFile && !hasFailFile && AUTOMATICALLY_MUTE_FAILED_TESTS_WITH_CONTENT != null) {
|
||||||
|
createMuteFile(testDataFile, AUTOMATICALLY_MUTE_FAILED_TESTS_WITH_CONTENT)
|
||||||
|
}
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.assertNull("Test is good but there is a fail file", failFile)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun isMutedWithFile(testDataFile: File): Boolean {
|
||||||
|
if (!testDataFile.isFile) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
val muteFile = File("${testDataFile.path}.mute")
|
||||||
|
return muteFile.exists() && muteFile.isFile
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createMuteFile(testDataFile: File, text: String) {
|
||||||
|
require(text.isNotEmpty()) { "Mute text must not be empty" }
|
||||||
|
|
||||||
|
File("${testDataFile.path}.mute").writeText(text)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun failFile(testDataFile: File): File? {
|
||||||
|
if (!testDataFile.isFile) return null
|
||||||
|
|
||||||
|
val failFile = File("${testDataFile.path}.fail")
|
||||||
|
if (!failFile.exists() || !failFile.isFile) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
return failFile
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun checkFailFile(failure: Throwable, testDataFile: File): Boolean {
|
||||||
|
val failFile = failFile(testDataFile) ?: return false
|
||||||
|
val cause = failure.cause
|
||||||
|
val muteMessage = failure.message +
|
||||||
|
if (cause != null) {
|
||||||
|
"\n" + cause
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
}
|
||||||
|
|
||||||
|
KotlinTestUtils.assertEqualsToFile(failFile, muteMessage)
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user