Skip to content

Commit

Permalink
Add option to add use_public_rspm = FALSE in the r function
Browse files Browse the repository at this point in the history
  • Loading branch information
k-doering-NOAA committed Jan 31, 2024
1 parent 807feba commit beb8bd2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
21 changes: 20 additions & 1 deletion R/use_r_workflows.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,32 @@ use_r_cmd_check <- function(workflow_name = "call-r-cmd-check.yml",

#' workflow for calculating code coverage
#' @template workflow_name
#' @param use_public_rspm Use posit package manager instead of CRAN to install dependencies? The
#' advantage here is that dependencies are precompiled, so install should be much quicker. In
#' rare situations (like packages with TMB dependencies), using use_public_rspm = FALSE may be
#' a better option. Note a setting only needs to be specified in the yml if use_public_rspm is FALSE, so
#' there will be no setting added if use_public_rspm is TRUE.
#' @export
use_calc_coverage <- function(workflow_name = "call-calc-coverage.yml") {
use_calc_coverage <- function(workflow_name = "call-calc-coverage.yml", use_public_rspm = TRUE) {
check_workflow_name(workflow_name)
usethis::use_github_action("call-calc-coverage.yml",
save_as = workflow_name,
url = "https://raw.githubusercontent.com/nmfs-fish-tools/ghactions4r/main/inst/templates/call-calc-coverage.yml"
)
path_to_yml <- file.path(".github", "workflows", workflow_name)
gha <- readLines(path_to_yml)
if (use_public_rspm == FALSE) {
uses_line <- grep(
"uses: nmfs-fish-tools/ghactions4r/.github/workflows/calc-coverage.yml",
gha
)
with_line <- grep("with:", gha[uses_line + 1])
if (length(with_line) == 0) {
gha <- append(gha, " with:", after = uses_line)
}
gha <- append(gha, " use-public-rspm: false", after = uses_line + 1)
writeLines(gha, path_to_yml)
}
}

#' Use workflow in current pkg to automate documenting and styling
Expand Down
18 changes: 17 additions & 1 deletion tests/testthat/_snaps/use_r_workflows.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
[14] " with:"
[15] " depends_on_tmb: true"

# use_calc_coverage()) works
# use_calc_coverage() works

Code
test
Expand All @@ -95,6 +95,22 @@
[7] " call-workflow:"
[8] " uses: nmfs-fish-tools/ghactions4r/.github/workflows/calc-coverage.yml@main"

# use_calc_coverage() works with use-public-rspm = FALSE

Code
test
Output
[1] "# call a workflow that runs covr::codecov() to calculate code coverage"
[2] "name: call-calc_coverage"
[3] "# on specifies the build triggers. See more info at https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows"
[4] "# The default is to run the workflow on every push or pull request to every branch."
[5] "on: [push, pull_request]"
[6] "jobs:"
[7] " call-workflow:"
[8] " uses: nmfs-fish-tools/ghactions4r/.github/workflows/calc-coverage.yml@main"
[9] " with:"
[10] " use-public-rspm: false"

# use_doc_and_style_r() works

Code
Expand Down
10 changes: 9 additions & 1 deletion tests/testthat/test-use_r_workflows.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,21 @@ test_that("use_r_cmd_check() works with full build option and tmb", {
expect_snapshot(test)
})

test_that("use_calc_coverage()) works", {
test_that("use_calc_coverage() works", {
use_calc_coverage()
expect_true(file.exists(".github/workflows/call-calc-coverage.yml"))
test <- readLines(".github/workflows/call-calc-coverage.yml")
expect_snapshot(test)
})

test_that("use_calc_coverage() works with use-public-rspm = FALSE", {
workflow_name <- "call-calc-cov-false.yml"
use_calc_coverage(workflow_name = workflow_name, use_public_rspm = FALSE)
expect_true(file.exists(file.path(".github", "workflows", workflow_name)))
test <- readLines(file.path(".github", "workflows", workflow_name))
expect_snapshot(test)
})

test_that("use_doc_and_style_r() works", {
use_doc_and_style_r(
use_rm_dollar_sign = TRUE, how_to_commit = "directly",
Expand Down

0 comments on commit beb8bd2

Please sign in to comment.