JS: generate paths in source maps relative to .map file location
See KT-19818
This commit is contained in:
@@ -43,19 +43,29 @@ import org.junit.Assert;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class AbstractCliTest extends TestCaseWithTmpdir {
|
||||
@NotNull
|
||||
private static final String TESTDATA_DIR = "$TESTDATA_DIR$";
|
||||
|
||||
public static Pair<String, ExitCode> executeCompilerGrabOutput(@NotNull CLITool<?> compiler, @NotNull List<String> args) {
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
PrintStream origErr = System.err;
|
||||
try {
|
||||
System.setErr(new PrintStream(bytes));
|
||||
ExitCode exitCode = CLITool.doMainNoExit(compiler, ArrayUtil.toStringArray(args));
|
||||
ExitCode exitCode;
|
||||
int index = 0;
|
||||
do {
|
||||
int next = args.subList(index, args.size()).indexOf("---");
|
||||
if (next == -1) {
|
||||
next = args.size();
|
||||
}
|
||||
exitCode = CLITool.doMainNoExit(compiler, ArrayUtil.toStringArray(args.subList(index, next)));
|
||||
if (exitCode != ExitCode.OK) break;
|
||||
index = next + 1;
|
||||
} while (index < args.size());
|
||||
return new Pair<>(bytes.toString("utf-8"), exitCode);
|
||||
}
|
||||
catch (Exception e) {
|
||||
@@ -70,8 +80,8 @@ public abstract class AbstractCliTest extends TestCaseWithTmpdir {
|
||||
public static String getNormalizedCompilerOutput(@NotNull String pureOutput, @NotNull ExitCode exitCode, @NotNull String testDataDir) {
|
||||
String testDataAbsoluteDir = new File(testDataDir).getAbsolutePath();
|
||||
String normalizedOutputWithoutExitCode = StringUtil.convertLineSeparators(pureOutput)
|
||||
.replace(testDataAbsoluteDir, "$TESTDATA_DIR$")
|
||||
.replace(FileUtil.toSystemIndependentName(testDataAbsoluteDir), "$TESTDATA_DIR$")
|
||||
.replace(testDataAbsoluteDir, TESTDATA_DIR)
|
||||
.replace(FileUtil.toSystemIndependentName(testDataAbsoluteDir), TESTDATA_DIR)
|
||||
.replace(PathUtil.getKotlinPathsForDistDirectory().getHomePath().getAbsolutePath(), "$PROJECT_DIR$")
|
||||
.replace("expected version is " + JvmMetadataVersion.INSTANCE, "expected version is $ABI_VERSION$")
|
||||
.replace("expected version is " + JsMetadataVersion.INSTANCE, "expected version is $ABI_VERSION$")
|
||||
@@ -81,7 +91,7 @@ public abstract class AbstractCliTest extends TestCaseWithTmpdir {
|
||||
return normalizedOutputWithoutExitCode + exitCode + "\n";
|
||||
}
|
||||
|
||||
private void doTest(@NotNull String fileName, @NotNull CLITool<?> compiler) throws Exception {
|
||||
private void doTest(@NotNull String fileName, @NotNull CLITool<?> compiler) {
|
||||
System.setProperty("java.awt.headless", "true");
|
||||
Pair<String, ExitCode> outputAndExitCode = executeCompilerGrabOutput(compiler, readArgs(fileName, tmpdir.getPath()));
|
||||
String actual = getNormalizedCompilerOutput(
|
||||
@@ -93,17 +103,17 @@ public abstract class AbstractCliTest extends TestCaseWithTmpdir {
|
||||
|
||||
File additionalTestConfig = new File(fileName.replaceFirst("\\.args$", ".test"));
|
||||
if (additionalTestConfig.exists()) {
|
||||
doTestAdditionalChecks(additionalTestConfig);
|
||||
doTestAdditionalChecks(additionalTestConfig, fileName);
|
||||
}
|
||||
}
|
||||
|
||||
private void doTestAdditionalChecks(@NotNull File testConfigFile) throws IOException {
|
||||
private void doTestAdditionalChecks(@NotNull File testConfigFile, @NotNull String argsFilePath) {
|
||||
List<String> diagnostics = new ArrayList<>(0);
|
||||
String content = FilesKt.readText(testConfigFile, Charsets.UTF_8);
|
||||
|
||||
List<String> existsList = InTextDirectivesUtils.findListWithPrefixes(content, "// EXISTS: ");
|
||||
for (String fileName : existsList) {
|
||||
File file = new File(tmpdir, fileName);
|
||||
File file = checkedPathToFile(fileName, argsFilePath);
|
||||
if (!file.exists()) {
|
||||
diagnostics.add("File does not exist, but should: " + fileName);
|
||||
}
|
||||
@@ -114,7 +124,7 @@ public abstract class AbstractCliTest extends TestCaseWithTmpdir {
|
||||
|
||||
List<String> absentList = InTextDirectivesUtils.findListWithPrefixes(content, "// ABSENT: ");
|
||||
for (String fileName : absentList) {
|
||||
File file = new File(tmpdir, fileName);
|
||||
File file = checkedPathToFile(fileName, argsFilePath);
|
||||
if (file.exists() && file.isFile()) {
|
||||
diagnostics.add("File exists, but shouldn't: " + fileName);
|
||||
}
|
||||
@@ -125,7 +135,7 @@ public abstract class AbstractCliTest extends TestCaseWithTmpdir {
|
||||
String[] parts = containsSpec.split(",", 2);
|
||||
String fileName = parts[0].trim();
|
||||
String contentToSearch = parts[1].trim();
|
||||
File file = new File(tmpdir, fileName);
|
||||
File file = checkedPathToFile(fileName, argsFilePath);
|
||||
if (!file.exists()) {
|
||||
diagnostics.add("File does not exist: " + fileName);
|
||||
}
|
||||
@@ -147,7 +157,17 @@ public abstract class AbstractCliTest extends TestCaseWithTmpdir {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static List<String> readArgs(@NotNull String argsFilePath, @NotNull String tempDir) throws IOException {
|
||||
private File checkedPathToFile(@NotNull String path, @NotNull String argsFilePath) {
|
||||
if (path.startsWith(TESTDATA_DIR + "/")) {
|
||||
return new File(new File(argsFilePath).getParent(), path.substring(TESTDATA_DIR.length() + 1));
|
||||
}
|
||||
else {
|
||||
return new File(tmpdir, path);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static List<String> readArgs(@NotNull String argsFilePath, @NotNull String tempDir) {
|
||||
List<String> lines = FilesKt.readLines(new File(argsFilePath), Charsets.UTF_8);
|
||||
|
||||
return CollectionsKt.mapNotNull(lines, arg -> {
|
||||
@@ -163,7 +183,7 @@ public abstract class AbstractCliTest extends TestCaseWithTmpdir {
|
||||
|
||||
return argsWithColonsReplaced
|
||||
.replace("$TEMP_DIR$", tempDir)
|
||||
.replace("$TESTDATA_DIR$", new File(argsFilePath).getParent())
|
||||
.replace(TESTDATA_DIR, new File(argsFilePath).getParent())
|
||||
.replace(
|
||||
"$FOREIGN_ANNOTATIONS_DIR$",
|
||||
new File(AbstractForeignAnnotationsTestKt.getFOREIGN_ANNOTATIONS_SOURCES_PATH()).getPath()
|
||||
@@ -171,15 +191,15 @@ public abstract class AbstractCliTest extends TestCaseWithTmpdir {
|
||||
});
|
||||
}
|
||||
|
||||
protected void doJvmTest(@NotNull String fileName) throws Exception {
|
||||
protected void doJvmTest(@NotNull String fileName) {
|
||||
doTest(fileName, new K2JVMCompiler());
|
||||
}
|
||||
|
||||
protected void doJsTest(@NotNull String fileName) throws Exception {
|
||||
protected void doJsTest(@NotNull String fileName) {
|
||||
doTest(fileName, new K2JSCompiler());
|
||||
}
|
||||
|
||||
protected void doJsDceTest(@NotNull String fileName) throws Exception {
|
||||
protected void doJsDceTest(@NotNull String fileName) {
|
||||
doTest(fileName, new K2JSDce());
|
||||
}
|
||||
|
||||
|
||||
@@ -665,6 +665,12 @@ public class CliTestGenerated extends AbstractCliTest {
|
||||
doJsTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("sourceMapRelativeRoot.args")
|
||||
public void testSourceMapRelativeRoot() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/js/sourceMapRelativeRoot.args");
|
||||
doJsTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("sourceMapRootAuto.args")
|
||||
public void testSourceMapRootAuto() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/js/sourceMapRootAuto.args");
|
||||
|
||||
Reference in New Issue
Block a user