Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Swift] Check process exit status during tests. #2604

Merged
merged 1 commit into from
Dec 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,23 @@ public class BaseSwiftTest implements RuntimeTestSupport {
throw new RuntimeException("Swift runtime file not found at:" + swiftRuntime.getPath());
}
ANTLR_RUNTIME_PATH = swiftRuntime.getPath();
fastFailRunProcess(ANTLR_RUNTIME_PATH, SWIFT_CMD, "build");
try {
fastFailRunProcess(ANTLR_RUNTIME_PATH, SWIFT_CMD, "build");
}
catch (IOException | InterruptedException e) {
e.printStackTrace();
throw new RuntimeException(e);
}

// shutdown logic
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
fastFailRunProcess(ANTLR_RUNTIME_PATH, SWIFT_CMD, "package", "clean");
try {
fastFailRunProcess(ANTLR_RUNTIME_PATH, SWIFT_CMD, "package", "clean");
}
catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
});
}
Expand Down Expand Up @@ -145,8 +156,14 @@ public String execLexer(String grammarFileName, String grammarStr, String lexerN

String projectName = "testcase-" + System.currentTimeMillis();
String projectDir = getTmpDir() + "/" + projectName;
buildProject(projectDir, projectName);
return execTest(projectDir, projectName);
try {
buildProject(projectDir, projectName);
return execTest(projectDir, projectName);
}
catch (IOException | InterruptedException e) {
e.printStackTrace();
return null;
}
}

@Override
Expand Down Expand Up @@ -183,28 +200,24 @@ private void addSourceFiles(String... files) {
Collections.addAll(this.sourceFiles, files);
}

private void buildProject(String projectDir, String projectName) {
private void buildProject(String projectDir, String projectName) throws IOException, InterruptedException {
mkdir(projectDir);
fastFailRunProcess(projectDir, SWIFT_CMD, "package", "init", "--type", "executable");
for (String sourceFile: sourceFiles) {
String absPath = getTmpDir() + "/" + sourceFile;
fastFailRunProcess(getTmpDir(), "mv", "-f", absPath, projectDir + "/Sources/" + projectName);
}
fastFailRunProcess(getTmpDir(), "mv", "-f", "input", projectDir);

try {
String dylibPath = ANTLR_RUNTIME_PATH + "/.build/debug/";
Pair<String, String> buildResult = runProcess(projectDir, SWIFT_CMD, "build",
"-Xswiftc", "-I"+dylibPath,
"-Xlinker", "-L"+dylibPath,
"-Xlinker", "-lAntlr4",
"-Xlinker", "-rpath",
"-Xlinker", dylibPath);
if (buildResult.b.length() > 0) {
throw new RuntimeException("unit test build failed: " + buildResult.a + "\n" + buildResult.b);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
String dylibPath = ANTLR_RUNTIME_PATH + "/.build/debug/";
// System.err.println(dylibPath);
Pair<String, String> buildResult = runProcess(projectDir, SWIFT_CMD, "build",
"-Xswiftc", "-I"+dylibPath,
"-Xlinker", "-L"+dylibPath,
"-Xlinker", "-lAntlr4",
"-Xlinker", "-rpath",
"-Xlinker", dylibPath);
if (buildResult.b.length() > 0) {
throw new IOException("unit test build failed: " + buildResult.a + "\n" + buildResult.b);
}
}

Expand All @@ -214,20 +227,22 @@ private static Pair<String,String> runProcess(String execPath, String... args) t
StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream());
stdoutVacuum.start();
stderrVacuum.start();
process.waitFor();
int status = process.waitFor();
stdoutVacuum.join();
stderrVacuum.join();
if (status != 0) {
throw new IOException("Process exited with status " + status + ":\n" + stdoutVacuum.toString() + "\n" + stderrVacuum.toString());
}
return new Pair<>(stdoutVacuum.toString(), stderrVacuum.toString());
}

private static void fastFailRunProcess(String workingDir, String... command) {
private static void fastFailRunProcess(String workingDir, String... command) throws IOException, InterruptedException {
ProcessBuilder builder = new ProcessBuilder(command);
builder.directory(new File(workingDir));
try {
Process p = builder.start();
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
Process p = builder.start();
int status = p.waitFor();
if (status != 0) {
throw new IOException("Process exited with status " + status);
}
}

Expand All @@ -251,8 +266,14 @@ private String execParser(String parserName,
addSourceFiles("main.swift");
String projectName = "testcase-" + System.currentTimeMillis();
String projectDir = getTmpDir() + "/" + projectName;
buildProject(projectDir, projectName);
return execTest(projectDir, projectName);
try {
buildProject(projectDir, projectName);
return execTest(projectDir, projectName);
}
catch (IOException | InterruptedException e) {
e.printStackTrace();
return null;
}
}

private void writeParserTestFile(String parserName,
Expand Down