Created
December 9, 2019 21:03
-
-
Save cimentadaj/45c8d0408999e9ec8875fb3dc6a88451 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script loops over all `test_that` expressions in testthat files and adds | |
# a `skip_on_cran()` to any `test_that` expressions that **doesn't** have a a | |
# `skip_on_cran()`. | |
library(glue) | |
all_files <- list.files(here::here("tests/testthat/"), | |
full.names = TRUE, | |
pattern = "test-[0-9]") | |
for (file_i in all_files) { | |
print(glue("Working on {file_i}")) | |
all_lines <- readLines(file_i) | |
testthat_calls <- cumsum(grepl("test_that", all_lines)) | |
list_to_replace <- lapply(unique(testthat_calls), | |
function(x) all_lines[testthat_calls == x]) | |
first_testthat <- sapply(list_to_replace, function(x) any(grepl("test_that", x))) | |
first_testthat <- match(TRUE, first_testthat) | |
# Start from 2 because 1 is the preamble of the test file | |
# that doesn't contain any test_that expressions | |
for (i in first_testthat:length(list_to_replace)) { | |
specific_test <- list_to_replace[[i]] | |
has_skip <- any(grepl("skip_on_cran", specific_test)) | |
if (!has_skip) { | |
line_num <- grep("test_that", specific_test) | |
next_line <- (line_num + 1) | |
# Add skip_on_cran on next line after "test_that" | |
# but add it as new line to avoid replacing the | |
# second line | |
list_to_replace[[i]] <- c(specific_test[line_num], | |
" skip_on_cran()", | |
specific_test[next_line:length(specific_test)] | |
) | |
} | |
} | |
corrected_lines <- Reduce(`c`, list_to_replace) | |
writeLines(corrected_lines, file_i) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment