Skip to content

Commit

Permalink
Merge pull request #583 from r-lib/test-tweaks
Browse files Browse the repository at this point in the history
Test tweaks
  • Loading branch information
jimhester authored Nov 19, 2024
2 parents 742028a + ad71c36 commit 48d84d3
Show file tree
Hide file tree
Showing 38 changed files with 57 additions and 90 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Suggests:
rmarkdown,
htmltools,
DT (>= 0.2),
testthat,
testthat (>= 3.0.0),
rlang,
rstudioapi (>= 0.2),
xml2 (>= 1.0.0),
Expand All @@ -75,3 +75,5 @@ License: MIT + file LICENSE
VignetteBuilder: knitr
RoxygenNote: 7.2.3
Roxygen: list(markdown = TRUE)
Config/testthat/edition: 3
Config/testthat/parallel: TRUE
7 changes: 6 additions & 1 deletion R/covr.R
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,12 @@ package_coverage <- function(path = ".",

name <- if (.Platform$OS.type == "windows") "R.exe" else "R"
path <- file.path(R.home("bin"), name)
system2(path, args)
system2(
path,
args,
stdout = if (quiet) NULL else "",
stderr = if (quiet) NULL else ""
)
})
)

Expand Down
20 changes: 10 additions & 10 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
ops <- options("crayon.enabled" = FALSE, warn = 1)
library(testthat)
library("covr")
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html

# Skip tests on Solaris as gcc is not in the PATH and I do not have an easy way
# to mimic the CRAN build environment
if (!tolower(Sys.info()[["sysname"]]) == "sunos") {
Sys.setenv("R_TESTS" = "")
test_check("covr")
}
library(testthat)
library(covr)

