diff --git a/.github/workflows/build-pkgdown.yml b/.github/workflows/build-pkgdown.yml index f8b5485..b0484d4 100644 --- a/.github/workflows/build-pkgdown.yml +++ b/.github/workflows/build-pkgdown.yml @@ -1,36 +1,61 @@ -# Workflow derived from https://github.com/r-lib/actions/tree/master/examples -# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help -on: - workflow_call: - -name: build-pkgdown - -jobs: - pkgdown: - runs-on: ubuntu-latest - env: - GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} - steps: - - uses: actions/checkout@v4 - - - uses: r-lib/actions/setup-pandoc@v2 - - - uses: r-lib/actions/setup-r@v2 - with: - use-public-rspm: true - - - uses: r-lib/actions/setup-r-dependencies@v2 - with: - extra-packages: any::pkgdown, local::. - needs: website - - - name: build pkgdown - run: | - Rscript -e 'pkgdown::build_site()' - - - name: save pkgdown output - uses: actions/upload-artifact@v4 - with: - name: pkgdown-site - path: docs/ - +# Workflow derived from https://github.com/r-lib/actions/tree/master/examples +# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help +on: + workflow_call: + inputs: + additional_args_windows: + required: false + type: string + default: "" + additional_args_macos: + required: false + type: string + default: "" + additional_args_ubuntu: + required: false + type: string + default: "" + +name: build-pkgdown + +jobs: + pkgdown: + runs-on: ubuntu-latest + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + steps: + - uses: actions/checkout@v4 + + - name: run additional args on windows + if: matrix.config.os == 'windows-latest' + run: ${{ inputs.additional_args_windows }} + + - name: run additional args on macOS + if: matrix.config.os == 'macOS-latest' + run: ${{ inputs.additional_args_macos }} + + - name: run additional args on ubuntu + if: matrix.config.os == 'ubuntu-latest' + run: ${{ inputs.additional_args_ubuntu }} + + - uses: r-lib/actions/setup-pandoc@v2 + + - uses: r-lib/actions/setup-r@v2 + with: + use-public-rspm: true + + - uses: r-lib/actions/setup-r-dependencies@v2 + with: + extra-packages: any::pkgdown, local::. + needs: website + + - name: build pkgdown + run: | + Rscript -e 'pkgdown::build_site()' + + - name: save pkgdown output + uses: actions/upload-artifact@v4 + with: + name: pkgdown-site + path: docs/ + diff --git a/.github/workflows/call-build-pkgdown.yml b/.github/workflows/call-build-pkgdown.yml new file mode 100644 index 0000000..24b17f4 --- /dev/null +++ b/.github/workflows/call-build-pkgdown.yml @@ -0,0 +1,15 @@ +# Checks that the pkgdown site builds for a repository. +# this assumes pkgdown is already set up. +name: call-build-pkgdown +# on specifies the build triggers. See more info at https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows +on: +# this workflow runs on pushes to main or master or any time a new tag is pushed +# it also runs everytime a pull request to main or master is opened. + push: + branches: [main, master] + tags: ['*'] + pull_request: + branches: [main, master] +jobs: + call-workflow: + uses: nmfs-fish-tools/ghactions4r/.github/workflows/build-pkgdown.yml@main \ No newline at end of file diff --git a/.github/workflows/call-r-cmd-check.yml b/.github/workflows/call-r-cmd-check.yml index 51283d1..c00ef4a 100644 --- a/.github/workflows/call-r-cmd-check.yml +++ b/.github/workflows/call-r-cmd-check.yml @@ -1,17 +1,17 @@ -# Run r cmd check -on: - workflow_dispatch: - push: - paths-ignore: - - '.github/workflows/call**' - pull_request: - paths-ignore: - - '.github/workflows/call**' - schedule: - # This should run the default branch weekly on sunday - - cron: '0 0 * * 0' -name: call-r-cmd-check - -jobs: - call-workflow: - uses: nmfs-fish-tools/ghactions4r/.github/workflows/r-cmd-check.yml@main +# Run r cmd check +on: + workflow_dispatch: + push: + paths-ignore: + - '.github/workflows/call**' + pull_request: + paths-ignore: + - '.github/workflows/call**' + schedule: + # This should run the default branch weekly on sunday + - cron: '0 0 * * 0' +name: call-r-cmd-check + +jobs: + call-workflow: + uses: nmfs-fish-tools/ghactions4r/.github/workflows/r-cmd-check.yml@main \ No newline at end of file diff --git a/.github/workflows/call-update-pkgdown.yml b/.github/workflows/call-update-pkgdown.yml index ec15c53..68905f9 100644 --- a/.github/workflows/call-update-pkgdown.yml +++ b/.github/workflows/call-update-pkgdown.yml @@ -1,13 +1,13 @@ -# updates exiting pkgdown site for a repository -# deploys to a branch gh-pages -name: call-update-pkgdown -# on specifies the build triggers. See more info at https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows -on: -# this workflow runs on pushes to main or master or any time a new tag is pushed - workflow_dispatch: - push: - branches: [main, master] - tags: ['*'] -jobs: - call-workflow: - uses: nmfs-fish-tools/ghactions4r/.github/workflows/update-pkgdown.yml@main +# updates exiting pkgdown site for a repository +# deploys to a branch gh-pages +name: call-update-pkgdown +# on specifies the build triggers. See more info at https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows +on: +# this workflow runs on pushes to main or master or any time a new tag is pushed + workflow_dispatch: + push: + branches: [main, master] + tags: ['*'] +jobs: + call-workflow: + uses: nmfs-fish-tools/ghactions4r/.github/workflows/update-pkgdown.yml@main \ No newline at end of file diff --git a/.github/workflows/r-cmd-check.yml b/.github/workflows/r-cmd-check.yml index e472a88..a808238 100644 --- a/.github/workflows/r-cmd-check.yml +++ b/.github/workflows/r-cmd-check.yml @@ -1,109 +1,125 @@ -# Reusable workflow for R-CMD-check -# based on https://github.com/r-lib/actions/blob/v2/examples/check-full.yaml - -on: - workflow_call: - inputs: - use_full_build_matrix: - required: false - type: boolean - gha_timeout_minutes: - required: false - type: number - default: 360 - depends_on_tmb: - required: false - type: boolean - default: false - -name: R-CMD-check -jobs: - matrix_prep: - # Determine the build matrix (OS and versions of R) - runs-on: ubuntu-latest - outputs: - matrix: ${{ steps.set-matrix.outputs.matrix }} - steps: - - uses: actions/checkout@v4 - with: - repository: 'nmfs-fish-tools/ghactions4r' - ref: 'main' - - name: check path - run: | - ls -r - pwd - - id: set-matrix - run: | - if ${{ inputs.use_full_build_matrix }} == 'true'; - then - JSON=$(cat ./github/matrix_includes_full.json) - else - JSON=$(cat ./github/matrix_includes_standard.json) - fi - # note: if users were inputting the $JSON info, would want to - # randomize the delimiter. See - # https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings - echo 'matrix<> $GITHUB_OUTPUT - echo "$JSON">> $GITHUB_OUTPUT - echo 'EOF' >> $GITHUB_OUTPUT - - R-CMD-check: - needs: matrix_prep - runs-on: ${{matrix.config.os }} - timeout-minutes: ${{ inputs.gha_timeout_minutes }} - - name: ${{ matrix.config.os }} (${{ matrix.config.r }}) - - strategy: - fail-fast: false - matrix: - config: ${{ fromJson(needs.matrix_prep.outputs.matrix) }} - env: - R_REMOTES_NO_ERRORS_FROM_WARNINGS: true - GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} - R_KEEP_PKG_SOURCE: yes - - steps: - - uses: actions/checkout@v4 - - - name: update linux libraries - if: matrix.config.os == 'ubuntu-latest' - run: | - sudo apt-get update - sudo apt-get install -y libcurl4-openssl-dev - sudo add-apt-repository ppa:ubuntu-toolchain-r/test - sudo apt-get install --only-upgrade libstdc++6 - - - uses: r-lib/actions/setup-r@v2 - with: - r-version: ${{ matrix.config.r }} - - - uses: r-lib/actions/setup-pandoc@v2 - - - uses: r-lib/actions/setup-r-dependencies@v2 - with: - extra-packages: any::rcmdcheck - needs: check - - - name: If dependent on TMB, install Matrix from source for windows and mac - if: inputs.depends_on_tmb == true && runner.os != 'Linux' - run: install.packages("Matrix", type = "source") - shell: Rscript {0} - - - - uses: r-lib/actions/check-r-package@v2 - with: - upload-snapshots: true - error-on: '"error"' - - - name: Show testthat output - if: always() - run: find check -name 'testthat.Rout*' -exec cat '{}' \; || true - shell: bash - - - name: Upload check results - if: failure() - uses: actions/upload-artifact@main - with: - name: ${{ runner.os }}-r${{ matrix.config.r }}-results - path: check +# Reusable workflow for R-CMD-check +# based on https://github.com/r-lib/actions/blob/v2/examples/check-full.yaml + +on: + workflow_call: + inputs: + use_full_build_matrix: + required: false + type: boolean + gha_timeout_minutes: + required: false + type: number + default: 360 + depends_on_tmb: + required: false + type: boolean + default: false + additional_args_windows: + required: false + type: string + default: "" + additional_args_macos: + required: false + type: string + default: "" + additional_args_ubuntu: + required: false + type: string + default: "" + +name: R-CMD-check +jobs: + matrix_prep: + # Determine the build matrix (OS and versions of R) + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.set-matrix.outputs.matrix }} + steps: + - uses: actions/checkout@v4 + with: + repository: 'nmfs-fish-tools/ghactions4r' + ref: 'main' + - name: check path + run: | + ls -r + pwd + - id: set-matrix + run: | + if ${{ inputs.use_full_build_matrix }} == 'true'; + then + JSON=$(cat ./github/matrix_includes_full.json) + else + JSON=$(cat ./github/matrix_includes_standard.json) + fi + # note: if users were inputting the $JSON info, would want to + # randomize the delimiter. See + # https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings + echo 'matrix<> $GITHUB_OUTPUT + echo "$JSON">> $GITHUB_OUTPUT + echo 'EOF' >> $GITHUB_OUTPUT + + R-CMD-check: + needs: matrix_prep + runs-on: ${{matrix.config.os }} + timeout-minutes: ${{ inputs.gha_timeout_minutes }} + + name: ${{ matrix.config.os }} (${{ matrix.config.r }}) + + strategy: + fail-fast: false + matrix: + config: ${{ fromJson(needs.matrix_prep.outputs.matrix) }} + env: + R_REMOTES_NO_ERRORS_FROM_WARNINGS: true + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + R_KEEP_PKG_SOURCE: yes + + steps: + - uses: actions/checkout@v4 + + - name: run additional args on windows + if: matrix.config.os == 'windows-latest' + run: ${{ inputs.additional_args_windows }} + + - name: run additional args on macOS + if: matrix.config.os == 'macOS-latest' + run: ${{ inputs.additional_args_macos }} + + - name: run additional args on ubuntu + if: matrix.config.os == 'ubuntu-latest' + run: ${{ inputs.additional_args_ubuntu }} + + - uses: r-lib/actions/setup-r@v2 + with: + r-version: ${{ matrix.config.r }} + + - uses: r-lib/actions/setup-pandoc@v2 + + - uses: r-lib/actions/setup-r-dependencies@v2 + with: + extra-packages: any::rcmdcheck + needs: check + + - name: If dependent on TMB, install Matrix from source for windows and mac + if: inputs.depends_on_tmb == true && runner.os != 'Linux' + run: install.packages("Matrix", type = "source") + shell: Rscript {0} + + + - uses: r-lib/actions/check-r-package@v2 + with: + upload-snapshots: true + error-on: '"error"' + + - name: Show testthat output + if: always() + run: find check -name 'testthat.Rout*' -exec cat '{}' \; || true + shell: bash + + - name: Upload check results + if: failure() + uses: actions/upload-artifact@main + with: + name: ${{ runner.os }}-r${{ matrix.config.r }}-results + path: check diff --git a/.github/workflows/update-pkgdown.yml b/.github/workflows/update-pkgdown.yml index 1f83d2b..e1bf630 100644 --- a/.github/workflows/update-pkgdown.yml +++ b/.github/workflows/update-pkgdown.yml @@ -1,38 +1,56 @@ -# Workflow derived from https://github.com/r-lib/actions/tree/master/examples -# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help -on: - workflow_call: - -name: update-pkgdown - -jobs: - pkgdown: - runs-on: ubuntu-latest - env: - GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} - steps: - - uses: actions/checkout@v4 - - - name: update linux packages - run: | - sudo apt-get update - sudo apt-get install -y libcurl4-openssl-dev - sudo add-apt-repository ppa:ubuntu-toolchain-r/test - sudo apt-get install --only-upgrade libstdc++6 - - - uses: r-lib/actions/setup-pandoc@v2 - - - uses: r-lib/actions/setup-r@v2 - with: - use-public-rspm: true - - - uses: r-lib/actions/setup-r-dependencies@v2 - with: - extra-packages: any::pkgdown, local::. - needs: website - - - name: Deploy package - run: | - git config --local user.name "$GITHUB_ACTOR" - git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" - Rscript -e 'pkgdown::deploy_to_branch(new_process = FALSE)' +# Workflow derived from https://github.com/r-lib/actions/tree/master/examples +# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help +on: + workflow_call: + inputs: + additional_args_windows: + required: false + type: string + default: "" + additional_args_macos: + required: false + type: string + default: "" + additional_args_ubuntu: + required: false + type: string + default: "" + +name: update-pkgdown + +jobs: + pkgdown: + runs-on: ubuntu-latest + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + steps: + - uses: actions/checkout@v4 + + - name: run additional args on windows + if: matrix.config.os == 'windows-latest' + run: ${{ inputs.additional_args_windows }} + + - name: run additional args on macOS + if: matrix.config.os == 'macOS-latest' + run: ${{ inputs.additional_args_macos }} + + - name: run additional args on ubuntu + if: matrix.config.os == 'ubuntu-latest' + run: ${{ inputs.additional_args_ubuntu }} + + - uses: r-lib/actions/setup-pandoc@v2 + + - uses: r-lib/actions/setup-r@v2 + with: + use-public-rspm: true + + - uses: r-lib/actions/setup-r-dependencies@v2 + with: + extra-packages: any::pkgdown, local::. + needs: website + + - name: Deploy package + run: | + git config --local user.name "$GITHUB_ACTOR" + git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" + Rscript -e 'pkgdown::deploy_to_branch(new_process = FALSE)' diff --git a/DESCRIPTION b/DESCRIPTION index 9b46f2a..4069a36 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,28 +1,29 @@ -Package: ghactions4r -Title: Use GitHub Actions Reusable Workflows for R Packages -Version: 0.2.0 -Authors@R: c( - person("Kathryn", "Doering", , "kathryn.doering@noaa.gov", role = c("aut", "cre"), - comment = c(ORCID = "0000-0002-0396-7044")), - person("Kelli", "Johnson", , "kelli.johnson@noaa.gov", role = "aut"), - person("Bai", "Li", role = "aut") - ) -Description: Provides functions to allow users to set up github action - workflows related to R packages. -License: CC0 -Imports: - usethis -Suggests: - dplyr, - knitr, - rmarkdown, - rvest, - testthat (>= 3.0.0), - visNetwork -VignetteBuilder: - knitr -Config/testthat/edition: 3 -Encoding: UTF-8 -LazyData: true -Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.2 +Package: ghactions4r +Title: Use GitHub Actions Reusable Workflows for R Packages +Version: 0.2.0 +Authors@R: c( + person("Kathryn", "Doering", , "kathryn.doering@noaa.gov", role = c("aut", "cre"), + comment = c(ORCID = "0000-0002-0396-7044")), + person("Kelli", "Johnson", , "kelli.johnson@noaa.gov", role = "aut"), + person("Bai", "Li", role = "aut") + ) +Description: Provides functions to allow users to set up github action + workflows related to R packages. +License: CC0 +Imports: + cli, + usethis +Suggests: + dplyr, + knitr, + rmarkdown, + rvest, + testthat (>= 3.0.0), + visNetwork +VignetteBuilder: + knitr +Config/testthat/edition: 3 +Encoding: UTF-8 +LazyData: true +Roxygen: list(markdown = TRUE) +RoxygenNote: 7.3.2 diff --git a/R/input_checks.R b/R/input_checks.R index f725639..af96fe6 100644 --- a/R/input_checks.R +++ b/R/input_checks.R @@ -1,13 +1,32 @@ -# functions to check inputs - -#' Check the workflow name is formatted correctly -#' -#' Basically check that it is a filename that ends in .yml -#' @template workflow_name -check_workflow_name <- function(workflow_name) { - stopifnot(is.character(workflow_name)) - stopifnot(length(workflow_name) == 1) - get_ext <- grep("\\.yml$", workflow_name) - stopifnot(isTRUE(length(get_ext) == 1)) - return(invisible(workflow_name)) -} +# functions to check inputs + +#' Check the workflow name is formatted correctly +#' +#' Basically check that it is a filename that ends in .yml +#' @template workflow_name +check_workflow_name <- function(workflow_name) { + stopifnot(is.character(workflow_name)) + stopifnot(length(workflow_name) == 1) + get_ext <- grep("\\.yml$", workflow_name) + stopifnot(isTRUE(length(get_ext) == 1)) + return(invisible(workflow_name)) +} + +#' Validate additional arguments for R functions +#' +#' @inheritParams use_r_cmd_check +validate_additional_args <- function(additional_args) { + if (!is.null(additional_args)) { + if (!is.list(additional_args)) { + cli::cli_abort("{.var additional_args} must be a named list.") + } + invalid_platforms <- setdiff(names(additional_args), c("windows", "macos", "ubuntu")) + if (length(invalid_platforms) > 0 | is.null(invalid_platforms)) { + cli::cli_abort("Invalid platform in {.var additional_args}: {.val {invalid_platforms}}. + Allowed platforms are {.val windows}, {.val macos}, and {.val ubuntu}.") + } + if (!all(vapply(additional_args, is.character, logical(1)))) { + cli::cli_abort("All values in {.var additional_args} must be character vectors.") + } + } +} \ No newline at end of file diff --git a/R/use_r_workflows.R b/R/use_r_workflows.R index b6552e7..bc508fc 100644 --- a/R/use_r_workflows.R +++ b/R/use_r_workflows.R @@ -1,248 +1,323 @@ -#' Use workflow to run r cmd check on Linux, Mac, and Windows GitHub Actions -#' @template workflow_name -#' @param use_full_build_matrix Run R cmd check with two older versions of R in -#' addition to the three runs that use the release version. -#' @param depends_on_tmb An option that install Matrix from source for windows -#' and Mac builds to solved a nuanced issue for packages dependent on TMB. -#' See this [google groups thread](https://groups.google.com/g/tmb-users/c/-GhmuuDP_OQ) -#' for more information. -#' @export -use_r_cmd_check <- function(workflow_name = "call-r-cmd-check.yml", - use_full_build_matrix = FALSE, - depends_on_tmb = FALSE) { - check_workflow_name(workflow_name) - if (use_full_build_matrix) { - url_name <- "https://raw.githubusercontent.com/nmfs-fish-tools/ghactions4r/main/inst/templates/call-r-cmd-check-full.yml" - } else { - url_name <- "https://raw.githubusercontent.com/nmfs-fish-tools/ghactions4r/main/inst/templates/call-r-cmd-check.yml" - } - usethis::use_github_action("call-r-cmd-check.yml", - save_as = workflow_name, - url = url_name - ) - if (depends_on_tmb) { - path_to_yml <- file.path(".github", "workflows", workflow_name) - txt <- readLines(path_to_yml) - if (use_full_build_matrix) { - prev_line <- grep("use_full_build_matrix: true", txt, fixed = TRUE) - } else { - prev_line <- grep( - "uses: nmfs-fish-tools/ghactions4r/.github/workflows/r-cmd-check.yml@main", - txt, - fixed = TRUE - ) - txt <- append(txt, " with:", prev_line) - prev_line <- prev_line + 1 - } - txt <- append(txt, " depends_on_tmb: true", prev_line) - writeLines(txt, path_to_yml) - } - invisible(workflow_name) -} - -#' 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_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 -#' -#' Style your R package components automatically by running devtools::document(), -#' styler::style_pkg(), and usethis::use_tidy_description(). -#' @details -#' Note that allow the pull request created from this workflow can -#' be used to trigger github action runs, by using a personal access -#' token to provide the right permissions. The PAT is first -#' [generated on a user account](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens), -#' then [added to the repository or organization as a secret](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository). -#' Using a PAT can cause recursive runs, so should be used -#' with caution. See github documentation on -#' [triggering a workflow from a workflow](https://docs.github.com/en/actions/using-workflows/triggering-a-workflow#triggering-a-workflow-from-a-workflow) -#' for more information. -#' @template workflow_name -#' @param use_rm_dollar_sign in addition to devtools::document and -#' styler::style_pkg, should ghactions4r::rm_dollar_sign be run? Defaults to FALSE. -#' @param how_to_commit Where should changes made to style and documentation be -#' committed? Options are 1) in a pull request to the branch ("pull_request") -#' where the workflow started; or 2) directly to the branch ("directly") where -#' the workflow started. -#' @param build_trigger Select the build trigger. Options are to run on pushing -#' commits to main ("push_to_main", the default); run when a pull request is -#' opened, reopened, or updated ("pull_request"); run manually with the -#' workflow_dispatch trigger ("manually"); run on the default branch (usually -#' main) once a week ("weekly"). -#' @param use_pat Should a personal access token (PAT) stored as a github secret -#' be used? This is only necessary if you want the pull request generated by -#' the doc and style workflow to be able to start other github action runs. -#' @param pat_name Name of the personal access token (PAT), as stored in secrets. -#' Only used if `use_pat = TRUE`. After -#' [generating the personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens), -#' See [how to add it as a secret](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository). -#' @examples -#' # set up running the doc and style workflow on each push to main, opening a -#' # pull request to main when changes are found. -#' \dontrun{ -#' use_doc_and_style_r() -#' } -#' # the same as -#' \dontrun{ -#' use_doc_and_style_r( -#' how_to_commit = "pull_request", -#' build_trigger = "push_to_main" -#' ) -#' } -#' # Set up running doc and style on each pull request, commiting directly to -#' # the pull request branch -#' \dontrun{ -#' use_doc_and_style_r( -#' how_to_commit = "directly", -#' build_trigger = "pull_request" -#' ) -#' } -#' # Set up the workflow to use a personal access token (PAT) -#' \dontrun{ -#' use_doc_and_style_r(use_pat = TRUE, pat_name = "PAT") -#' } -#' @export -use_doc_and_style_r <- function(workflow_name = "call-doc-and-style-r.yml", - use_rm_dollar_sign = FALSE, - how_to_commit = c("pull_request", "directly"), - build_trigger = c( - "push_to_main", - "pull_request", - "manually", - "weekly" - ), - use_pat = FALSE, - pat_name = "PAT") { - # input checks - check_workflow_name(workflow_name) - how_to_commit <- match.arg(arg = how_to_commit, choices = c("pull_request", "directly")) - build_trigger <- match.arg( - arg = build_trigger, - choices = c( - "push_to_main", "pull_request", "manually", - "weekly" - ) - ) - if (how_to_commit == "directly" & use_pat == TRUE) { - stop("Using how_to_commit = 'directly' and use_pat = TRUE can lead to recursive runs.") - } - - if (how_to_commit == "pull_request" & build_trigger == "pull_request") { - stop("Currently it is not possible to use how_to_commit == 'pull_request' and build_trigger = 'pull_request' in ghactions4r. Instead, create your own workflow and follow this example: https://github.com/peter-evans/create-pull-request/blob/main/docs/examples.md#use-case-create-a-pull-request-to-modifyfix-pull-requests") - } - # get the template github action - usethis::use_github_action("call-doc-and-style-r.yml", - save_as = workflow_name, - url = "https://raw.githubusercontent.com/nmfs-fish-tools/ghactions4r/main/inst/templates/call-doc-and-style-r.yml" - ) - path_to_yml <- file.path(".github", "workflows", workflow_name) - gha <- readLines(path_to_yml) - # modify the build trigger as needed - build_trigger_lines <- switch(build_trigger, - push_to_main = c(" push:", " branches: [main]"), - pull_request = " pull_request:", - manually = " workflow_dispatch:", - weekly = c( - " schedule:", - "# Use https://crontab.guru/ to edit the time", - " - cron: '15 02 * * 0'" - ) - ) - # remove existing build trigger - build_trigger_rm_lines <- grep("(push)|(branches)", gha) - insert_line <- build_trigger_rm_lines[1] - 1 - gha <- gha[-build_trigger_rm_lines] - # add new build trigger - gha <- append(gha, build_trigger_lines, after = insert_line) - # additional options - if (use_rm_dollar_sign == TRUE | how_to_commit == "directly") { - uses_line <- grep( - "uses: nmfs-fish-tools/ghactions4r/.github/workflows/doc-and-style-r.yml", - gha - ) - with_line <- grep("with:", gha[uses_line + 1]) - if (length(with_line) == 0) { - gha <- append(gha, " with:", after = uses_line) - } - if (how_to_commit == "directly") { - gha <- append(gha, " commit-directly: true", after = uses_line + 1) - } - if (use_rm_dollar_sign == TRUE) { - gha <- append(gha, " run-rm_dollar_sign: true", after = uses_line + 1) - } - } - if (use_pat == TRUE) { - uses_line <- grep( - "uses: nmfs-fish-tools/ghactions4r/.github/workflows/doc-and-style-r.yml", - gha - ) - pat_line <- paste0(" PAT: ${{ secrets.", pat_name, " }}") - gha <- append(gha, " secrets:", after = uses_line) - gha <- append(gha, pat_line, after = uses_line + 1) - } - writeLines(gha, path_to_yml) - usethis::use_git_ignore(ignores = "*.rds", directory = file.path(".github")) -} - -#' use workflow in current pkg to update pkg down, where the site is deployed to a branch called gh-pages -#' @template workflow_name -#' @export -use_update_pkgdown <- function(workflow_name = "call-update-pkgdown.yml") { - check_workflow_name(workflow_name) - usethis::use_github_action("call-update-pkgdown.yml", - save_as = workflow_name, - url = "https://raw.githubusercontent.com/nmfs-fish-tools/ghactions4r/main/inst/templates/call-update-pkgdown.yml" - ) -} - -#' use workflow in current pkg to check pkgdown site builds. -#' @template workflow_name -#' @export -use_build_pkgdown <- function(workflow_name = "call-build-pkgdown.yml") { - check_workflow_name(workflow_name) - usethis::use_github_action("call-build-pkgdown.yml", - save_as = workflow_name, - url = "https://raw.githubusercontent.com/nmfs-fish-tools/ghactions4r/main/inst/templates/call-build-pkgdown.yml" - ) -} - -#' use workflow to run spelling::spell_check_package() -#' @template workflow_name -#' @return The path to the new github action file. -#' @export -use_spell_check <- function(workflow_name = "call-spell-check.yml") { - check_workflow_name(workflow_name) - usethis::use_github_action("call-spell-check.yml", - save_as = workflow_name, - url = "https://raw.githubusercontent.com/nmfs-fish-tools/ghactions4r/main/inst/templates/call-spell-check.yml" - ) - path_to_yml <- file.path(".github", "workflows", workflow_name) - return(path_to_yml) -} +#' Use workflow to run r cmd check on Linux, Mac, and Windows GitHub Actions +#' @template workflow_name +#' @param use_full_build_matrix Run R cmd check with two older versions of R in +#' addition to the three runs that use the release version. +#' @param depends_on_tmb An option that install Matrix from source for windows +#' and Mac builds to solved a nuanced issue for packages dependent on TMB. +#' See this [google groups thread](https://groups.google.com/g/tmb-users/c/-GhmuuDP_OQ) +#' for more information. +#' @param additional_args A named list of additional command line arguments to be +#' passed to the workflow. The names of the list represent the platforms (windows, +#' macos, or ubuntu), and the values are character vectors of arguments. +#' These additional arguments are executed after the step that checks out the +#' repository and before the step that sets up R. +#' @examples +#' \dontrun{ +#' use_r_cmd_check( +#' additional_args = list( +#' ubuntu = c( +#' "sudo apt-get update", +#' "sudo apt-get install -y libcurl4-openssl-dev", +#' "sudo add-apt-repository ppa:ubuntu-toolchain-r/test", +#' "sudo apt-get install --only-upgrade libstdc++6" +#' ), +#' macos = c("brew install curl") +#' ) +#' ) +#' } +#' @export +use_r_cmd_check <- function(workflow_name = "call-r-cmd-check.yml", + use_full_build_matrix = FALSE, + depends_on_tmb = FALSE, + additional_args = NULL) { + validate_additional_args(additional_args) + + check_workflow_name(workflow_name) + if (use_full_build_matrix) { + url_name <- "https://raw.githubusercontent.com/nmfs-fish-tools/ghactions4r/main/inst/templates/call-r-cmd-check-full.yml" + } else { + url_name <- "https://raw.githubusercontent.com/nmfs-fish-tools/ghactions4r/main/inst/templates/call-r-cmd-check.yml" + } + usethis::use_github_action("call-r-cmd-check.yml", + save_as = workflow_name, + url = url_name + ) + + if (depends_on_tmb | !is.null(additional_args)) { + path_to_yml <- file.path(".github", "workflows", workflow_name) + txt <- readLines(path_to_yml) + if (use_full_build_matrix) { + prev_line <- grep("use_full_build_matrix: true", txt, fixed = TRUE) + } else { + prev_line <- grep( + "uses: nmfs-fish-tools/ghactions4r/.github/workflows/r-cmd-check.yml@main", + txt, + fixed = TRUE + ) + txt <- append(txt, " with:", prev_line) + prev_line <- prev_line + 1 + } + + if (depends_on_tmb) txt <- append(txt, " depends_on_tmb: true", prev_line) + + if (!is.null(additional_args)) { + add_args( + workflow_name = workflow_name, + additional_args = additional_args, + txt = txt, + prev_line = prev_line + ) + } + } + invisible(workflow_name) +} + +#' 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_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 +#' +#' Style your R package components automatically by running devtools::document(), +#' styler::style_pkg(), and usethis::use_tidy_description(). +#' @details +#' Note that allow the pull request created from this workflow can +#' be used to trigger github action runs, by using a personal access +#' token to provide the right permissions. The PAT is first +#' [generated on a user account](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens), +#' then [added to the repository or organization as a secret](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository). +#' Using a PAT can cause recursive runs, so should be used +#' with caution. See github documentation on +#' [triggering a workflow from a workflow](https://docs.github.com/en/actions/using-workflows/triggering-a-workflow#triggering-a-workflow-from-a-workflow) +#' for more information. +#' @template workflow_name +#' @param use_rm_dollar_sign in addition to devtools::document and +#' styler::style_pkg, should ghactions4r::rm_dollar_sign be run? Defaults to FALSE. +#' @param how_to_commit Where should changes made to style and documentation be +#' committed? Options are 1) in a pull request to the branch ("pull_request") +#' where the workflow started; or 2) directly to the branch ("directly") where +#' the workflow started. +#' @param build_trigger Select the build trigger. Options are to run on pushing +#' commits to main ("push_to_main", the default); run when a pull request is +#' opened, reopened, or updated ("pull_request"); run manually with the +#' workflow_dispatch trigger ("manually"); run on the default branch (usually +#' main) once a week ("weekly"). +#' @param use_pat Should a personal access token (PAT) stored as a github secret +#' be used? This is only necessary if you want the pull request generated by +#' the doc and style workflow to be able to start other github action runs. +#' @param pat_name Name of the personal access token (PAT), as stored in secrets. +#' Only used if `use_pat = TRUE`. After +#' [generating the personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens), +#' See [how to add it as a secret](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository). +#' @examples +#' # set up running the doc and style workflow on each push to main, opening a +#' # pull request to main when changes are found. +#' \dontrun{ +#' use_doc_and_style_r() +#' } +#' # the same as +#' \dontrun{ +#' use_doc_and_style_r( +#' how_to_commit = "pull_request", +#' build_trigger = "push_to_main" +#' ) +#' } +#' # Set up running doc and style on each pull request, commiting directly to +#' # the pull request branch +#' \dontrun{ +#' use_doc_and_style_r( +#' how_to_commit = "directly", +#' build_trigger = "pull_request" +#' ) +#' } +#' # Set up the workflow to use a personal access token (PAT) +#' \dontrun{ +#' use_doc_and_style_r(use_pat = TRUE, pat_name = "PAT") +#' } +#' @export +use_doc_and_style_r <- function(workflow_name = "call-doc-and-style-r.yml", + use_rm_dollar_sign = FALSE, + how_to_commit = c("pull_request", "directly"), + build_trigger = c( + "push_to_main", + "pull_request", + "manually", + "weekly" + ), + use_pat = FALSE, + pat_name = "PAT") { + # input checks + check_workflow_name(workflow_name) + how_to_commit <- match.arg(arg = how_to_commit, choices = c("pull_request", "directly")) + build_trigger <- match.arg( + arg = build_trigger, + choices = c( + "push_to_main", "pull_request", "manually", + "weekly" + ) + ) + if (how_to_commit == "directly" & use_pat == TRUE) { + stop("Using how_to_commit = 'directly' and use_pat = TRUE can lead to recursive runs.") + } + + if (how_to_commit == "pull_request" & build_trigger == "pull_request") { + stop("Currently it is not possible to use how_to_commit == 'pull_request' and build_trigger = 'pull_request' in ghactions4r. Instead, create your own workflow and follow this example: https://github.com/peter-evans/create-pull-request/blob/main/docs/examples.md#use-case-create-a-pull-request-to-modifyfix-pull-requests") + } + # get the template github action + usethis::use_github_action("call-doc-and-style-r.yml", + save_as = workflow_name, + url = "https://raw.githubusercontent.com/nmfs-fish-tools/ghactions4r/main/inst/templates/call-doc-and-style-r.yml" + ) + path_to_yml <- file.path(".github", "workflows", workflow_name) + gha <- readLines(path_to_yml) + # modify the build trigger as needed + build_trigger_lines <- switch(build_trigger, + push_to_main = c(" push:", " branches: [main]"), + pull_request = " pull_request:", + manually = " workflow_dispatch:", + weekly = c( + " schedule:", + "# Use https://crontab.guru/ to edit the time", + " - cron: '15 02 * * 0'" + ) + ) + # remove existing build trigger + build_trigger_rm_lines <- grep("(push)|(branches)", gha) + insert_line <- build_trigger_rm_lines[1] - 1 + gha <- gha[-build_trigger_rm_lines] + # add new build trigger + gha <- append(gha, build_trigger_lines, after = insert_line) + # additional options + if (use_rm_dollar_sign == TRUE | how_to_commit == "directly") { + uses_line <- grep( + "uses: nmfs-fish-tools/ghactions4r/.github/workflows/doc-and-style-r.yml", + gha + ) + with_line <- grep("with:", gha[uses_line + 1]) + if (length(with_line) == 0) { + gha <- append(gha, " with:", after = uses_line) + } + if (how_to_commit == "directly") { + gha <- append(gha, " commit-directly: true", after = uses_line + 1) + } + if (use_rm_dollar_sign == TRUE) { + gha <- append(gha, " run-rm_dollar_sign: true", after = uses_line + 1) + } + } + if (use_pat == TRUE) { + uses_line <- grep( + "uses: nmfs-fish-tools/ghactions4r/.github/workflows/doc-and-style-r.yml", + gha + ) + pat_line <- paste0(" PAT: ${{ secrets.", pat_name, " }}") + gha <- append(gha, " secrets:", after = uses_line) + gha <- append(gha, pat_line, after = uses_line + 1) + } + writeLines(gha, path_to_yml) + usethis::use_git_ignore(ignores = "*.rds", directory = file.path(".github")) +} + +#' use workflow in current pkg to update pkg down, where the site is deployed to a branch called gh-pages +#' @template workflow_name +#' @inheritParams use_r_cmd_check +#' @examples +#' \dontrun{ +#' use_update_pkgdown( +#' additional_args = list( +#' ubuntu = c( +#' "sudo apt-get update", +#' "sudo apt-get install -y libcurl4-openssl-dev", +#' "sudo add-apt-repository ppa:ubuntu-toolchain-r/test", +#' "sudo apt-get install --only-upgrade libstdc++6" +#' ), +#' macos = c("brew install curl") +#' ) +#' ) +#' } +#' @export +use_update_pkgdown <- function(workflow_name = "call-update-pkgdown.yml", + additional_args = NULL) { + validate_additional_args(additional_args) + + check_workflow_name(workflow_name) + usethis::use_github_action("call-update-pkgdown.yml", + save_as = workflow_name, + url = "https://raw.githubusercontent.com/nmfs-fish-tools/ghactions4r/main/inst/templates/call-update-pkgdown.yml" + ) + + if (!is.null(additional_args)) { + add_args(workflow_name = workflow_name, additional_args = additional_args) + } +} + +#' use workflow in current pkg to check pkgdown site builds. +#' @template workflow_name +#' @inheritParams use_r_cmd_check +#' @examples +#' \dontrun{ +#' use_build_pkgdown( +#' additional_args = list( +#' ubuntu = c( +#' "sudo apt-get update", +#' "sudo apt-get install -y libcurl4-openssl-dev", +#' "sudo add-apt-repository ppa:ubuntu-toolchain-r/test", +#' "sudo apt-get install --only-upgrade libstdc++6" +#' ), +#' macos = c("brew install curl") +#' ) +#' ) +#' } +#' @export +use_build_pkgdown <- function(workflow_name = "call-build-pkgdown.yml", additional_args = NULL) { + validate_additional_args(additional_args) + + check_workflow_name(workflow_name) + usethis::use_github_action("call-build-pkgdown.yml", + save_as = workflow_name, + url = "https://raw.githubusercontent.com/nmfs-fish-tools/ghactions4r/main/inst/templates/call-build-pkgdown.yml" + ) + + if (!is.null(additional_args)) { + add_args(workflow_name = workflow_name, additional_args = additional_args) + } +} + +#' use workflow to run spelling::spell_check_package() +#' @template workflow_name +#' @return The path to the new github action file. +#' @export +use_spell_check <- function(workflow_name = "call-spell-check.yml") { + check_workflow_name(workflow_name) + usethis::use_github_action("call-spell-check.yml", + save_as = workflow_name, + url = "https://raw.githubusercontent.com/nmfs-fish-tools/ghactions4r/main/inst/templates/call-spell-check.yml" + ) + path_to_yml <- file.path(".github", "workflows", workflow_name) + return(path_to_yml) +} diff --git a/R/utils.R b/R/utils.R new file mode 100644 index 0000000..4b59182 --- /dev/null +++ b/R/utils.R @@ -0,0 +1,34 @@ +#' Add platform-specific additional arguments to a GitHub workflow YAML file +#' +#' @inheritParams use_r_cmd_check +#' @param txt A character vector of YAML file lines. If NULL, the function reads the YAML file +#' automatically. Default is NULL. +#' @param prev_line An integer specifying the line number in txt where the arguments +#' should be added. If NULL, the function determines this value automatically. Default is NULL. +add_args <- function(workflow_name, additional_args, txt = NULL, prev_line = NULL) { + path_to_yml <- file.path(".github", "workflows", workflow_name) + if (is.null(txt) | is.null(prev_line)) { + txt <- readLines(path_to_yml) + + prev_line <- grep( + paste0( + "uses: nmfs-fish-tools/ghactions4r/.github/workflows/", + gsub("call-", "", workflow_name), + "@main" + ), + txt, + fixed = TRUE + ) + txt <- append(txt, " with:", prev_line) + prev_line <- prev_line + 1 + } + + # Add platform-specific additional arguments + for (platform in c("windows", "macos", "ubuntu")) { + if (!is.null(additional_args[[platform]])) { + txt <- append(txt, paste0(" ", additional_args[[platform]]), prev_line) + txt <- append(txt, paste0(" additional_args_", platform, ": |"), prev_line) + } + } + writeLines(txt, path_to_yml) +} diff --git a/inst/WORDLIST b/inst/WORDLIST index d6b3a2a..51888fd 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -1,27 +1,30 @@ -CMD -Codecov -Lifecycle -NOAA -TMB -bookdown -calc -cmd -codecov -devtools -gh -ghactions -github -io -parsable -pkgdown -precompiled -readme -repo -repos -rspm -ss -styler -tmb -usethis -writeLines -yml +CMD +Codecov +Lifecycle +NOAA +TMB +bookdown +calc +cmd +codecov +devtools +gh +ghactions +github +io +macos +parsable +pkgdown +precompiled +readme +repo +repos +rspm +ss +styler +tmb +ubuntu +usethis +writeLines +YAML +yml diff --git a/man/add_args.Rd b/man/add_args.Rd new file mode 100644 index 0000000..f91b4ab --- /dev/null +++ b/man/add_args.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{add_args} +\alias{add_args} +\title{Add platform-specific additional arguments to a GitHub workflow YAML file} +\usage{ +add_args(workflow_name, additional_args, txt = NULL, prev_line = NULL) +} +\arguments{ +\item{workflow_name}{What to name the github action workflow locally. Must +have the file extension \code{.yml}} + +\item{additional_args}{A named list of additional command line arguments to be +passed to the workflow. The names of the list represent the platforms (windows, +macos, or ubuntu), and the values are character vectors of arguments. +These additional arguments are executed after the step that checks out the +repository and before the step that sets up R.} + +\item{txt}{A character vector of YAML file lines. If NULL, the function reads the YAML file +automatically. Default is NULL.} + +\item{prev_line}{An integer specifying the line number in txt where the arguments +should be added. If NULL, the function determines this value automatically. Default is NULL.} +} +\description{ +Add platform-specific additional arguments to a GitHub workflow YAML file +} diff --git a/man/use_build_pkgdown.Rd b/man/use_build_pkgdown.Rd index 5243e60..3718e4b 100644 --- a/man/use_build_pkgdown.Rd +++ b/man/use_build_pkgdown.Rd @@ -1,15 +1,39 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/use_r_workflows.R -\name{use_build_pkgdown} -\alias{use_build_pkgdown} -\title{use workflow in current pkg to check pkgdown site builds.} -\usage{ -use_build_pkgdown(workflow_name = "call-build-pkgdown.yml") -} -\arguments{ -\item{workflow_name}{What to name the github action workflow locally. Must -have the file extension \code{.yml}} -} -\description{ -use workflow in current pkg to check pkgdown site builds. -} +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/use_r_workflows.R +\name{use_build_pkgdown} +\alias{use_build_pkgdown} +\title{use workflow in current pkg to check pkgdown site builds.} +\usage{ +use_build_pkgdown( + workflow_name = "call-build-pkgdown.yml", + additional_args = NULL +) +} +\arguments{ +\item{workflow_name}{What to name the github action workflow locally. Must +have the file extension \code{.yml}} + +\item{additional_args}{A named list of additional command line arguments to be +passed to the workflow. The names of the list represent the platforms (windows, +macos, or ubuntu), and the values are character vectors of arguments. +These additional arguments are executed after the step that checks out the +repository and before the step that sets up R.} +} +\description{ +use workflow in current pkg to check pkgdown site builds. +} +\examples{ +\dontrun{ +use_build_pkgdown( + additional_args = list( + ubuntu = c( + "sudo apt-get update", + "sudo apt-get install -y libcurl4-openssl-dev", + "sudo add-apt-repository ppa:ubuntu-toolchain-r/test", + "sudo apt-get install --only-upgrade libstdc++6" + ), + macos = c("brew install curl") + ) +) +} +} diff --git a/man/use_r_cmd_check.Rd b/man/use_r_cmd_check.Rd index 4ac99c3..fcd32f3 100644 --- a/man/use_r_cmd_check.Rd +++ b/man/use_r_cmd_check.Rd @@ -1,27 +1,49 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/use_r_workflows.R -\name{use_r_cmd_check} -\alias{use_r_cmd_check} -\title{Use workflow to run r cmd check on Linux, Mac, and Windows GitHub Actions} -\usage{ -use_r_cmd_check( - workflow_name = "call-r-cmd-check.yml", - use_full_build_matrix = FALSE, - depends_on_tmb = FALSE -) -} -\arguments{ -\item{workflow_name}{What to name the github action workflow locally. Must -have the file extension \code{.yml}} - -\item{use_full_build_matrix}{Run R cmd check with two older versions of R in -addition to the three runs that use the release version.} - -\item{depends_on_tmb}{An option that install Matrix from source for windows -and Mac builds to solved a nuanced issue for packages dependent on TMB. -See this \href{https://groups.google.com/g/tmb-users/c/-GhmuuDP_OQ}{google groups thread} -for more information.} -} -\description{ -Use workflow to run r cmd check on Linux, Mac, and Windows GitHub Actions -} +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/use_r_workflows.R +\name{use_r_cmd_check} +\alias{use_r_cmd_check} +\title{Use workflow to run r cmd check on Linux, Mac, and Windows GitHub Actions} +\usage{ +use_r_cmd_check( + workflow_name = "call-r-cmd-check.yml", + use_full_build_matrix = FALSE, + depends_on_tmb = FALSE, + additional_args = NULL +) +} +\arguments{ +\item{workflow_name}{What to name the github action workflow locally. Must +have the file extension \code{.yml}} + +\item{use_full_build_matrix}{Run R cmd check with two older versions of R in +addition to the three runs that use the release version.} + +\item{depends_on_tmb}{An option that install Matrix from source for windows +and Mac builds to solved a nuanced issue for packages dependent on TMB. +See this \href{https://groups.google.com/g/tmb-users/c/-GhmuuDP_OQ}{google groups thread} +for more information.} + +\item{additional_args}{A named list of additional command line arguments to be +passed to the workflow. The names of the list represent the platforms (windows, +macos, or ubuntu), and the values are character vectors of arguments. +These additional arguments are executed after the step that checks out the +repository and before the step that sets up R.} +} +\description{ +Use workflow to run r cmd check on Linux, Mac, and Windows GitHub Actions +} +\examples{ +\dontrun{ +use_r_cmd_check( + additional_args = list( + ubuntu = c( + "sudo apt-get update", + "sudo apt-get install -y libcurl4-openssl-dev", + "sudo add-apt-repository ppa:ubuntu-toolchain-r/test", + "sudo apt-get install --only-upgrade libstdc++6" + ), + macos = c("brew install curl") + ) +) +} +} diff --git a/man/use_update_pkgdown.Rd b/man/use_update_pkgdown.Rd index 1c2083d..12f6e78 100644 --- a/man/use_update_pkgdown.Rd +++ b/man/use_update_pkgdown.Rd @@ -1,15 +1,39 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/use_r_workflows.R -\name{use_update_pkgdown} -\alias{use_update_pkgdown} -\title{use workflow in current pkg to update pkg down, where the site is deployed to a branch called gh-pages} -\usage{ -use_update_pkgdown(workflow_name = "call-update-pkgdown.yml") -} -\arguments{ -\item{workflow_name}{What to name the github action workflow locally. Must -have the file extension \code{.yml}} -} -\description{ -use workflow in current pkg to update pkg down, where the site is deployed to a branch called gh-pages -} +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/use_r_workflows.R +\name{use_update_pkgdown} +\alias{use_update_pkgdown} +\title{use workflow in current pkg to update pkg down, where the site is deployed to a branch called gh-pages} +\usage{ +use_update_pkgdown( + workflow_name = "call-update-pkgdown.yml", + additional_args = NULL +) +} +\arguments{ +\item{workflow_name}{What to name the github action workflow locally. Must +have the file extension \code{.yml}} + +\item{additional_args}{A named list of additional command line arguments to be +passed to the workflow. The names of the list represent the platforms (windows, +macos, or ubuntu), and the values are character vectors of arguments. +These additional arguments are executed after the step that checks out the +repository and before the step that sets up R.} +} +\description{ +use workflow in current pkg to update pkg down, where the site is deployed to a branch called gh-pages +} +\examples{ +\dontrun{ +use_update_pkgdown( + additional_args = list( + ubuntu = c( + "sudo apt-get update", + "sudo apt-get install -y libcurl4-openssl-dev", + "sudo add-apt-repository ppa:ubuntu-toolchain-r/test", + "sudo apt-get install --only-upgrade libstdc++6" + ), + macos = c("brew install curl") + ) +) +} +} diff --git a/man/validate_additional_args.Rd b/man/validate_additional_args.Rd new file mode 100644 index 0000000..6e2b976 --- /dev/null +++ b/man/validate_additional_args.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/input_checks.R +\name{validate_additional_args} +\alias{validate_additional_args} +\title{Validate additional arguments for R functions} +\usage{ +validate_additional_args(additional_args) +} +\arguments{ +\item{additional_args}{A named list of additional command line arguments to be +passed to the workflow. The names of the list represent the platforms (windows, +macos, or ubuntu), and the values are character vectors of arguments. +These additional arguments are executed after the step that checks out the +repository and before the step that sets up R.} +} +\description{ +Validate additional arguments for R functions +} diff --git a/tests/testthat/_snaps/use_r_workflows.md b/tests/testthat/_snaps/use_r_workflows.md index 30769de..7bc97c0 100644 --- a/tests/testthat/_snaps/use_r_workflows.md +++ b/tests/testthat/_snaps/use_r_workflows.md @@ -58,7 +58,55 @@ [13] " uses: nmfs-fish-tools/ghactions4r/.github/workflows/r-cmd-check.yml@main" [14] " with:" [15] " use_full_build_matrix: true" - [16] " depends_on_tmb: true" + +# use_r_cmd_check() works with tmb + + Code + test + Output + [1] "# Run r cmd check" + [2] "name: call-r-cmd-check" + [3] "# on specifies the build triggers. See more info at https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows" + [4] "on:" + [5] "# The default build trigger is to run the action on every push and pull request, for any branch" + [6] " push:" + [7] " pull_request:" + [8] " # To run the default repository branch weekly on sunday, uncomment the following 2 lines" + [9] " #schedule:" + [10] " #- cron: '0 0 * * 0'" + [11] "jobs:" + [12] " call-workflow:" + [13] " uses: nmfs-fish-tools/ghactions4r/.github/workflows/r-cmd-check.yml@main" + +# use_r_cmd_check() works with additional_args + + Code + test + Output + [1] "# Run r cmd check" + [2] "name: call-r-cmd-check" + [3] "# on specifies the build triggers. See more info at https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows" + [4] "on:" + [5] "# The default build trigger is to run the action on every push and pull request, for any branch" + [6] " push:" + [7] " pull_request:" + [8] " # To run the default repository branch weekly on sunday, uncomment the following 2 lines" + [9] " #schedule:" + [10] " #- cron: '0 0 * * 0'" + [11] "jobs:" + [12] " call-workflow:" + [13] " uses: nmfs-fish-tools/ghactions4r/.github/workflows/r-cmd-check.yml@main" + [14] " with:" + [15] " additional_args_ubuntu: |" + [16] " sudo apt-get update" + [17] " sudo apt-get install -y libcurl4-openssl-dev" + [18] " sudo add-apt-repository ppa:ubuntu-toolchain-r/test" + [19] " sudo apt-get install --only-upgrade libstdc++6" + [20] " additional_args_macos: |" + [21] " brew install curl" + [22] " additional_args_windows: |" + [23] " tree" + [24] " depends_on_tmb: true" --- @@ -79,7 +127,9 @@ [12] " call-workflow:" [13] " uses: nmfs-fish-tools/ghactions4r/.github/workflows/r-cmd-check.yml@main" [14] " with:" - [15] " depends_on_tmb: true" + [15] " use_full_build_matrix: true" + [16] " additional_args_macos: |" + [17] " brew install curl" # use_calc_coverage() works @@ -217,7 +267,35 @@ [11] " call-workflow:" [12] " uses: nmfs-fish-tools/ghactions4r/.github/workflows/update-pkgdown.yml@main" -# use_build_pkgdown()) works +# use_update_pkgdown()) works with additional_args + + Code + test + Output + [1] "# updates exiting pkgdown site for a repository" + [2] "# deploys to a branch gh-pages" + [3] "name: call-update-pkgdown" + [4] "# on specifies the build triggers. See more info at https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows" + [5] "on:" + [6] "# this workflow runs on pushes to main or master or any time a new tag is pushed" + [7] " push:" + [8] " branches: [main, master]" + [9] " tags: ['*']" + [10] "jobs:" + [11] " call-workflow:" + [12] " uses: nmfs-fish-tools/ghactions4r/.github/workflows/update-pkgdown.yml@main" + [13] " with:" + [14] " additional_args_ubuntu: |" + [15] " sudo apt-get update" + [16] " sudo apt-get install -y libcurl4-openssl-dev" + [17] " sudo add-apt-repository ppa:ubuntu-toolchain-r/test" + [18] " sudo apt-get install --only-upgrade libstdc++6" + [19] " additional_args_macos: |" + [20] " brew install curl" + [21] " additional_args_windows: |" + [22] " tree" + +# use_build_pkgdown()) works with additional_args Code test @@ -237,6 +315,16 @@ [13] "jobs:" [14] " call-workflow:" [15] " uses: nmfs-fish-tools/ghactions4r/.github/workflows/build-pkgdown.yml@main" + [16] " with:" + [17] " additional_args_ubuntu: |" + [18] " sudo apt-get update" + [19] " sudo apt-get install -y libcurl4-openssl-dev" + [20] " sudo add-apt-repository ppa:ubuntu-toolchain-r/test" + [21] " sudo apt-get install --only-upgrade libstdc++6" + [22] " additional_args_macos: |" + [23] " brew install curl" + [24] " additional_args_windows: |" + [25] " tree" # use_spell_check() works diff --git a/tests/testthat/_snaps/utils.md b/tests/testthat/_snaps/utils.md new file mode 100644 index 0000000..6687167 --- /dev/null +++ b/tests/testthat/_snaps/utils.md @@ -0,0 +1,54 @@ +# add_args() works with additional_args + + Code + test + Output + [1] "# updates exiting pkgdown site for a repository" + [2] "# deploys to a branch gh-pages" + [3] "name: call-update-pkgdown" + [4] "# on specifies the build triggers. See more info at https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows" + [5] "on:" + [6] "# this workflow runs on pushes to main or master or any time a new tag is pushed" + [7] " push:" + [8] " branches: [main, master]" + [9] " tags: ['*']" + [10] "jobs:" + [11] " call-workflow:" + [12] " uses: nmfs-fish-tools/ghactions4r/.github/workflows/update-pkgdown.yml@main" + [13] " with:" + [14] " additional_args_ubuntu: |" + [15] " sudo apt-get update" + [16] " sudo apt-get install -y libcurl4-openssl-dev" + [17] " additional_args_macos: |" + [18] " brew install curl" + [19] " additional_args_windows: |" + [20] " tree" + +# add_args() works with txt and prev_line + + Code + test + Output + [1] "# updates exiting pkgdown site for a repository" + [2] "# deploys to a branch gh-pages" + [3] "name: call-update-pkgdown" + [4] "# on specifies the build triggers. See more info at https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows" + [5] "on:" + [6] "# this workflow runs on pushes to main or master or any time a new tag is pushed" + [7] " push:" + [8] " branches: [main, master]" + [9] " tags: ['*']" + [10] "jobs:" + [11] " call-workflow:" + [12] " uses: nmfs-fish-tools/ghactions4r/.github/workflows/update-pkgdown.yml@main" + [13] " additional_args_ubuntu: |" + [14] " sudo apt-get install --only-upgrade libstdc++6" + [15] " with:" + [16] " additional_args_ubuntu: |" + [17] " sudo apt-get update" + [18] " sudo apt-get install -y libcurl4-openssl-dev" + [19] " additional_args_macos: |" + [20] " brew install curl" + [21] " additional_args_windows: |" + [22] " tree" + diff --git a/tests/testthat/test-use_r_workflows.R b/tests/testthat/test-use_r_workflows.R index 48cd684..867e4a9 100644 --- a/tests/testthat/test-use_r_workflows.R +++ b/tests/testthat/test-use_r_workflows.R @@ -1,159 +1,253 @@ -temp_path <- file.path(tempdir(), "rcmdcheck") -dir.create(temp_path) -old_wd <- getwd() -setwd(temp_path) -on.exit(setwd(old_wd), add = TRUE) -on.exit(unlink(temp_path, recursive = TRUE), add = TRUE) -# comment out below b/c causes an error when running check on codespaces -# on.exit(usethis::proj_set("."), add = TRUE) -pkg <- usethis::create_package(".") -usethis::proj_set(".") - -test_that("use_r_cmd_check() works with defaults", { - use_r_cmd_check() - expect_true(file.exists(".github/workflows/call-r-cmd-check.yml")) - expect_true(file.exists(".github/.gitignore")) - test <- readLines(".github/workflows/call-r-cmd-check.yml") - expect_snapshot(test) -}) - -test_that("use_r_cmd_check() works with full build option", { - name <- "call-full-build-check.yml" - path <- file.path(".github", "workflows", name) - use_r_cmd_check(workflow_name = name, use_full_build_matrix = TRUE) - expect_true(file.exists(path)) - test <- readLines(path) - expect_snapshot(test) -}) - -test_that("use_r_cmd_check() works with full build option and tmb", { - name <- "call-full-build-check.yml" - path <- file.path(".github", "workflows", name) - use_r_cmd_check(workflow_name = name, use_full_build_matrix = TRUE, depends_on_tmb = TRUE) - expect_true(file.exists(path)) - test <- readLines(path) - expect_snapshot(test) -}) - -test_that("use_r_cmd_check() works with full build option and tmb", { - name <- "call-full-tmb.yml" - path <- file.path(".github", "workflows", name) - use_r_cmd_check(workflow_name = name, use_full_build_matrix = FALSE, depends_on_tmb = TRUE) - expect_true(file.exists(path)) - test <- readLines(path) - expect_snapshot(test) -}) - -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", - build_trigger = "pull_request" - ) - expect_true(file.exists(".github/workflows/call-doc-and-style-r.yml")) - test <- readLines(".github/workflows/call-doc-and-style-r.yml") - expect_length(grep("run-rm_dollar_sign: true", test, fixed = TRUE), 1) - expect_length(grep("commit-directly: true", test, fixed = TRUE), 1) - expect_length(grep("pull_request:", test, fixed = TRUE), 1) - expect_snapshot(test) -}) - -test_that("use_doc_and_style_r() works with other options", { - use_doc_and_style_r( - workflow_name = "doc_style_cron.yml", - use_rm_dollar_sign = FALSE, how_to_commit = "pull_request", - build_trigger = "weekly" - ) - expect_true(file.exists(".github/workflows/doc_style_cron.yml")) - test <- readLines(".github/workflows/doc_style_cron.yml") - expect_length(grep("run-rm_dollar_sign: true", test, fixed = TRUE), 0) - expect_length(grep("commit-directly: true", test, fixed = TRUE), 0) - expect_length(grep("schedule:", test, fixed = TRUE), 1) - expect_length(grep("cron:", test, fixed = TRUE), 1) - expect_snapshot(test) -}) - -test_that("use_doc_and_style_r() works with push to main", { - use_doc_and_style_r( - workflow_name = "doc_style_main.yml", - use_rm_dollar_sign = FALSE, how_to_commit = "pull_request", - build_trigger = "push_to_main" - ) - expect_true(file.exists(".github/workflows/doc_style_main.yml")) - test <- readLines(".github/workflows/doc_style_main.yml") - expect_length(grep("run-rm_dollar_sign: true", test, fixed = TRUE), 0) - expect_length(grep("commit-directly: true", test, fixed = TRUE), 0) - expect_length(grep("branches:", test, fixed = TRUE), 1) - expect_length(grep("push:", test, fixed = TRUE), 1) - expect_snapshot(test) -}) - -test_that("use_doc_and_style_r() works with manual trigger", { - use_doc_and_style_r( - workflow_name = "doc_style_manual.yml", - use_rm_dollar_sign = FALSE, how_to_commit = "pull_request", - build_trigger = "manual" - ) - expect_true(file.exists(".github/workflows/doc_style_manual.yml")) - test <- readLines(".github/workflows/doc_style_manual.yml") - expect_length(grep("run-rm_dollar_sign: true", test, fixed = TRUE), 0) - expect_length(grep("commit-directly: true", test, fixed = TRUE), 0) - expect_true(length(grep("workflow_dispatch:", test, fixed = TRUE)) > 0) - expect_snapshot(test) -}) - -test_that("use_doc_and_style_r() works with pat option", { - use_doc_and_style_r( - workflow_name = "doc_style_pat.yml", - use_rm_dollar_sign = FALSE, how_to_commit = "pull_request", - use_pat = TRUE, pat_name = "MYPAT" - ) - expect_true(file.exists(".github/workflows/doc_style_pat.yml")) - test <- readLines(".github/workflows/doc_style_pat.yml") - expect_length(grep("secrets.MYPAT", test, fixed = TRUE), 1) - expect_length(grep("^ secrets:", test), 1) - expect_snapshot(test) -}) - -test_that("use_doc_and_style_r() errors when a bad combo", { - expect_error(use_doc_and_style_r( - workflow_name = "doc_style_bad.yml", - how_to_commit = "directly", - use_pat = TRUE - ), "recursive") -}) - -test_that("use_update_pkgdown()) works", { - use_update_pkgdown() - expect_true(file.exists(".github/workflows/call-update-pkgdown.yml")) - test <- readLines(".github/workflows/call-update-pkgdown.yml") - expect_snapshot(test) -}) - -test_that("use_build_pkgdown()) works", { - use_build_pkgdown() - expect_true(file.exists(".github/workflows/call-build-pkgdown.yml")) - test <- readLines(".github/workflows/call-build-pkgdown.yml") - expect_snapshot(test) -}) - -test_that("use_spell_check() works", { - use_spell_check() - expect_true(file.exists(".github/workflows/call-spell-check.yml")) - test <- readLines(".github/workflows/call-spell-check.yml") - expect_snapshot(test) -}) +temp_path <- file.path(tempdir(), "rcmdcheck") +dir.create(temp_path) +old_wd <- getwd() +setwd(temp_path) +on.exit(setwd(old_wd), add = TRUE) +on.exit(unlink(temp_path, recursive = TRUE), add = TRUE) +# comment out below b/c causes an error when running check on codespaces +# on.exit(usethis::proj_set("."), add = TRUE) +pkg <- usethis::create_package(".") +usethis::proj_set(".") + +test_that("use_r_cmd_check() works with defaults", { + use_r_cmd_check() + expect_true(file.exists(".github/workflows/call-r-cmd-check.yml")) + expect_true(file.exists(".github/.gitignore")) + test <- readLines(".github/workflows/call-r-cmd-check.yml") + expect_snapshot(test) +}) + +test_that("use_r_cmd_check() works with full build option", { + name <- "call-full-build-check.yml" + path <- file.path(".github", "workflows", name) + use_r_cmd_check(workflow_name = name, use_full_build_matrix = TRUE) + expect_true(file.exists(path)) + test <- readLines(path) + expect_snapshot(test) +}) + +test_that("use_r_cmd_check() works with full build option and tmb", { + name <- "call-full-build-check.yml" + path <- file.path(".github", "workflows", name) + use_r_cmd_check(workflow_name = name, use_full_build_matrix = TRUE, depends_on_tmb = TRUE) + expect_true(file.exists(path)) + test <- readLines(path) + expect_snapshot(test) +}) + +test_that("use_r_cmd_check() works with tmb", { + name <- "call-full-tmb.yml" + path <- file.path(".github", "workflows", name) + use_r_cmd_check(workflow_name = name, use_full_build_matrix = FALSE, depends_on_tmb = TRUE) + expect_true(file.exists(path)) + test <- readLines(path) + expect_snapshot(test) +}) + +test_that("use_r_cmd_check() works with additional_args", { + name <- "call-rcmd-additional-args-tmb.yml" + path <- file.path(".github", "workflows", name) + use_r_cmd_check( + workflow_name = name, + use_full_build_matrix = FALSE, + depends_on_tmb = TRUE, + additional_args = list( + "ubuntu" = c( + "sudo apt-get update", + "sudo apt-get install -y libcurl4-openssl-dev", + "sudo add-apt-repository ppa:ubuntu-toolchain-r/test", + "sudo apt-get install --only-upgrade libstdc++6" + ), + "windows" = c("tree"), + "macos" = c("brew install curl") + ) + ) + expect_true(file.exists(path)) + test <- readLines(path) + expect_snapshot(test) + + name <- "call-rcmd-additional-args-full.yml" + path <- file.path(".github", "workflows", name) + use_r_cmd_check( + workflow_name = name, + use_full_build_matrix = TRUE, + depends_on_tmb = FALSE, + additional_args = list( + "macos" = c("brew install curl") + ) + ) + expect_true(file.exists(path)) + test <- readLines(path) + expect_snapshot(test) +}) + +test_that("use_r_cmd_check() doesn't work with invalid additional_args", { + # Invalid inputs: not a list + expect_error( + use_r_cmd_check(additional_args = "brew install curl"), + regexp = "must be a named list" + ) + + # Invalid inputs: list without names + expect_error( + use_r_cmd_check(additional_args = list("brew install curl")), + regexp = "Invalid platform in" + ) + + # Invalid inputs: list with invalid names + expect_error( + use_r_cmd_check(additional_args = list(linux = "sudo apt-get update")), + regexp = "Invalid platform in" + ) + + # Invalid inputs: list with non-character values + expect_error( + use_r_cmd_check(additional_args = list(windows = 123)), + regexp = "must be character vectors" + ) +}) + +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", + build_trigger = "pull_request" + ) + expect_true(file.exists(".github/workflows/call-doc-and-style-r.yml")) + test <- readLines(".github/workflows/call-doc-and-style-r.yml") + expect_length(grep("run-rm_dollar_sign: true", test, fixed = TRUE), 1) + expect_length(grep("commit-directly: true", test, fixed = TRUE), 1) + expect_length(grep("pull_request:", test, fixed = TRUE), 1) + expect_snapshot(test) +}) + +test_that("use_doc_and_style_r() works with other options", { + use_doc_and_style_r( + workflow_name = "doc_style_cron.yml", + use_rm_dollar_sign = FALSE, how_to_commit = "pull_request", + build_trigger = "weekly" + ) + expect_true(file.exists(".github/workflows/doc_style_cron.yml")) + test <- readLines(".github/workflows/doc_style_cron.yml") + expect_length(grep("run-rm_dollar_sign: true", test, fixed = TRUE), 0) + expect_length(grep("commit-directly: true", test, fixed = TRUE), 0) + expect_length(grep("schedule:", test, fixed = TRUE), 1) + expect_length(grep("cron:", test, fixed = TRUE), 1) + expect_snapshot(test) +}) + +test_that("use_doc_and_style_r() works with push to main", { + use_doc_and_style_r( + workflow_name = "doc_style_main.yml", + use_rm_dollar_sign = FALSE, how_to_commit = "pull_request", + build_trigger = "push_to_main" + ) + expect_true(file.exists(".github/workflows/doc_style_main.yml")) + test <- readLines(".github/workflows/doc_style_main.yml") + expect_length(grep("run-rm_dollar_sign: true", test, fixed = TRUE), 0) + expect_length(grep("commit-directly: true", test, fixed = TRUE), 0) + expect_length(grep("branches:", test, fixed = TRUE), 1) + expect_length(grep("push:", test, fixed = TRUE), 1) + expect_snapshot(test) +}) + +test_that("use_doc_and_style_r() works with manual trigger", { + use_doc_and_style_r( + workflow_name = "doc_style_manual.yml", + use_rm_dollar_sign = FALSE, how_to_commit = "pull_request", + build_trigger = "manual" + ) + expect_true(file.exists(".github/workflows/doc_style_manual.yml")) + test <- readLines(".github/workflows/doc_style_manual.yml") + expect_length(grep("run-rm_dollar_sign: true", test, fixed = TRUE), 0) + expect_length(grep("commit-directly: true", test, fixed = TRUE), 0) + expect_true(length(grep("workflow_dispatch:", test, fixed = TRUE)) > 0) + expect_snapshot(test) +}) + +test_that("use_doc_and_style_r() works with pat option", { + use_doc_and_style_r( + workflow_name = "doc_style_pat.yml", + use_rm_dollar_sign = FALSE, how_to_commit = "pull_request", + use_pat = TRUE, pat_name = "MYPAT" + ) + expect_true(file.exists(".github/workflows/doc_style_pat.yml")) + test <- readLines(".github/workflows/doc_style_pat.yml") + expect_length(grep("secrets.MYPAT", test, fixed = TRUE), 1) + expect_length(grep("^ secrets:", test), 1) + expect_snapshot(test) +}) + +test_that("use_doc_and_style_r() errors when a bad combo", { + expect_error(use_doc_and_style_r( + workflow_name = "doc_style_bad.yml", + how_to_commit = "directly", + use_pat = TRUE + ), "recursive") +}) + +test_that("use_update_pkgdown()) works", { + use_update_pkgdown() + expect_true(file.exists(".github/workflows/call-update-pkgdown.yml")) + test <- readLines(".github/workflows/call-update-pkgdown.yml") + expect_snapshot(test) +}) + +test_that("use_update_pkgdown()) works with additional_args", { + use_update_pkgdown( + workflow_name = "call-update-pkgdown.yml", + additional_args = list( + "ubuntu" = c( + "sudo apt-get update", + "sudo apt-get install -y libcurl4-openssl-dev", + "sudo add-apt-repository ppa:ubuntu-toolchain-r/test", + "sudo apt-get install --only-upgrade libstdc++6" + ), + "windows" = c("tree"), + "macos" = c("brew install curl") + ) + ) + expect_true(file.exists(".github/workflows/call-update-pkgdown.yml")) + test <- readLines(".github/workflows/call-update-pkgdown.yml") + expect_snapshot(test) +}) + +test_that("use_build_pkgdown()) works with additional_args", { + use_build_pkgdown( + workflow_name = "call-build-pkgdown.yml", + additional_args = list( + "ubuntu" = c( + "sudo apt-get update", + "sudo apt-get install -y libcurl4-openssl-dev", + "sudo add-apt-repository ppa:ubuntu-toolchain-r/test", + "sudo apt-get install --only-upgrade libstdc++6" + ), + "windows" = c("tree"), + "macos" = c("brew install curl") + ) + ) + expect_true(file.exists(".github/workflows/call-build-pkgdown.yml")) + test <- readLines(".github/workflows/call-build-pkgdown.yml") + expect_snapshot(test) +}) + +test_that("use_spell_check() works", { + use_spell_check() + expect_true(file.exists(".github/workflows/call-spell-check.yml")) + test <- readLines(".github/workflows/call-spell-check.yml") + expect_snapshot(test) +}) diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R new file mode 100644 index 0000000..b7e9a55 --- /dev/null +++ b/tests/testthat/test-utils.R @@ -0,0 +1,52 @@ +temp_path <- file.path(tempdir(), "rcmdcheck") +dir.create(temp_path) +old_wd <- getwd() +setwd(temp_path) +on.exit(setwd(old_wd), add = TRUE) +on.exit(unlink(temp_path, recursive = TRUE), add = TRUE) +# comment out below b/c causes an error when running check on codespaces +# on.exit(usethis::proj_set("."), add = TRUE) +pkg <- usethis::create_package(".") +usethis::proj_set(".") + +test_that("add_args() works with additional_args", { + use_update_pkgdown() + workflow_name <- "call-update-pkgdown.yml" + additional_args <- list( + ubuntu = c("sudo apt-get update", + "sudo apt-get install -y libcurl4-openssl-dev"), + windows = c("tree"), + macos = c("brew install curl") + ) + + add_args(workflow_name = workflow_name, + additional_args = additional_args) + path <- file.path(".github", "workflows", workflow_name) + expect_true(file.exists(path)) + test <- readLines(path) + expect_snapshot(test) +}) + +test_that("add_args() works with txt and prev_line", { + use_update_pkgdown() + workflow_name <- "call-update-pkgdown.yml" + path <- file.path(".github", "workflows", workflow_name) + + txt <- readLines(path) + prev_line <- grep( + paste0( + "uses: nmfs-fish-tools/ghactions4r/.github/workflows/", + gsub("call-", "", workflow_name), + "@main" + ), + txt, + fixed = TRUE + ) + add_args(workflow_name = workflow_name, + additional_args = list(ubuntu = "sudo apt-get install --only-upgrade libstdc++6"), + txt = txt, + prev_line = prev_line) + expect_true(file.exists(path)) + test <- readLines(path) + expect_snapshot(test) +}) \ No newline at end of file