From 1ea315a443a563d70f5fc7afe43a02a28435df45 Mon Sep 17 00:00:00 2001
From: Florian Angerer <florian.angerer@oracle.com>
Date: Tue, 7 Feb 2017 07:54:22 +0100
Subject: [PATCH] Implemented improvement preventing a previously stored
 session to be destroyed by TestBuiltin_quit.

---
 .../r/test/builtins/TestBuiltin_quit.java     | 60 +++++++++++++++++++
 1 file changed, 60 insertions(+)

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 7c3462c7f8..28562baadd 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);
+        }
+    }
 }
-- 
GitLab