Skip to content

Commit 088c5c6

Browse files
committed
Add test for client packages compiled without protected evaluation
1 parent b4cc4ef commit 088c5c6

File tree

7 files changed

+53
-30
lines changed

7 files changed

+53
-30
lines changed

ChangeLog

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11

2+
2018-06-02 Lionel Henry <[email protected]>
3+
4+
* inst/unitTests/runit.interface.R: New test for the case where
5+
the client package was compiled without protected evaluation
6+
enabled. On R 3.5, longjump exceptions thrown from imported
7+
functions are still caught and dealt with properly by the client
8+
package.
9+
210
2018-06-01 Lionel Henry <[email protected]>
311

412
* inst/unitTests/runit.interface.R: New tests for interfaces and unwind.

inst/unitTests/runit.interface.R

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,25 @@
2121

2222
if (.runThisTest) {
2323

24+
build_package <- function(name, lib_path, tempdir = getwd(),
25+
config = character()) {
26+
file.copy(system.file("unitTests", name, package = "Rcpp"),
27+
getwd(),
28+
recursive = TRUE)
29+
30+
src_path <- file.path(tempdir, name)
31+
Rcpp::compileAttributes(src_path)
32+
writeLines(config, file.path(src_path, "src", "config.h"))
33+
34+
install.packages(
35+
src_path,
36+
lib_path,
37+
repos = NULL,
38+
type = "source",
39+
INSTALL_opts = "--install-tests"
40+
)
41+
}
42+
2443
test.interface.unwind <- function() {
2544
exporter_name <- "testRcppInterfaceExporter"
2645
user_name <- "testRcppInterfaceUser"
@@ -33,37 +52,12 @@ if (.runThisTest) {
3352
unlink(tempdir, recursive = TRUE)
3453
})
3554

36-
file.copy(system.file("unitTests", exporter_name, package = "Rcpp"),
37-
tempdir,
38-
recursive = TRUE)
39-
file.copy(system.file("unitTests", user_name, package = "Rcpp"),
40-
tempdir,
41-
recursive = TRUE)
42-
43-
exporter_path <- file.path(tempdir, exporter_name)
44-
user_path <- file.path(tempdir, user_name)
45-
46-
Rcpp::compileAttributes(exporter_path)
47-
Rcpp::compileAttributes(user_path)
48-
4955
lib_path <- file.path(tempdir, "templib")
5056
dir.create(lib_path)
5157

52-
install <- function(path, lib_path) {
53-
install.packages(
54-
path,
55-
lib_path,
56-
repos = NULL,
57-
type = "source",
58-
INSTALL_opts = "--install-tests"
59-
)
60-
}
61-
install(exporter_path, lib_path)
62-
install(user_path, lib_path)
63-
6458
old_lib_paths <- .libPaths()
65-
on.exit(.libPaths(old_lib_paths))
66-
.libPaths(lib_path)
59+
on.exit(.libPaths(old_lib_paths), add = TRUE)
60+
.libPaths(c(lib_path, old_lib_paths))
6761

6862
# Without this testInstalledPackage() won't find installed
6963
# packages even though we've passed `lib.loc`
@@ -73,12 +67,31 @@ if (.runThisTest) {
7367
sys_sep <- if (.Platform$OS.type == "windows") ";" else ":"
7468
Sys.setenv(R_LIBS = paste(c(lib_path, old_lib_paths), collapse = sys_sep))
7569

70+
cfg <- "#define RCPP_PROTECTED_EVAL"
71+
build_package(exporter_name, lib_path, config = cfg)
72+
build_package(user_name, lib_path, config = cfg)
73+
7674
result <- tools::testInstalledPackage(user_name, lib.loc = lib_path, types = "test")
7775

7876
# Be verbose if tests were not successful
7977
if (result) {
8078
log <- file.path(paste0(user_name, "-tests"), "tests.Rout.fail")
81-
cat(">> tests.Rout.fail", readLines(log), sep = "\n", file = stderr())
79+
cat(">> PROTECTED tests.Rout.fail", readLines(log), sep = "\n", file = stderr())
80+
}
81+
82+
checkEquals(result, 0L)
83+
84+
85+
# Now test client package without protected evaluation
86+
unlink(user_name, recursive = TRUE)
87+
unlink(paste0(user_name, "-tests"), recursive = TRUE)
88+
build_package(user_name, lib_path, config = character())
89+
90+
result <- tools::testInstalledPackage(user_name, lib.loc = lib_path, types = "test")
91+
92+
if (result) {
93+
log <- file.path(paste0(user_name, "-tests"), "tests.Rout.fail")
94+
cat(">> UNPROTECTED tests.Rout.fail", readLines(log), sep = "\n", file = stderr())
8295
}
8396

8497
checkEquals(result, 0L)

inst/unitTests/testRcppInterfaceExporter/src/config.h

Whitespace-only changes.

inst/unitTests/testRcppInterfaceExporter/src/exporter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#define RCPP_PROTECTED_EVAL
1+
#include "config.h"
22

33
#include <Rcpp.h>
44
#include "unwound.h"

inst/unitTests/testRcppInterfaceUser/src/config.h

Whitespace-only changes.

inst/unitTests/testRcppInterfaceUser/src/user.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#define RCPP_PROTECTED_EVAL
1+
#include "config.h"
22

33
#include <Rcpp.h>
44
#include <testRcppInterfaceExporter.h>

inst/unitTests/testRcppInterfaceUser/tests/tests.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
# This tests errors converted to exceptions by Rcpp_eval()
23
x <- tryCatch(
34
error = identity,
45
testRcppInterfaceUser::use_cpp_interface(quote(stop("jump!")))
@@ -17,6 +18,7 @@ if (getRversion() >= "3.5.0") {
1718
testRcppInterfaceUser::reset_flags()
1819
testRcppInterfaceExporter::reset_flags()
1920

21+
# This tests longjumps not caught by Rcpp_eval()
2022
x <- withRestarts(
2123
here = identity,
2224
testRcppInterfaceUser::use_cpp_interface(quote(invokeRestart("here", "value")))

0 commit comments

Comments
 (0)