diff --git a/compiler/integration-tests/compiler-integration-tests.iml b/compiler/integration-tests/compiler-integration-tests.iml index e1898141087..535159981db 100644 --- a/compiler/integration-tests/compiler-integration-tests.iml +++ b/compiler/integration-tests/compiler-integration-tests.iml @@ -8,6 +8,7 @@ + diff --git a/compiler/integration-tests/src/org/jetbrains/kotlin/AntTaskTest.java b/compiler/integration-tests/src/org/jetbrains/kotlin/AntTaskTest.java index 63d5c095d5e..c0a9674a760 100644 --- a/compiler/integration-tests/src/org/jetbrains/kotlin/AntTaskTest.java +++ b/compiler/integration-tests/src/org/jetbrains/kotlin/AntTaskTest.java @@ -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); } diff --git a/compiler/integration-tests/src/org/jetbrains/kotlin/CompilerSmokeTest.java b/compiler/integration-tests/src/org/jetbrains/kotlin/CompilerSmokeTest.java index 0d3a34cd3ba..15250683c7b 100644 --- a/compiler/integration-tests/src/org/jetbrains/kotlin/CompilerSmokeTest.java +++ b/compiler/integration-tests/src/org/jetbrains/kotlin/CompilerSmokeTest.java @@ -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); } diff --git a/compiler/integration-tests/src/org/jetbrains/kotlin/KotlinIntegrationTestBase.java b/compiler/integration-tests/src/org/jetbrains/kotlin/KotlinIntegrationTestBase.java index 6bd46107cad..f05a75d1c42 100644 --- a/compiler/integration-tests/src/org/jetbrains/kotlin/KotlinIntegrationTestBase.java +++ b/compiler/integration-tests/src/org/jetbrains/kotlin/KotlinIntegrationTestBase.java @@ -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; } diff --git a/compiler/tests/org/jetbrains/jet/JetTestUtils.java b/compiler/tests/org/jetbrains/jet/JetTestUtils.java index 11054bc4e2a..75c4c2f0243 100644 --- a/compiler/tests/org/jetbrains/jet/JetTestUtils.java +++ b/compiler/tests/org/jetbrains/jet/JetTestUtils.java @@ -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) { diff --git a/compiler/tests/org/jetbrains/jet/test/Tmpdir.java b/compiler/tests/org/jetbrains/jet/test/Tmpdir.java new file mode 100644 index 00000000000..261210426a5 --- /dev/null +++ b/compiler/tests/org/jetbrains/jet/test/Tmpdir.java @@ -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; + } +}