Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
olivroy committed Jan 13, 2025
1 parent 8eadd93 commit 4f1366a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 33 deletions.
23 changes: 6 additions & 17 deletions R/compiled.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# this does not handle LCOV_EXCL_START ect.
parse_gcov <- function(file, package_path = "") {
if (!source_file_exists(file)) {
if (!file.exists(file)) {
return(NULL)
}

lines <- read_lines_impl(file)
lines <- readLines(file)
source_file <- rex::re_matches(lines[1], rex::rex("Source:", capture(name = "source", anything)))$source

# retrieve full path to the source files
source_file <- normalize_path(source_file)

# If the source file does not start with the package path or does not exist ignore it.
if (!source_file_exists(source_file) || !grepl(rex::rex(start, rex::regex(paste0(rex::escape(package_path), collapse = "|"))), source_file)) {
if (!file.exists(source_file) || !grepl(rex::rex(start, rex::regex(paste0(rex::escape(package_path), collapse = "|"))), source_file)) {
return(NULL)
}

Expand Down Expand Up @@ -46,23 +46,12 @@ parse_gcov <- function(file, package_path = "") {
# There are no functions for gcov, so we set everything to NA
functions <- rep(NA_character_, length(values))

line_coverages_impl(source_file, matches, values, functions)
}

# For mocking (only use it within parse_gcov())
line_coverages_impl <- function(source_file, matches, values, functions) {
line_coverages(source_file, matches, values, functions)
}

# For mocking (only use it within parse_gcov())
source_file_exists <- function(path) {
file.exists(path)
}

# For mocking (only use it within parse_gcov())
read_lines_impl <- function(path) {
readLines(path)
}
# for mocking
readLines <- NULL
file.exists <- NULL

clean_gcov <- function(path) {
src_dir <- file.path(path, "src")
Expand Down
3 changes: 0 additions & 3 deletions R/covr.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@
#' @import methods
#' @importFrom stats aggregate na.omit na.pass setNames
#' @importFrom utils capture.output getSrcFilename relist str head
NULL

# For mocking
#' @importFrom httr content RETRY upload_file
NULL

Expand Down
7 changes: 1 addition & 6 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -322,16 +322,11 @@ env_path <- function(...) {
}

normalize_path <- function(x) {
path <- normalize_path_impl(x)
path <- normalizePath(path, winslash = "/", mustWork = FALSE)
# Strip any trailing slashes as they are invalid on windows
sub("/*$", "", path)
}

# For mocking (inside normalize_path)
normalize_path_impl <- function(path) {
normalizePath(path, winslash = "/", mustWork = FALSE)
}

temp_dir <- function() {
normalize_path(tempdir())
}
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test-gcov.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
test_that("parse_gcov parses files properly", {
local_mocked_bindings(
# Only called within parse_gcov
source_file_exists = function(path) TRUE,
file.exists = function(path) TRUE,
# Only called within normalize_path
normalize_path_impl = function(path) "simple.c",
normalize_path = function(path) "simple.c",
# Only called within parse_gcov
line_coverages_impl = function(source_file, matches, values, ...) values
line_coverages = function(source_file, matches, values, ...) values
)

with_mocked_bindings(
expect_equal(parse_gcov("hi.c.gcov"), numeric()),
read_lines_impl = function(x) {
readLines = function(x) {
" -: 0:Source:simple.c"
}
)

with_mocked_bindings(
read_lines_impl = function(x) {
readLines = function(x) {
c(
" -: 0:Source:simple.c",
" -: 1:#define USE_RINTERNALS"
Expand All @@ -26,7 +26,7 @@ test_that("parse_gcov parses files properly", {
)

with_mocked_bindings(
read_lines_impl = function(x) {
readLines = function(x) {
c(
" -: 0:Source:simple.c",
" -: 0:Graph:simple.gcno",
Expand All @@ -44,7 +44,7 @@ test_that("parse_gcov parses files properly", {
code = expect_equal(parse_gcov("hi.c.gcov"), 4)
)
with_mocked_bindings(
read_lines_impl = function(x) {
readLines = function(x) {
c(
" -: 0:Source:simple.c",
" -: 0:Graph:simple.gcno",
Expand Down

0 comments on commit 4f1366a

Please sign in to comment.