From fe829add440f4ab7ea36f155efbb5095f81d3a60 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 2 Aug 2018 15:02:16 +0200 Subject: [PATCH] Minor, use TEMP_DIR/TESTDATA_DIR in CLI tests on xml build files Otherwise the "outputDir" in these tests was treated as relative to the project root, and starting from 63b4302cea, running these tests resulted in the "whatever" directory being created in the project root --- .../cli/jvm/duplicateSourcesInModule.xml | 8 ++-- .../cli/jvm/nonexistentPathInModule.xml | 6 +-- .../jetbrains/kotlin/cli/AbstractCliTest.java | 44 ++++++++++++------- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/compiler/testData/cli/jvm/duplicateSourcesInModule.xml b/compiler/testData/cli/jvm/duplicateSourcesInModule.xml index ce4f16a0fdc..409ec1db942 100644 --- a/compiler/testData/cli/jvm/duplicateSourcesInModule.xml +++ b/compiler/testData/cli/jvm/duplicateSourcesInModule.xml @@ -1,6 +1,6 @@ - - - + + + - \ No newline at end of file + diff --git a/compiler/testData/cli/jvm/nonexistentPathInModule.xml b/compiler/testData/cli/jvm/nonexistentPathInModule.xml index 1ddd1289d9a..15459bf5184 100644 --- a/compiler/testData/cli/jvm/nonexistentPathInModule.xml +++ b/compiler/testData/cli/jvm/nonexistentPathInModule.xml @@ -1,6 +1,6 @@ - - - + + + diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/cli/AbstractCliTest.java b/compiler/tests-common/tests/org/jetbrains/kotlin/cli/AbstractCliTest.java index 5d41d96929c..1e1af36c1ce 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/cli/AbstractCliTest.java +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/cli/AbstractCliTest.java @@ -49,6 +49,7 @@ public abstract class AbstractCliTest extends TestCaseWithTmpdir { private static final String TESTDATA_DIR = "$TESTDATA_DIR$"; private static final String EXPERIMENTAL_ARGFILE_ARGUMENT_PREFIX = "-Xargfile="; + private static final String BUILD_FILE_ARGUMENT_PREFIX = "-Xbuild-file="; public static Pair executeCompilerGrabOutput(@NotNull CLITool compiler, @NotNull List args) { StringBuilder output = new StringBuilder(); @@ -200,29 +201,38 @@ public abstract class AbstractCliTest extends TestCaseWithTmpdir { String argWithTestPathsReplaced = replaceTestPaths(argWithColonsReplaced, testDataDir, tempDir); + if (arg.startsWith(BUILD_FILE_ARGUMENT_PREFIX)) { + return createTempFileWithPathsReplaced(argWithTestPathsReplaced, BUILD_FILE_ARGUMENT_PREFIX, ".xml", testDataDir, tempDir); + } + if (arg.startsWith(EXPERIMENTAL_ARGFILE_ARGUMENT_PREFIX)) { - return mockArgfile(argWithTestPathsReplaced, testDataDir, tempDir); - } - else { - return argWithTestPathsReplaced; + return createTempFileWithPathsReplaced( + argWithTestPathsReplaced, EXPERIMENTAL_ARGFILE_ARGUMENT_PREFIX, "", testDataDir, tempDir + ); } + + return argWithTestPathsReplaced; } - // Create new temp. argfile with all test paths replaced and return argfile-argument pointing to that file - private static String mockArgfile(@NotNull String argfileArgument, @NotNull String testDataDir, @NotNull String tempDir) { - String argfilePath = kotlin.text.StringsKt.substringAfter(argfileArgument, EXPERIMENTAL_ARGFILE_ARGUMENT_PREFIX, argfileArgument); - File argfile = new File(argfilePath); + // Create new temporary file with all test paths replaced and return the new argument value with the new file path + @NotNull + private static String createTempFileWithPathsReplaced( + @NotNull String argument, + @NotNull String argumentPrefix, + @NotNull String tempFileSuffix, + @NotNull String testDataDir, + @NotNull String tempDir + ) { + String filePath = kotlin.text.StringsKt.substringAfter(argument, argumentPrefix, argument); + File file = new File(filePath); + if (!file.exists()) return argument; - if (argfile.exists()) { - File mockArgfile = FilesKt.createTempFile(argfile.getAbsolutePath(), "", new File(tempDir)); - String oldArgfileContent = FilesKt.readText(argfile, Charsets.UTF_8); - String newArgfileContent = replaceTestPaths(oldArgfileContent, testDataDir, tempDir); - FilesKt.writeText(mockArgfile, newArgfileContent, Charsets.UTF_8); - return EXPERIMENTAL_ARGFILE_ARGUMENT_PREFIX + mockArgfile.getAbsolutePath(); - } else { - return argfileArgument; - } + File result = FilesKt.createTempFile(file.getAbsolutePath(), tempFileSuffix, new File(tempDir)); + String oldContent = FilesKt.readText(file, Charsets.UTF_8); + String newContent = replaceTestPaths(oldContent, testDataDir, tempDir); + FilesKt.writeText(result, newContent, Charsets.UTF_8); + return argumentPrefix + result.getAbsolutePath(); } private static String replaceTestPaths(@NotNull String str, @NotNull String testDataDir, @NotNull String tempDir) {