From d28c1db92d3524325d1aaaeaadc224876f4c5aaa Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Sun, 1 Feb 2015 13:46:28 +0100 Subject: [PATCH] add tests to sample package; fix load/unload behavior --- .gitignore | 3 ++- R/load.R | 13 ++++++------- R/runRtests.R | 10 +++++----- R/zzz.R | 10 ++++++++++ pkgTest/NAMESPACE | 2 +- pkgTest/R/tests/t.a.R | 7 +++++++ pkgTest/R/zzz.R | 4 ++-- pkgTest/src/{ => sources}/d.c | 0 pkgTest/src/tests/t.d.c | 3 +++ 9 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 R/zzz.R create mode 100644 pkgTest/R/tests/t.a.R rename pkgTest/src/{ => sources}/d.c (100%) create mode 100644 pkgTest/src/tests/t.d.c diff --git a/.gitignore b/.gitignore index 5393fd3..a6e78fd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ -/pkgTest/src/d.o +/pkgTest/src/sources/d.o +/pkgTest/src/tests/t.d.o /pkgTest/src/pkgTest.so diff --git a/R/load.R b/R/load.R index 2d6d122..646f60d 100644 --- a/R/load.R +++ b/R/load.R @@ -36,13 +36,12 @@ if (file.exists(file.path(path,"src"))) { # C code -- Warning: src/tests folder should not be listed - cFiles = list.files( file.path(path,"src"), pattern="\\.[cChH]$", - full.names=TRUE, recursive=TRUE, no..=TRUE ) - cFiles = lapply(cFiles, function(x) { - if (nchar(x)>=5 && substr(x,1,5)=="tests") return ("") - return (x) - }) - + cFiles = c( + list.files(file.path(path,"src","sources"),pattern="\\.[cChH]$", + full.names=TRUE, recursive=TRUE, no..=TRUE), + list.files(file.path(path,"src","adapters"),pattern="\\.[cChH]$", + full.names=TRUE, recursive=TRUE, no..=TRUE)) + # Create folder R_HOME_USER/pkgdev/pkgs/pkgName/src (if not existing) dir.create(file.path(pkdev_path,"pkgs",pkgName,"src"), showWarnings=FALSE) diff --git a/R/runRtests.R b/R/runRtests.R index b649ead..2350a8e 100644 --- a/R/runRtests.R +++ b/R/runRtests.R @@ -3,9 +3,9 @@ # Initial step: list every potential unit test under path/R/tests. allFuncNames = .parseRunitTests(file.path(path,"R","tests")) - + # Filter functions names matching prefix - funcNames = grep( paste("^test\\.",prefix,sep=''), allFuncNames, value=TRUE ) + funcNames = grep( paste("^test_",prefix,sep=''), allFuncNames, value=TRUE ) if (length(funcNames) == 0) return #shortcut: nothing to do... # If show==TRUE, display every potential test starting with prefix, and exit @@ -58,15 +58,15 @@ # If the file is not a source, skip if ( length( grep("\\.[RrSsq]$", fileName) ) == 0) next - # Every test function has a name starting with "test." + # Every test function has a name starting with "test_" matches = grep( - "^[ \t]*test\\.[a-zA-Z0-9_]*[ \t]*(=|<-)[ \t]*function.*", + "^[ \t]*test_[a-zA-Z0-9_]*[ \t]*(=|<-)[ \t]*function.*", scan(fileName, what="character", sep='\n', quiet=TRUE), value = TRUE) # We matched more to be 100% sure we got test functions, but need to strip now funcNames = c(funcNames, sub( - "^[ \t]*(test\\.[a-zA-Z0-9_]*)[ \t]*(=|<-)[ \t]*function.*", + "^[ \t]*(test_[a-zA-Z0-9_]*)[ \t]*(=|<-)[ \t]*function.*", "\\1", matches)) } diff --git a/R/zzz.R b/R/zzz.R new file mode 100644 index 0000000..6c2d872 --- /dev/null +++ b/R/zzz.R @@ -0,0 +1,10 @@ +#called by library(pkgdev) +.onAttach = function(libname, pkgname) { + #wipe all previous "loaded" tags + R_HOME_USER = Sys.getenv("R_HOME_USER") + if (R_HOME_USER != "") { + pkgdevPackagesPath = file.path( Sys.getenv("R_HOME_USER"),"pkgdev","pkgs" ) + for (package in list.dirs(pkgdevPackagesPath, recursive=FALSE)) + file.remove(file.path(package,"loaded")) + } +} diff --git a/pkgTest/NAMESPACE b/pkgTest/NAMESPACE index c969d76..b9d040d 100755 --- a/pkgTest/NAMESPACE +++ b/pkgTest/NAMESPACE @@ -1,4 +1,4 @@ # Export all user-level R functions -export (a, b, c, .Last.lib) +export (a, b, c) useDynLib(pkgTest) diff --git a/pkgTest/R/tests/t.a.R b/pkgTest/R/tests/t.a.R new file mode 100644 index 0000000..970ef5d --- /dev/null +++ b/pkgTest/R/tests/t.a.R @@ -0,0 +1,7 @@ +test_a = function() { + a() +} + +test_c = function() { + c() +} diff --git a/pkgTest/R/zzz.R b/pkgTest/R/zzz.R index 415c03a..ce4432e 100644 --- a/pkgTest/R/zzz.R +++ b/pkgTest/R/zzz.R @@ -1,5 +1,5 @@ #called when package is detached ( detach("package:pkg_name") ) -.Last.lib = function(path) +.onDetach = function(libpath) { - library.dynam.unload("pkgTest", path) + library.dynam.unload("pkgTest", libpath) } diff --git a/pkgTest/src/d.c b/pkgTest/src/sources/d.c similarity index 100% rename from pkgTest/src/d.c rename to pkgTest/src/sources/d.c diff --git a/pkgTest/src/tests/t.d.c b/pkgTest/src/tests/t.d.c new file mode 100644 index 0000000..63939a9 --- /dev/null +++ b/pkgTest/src/tests/t.d.c @@ -0,0 +1,3 @@ +void test_d() { + d(); +} -- 2.44.0