Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc cleanups #590

Merged
merged 3 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .lintr
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
linters: with_defaults(line_length_linter(120))
linters: linters_with_defaults(line_length_linter(120))
exclusions: list("inst/doc/how_it_works.R")
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Suggests:
box (>= 1.2.0)
License: MIT + file LICENSE
VignetteBuilder: knitr
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
Roxygen: list(markdown = TRUE)
Config/testthat/edition: 3
Config/testthat/parallel: TRUE
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

S3method("[",coverage)
S3method(as.data.frame,coverage)
S3method(markers,coverage)
S3method(markers,coverages)
S3method(markers,data.frame)
S3method(merge_coverage,character)
S3method(merge_coverage,list)
S3method(print,coverage)
Expand Down
6 changes: 3 additions & 3 deletions R/cobertura.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#' @param cov the coverage object returned from [package_coverage()]
#' @param filename the name of the Cobertura XML file
#' @export
to_cobertura <- function(cov, filename = "cobertura.xml"){
to_cobertura <- function(cov, filename = "cobertura.xml") {

loadNamespace("xml2")

Expand All @@ -24,7 +24,7 @@ to_cobertura <- function(cov, filename = "cobertura.xml"){
percent_per_function <- tapply(df$value, df$functions, FUN = function(x) (sum(x > 0) / length(x)))
lines_valid <- nrow(df)
lines_covered <- sum(df$value > 0)

d <- xml2::xml_new_document()

xml2::xml_add_child(d, xml2::xml_dtd(
Expand All @@ -34,7 +34,7 @@ to_cobertura <- function(cov, filename = "cobertura.xml"){
top <- xml2::xml_add_child(d,
"coverage",
"line-rate" = as.character(percent_overall),
"branch-rate" = "0",
"branch-rate" = "0",
`lines-covered` = as.character(lines_covered),
`lines-valid` = as.character(lines_valid),
`branches-covered` = "0",
Expand Down
2 changes: 1 addition & 1 deletion R/codecov.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ codecov <- function(...,
codecov_query <- list(service = "github-actions",
branch = branch,
build = github_run_id,
build_url = utils::URLencode(sprintf("http://github.com/%s/actions/runs/%s", slug, github_run_id)),
build_url = utils::URLencode(sprintf("https://github.com/%s/actions/runs/%s", slug, github_run_id)),
pr = pr,
slug = slug,
commit = commit %||% Sys.getenv("GITHUB_SHA"))
Expand Down
2 changes: 1 addition & 1 deletion R/compiled.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ run_gcov <- function(path, quiet = TRUE, clean = TRUE,
res <- withr::with_dir(src_path, {
compact(unlist(lapply(gcov_inputs, run_gcov_one), recursive = FALSE))
})
if (!length(res) & length(gcov_inputs))
if (!length(res) && length(gcov_inputs))
warning('parsed gcov output was empty')
res
}
Expand Down
7 changes: 3 additions & 4 deletions R/covr.R
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ package_coverage <- function(path = ".",
code = character(),
install_path = temp_file("R_LIBS"),
...,
exclusions, pre_clean=TRUE) {
exclusions, pre_clean = TRUE) {

if (!missing(exclusions)) {
warning(
Expand Down Expand Up @@ -420,8 +420,7 @@ package_coverage <- function(path = ".",
# check for compiler
if (!uses_icc()) {
flags <- getOption("covr.flags")
}
else {
} else {
if (length(getOption("covr.icov")) > 0L) {
flags <- getOption("covr.icov_flags")
# clean up old icov files
Expand Down Expand Up @@ -706,7 +705,7 @@ merge_coverage_tests <- function(from, into = NULL) {
# modify trace test tallies
for (name in intersect(names(into), names(from))) {
if (name == "tests") next
from[[name]]$tests$tally[,1L] <- test_idx[from[[name]]$tests$tally[,1L]]
from[[name]]$tests$tally[, 1L] <- test_idx[from[[name]]$tests$tally[, 1L]]
into[[name]]$tests$tally <- rbind(into[[name]]$tests$tally, from[[name]]$tests$tally)
}

Expand Down
2 changes: 1 addition & 1 deletion R/icc.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ parse_icov <- function(lines, package_path = "") {
by = list(line=m1$line, functions=m1$functions), sum)
names(m2) <- c("line", "functions", "coverage")
}
matches <- m2[order(m2$line),]
matches <- m2[order(m2$line), ]

values <- as.numeric(matches$coverage > 0L)
functions <- matches$functions
Expand Down
3 changes: 3 additions & 0 deletions R/summary_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ format_percentage <- function(x) {

markers <- function(x, ...) UseMethod("markers")

#' @export
markers.coverages <- function(x, ...) {
mrks <- unlist(lapply(unname(x), markers), recursive = FALSE)

Expand All @@ -216,6 +217,7 @@ markers.coverages <- function(x, ...) {
autoSelect = "first")
invisible()
}
#' @export
markers.coverage <- function(x, ...) {

# generate the markers
Expand All @@ -233,6 +235,7 @@ markers.coverage <- function(x, ...) {

}

#' @export
markers.data.frame <- function(x, ..., type = "test") { # nolint
# generate the markers
markers <- Map(function(filename, line, column) {
Expand Down
21 changes: 7 additions & 14 deletions R/trace_calls.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,15 @@ trace_calls <- function (x, parent_functions = NULL, parent_ref = NULL) {
if (is.atomic(x) || is.name(x) || is.null(x)) {
if (is.null(parent_ref)) {
x
}
else {
} else {
if (is_na(x) || is_brace(x)) {
x
} else {
key <- new_counter(parent_ref, parent_functions) # nolint
count(key, x)
}
}
}
else if (is.call(x)) {
} else if (is.call(x)) {
src_ref <- attr(x, "srcref") %||% impute_srcref(x, parent_ref)
if ((identical(x[[1]], as.name("<-")) || identical(x[[1]], as.name("="))) && # nolint
(is.call(x[[3]]) && identical(x[[3]][[1]], as.name("function")))) {
Expand All @@ -59,8 +57,7 @@ trace_calls <- function (x, parent_functions = NULL, parent_ref = NULL) {
} else {
as.call(recurse(x))
}
}
else if (is.function(x)) {
} else if (is.function(x)) {

# We cannot trace primitive functions
if (is.primitive(x)) {
Expand All @@ -83,17 +80,13 @@ trace_calls <- function (x, parent_functions = NULL, parent_ref = NULL) {
formals(x) <- new_formals
body(x) <- fun_body
x
}
else if (is.pairlist(x)) {
} else if (is.pairlist(x)) {
as.pairlist(recurse(x))
}
else if (is.expression(x)) {
} else if (is.expression(x)) {
as.expression(recurse(x))
}
else if (is.list(x)) {
} else if (is.list(x)) {
recurse(x)
}
else {
} else {
message("Unknown language class: ", paste(class(x), collapse = "/"))
x
}
Expand Down
8 changes: 4 additions & 4 deletions R/trace_tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ count_test <- function(key) {
tests$.data[[4L]] <- .current_test$i

tests$.value <- .counters[[key]]$value
with(tests, tally[.value,] <- .data)
with(tests, tally[.value, ] <- .data)
}

#' Initialize a new test counter for a coverage trace
Expand Down Expand Up @@ -339,9 +339,9 @@ truncate_call <- function(call_obj, limit = 1e4) {
#'
is_current_test_finished <- function() {
is.null(.current_test$src) ||
.current_test$last_frame > sys.nframe() ||
!identical(.current_test$src_call, sys.call(which = .current_test$last_frame)) ||
!identical(.current_test$src_env, sys.frame(which = .current_test$last_frame - 1L))
.current_test$last_frame > sys.nframe() ||
!identical(.current_test$src_call, sys.call(which = .current_test$last_frame)) ||
!identical(.current_test$src_env, sys.frame(which = .current_test$last_frame - 1L))
}

#' Is the source bound to the expression
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<!-- badges: start -->
[![R-CMD-check](https://github.com/r-lib/covr/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/r-lib/covr/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/r-lib/covr/branch/master/graph/badge.svg)](https://app.codecov.io/gh/r-lib/covr?branch=master)
[![CRAN version](http://www.r-pkg.org/badges/version/covr)](https://cran.r-project.org/package=covr)
[![CRAN version](https://www.r-pkg.org/badges/version/covr)](https://cran.r-project.org/package=covr)
<!-- badges: end -->

Track test coverage for your R package and view reports locally or (optionally)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-S7.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that("S7 coverage is reported", {
local_edition(3)
skip_if_not_installed("S7")
cov <- as.data.frame(package_coverage(test_path("TestS7")))

expect_equal(cov$value, c(1, 1, 1, 2, 5, 0, 5, 0, 5, 1, 1, 2, 1, 1, 0))
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-codecov.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ ci_vars <- c(
"WERCKER_GIT_COMMIT" = NA,
"WERCKER_GIT_OWNER" = NA,
"WERCKER_GIT_REPOSITORY" = NA,
"WERCKER_MAIN_PIPELINE_STARTED" = NA)
"WERCKER_MAIN_PIPELINE_STARTED" = NA
)

cov <- package_coverage(test_path("TestS4"))

Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/test-coveralls.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ ci_vars <- c(
"WERCKER_GIT_COMMIT" = NA,
"WERCKER_GIT_OWNER" = NA,
"WERCKER_GIT_REPOSITORY" = NA,
"WERCKER_MAIN_PIPELINE_STARTED" = NA)
"WERCKER_MAIN_PIPELINE_STARTED" = NA
)

read_file <- function(file) paste(collapse = "\n", readLines(file))

cov <- package_coverage("TestS4")
cov <- package_coverage(test_path("TestS4"))

test_that("coveralls generates a properly formatted json file", {
withr::with_envvar(
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-parallel.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test_that("mcparallel with the fix", {
skip_on_os("windows")

# using auto detection
cov <- package_coverage("TestParallel", type = "test")
cov <- package_coverage(test_path("TestParallel"), type = "test")
# only the non parallel code is covered
expect_equal(percent_coverage(cov), 100)
})
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-print.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ test_that("print.coverage prints by = \"line\" by default", {

expect_message(print(cov),
rex::rex("R/TestPrint.R: ", anything, "0.00%"))
})
test_that("print.coverage prints by = \"line\" by default", {
cov <- package_coverage("TestPrint")

expect_message(print(cov, group = "functions"),
rex::rex("test_me", anything, "0.00%"))
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-report.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ skip_on_ci <- function() {

#tmp <- tempfile()
#set.seed(42)
#cov <- package_coverage("TestS4")
#cov <- package_coverage(test_path("TestS4"))
## Shiny uses its own seed which is not affected by set.seed, so we need to
## set that as well to have reproducibility
#g <- shiny:::.globals
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-summary.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that("Summary gives 50% coverage and two lines with zero coverage", {
cv <- package_coverage("TestSummary")
cv <- package_coverage(test_path("TestSummary"))
expect_equal(percent_coverage(cv), 50)
expect_equal(nrow(zero_coverage(cv)), 2)
})
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ test_that("per_line removes blank lines and lines with only punctuation (#387)",
expect_equal(line_cov[[1]]$coverage, c(NA, 0, 0, 2, NA, 1, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA))
})

context("split_on_line_directives")

test_that("split_on_line_directives returns NULL for input without directive (#588)", {
expect_identical(
split_on_line_directives(NULL),
Expand Down
Loading