Skip to content

Commit 08946da

Browse files
committed
fix: Avoid iterating over already processed files with gcov
1 parent 40122df commit 08946da

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

R/compiled.R

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,30 @@ run_gcov <- function(path, quiet = TRUE, clean = TRUE,
7777
if (length(gcov_inputs)) stop('gcov not found')
7878
return()
7979
}
80+
81+
if (clean) {
82+
on.exit({
83+
gcov_outputs <- list.files(path, pattern = rex::rex(".gcov", end), recursive = TRUE, full.names = TRUE)
84+
unlink(gcov_outputs)
85+
})
86+
}
87+
8088
run_gcov_one <- function(src) {
8189
system_check(gcov_path,
8290
args = c(gcov_args, src, "-p", "-o", dirname(src)),
8391
quiet = quiet, echo = !quiet)
84-
gcov_outputs <- list.files(path, pattern = rex::rex(".gcov", end), recursive = TRUE, full.names = TRUE)
85-
if (clean) {
86-
on.exit(unlink(gcov_outputs))
87-
}
88-
unlist(lapply(gcov_outputs, parse_gcov, package_path = c(path, getOption("covr.gcov_additional_paths", NULL))), recursive = FALSE)
8992
}
9093

91-
res <- withr::with_dir(src_path, {
92-
compact(unlist(lapply(gcov_inputs, run_gcov_one), recursive = FALSE))
93-
})
94-
if (!length(res) && length(gcov_inputs))
94+
withr::with_dir(src_path, {
95+
compact(unlist(lapply(gcov_inputs, run_gcov_one), recursive = FALSE))
96+
gcov_outputs <- list.files(path, pattern = rex::rex(".gcov", end), recursive = TRUE, full.names = TRUE)
97+
res <- unlist(lapply(gcov_outputs, parse_gcov, package_path = c(path, getOption("covr.gcov_additional_paths", NULL))), recursive = FALSE)
98+
})
99+
100+
if (!length(res) && length(gcov_inputs)) {
95101
warning('parsed gcov output was empty')
102+
}
103+
96104
res
97105
}
98106

tests/testthat/test-Compiled.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ test_that("Compiled code coverage is reported including code in headers", {
1818
# This header contains a C++ template, which requires you to run gcov for
1919
# each object file separately and merge the results together.
2020
simple_h <- cov[cov$filename == "src/simple-header.h", ]
21-
expect_equal(simple_h[simple_h$first_line == "12", "value"], 4)
21+
expect_equal(simple_h[simple_h$first_line == "12", "value"], 2)
2222

23-
expect_equal(simple_h[simple_h$first_line == "18", "value"], 3)
23+
expect_equal(simple_h[simple_h$first_line == "18", "value"], 1)
2424

2525
expect_equal(simple_h[simple_h$first_line == "21", "value"], 0)
2626

2727
expect_equal(simple_h[simple_h$first_line == "23", "value"], 1)
2828

29-
expect_equal(simple_h[simple_h$first_line == "25", "value"], 4)
29+
expect_equal(simple_h[simple_h$first_line == "25", "value"], 2)
3030

3131
expect_true(all(unique(cov$filename) %in% c("R/TestCompiled.R", "src/simple-header.h", "src/simple.cc", "src/simple4.cc")))
3232
})

0 commit comments

Comments
 (0)