TmpdirRule

junit4 @Rule to create tmp dir from idea SDK
This commit is contained in:
Stepan Koltsov
2012-06-14 15:29:16 +04:00
parent 9db8a63099
commit 4e99edccaa
6 changed files with 82 additions and 29 deletions
@@ -8,6 +8,7 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="idea-full" level="project" />
<orderEntry type="module" module-name="compiler-tests" />
</component>
</module>
@@ -25,7 +25,7 @@ import static junit.framework.Assert.assertEquals;
public class AntTaskTest extends KotlinIntegrationTestBase {
@Test
public void antTaskJvm() throws Exception {
final String jar = tempDir.getAbsolutePath() + File.separator + "hello.jar";
final String jar = tmpdir.getTmpDir().getAbsolutePath() + File.separator + "hello.jar";
assertEquals("compilation failed", 0, runAnt("build.log", "build.xml"));
runJava("hello.run", "-cp", jar, "Hello.namespace");
@@ -41,7 +41,7 @@ public class AntTaskTest extends KotlinIntegrationTestBase {
return runJava(logName, "-jar", getAntHome() + File.separator + "lib" + File.separator + "ant-launcher.jar",
"-Dkotlin.lib=" + getCompilerLib(),
"-Dtest.data=" + testDataDir,
"-Dtemp=" + tempDir,
"-Dtemp=" + tmpdir.getTmpDir(),
"-f", scriptName);
}
@@ -30,7 +30,7 @@ public class CompilerSmokeTest extends KotlinIntegrationTestBase {
@Test
public void compileAndRunHelloApp() throws Exception {
final String jar = tempDir.getAbsolutePath() + File.separator + "hello.jar";
final String jar = tmpdir.getTmpDir().getAbsolutePath() + File.separator + "hello.jar";
assertEquals("compilation failed", 0, runCompiler("hello.compile", "-src", "hello.kt", "-jar", jar));
runJava("hello.run", "-cp", jar, "Hello.namespace");
@@ -38,7 +38,7 @@ public class CompilerSmokeTest extends KotlinIntegrationTestBase {
@Test
public void compileAndRunModule() throws Exception {
final String jar = tempDir.getAbsolutePath() + File.separator + "smoke.jar";
final String jar = tmpdir.getTmpDir().getAbsolutePath() + File.separator + "smoke.jar";
assertEquals("compilation failed", 0, runCompiler("Smoke.compile", "-module", "Smoke.kts", "-jar", jar));
runJava("Smoke.run", "-cp", jar + File.pathSeparator + getKotlinRuntimePath(), "Smoke.namespace", "1", "2", "3");
@@ -46,14 +46,14 @@ public class CompilerSmokeTest extends KotlinIntegrationTestBase {
@Test
public void compilationFailed() throws Exception {
final String jar = tempDir.getAbsolutePath() + File.separator + "smoke.jar";
final String jar = tmpdir.getTmpDir().getAbsolutePath() + File.separator + "smoke.jar";
runCompiler("hello.compile", "-src", "hello.kt", "-jar", jar);
}
@Test
public void syntaxErrors() throws Exception {
final String jar = tempDir.getAbsolutePath() + File.separator + "smoke.jar";
final String jar = tmpdir.getTmpDir().getAbsolutePath() + File.separator + "smoke.jar";
runCompiler("test.compile", "-src", "test.kt", "-jar", jar);
}
@@ -29,6 +29,7 @@ import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.ArrayUtil;
import org.jetbrains.jet.test.Tmpdir;
import org.junit.ComparisonFailure;
import org.junit.Rule;
import org.junit.rules.TestRule;
@@ -45,39 +46,20 @@ import static com.google.common.base.Charsets.UTF_8;
import static org.junit.Assert.*;
public abstract class KotlinIntegrationTestBase {
protected File tempDir;
protected File testDataDir;
@Rule
public final Tmpdir tmpdir = new Tmpdir();
@Rule
public TestRule watchman = new TestWatcher() {
@Override
protected void starting(Description description) {
try {
tempDir = Files.createTempDir().getCanonicalFile();
}
catch (IOException e) {
throw new RuntimeException(e);
}
final File baseTestDataDir =
new File(getKotlinProjectHome(), "compiler" + File.separator + "integration-tests" + File.separator + "data");
testDataDir = new File(baseTestDataDir, description.getMethodName());
}
@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 {
@@ -124,7 +106,7 @@ public abstract class KotlinIntegrationTestBase {
protected String normalizeOutput(String content) {
content = replacePath(content, testDataDir, "[TestData]");
content = replacePath(content, tempDir, "[Temp]");
content = replacePath(content, tmpdir.getTmpDir(), "[Temp]");
content = replacePath(content, getCompilerLib(), "[CompilerLib]");
return content;
}
@@ -240,6 +240,15 @@ public class JetTestUtils {
filesToDelete.add(file);
}
public static void rmrf(File file) {
if (file == null) {
return;
}
if (!FileUtil.delete(file)) {
throw new RuntimeException("failed to delete " + file);
}
}
public static final Pattern FILE_PATTERN = Pattern.compile("//\\s*FILE:\\s*(.*)$", Pattern.MULTILINE);
public static JetCoreEnvironment createEnvironmentWithFullJdk(Disposable disposable) {
@@ -0,0 +1,61 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.test;
import com.google.common.io.Files;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.utils.ExceptionUtils;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import java.io.File;
import java.io.IOException;
/**
* @author Stepan Koltsov
*/
public class Tmpdir extends TestWatcher {
private File tmpDir;
@Override
protected void starting(Description description) {
try {
tmpDir = Files.createTempDir().getCanonicalFile();
}
catch (IOException e) {
throw ExceptionUtils.rethrow(e);
}
}
@Override
protected void succeeded(Description description) {
JetTestUtils.rmrf(tmpDir);
}
@Override
protected void failed(Throwable e, Description description) {
System.err.println("Temp directory: " + tmpDir);
}
@NotNull
public File getTmpDir() {
return tmpDir;
}
}