Skip to content

Commit 04ccca8

Browse files
committed
CLI tests: convert to generated
1 parent a53166b commit 04ccca8

File tree

7 files changed

+441
-217
lines changed

7 files changed

+441
-217
lines changed

compiler/tests/org/jetbrains/kotlin/cli/AbstractCliTest.java

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
package org.jetbrains.kotlin.cli;
1818

1919
import com.intellij.util.ArrayUtil;
20-
import com.intellij.util.Function;
21-
import com.intellij.util.containers.ContainerUtil;
2220
import kotlin.Pair;
21+
import kotlin.collections.CollectionsKt;
2322
import kotlin.io.FilesKt;
23+
import kotlin.jvm.functions.Function1;
2424
import kotlin.text.Charsets;
2525
import org.jetbrains.annotations.NotNull;
2626
import org.jetbrains.kotlin.cli.common.CLICompiler;
@@ -46,9 +46,6 @@
4646
import java.util.List;
4747

4848
public abstract class AbstractCliTest extends TestCaseWithTmpdir {
49-
static final String JS_TEST_DATA = "compiler/testData/cli/js";
50-
static final String JVM_TEST_DATA = "compiler/testData/cli/jvm";
51-
5249
@NotNull
5350
public static Pair<String, ExitCode> executeCompilerGrabOutput(@NotNull CLICompiler<?> compiler, @NotNull List<String> args) {
5451
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
@@ -88,17 +85,16 @@ public static String getNormalizedCompilerOutput(
8885
return normalizedOutputWithoutExitCode + exitCode;
8986
}
9087

91-
private void executeCompilerCompareOutput(@NotNull CLICompiler<?> compiler, @NotNull String testDataDir) throws Exception {
88+
private void doTest(@NotNull String fileName, @NotNull CLICompiler<?> compiler) throws Exception {
9289
System.setProperty("java.awt.headless", "true");
93-
String testMethodName = getTestName(true);
94-
Pair<String, ExitCode> outputAndExitCode =
95-
executeCompilerGrabOutput(compiler, readArgs(testDataDir + "/" + testMethodName + ".args", testDataDir,
96-
tmpdir.getPath()));
97-
String actual = getNormalizedCompilerOutput(outputAndExitCode.getFirst(), outputAndExitCode.getSecond(), testDataDir);
90+
Pair<String, ExitCode> outputAndExitCode = executeCompilerGrabOutput(compiler, readArgs(fileName, tmpdir.getPath()));
91+
String actual =
92+
getNormalizedCompilerOutput(outputAndExitCode.getFirst(), outputAndExitCode.getSecond(), new File(fileName).getParent());
9893

99-
KotlinTestUtils.assertEqualsToFile(new File(testDataDir + "/" + testMethodName + ".out"), actual);
94+
File outFile = new File(fileName.replaceFirst("\\.args$", ".out"));
95+
KotlinTestUtils.assertEqualsToFile(outFile, actual);
10096

101-
File additionalTestConfig = new File(testDataDir + "/" + testMethodName + ".test");
97+
File additionalTestConfig = new File(fileName.replaceFirst("\\.args$", ".test"));
10298
if (additionalTestConfig.exists()) {
10399
doTestAdditionalChecks(additionalTestConfig);
104100
}
@@ -134,37 +130,34 @@ else if (!file.isFile()) {
134130
}
135131

136132
@NotNull
137-
static List<String> readArgs(
138-
@NotNull String argsFilePath,
139-
@NotNull final String testDataDir,
140-
@NotNull final String tempDir
141-
) throws IOException {
133+
static List<String> readArgs(@NotNull final String argsFilePath, @NotNull final String tempDir) throws IOException {
142134
List<String> lines = FilesKt.readLines(new File(argsFilePath), Charsets.UTF_8);
143135

144-
return ContainerUtil.mapNotNull(lines, new Function<String, String>() {
136+
return CollectionsKt.mapNotNull(lines, new Function1<String, String>() {
145137
@Override
146-
public String fun(String arg) {
138+
public String invoke(String arg) {
147139
if (arg.isEmpty()) {
148140
return null;
149141
}
150-
// Do not replace : after \ (used in compiler plugin tests)
142+
143+
// Do not replace ':' after '\' (used in compiler plugin tests)
151144
String argsWithColonsReplaced = arg
152145
.replace("\\:", "$COLON$")
153146
.replace(":", File.pathSeparator)
154147
.replace("$COLON$", ":");
155148

156149
return argsWithColonsReplaced
157150
.replace("$TEMP_DIR$", tempDir)
158-
.replace("$TESTDATA_DIR$", testDataDir);
151+
.replace("$TESTDATA_DIR$", new File(argsFilePath).getParent());
159152
}
160153
});
161154
}
162155

163-
protected void executeCompilerCompareOutputJVM() throws Exception {
164-
executeCompilerCompareOutput(new K2JVMCompiler(), JVM_TEST_DATA);
156+
protected void doJvmTest(@NotNull String fileName) throws Exception {
157+
doTest(fileName, new K2JVMCompiler());
165158
}
166159

167-
protected void executeCompilerCompareOutputJS() throws Exception {
168-
executeCompilerCompareOutput(new K2JSCompiler(), JS_TEST_DATA);
160+
protected void doJsTest(@NotNull String fileName) throws Exception {
161+
doTest(fileName, new K2JSCompiler());
169162
}
170163
}

compiler/tests/org/jetbrains/kotlin/cli/AbstractKotlincExecutableTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,21 @@
2929
import java.util.List;
3030

3131
public abstract class AbstractKotlincExecutableTest extends TestCaseWithTmpdir {
32-
private void doTest(@NotNull String argsFilePath, @NotNull String executableName, @NotNull String testDataDir) throws Exception {
32+
private void doTest(@NotNull String argsFilePath, @NotNull String executableName) throws Exception {
3333
String executableFileName = SystemInfo.isWindows ? executableName + ".bat" : executableName;
3434
File kotlincFile = new File(PathUtil.getKotlinPathsForDistDirectory().getHomePath(), "bin/" + executableFileName);
3535
assertTrue("kotlinc executable not found, probably you need to invoke 'dist' Ant target: " + kotlincFile.getAbsolutePath(), kotlincFile.exists());
3636

37-
List<String> args = AbstractCliTest.readArgs(argsFilePath, testDataDir, tmpdir.getAbsolutePath());
37+
List<String> args = AbstractCliTest.readArgs(argsFilePath, tmpdir.getAbsolutePath());
3838
args.add(0, kotlincFile.getAbsolutePath());
3939
ProcessOutput processOutput = ExecUtil.execAndGetOutput(args, null);
4040

4141
String stdout = processOutput.getStdout();
4242
String stderr = processOutput.getStderr();
4343
int exitCode = processOutput.getExitCode();
4444

45-
String normalizedOutput = AbstractCliTest.getNormalizedCompilerOutput(stderr, ExitCode.values()[exitCode], testDataDir);
45+
String normalizedOutput =
46+
AbstractCliTest.getNormalizedCompilerOutput(stderr, ExitCode.values()[exitCode], new File(argsFilePath).getParent());
4647
File outFile = new File(argsFilePath.replace(".args", ".out"));
4748

4849
try {
@@ -58,10 +59,10 @@ private void doTest(@NotNull String argsFilePath, @NotNull String executableName
5859
}
5960

6061
protected void doJvmTest(@NotNull String argsFilePath) throws Exception {
61-
doTest(argsFilePath, "kotlinc-jvm", AbstractCliTest.JVM_TEST_DATA);
62+
doTest(argsFilePath, "kotlinc-jvm");
6263
}
6364

6465
protected void doJsTest(@NotNull String argsFilePath) throws Exception {
65-
doTest(argsFilePath, "kotlinc-js", AbstractCliTest.JS_TEST_DATA);
66+
doTest(argsFilePath, "kotlinc-js");
6667
}
6768
}

0 commit comments

Comments
 (0)