From 1713709cf2a46d52bd14e3a024b26f548e446bc3 Mon Sep 17 00:00:00 2001
From: Mick Jordan <mick.jordan@oracle.com>
Date: Mon, 14 Nov 2016 14:20:32 -0800
Subject: [PATCH] pkgtest: revert to R_LIBS_USER owing to testInstalledPackage
 (sometimes) undefining R_LIBS

---
 .../r/install.cran.packages.R                 | 14 +++++++-----
 mx.fastr/mx_fastr_pkgs.py                     | 22 +++++++++++--------
 2 files changed, 21 insertions(+), 15 deletions(-)

diff --git a/com.oracle.truffle.r.test.cran/r/install.cran.packages.R b/com.oracle.truffle.r.test.cran/r/install.cran.packages.R
index a800cb1e9b..f582e23ad8 100644
--- a/com.oracle.truffle.r.test.cran/r/install.cran.packages.R
+++ b/com.oracle.truffle.r.test.cran/r/install.cran.packages.R
@@ -35,7 +35,7 @@
 # If unset, defaults to "http://cran.cnr.berkeley.edu/"
 # However, a local copy of the CRAN repo can be used either by setting the LOCAL_CRAN_REPO env variable or setting --contrib-url
 
-# Packages are installed into the directory specified by the --lib arg (or R_LIBS env var)
+# Packages are installed into the directory specified by the --lib arg (or R_LIBS_USER env var)
 
 # Blacklisted packages nor their dependents will not be installed. By default the list of blacklisted
 # packages will be read from the file in the --blacklist-file arg or the PACKAGE_BLACKLIST env var.
@@ -46,10 +46,12 @@
 # Package: name
 # Reason: reason
 
-# The env var R_LIBS or the option --lib must be set to the directory where the install should take place.
+# The env var R_LIBS_USER or the option --lib must be set to the directory where the install should take place.
 # N.B. --lib works for installation. However, when running tests ( --run-tests), it does not and
-# R_LIBS must be set instead (as well) since some of the test code has explicit "library(foo)" calls
-# without a "lib.loc" argument.
+# R_LIBS_USER must be set instead (as well) since some of the test code has explicit "library(foo)" calls
+# without a "lib.loc" argument. N.B. For reasons I do not understand tools::testInstalledPackage
+# explicitly sets R_LIBS to the empty string before testing the main test file (but paradoxically not when
+# testing the "examples"), which is why we use R_LIBS_USER.
 
 # A single package install can be handled in three ways, based on the run-mode argument (default system):
 #   system: use a subprocess via the system2 command
@@ -808,9 +810,9 @@ cat.args <- function() {
 }
 
 check.libs <- function() {
-    lib.install <<- Sys.getenv("R_LIBS", unset=NA)
+    lib.install <<- Sys.getenv("R_LIBS_USER", unset=NA)
 	if (is.na(lib.install)) {
-		abort("R_LIBS must be set")
+		abort("R_LIBS_USER must be set")
 	}
 	if (!file.exists(lib.install) || is.na(file.info(lib.install)$isdir)) {
 		abort(paste(lib.install, "does not exist or is not a directory"))
diff --git a/mx.fastr/mx_fastr_pkgs.py b/mx.fastr/mx_fastr_pkgs.py
index 2abc87dd14..07b8645248 100644
--- a/mx.fastr/mx_fastr_pkgs.py
+++ b/mx.fastr/mx_fastr_pkgs.py
@@ -188,7 +188,7 @@ def pkgtest(args):
                             f.write(test_time)
     env = os.environ.copy()
     env["TMPDIR"] = fastr_install_tmp
-    env['R_LIBS'] = fastr_libinstall
+    env['R_LIBS_USER'] = fastr_libinstall
     if not env.has_key('FASTR_PROCESS_TIMEOUT'):
         env['FASTR_PROCESS_TIMEOUT'] = '5'
     env['FASTR_OPTION_PrintErrorStacktracesToFile'] = 'false'
@@ -233,16 +233,19 @@ def pkgtest(args):
             print '{0}: {1}'.format(pkg, test_status.status)
 
         # tar up the test results
-        fastr_tar = join(_fastr_suite_dir, fastr_testdir + '.tar')
-        subprocess.call(['tar', 'cf', fastr_tar, os.path.basename(fastr_testdir)])
-        subprocess.call(['gzip', fastr_tar])
-        gnur_tar = join(_fastr_suite_dir, gnur_testdir + '.tar')
-        subprocess.call(['tar', 'cf', gnur_tar, os.path.basename(gnur_testdir)])
-        subprocess.call(['gzip', gnur_tar])
+        tar_tests(fastr_testdir)
+        tar_tests(gnur_testdir)
 
     shutil.rmtree(fastr_install_tmp, ignore_errors=True)
     return rc
 
+def tar_tests(testdir):
+        test_tar = join(_fastr_suite_dir, testdir + '.tar')
+        subprocess.call(['tar', 'cf', test_tar, os.path.basename(testdir)])
+        if os.path.exists(test_tar + '.gz'):
+            os.remove(test_tar + '.gz')
+        subprocess.call(['gzip', test_tar])
+
 class TestFileStatus:
     '''
     Records the status of a test file. status is either "OK" or "FAILED".
@@ -306,7 +309,7 @@ def _gnur_install_test(pkgs, gnur_libinstall, gnur_install_tmp):
             f.write('\n')
     env = os.environ.copy()
     env["TMPDIR"] = gnur_install_tmp
-    env['R_LIBS'] = gnur_libinstall
+    env['R_LIBS_USER'] = gnur_libinstall
 
     # TODO enable but via installing Suggests
     # _install_vignette_support('GnuR', env)
@@ -317,7 +320,8 @@ def _gnur_install_test(pkgs, gnur_libinstall, gnur_install_tmp):
     args += [_installpkgs_script()]
     args += ['--pkg-filelist', gnur_packages]
     args += ['--run-tests']
-    args += ['--run-mode', 'internal']
+# GNU R will abort the entire run otherwise if a failure occurs
+#    args += ['--run-mode', 'internal']
     args += ['--ignore-blacklist']
     args += ['--testdir', 'test.gnur']
     _log_step('BEGIN', 'install/test', 'GnuR')
-- 
GitLab