integration tests: temp dir, stable test output

This commit is contained in:
Leonid Shalupov
2012-04-22 23:10:49 +04:00
parent 40e594668b
commit 32a55f786c
2 changed files with 45 additions and 9 deletions
@@ -18,6 +18,8 @@ package org.jetbrains.kotlin;
import org.junit.Test; import org.junit.Test;
import java.io.File;
import static junit.framework.Assert.*; import static junit.framework.Assert.*;
public class CompilerSmokeTest extends KotlinIntegrationTestBase { public class CompilerSmokeTest extends KotlinIntegrationTestBase {
@@ -28,7 +30,9 @@ public class CompilerSmokeTest extends KotlinIntegrationTestBase {
@Test @Test
public void compileAndRunHelloApp() throws Exception { public void compileAndRunHelloApp() throws Exception {
assertEquals("compilation failed", 0, runCompiler("hello.compile", "-src", "hello.kt", "-jar", "hello.jar")); final String jar = tempDir.getAbsolutePath() + File.separator + "hello.jar";
runJava("hello.run", "-cp", "hello.jar", "Hello.namespace");
assertEquals("compilation failed", 0, runCompiler("hello.compile", "-src", "hello.kt", "-jar", jar));
runJava("hello.run", "-cp", jar, "Hello.namespace");
} }
} }
@@ -30,7 +30,10 @@ import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.ArrayUtil; import com.intellij.util.ArrayUtil;
import com.intellij.util.Function; import com.intellij.util.Function;
import org.apache.commons.lang.SystemUtils; import org.junit.Rule;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import sun.misc.JarFilter; import sun.misc.JarFilter;
import java.io.File; import java.io.File;
@@ -43,6 +46,30 @@ import static com.google.common.base.Charsets.UTF_8;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public abstract class KotlinIntegrationTestBase { public abstract class KotlinIntegrationTestBase {
protected File tempDir;
@Rule
public TestRule watchman = new TestWatcher() {
@Override
protected void starting(Description description) {
tempDir = Files.createTempDir();
}
@Override
protected void succeeded(Description description) {
try {
tempDir.delete();
} catch (Exception e) {
System.out.print("Can't delete temp directory " + tempDir + ": " + e);
}
}
@Override
protected void failed(Throwable e, Description description) {
System.err.println("Temp directory: " + tempDir);
}
};
protected int runCompiler(String logName, String... arguments) throws Exception { protected int runCompiler(String logName, String... arguments) throws Exception {
final File lib = getCompilerLib(); final File lib = getCompilerLib();
@@ -89,7 +116,8 @@ public abstract class KotlinIntegrationTestBase {
if (!goldFile.isFile()) { if (!goldFile.isFile()) {
Files.write(content, tmpFile, Charsets.UTF_8); Files.write(content, tmpFile, Charsets.UTF_8);
fail("No gold file " + goldFile); fail("No gold file " + goldFile);
} else { }
else {
final String goldContent = Files.toString(goldFile, UTF_8); final String goldContent = Files.toString(goldFile, UTF_8);
if (!goldContent.equals(content.toString())) { if (!goldContent.equals(content.toString())) {
Files.write(content, tmpFile, Charsets.UTF_8); Files.write(content, tmpFile, Charsets.UTF_8);
@@ -113,14 +141,18 @@ public abstract class KotlinIntegrationTestBase {
System.out.print(event.getText()); System.out.print(event.getText());
} }
else if (outputType == ProcessOutputTypes.STDOUT) { else if (outputType == ProcessOutputTypes.STDOUT) {
outContent.append("OUT "); appendToContent(outContent, "OUT ", event.getText());
outContent.append(event.getText());
} }
else if (outputType == ProcessOutputTypes.STDERR) { else if (outputType == ProcessOutputTypes.STDERR) {
errContent.append("ERR "); appendToContent(errContent, "ERR ", event.getText());
errContent.append(event.getText());
} }
} }
private void appendToContent(StringBuilder content, String prefix, String line) {
content.append(prefix);
content.append(StringUtil.trimTrailing(line));
content.append("\n");
}
}); });
handler.startNotify(); handler.startNotify();
@@ -129,7 +161,7 @@ public abstract class KotlinIntegrationTestBase {
executionLog.append(outContent); executionLog.append(outContent);
executionLog.append(errContent); executionLog.append(errContent);
executionLog.append("Return code: ").append(exitCode).append(SystemUtils.LINE_SEPARATOR); executionLog.append("Return code: ").append(exitCode).append("\n");
return exitCode; return exitCode;
} }