-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_test_report.R
More file actions
365 lines (344 loc) · 9.9 KB
/
parse_test_report.R
File metadata and controls
365 lines (344 loc) · 9.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# This script was created to parse the outputs from testthat and covr for
# DataSHIELD packages (e.g., dsBase). The input files are generate via a GHA
# workflow (see URL_TO_GHA_WORKFLOW).
#
# Roberto Villegas-Diaz <r.villegas-diaz@liverpool.ac.uk>
# July 2025
# SETUP ----
args <- commandArgs(trailingOnly = TRUE) # read CL arguments
# 1st argument: INPUT_DIR
if (length(args) >= 1) {
INPUT_DIR <- args[1]
} else {
INPUT_DIR <- getwd()
}
# 2nd argument: OUTPUT_DIR
if (length(args) >= 2) {
OUTPUT_DIR <- args[2]
} else {
OUTPUT_DIR <- INPUT_DIR
}
# 3rd argument: GH_REPO - GitHub repository URL
if (length(args) >= 3) {
GH_REPO <- args[3]
} else {
GH_REPO <- file.path(git2r::remote_url(), "blob/master")
}
# 4th argument: FN_NAME_PATTERN - Function name pattern
if (length(args) >= 4) {
FN_NAME_PATTERN <- args[4]
} else {
FN_NAME_PATTERN <- "[^-:.]+"
}
# 5th argument: FN_TEST_CLASS_PATTERN - Function test class pattern
if (length(args) >= 4) {
FN_TEST_CLASS_PATTERN <- args[5]
} else {
FN_TEST_CLASS_PATTERN <- "^[a-zA-Z]+-"
}
# HELPER FUNCTIONS ----
valid_url <- function(url, timeout = 5) {
req <- httr2::request(url) |>
httr2::req_method("HEAD") |>
httr2::req_options(timeout = timeout)
res <- tryCatch(
httr2::req_perform(req),
error = function(e) NULL
)
!is.null(res) && httr2::resp_status(res) < 400
}
xml2tibble_covr <- function(filepath) {
covr_xml <- filepath |>
xml2::read_xml()
# get total coverage
pkg_coverage <- covr_xml |>
xml2::xml_find_all("packages") |>
xml2::xml_find_all("package") |>
xml2::xml_attrs() |>
purrr::list_c() |>
tibble::as_tibble_row()
covr_xml |>
xml2::xml_find_all("packages") |>
xml2::xml_find_all("package") |>
xml2::xml_find_all("classes") |>
xml2::xml_find_all("class") |>
purrr::map(
function(x) {
tmp <- tempfile()
on.exit(unlink(tmp))
x |>
xml2::xml_attrs() |>
tibble::as_tibble_row() |>
readr::write_csv(tmp)
readr::read_csv(tmp, show_col_types = FALSE)
},
.progress = FALSE
) |>
purrr::list_c() |>
dplyr::mutate(
name = filename,
file_coverage = round(as.numeric(`line-rate`) * 100, 2),
total_coverage = round(as.numeric(pkg_coverage$`line-rate`) * 100, 2)
) |>
dplyr::select(name, file_coverage, total_coverage)
}
xml2tibble_test <- function(filepath) {
filepath |>
xml2::read_xml() |>
xml2::xml_find_all("testsuite") |>
purrr::map(function(ts) {
tmp <- tempfile()
on.exit(unlink(tmp))
ts |>
xml2::xml_attrs() |>
tibble::as_tibble_row() |>
readr::write_csv(tmp)
readr::read_csv(tmp, show_col_types = FALSE)
}) |>
purrr::list_c()
}
# LOAD RESULTS ----
message("Loading results...")
# Load coverage output
covr_files <- list.files(
INPUT_DIR,
pattern = "^coverage.*xml$",
full.names = TRUE
)
if (length(covr_files) > 0) {
covr_csv <- purrr::map(covr_files, xml2tibble_covr) |>
purrr::list_c() |>
dplyr::distinct()
} else if (file.exists(file.path(INPUT_DIR, "coverage.xml"))) {
covr_csv <- file.path(INPUT_DIR, "coveragelist.csv") |>
readr::read_csv(
show_col_types = FALSE,
skip = 1,
col_names = c("name", "file_coverage", "total_coverage")
) |>
dplyr::mutate(
file_coverage = round(as.numeric(file_coverage), 2),
total_coverage = round(as.numeric(total_coverage), 2)
)
} else {
covr_csv <- tibble::tibble(
name = '<PLACEHOLDER>',
file_coverage = NA_real_,
total_coverage = NA_real_
)
}
# Load test results
test_files <- list.files(
INPUT_DIR,
pattern = "^test_results.*xml$",
full.names = TRUE
)
# Parse XML results into tibble
tests_tbl <- purrr::map(test_files, xml2tibble_test) |>
purrr::list_c() |>
dplyr::group_by(name) |>
dplyr::reframe(
tests = sum(tests, na.rm = TRUE),
skipped = sum(skipped, na.rm = TRUE),
failures = sum(failures, na.rm = TRUE),
errors = sum(errors, na.rm = TRUE),
time = sum(time, na.rm = TRUE)
)
# AGGREGATE RESULTS ----
message("Aggregating results...")
tests_tbl_v2 <- tests_tbl |>
# drop rows without actual tests
dplyr::filter(tests > 0) |>
# extract details from testsuite
dplyr::mutate(
# detect if current record has a test class
has_test_class = stringr::str_detect(name, FN_TEST_CLASS_PATTERN),
# detect naming style
is_hyphen_style = stringr::str_detect(name, "^[^-:]+-[^-:]+"),
# extract function name (handles both `class-fn` and `fn::class::...`)
fn_name = name |>
stringr::str_extract(FN_NAME_PATTERN) |>
stringr::str_remove_all("[\\(\\)]"),
# extract test class (e.g., arg, smk, etc.)
test_class = name |>
stringr::str_extract(FN_TEST_CLASS_PATTERN),
test_class = dplyr::if_else(has_test_class, test_class, NA_character_),
# fn_name_sub ONLY for hyphen-style: class-function-sub
fn_name_sub = dplyr::if_else(
is_hyphen_style,
name |>
# remove "class-function-" prefix (optional trailing dash)
stringr::str_remove("^[^-:]+-[^-:]+-?") |>
# keep only the first condition token if present
stringr::str_extract("^[^:-]+") |>
stringr::str_remove_all("[\\(\\)]") |>
tidyr::replace_na(""),
""
)
) |>
dplyr::select(-is_hyphen_style) |>
# fill in sub-tests with test class
tidyr::fill(test_class, .direction = "down") |>
dplyr::mutate(
fn_name_sub2 = ifelse(fn_name_sub == "", "", paste0("-", fn_name_sub)),
# create links to function script and test file
github_script_link = file.path(
GH_REPO,
"R",
paste0(fn_name, ".R")
),
github_test_link = file.path(
GH_REPO,
"tests/testthat",
paste0("test-", test_class, "-", fn_name, fn_name_sub2, ".R")
)
) |>
dplyr::select(-fn_name_sub2)
# aggregate results by function name and test class
tests_tbl_v3 <- tests_tbl_v2 |>
dplyr::group_by(fn_name, fn_name_sub, test_class) |>
dplyr::mutate(
tests = sum(tests, na.rm = TRUE),
skipped = sum(skipped, na.rm = TRUE),
failures = sum(failures, na.rm = TRUE),
errors = sum(errors, na.rm = TRUE),
time = sum(time, na.rm = TRUE),
) |>
dplyr::ungroup() |>
dplyr::select(
fn_name,
fn_name_sub,
test_class,
tests:github_test_link,
-has_test_class
) |>
dplyr::distinct(fn_name, fn_name_sub, test_class, .keep_all = TRUE)
# check/validate URLs
message("Validating URLs...")
tests_tbl_v4 <- tests_tbl_v3 |>
dplyr::mutate(
valid_github_script_link = github_script_link |>
purrr::map(valid_url, .progress = TRUE),
valid_github_test_link = github_test_link |>
purrr::map(valid_url, .progress = TRUE),
github_script_link = ifelse(
valid_github_script_link,
github_script_link,
NA
),
github_test_link = ifelse(valid_github_test_link, github_test_link, NA)
)
# ADD TEST COVERAGE ----
message("Adding test coverage...")
covr_csv_2 <- covr_csv |>
dplyr::mutate(
# extract function name
fn_name = name |>
basename() |>
stringr::str_remove("\\.R") |>
stringr::str_extract(FN_NAME_PATTERN),
# review if `fn_name` is missing
fn_name = ifelse(
is.na(fn_name),
name |>
stringr::str_remove("\\.R") |>
stringr::str_remove("R\\/*"),
fn_name
)
)
# Combine results
covr_and_test_results <- tests_tbl_v4 |>
dplyr::left_join(covr_csv_2, by = dplyr::join_by(fn_name))
# Tidy up results and combine into a wide table
covr_and_test_results_v2 <- covr_and_test_results |>
dplyr::filter(!is.na(fn_name)) |>
# subset columns
dplyr::select(
fn_name,
fn_name_sub,
github_script_link,
file_coverage,
test_class,
github_test_link,
tests:time
) |>
dplyr::arrange(fn_name) |>
tidyr::pivot_longer(
cols = -c(
fn_name,
fn_name_sub,
github_script_link,
test_class,
file_coverage
),
values_transform = as.character
) |>
dplyr::group_by(test_class) |>
dplyr::group_split()
covr_and_test_results_v3 <- covr_and_test_results_v2 |>
purrr::map(function(x) {
x |>
dplyr::distinct() |>
tidyr::pivot_wider(
id_cols = c(
fn_name,
fn_name_sub,
github_script_link,
test_class,
file_coverage
)
) |>
dplyr::rename(
script_url = github_script_link,
test_url = github_test_link
)
})
covr_and_test_results_v3 |>
readr::write_rds(file.path(
OUTPUT_DIR,
paste0(Sys.Date(), "_covr_and_test_results.Rds")
))
covr_and_test_results_v3 |>
purrr::reduce(dplyr::bind_rows) |>
readr::write_excel_csv(
file.path(OUTPUT_DIR, paste0(Sys.Date(), "_covr_and_test_results.csv")),
na = ""
)
# capture session info
session_info_files <- list.files(
INPUT_DIR,
pattern = "^session_info.*txt$",
full.names = TRUE
)
extract_session_info <- function(file) {
txt <- readLines(file) |>
paste(collapse = "\n")
tibble::tibble(
# file = basename(file),
shard = stringr::str_match(basename(file), "shard-([^\n]+)\\.txt")[, 2] |>
stringr::str_remove("\\-$"),
r_version = stringr::str_match(txt, "R version ([^\n]+)")[, 2],
platform = stringr::str_match(txt, "Platform: ([^\n]+)")[, 2],
running_under = stringr::str_match(txt, "Running under: ([^\n]+)")[, 2],
blas = stringr::str_match(txt, "BLAS:\\s+([^\n]+)")[, 2],
lapack = stringr::str_match(txt, "LAPACK:\\s+([^\n]+)")[, 2],
timezone = stringr::str_match(txt, "time zone: ([^\n]+)")[, 2],
base_packages = stringr::str_match(
txt,
"attached base packages:\\n\\[1\\] ([^\n]+)"
)[, 2] |>
stringr::str_squish(),
namespace_packages = stringr::str_match(
txt,
"loaded via a namespace \\(and not attached\\):\\n\\[1\\] ([^\n]+)"
)[, 2] |>
stringr::str_squish()
)
}
session_info_files |>
purrr::map(extract_session_info) |>
purrr::list_c() |>
readr::write_excel_csv(
file.path(OUTPUT_DIR, paste0(Sys.Date(), "_session_info.csv")),
na = ""
)