options(ops)
test_check("covr")
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("Test")
test_that("compiled function simple works", {
expect_equal(simple(1), 1)
expect_equal(simple(2), 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("Test")
test_that("compiled function simple works", {
expect_equal(simple(1), 1)
expect_equal(simple(2), 1)
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/_snaps/Compiled.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Error thrown for missing gcov

Code
package_coverage("TestCompiled", relative_path = TRUE)
Condition
Error in `run_gcov()`:
! gcov not found

# Warning thrown for empty gcov output

Code
. <- package_coverage("TestCompiled", relative_path = TRUE)
Condition
Warning in `run_gcov()`:
parsed gcov output was empty

30 changes: 22 additions & 8 deletions tests/testthat/test-Compiled.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("Compiled")
test_that("Compiled code coverage is reported including code in headers", {
skip_on_cran()
skip_if(is_win_r41())
Expand Down Expand Up @@ -79,16 +78,31 @@ test_that("Compiled code coverage is reported under non-standard char's", {

test_that("Error thrown for missing gcov", {
skip_on_cran()
withr::with_options(c(covr.gcov=''),
expect_error(package_coverage("TestCompiled", relative_path=TRUE),
"gcov not found")
)

withr::local_options(covr.gcov='')
expect_snapshot(package_coverage("TestCompiled", relative_path=TRUE), error = TRUE)
})

test_that("Warning thrown for empty gcov output", {
skip_on_cran()
withr::with_options(c(covr.gcov_args='-n'),
expect_warning(package_coverage("TestCompiled", relative_path=TRUE),
"parsed gcov output was empty")

withr::local_options(covr.gcov_args='-n')
expect_snapshot(
. <- package_coverage("TestCompiled", relative_path=TRUE),
transform = function(x) gsub(getwd(), "<wd>", x)
)
})

test_that("tally_coverage includes compiled code", {
skip_on_cran()
skip_if(is_win_r41())

cov <- package_coverage(test_path("TestCompiled"))
tall <- tally_coverage(cov)

expect_named(tall, c("filename", "functions", "line", "value"))

expect_equal(
unique(tall$filename),
c("R/TestCompiled.R", "src/simple-header.h", "src/simple.cc", "src/simple4.cc"))
})
2 changes: 0 additions & 2 deletions tests/testthat/test-R6.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("R6")

test_that("R6 methods coverage is reported", {
# There is some sort of bug that causes this test to fail during R CMD check
# in R-devel, not sure why, and can't reproduce it interactively
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-RC.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("RC")
test_that("RC methods coverage is reported", {
cov <- as.data.frame(package_coverage("TestRC"))

Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-S4.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("S4")
test_that("S4 methods coverage is reported", {
cov <- as.data.frame(package_coverage("TestS4"))

Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-box-R6.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("box-R6")

loaded_mods <- loadNamespace("box")$loaded_mods
rm(list = ls(loaded_mods), envir = loaded_mods)

Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-box.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("box")

loaded_mods <- loadNamespace("box")$loaded_mods
rm(list = ls(loaded_mods), envir = loaded_mods)

Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-box_attached_modules_functions-R6.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("box-attached-modules-functions-R6")

loaded_mods <- loadNamespace("box")$loaded_mods
rm(list = ls(loaded_mods), envir = loaded_mods)

Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-box_attached_modules_functions.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("box-attached-modules-functions")

loaded_mods <- loadNamespace("box")$loaded_mods
rm(list = ls(loaded_mods), envir = loaded_mods)

Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-braceless.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("braceless")

test_that("if", {
f <-
'f <- function(x) {
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-cobertura.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("cobertura_export")

test_that("it works with coverage objects", {
tmp <- tempfile()
cov <- package_coverage(test_path("TestSummary"))
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-codecov.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("codecov")
ci_vars <- c(
"APPVEYOR" = NA,
"APPVEYOR_ACCOUNT_NAME" = NA,
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-corner-cases.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("corner-cases")

test_that("corner-cases are handled as expected", {
expect_warning(withr::with_output_sink(tempfile(), {
cov <- file_coverage("corner-cases.R", "corner-cases-test.R")
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-coveralls.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("coveralls")

ci_vars <- c(
"APPVEYOR" = NA,
"APPVEYOR_BUILD_NUMBER" = NA,
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-covr.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("function_coverage")
test_that("function_coverage", {

withr::with_options(c(keep.source = TRUE), {
Expand Down Expand Up @@ -59,7 +58,6 @@ test_that("duplicated first_line", {
})
})

context("trace_calls")
test_that("trace calls handles all possibilities", {
expr <- expression(y <- x * 10)

Expand All @@ -68,7 +66,6 @@ test_that("trace calls handles all possibilities", {
expect_equal(trace_calls(list(expr)), list(expr))
})

context("show_failures")
test_that("show_failures shows as much text as it can from the end", {
withr::with_options(c(warning.length = 300), {
td <- tempfile()
Expand Down
4 changes: 0 additions & 4 deletions tests/testthat/test-exclusions.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ exclude_ops <- list(exclude_pattern = "#TeSt_NoLiNt",
exclude_start = "#TeSt_NoLiNt_StArT",
exclude_end = "#TeSt_NoLiNt_EnD")

context("parse_exclusions")
test_that("it returns an empty vector if there are no exclusions", {
t1 <- c("this",
"is",
Expand Down Expand Up @@ -64,7 +63,6 @@ test_that("it throws an error if start and end are unpaired", {
expect_error(do.call(parse_exclusions, c(list(t1), exclude_ops)), "but only")
})

context("normalize_exclusions")
expect_equal_vals <- function(x, y) {
testthat::expect_equal(unname(x), unname(y))
}
Expand Down Expand Up @@ -124,7 +122,6 @@ test_that("it handles redundant files", {

cov <- package_coverage("TestSummary")

context("exclude")
test_that("it excludes lines", {
expect_equal(length(cov), 4)
expect_equal(length(exclude(cov, list("R/TestSummary.R" = 5), path = "TestSummary")), 3)
Expand All @@ -149,7 +146,6 @@ test_that("it excludes properly", {
expect_equal(length(t1), 0)
})

context("file_exclusions")
test_that("it returns NULL if empty or no file exclusions", {
expect_equal(file_exclusions(NULL, ""), NULL)

Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-file_coverage.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("file_coverage")
s1 <- tempfile()
t1 <- tempfile()
writeLines(con = s1,
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-functions.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("evaluated functions")
test_that("function_coverage generates output", {
env <- new.env()
withr::with_options(c("keep.source" = TRUE), {
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-gcov.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("gcov")
test_that("parse_gcov parses files properly", {
mockery::stub(parse_gcov, "file.exists", TRUE)
mockery::stub(normalize_path, "normalizePath", "simple.c")
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-gitlab.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("gitlab")

test_that("gitlab", {
cov <- package_coverage("TestS4")

Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-memoised.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("memoised")
s1 <- tempfile()
t1 <- tempfile()
writeLines(con = s1,
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-null.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("NULL")

test_that("coverage of functions with NULL constructs", {
f1 <- function() NULL
f2 <- function() {
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-package_coverage.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("package_coverage")
test_that("package_coverage returns an error if the path does not exist", {
expect_error(package_coverage("blah"))
})
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-parallel.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
context("coverage of parallel code")


test_that("mcparallel without the fix", {
skip_on_os("windows")

Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-print.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("print function")
test_that("format_percentage works as expected", {
expect_equal(format_percentage(0), crayon::red("0.00%"))

Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-record_tests.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("record_tests")

cov_func <- withr::with_options(
list(covr.record_tests = TRUE),
package_coverage(test_path("TestFunctional")))
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-report.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("report")

skip_on_ci <- function() {
if (!identical(Sys.getenv("CI"), "true")) {
return(invisible(TRUE))
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-sonarqube.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("sonarqube_export")

test_that("it works with coverage objects", {
tmp <- tempfile()
cov <- package_coverage(test_path("TestSummary"))
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-summary.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("summary_functions")

test_that("Summary gives 50% coverage and two lines with zero coverage", {
cv <- package_coverage("TestSummary")
expect_equal(percent_coverage(cv), 50)
Expand Down
13 changes: 0 additions & 13 deletions tests/testthat/test-tally_coverage.R

This file was deleted.

1 change: 0 additions & 1 deletion tests/testthat/test-trace_calls.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("trace_calls")
test_that("one-line functions are traced correctly", {
old <- getOption("keep.source")
options(keep.source = TRUE)
Expand Down
4 changes: 0 additions & 4 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("as_package")
test_that("it throws error if no package", {
expect_error(as_package("arst11234"), "`path` is invalid:.*arst11234")
})
Expand All @@ -15,21 +14,18 @@ test_that("it returns the package if given the root or child directory", {
expect_equal(as_package("TestS4/tests/testthat")$package, "TestS4")
})

context("local_branch")
test_that("it works as expected", {
with_mock(`covr:::system_output` = function(...) { "test_branch " }, {
expect_equal(local_branch("TestSummary"), "test_branch")
})
})

context("current_commit")
test_that("it works as expected", {
with_mock(`covr:::system_output` = function(...) { " test_hash" }, {
expect_equal(current_commit("TestSummary"), "test_hash")
})
})

context("get_source_filename")
test_that("it works", {
# R 4.0.0 changes this behavior so `getSrcFilename()` will actually return
# "test-utils.R"
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-vectorized.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("Vectorize")
s1 = tempfile()
t1 = tempfile()
writeLines(con = s1,
Expand Down

0 comments on commit 48d84d3

Please sign in to comment.