diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_quit.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_quit.java index 7c3462c7f8ced5f4abaaaefd40f6780dedab55b9..28562baadd09d6ca16c269da44e0280629b73ade 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_quit.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_quit.java @@ -22,12 +22,46 @@ */ package com.oracle.truffle.r.test.builtins; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; + +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Test; import com.oracle.truffle.r.test.TestBase; public class TestBuiltin_quit extends TestBase { + private static final Path PATH_RDATA = Paths.get(".RData"); + private static final Path PATH_RHISTORY = Paths.get(".Rhistory"); + private static final Path PATH_RDATA_BAK = PATH_RDATA.resolveSibling(PATH_RDATA.getFileName() + ".bak"); + private static final Path PATH_RHISTORY_BAK = PATH_RHISTORY.resolveSibling(PATH_RHISTORY.getFileName() + ".bak"); + + /** + * Test case {@link #testQuitEmptyEnv()} possibly destroys previously stored sessions. If so, + * first backup {@code .RData} and {@code .Rhistory}. + * + * @throws IOException + */ + @BeforeClass + public static void backupHistory() throws IOException { + if (Files.exists(PATH_RDATA)) { + move(PATH_RDATA, PATH_RDATA_BAK); + } + if (Files.exists(PATH_RHISTORY)) { + move(PATH_RHISTORY, PATH_RHISTORY_BAK); + } + } + + private static void move(Path pathRData, Path dest) throws IOException { + + Files.move(pathRData, dest, StandardCopyOption.REPLACE_EXISTING); + } + @Test public void testQuitErrorSave() { assertEval("{ quit(\"xx\") }"); @@ -37,4 +71,30 @@ public class TestBuiltin_quit extends TestBase { public void testQuitEmptyEnv() { assertEval("{ quit(\"yes\") }"); } + + /** + * Removes temporarily created files {@code .RData} and {@code .Rhistory} and restore backups if + * available. + * + * @throws IOException + */ + @AfterClass + public static void restoreHistory() throws IOException { + + // remove any created ".RData" and ".Rhistory" file + if (Files.exists(PATH_RDATA)) { + Files.delete(PATH_RDATA); + } + if (Files.exists(PATH_RHISTORY)) { + Files.delete(PATH_RHISTORY); + } + + // restore previously rescued files + if (Files.exists(PATH_RDATA_BAK)) { + move(PATH_RDATA_BAK, PATH_RDATA); + } + if (Files.exists(PATH_RHISTORY_BAK)) { + move(PATH_RHISTORY_BAK, PATH_RHISTORY); + } + } }