From afe9395aa287348353f89973350fad93dc75eeb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Cs=C3=A1rdi?= Date: Tue, 2 Apr 2024 12:52:24 +0200 Subject: [PATCH] Test printing the output of installation failures --- src/library/zip/src/init.c | 1 - .../testthat/fixtures/badcompile/DESCRIPTION | 12 ++++++ tests/testthat/fixtures/badcompile/NAMESPACE | 3 ++ .../badcompile/R/badcompile-package.R | 4 ++ .../fixtures/badcompile/src/.gitignore | 3 ++ tests/testthat/fixtures/badcompile/src/bad.c | 31 +++++++++++++ tests/testthat/fixtures/badcompile/src/init.c | 14 ++++++ tests/testthat/test-failure-output.R | 43 +++++++++++++++++++ 8 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/fixtures/badcompile/DESCRIPTION create mode 100644 tests/testthat/fixtures/badcompile/NAMESPACE create mode 100644 tests/testthat/fixtures/badcompile/R/badcompile-package.R create mode 100644 tests/testthat/fixtures/badcompile/src/.gitignore create mode 100644 tests/testthat/fixtures/badcompile/src/bad.c create mode 100644 tests/testthat/fixtures/badcompile/src/init.c create mode 100644 tests/testthat/test-failure-output.R diff --git a/src/library/zip/src/init.c b/src/library/zip/src/init.c index b0f9d1c71..548a94d31 100644 --- a/src/library/zip/src/init.c +++ b/src/library/zip/src/init.c @@ -1,4 +1,3 @@ - #include #include #include // for NULL diff --git a/tests/testthat/fixtures/badcompile/DESCRIPTION b/tests/testthat/fixtures/badcompile/DESCRIPTION new file mode 100644 index 000000000..493d6c1b1 --- /dev/null +++ b/tests/testthat/fixtures/badcompile/DESCRIPTION @@ -0,0 +1,12 @@ +Package: badcompile +Title: What the Package Does (One Line, Title Case) +Version: 0.0.0.9000 +Authors@R: + person("First", "Last", , "first.last@example.com", role = c("aut", "cre"), + comment = c(ORCID = "YOUR-ORCID-ID")) +Description: What the package does (one paragraph). +License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a + license +Encoding: UTF-8 +Roxygen: list(markdown = TRUE) +RoxygenNote: 7.3.1.9000 diff --git a/tests/testthat/fixtures/badcompile/NAMESPACE b/tests/testthat/fixtures/badcompile/NAMESPACE new file mode 100644 index 000000000..8f469df3d --- /dev/null +++ b/tests/testthat/fixtures/badcompile/NAMESPACE @@ -0,0 +1,3 @@ +# Generated by roxygen2: do not edit by hand + +useDynLib(badcompile, .registration = TRUE) diff --git a/tests/testthat/fixtures/badcompile/R/badcompile-package.R b/tests/testthat/fixtures/badcompile/R/badcompile-package.R new file mode 100644 index 000000000..93e417315 --- /dev/null +++ b/tests/testthat/fixtures/badcompile/R/badcompile-package.R @@ -0,0 +1,4 @@ +## usethis namespace: start +#' @useDynLib badcompile, .registration = TRUE +## usethis namespace: end +NULL diff --git a/tests/testthat/fixtures/badcompile/src/.gitignore b/tests/testthat/fixtures/badcompile/src/.gitignore new file mode 100644 index 000000000..22034c461 --- /dev/null +++ b/tests/testthat/fixtures/badcompile/src/.gitignore @@ -0,0 +1,3 @@ +*.o +*.so +*.dll diff --git a/tests/testthat/fixtures/badcompile/src/bad.c b/tests/testthat/fixtures/badcompile/src/bad.c new file mode 100644 index 000000000..1b7fdf4ca --- /dev/null +++ b/tests/testthat/fixtures/badcompile/src/bad.c @@ -0,0 +1,31 @@ +void f1() { + +} + +void f2() { + +} + +void f3() { + +} + +void f4() { + +} + +void f5() { + +} + +void f6() { + +} + +void f7() { + +} + +void f() { + foobar; +} diff --git a/tests/testthat/fixtures/badcompile/src/init.c b/tests/testthat/fixtures/badcompile/src/init.c new file mode 100644 index 000000000..cd9b39741 --- /dev/null +++ b/tests/testthat/fixtures/badcompile/src/init.c @@ -0,0 +1,14 @@ +#include +#include +#include +#define r_export attribute_visible extern + +static const R_CallMethodDef CallEntries[] = { + { NULL, NULL, 0 } +}; + +r_export void R_init_zip(DllInfo *dll) { + R_registerRoutines(dll, NULL, CallEntries, NULL, NULL); + R_useDynamicSymbols(dll, FALSE); + R_forceSymbols(dll, TRUE); +} diff --git a/tests/testthat/test-failure-output.R b/tests/testthat/test-failure-output.R new file mode 100644 index 000000000..4d41879f3 --- /dev/null +++ b/tests/testthat/test-failure-output.R @@ -0,0 +1,43 @@ +test_that("output is printed on failure", { + skip_on_cran() + + # Full output in interactive sessions + badcompile <- paste0( + "local::", + normalizePath(test_path("fixtures/badcompile")) + ) + tmp <- tempfile() + on.exit(unlink(tmp), add = TRUE) + + expect_error( + callr::r(stdout = tmp, stderr = tmp, function(pkg) { + pkgload::load_all() + options(rlib_interactive = TRUE) + pak::pkg_install(pkg) + }, list(pkg = badcompile)) + ) + + lines <- readLines(tmp) + expect_true(any(grepl("Failed to build badcompile", lines))) + expect_true(any(grepl("Full installation output", lines))) + expect_true(any(grepl( + "compilation failed for package .*badcompile.*", + lines + ))) + + expect_error( + callr::r(stdout = tmp, stderr = tmp, function(pkg) { + pkgload::load_all() + options(rlib_interactive = FALSE) + pak::pkg_install(pkg) + }, list(pkg = badcompile)) + ) + + lines <- readLines(tmp) + expect_true(any(grepl("Failed to build badcompile", lines))) + expect_true(any(grepl("Full installation output", lines))) + expect_true(any(grepl( + "compilation failed for package .*badcompile.*", + lines + ))) +})