diff --git a/R/aaa-shared_params.R b/R/aaa-shared_params.R index a3c0675..7befacc 100644 --- a/R/aaa-shared_params.R +++ b/R/aaa-shared_params.R @@ -25,7 +25,6 @@ #' @param endpoint (`character(1)`) The operation endpoint. #' @param endpoints (`character`) Endpoint paths paired with operations. #' @param filter_in (`character(1)`) The parameter location to keep. -#' @param include_stbl (`logical(1)`) Whether to add `stbl` to Imports. #' @param methods (`character`) HTTP methods paired with endpoints. #' @param operation (`character(1)`) The HTTP method. #' @param operation_description (`character(1)`) The operation description. diff --git a/R/generate_pkg-setup.R b/R/generate_pkg-setup.R index f52873f..b960aad 100644 --- a/R/generate_pkg-setup.R +++ b/R/generate_pkg-setup.R @@ -33,16 +33,47 @@ #' @inheritParams .shared-params #' @returns (`NULL`, invisibly) Called for setup side effects. #' @keywords internal -.setup_r <- function(pkg_dir, include_stbl = FALSE) { - if (as.character(pkg_dir) != ".") { - usethis::local_project(pkg_dir, quiet = TRUE) # nocov - } - usethis::use_directory("R") - usethis::use_testthat() - purrr::quietly(httptest2::use_httptest2)() - usethis::use_package("nectar") - if (include_stbl) { - usethis::use_package("stbl") +.setup_r <- function(pkg_dir) { + usethis::with_project( + pkg_dir, + { + usethis::use_directory("R") + withr::with_options(list(usethis.quiet = TRUE), usethis::use_testthat()) + purrr::quietly(httptest2::use_httptest2)() + }, + quiet = TRUE + ) + .use_package("nectar", "Imports", pkg_dir) + .use_package("beekeeper", "Suggests", pkg_dir) +} + +#' Add a package dependency to the DESCRIPTION file +#' +#' @param pkg (`character(1)`) The package name. +#' @param type (`character(1)`) The dependency type, one of "Imports", +#' "Suggests", or "Depends". +#' @inheritParams .shared-params +#' @returns (`character(1)`, invisibly) The package name. +#' @keywords internal +.use_package <- function(pkg, type = "Imports", pkg_dir = ".") { + usethis::with_project( + pkg_dir, + { + usethis::use_package(pkg, type = type) + }, + quiet = TRUE + ) + invisible(pkg) +} + +#' Add stbl to dependencies if needed +#' +#' @inheritParams .shared-params +#' @returns (`character(1)` or `NULL`, invisibly) "stbl" if stbl is used, `NULL` +#' otherwise. +#' @keywords internal +.maybe_use_stbl <- function(pkg_dir, paths, security_arg_names) { + if (.paths_need_stbl(paths, security_arg_names)) { + .use_package("stbl", "Imports", pkg_dir) } - usethis::use_package("beekeeper", type = "Suggests") } diff --git a/R/generate_pkg.R b/R/generate_pkg.R index 42ffb63..3e50785 100644 --- a/R/generate_pkg.R +++ b/R/generate_pkg.R @@ -32,10 +32,8 @@ generate_pkg <- function( api_definition@components@security_schemes ) security_arg_names <- security_data$security_arg_names %|0|% character() - .setup_r( - pkg_dir, - include_stbl = .paths_need_stbl(api_definition@paths, security_arg_names) - ) + .setup_r(pkg_dir) + .maybe_use_stbl(pkg_dir, api_definition@paths, security_arg_names) touched_files <- .generate_pkg_impl(config, api_definition, security_data) return(invisible(touched_files)) } diff --git a/man/dot-maybe_use_stbl.Rd b/man/dot-maybe_use_stbl.Rd new file mode 100644 index 0000000..8f679ad --- /dev/null +++ b/man/dot-maybe_use_stbl.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/generate_pkg-setup.R +\name{.maybe_use_stbl} +\alias{.maybe_use_stbl} +\title{Add stbl to dependencies if needed} +\usage{ +.maybe_use_stbl(pkg_dir, paths, security_arg_names) +} +\arguments{ +\item{pkg_dir}{(\code{character(1)} or \code{fs_path}) The directory containing package +files.} + +\item{paths}{(\code{rapid::class_paths}) API path definitions.} + +\item{security_arg_names}{(\code{character}) Security argument names to exclude +from generated operation signatures (because they are defined once, +separately).} +} +\value{ +(\code{character(1)} or \code{NULL}, invisibly) "stbl" if stbl is used, \code{NULL} +otherwise. +} +\description{ +Add stbl to dependencies if needed +} +\keyword{internal} diff --git a/man/dot-setup_r.Rd b/man/dot-setup_r.Rd index 2aba67e..413ab74 100644 --- a/man/dot-setup_r.Rd +++ b/man/dot-setup_r.Rd @@ -4,13 +4,11 @@ \alias{.setup_r} \title{Set up package directories and dependencies} \usage{ -.setup_r(pkg_dir, include_stbl = FALSE) +.setup_r(pkg_dir) } \arguments{ \item{pkg_dir}{(\code{character(1)} or \code{fs_path}) The directory containing package files.} - -\item{include_stbl}{(\code{logical(1)}) Whether to add \code{stbl} to Imports.} } \value{ (\code{NULL}, invisibly) Called for setup side effects. diff --git a/man/dot-shared-params.Rd b/man/dot-shared-params.Rd index 3193049..7ff0906 100644 --- a/man/dot-shared-params.Rd +++ b/man/dot-shared-params.Rd @@ -41,8 +41,6 @@ written.} \item{filter_in}{(\code{character(1)}) The parameter location to keep.} -\item{include_stbl}{(\code{logical(1)}) Whether to add \code{stbl} to Imports.} - \item{methods}{(\code{character}) HTTP methods paired with endpoints.} \item{operation}{(\code{character(1)}) The HTTP method.} diff --git a/man/dot-use_package.Rd b/man/dot-use_package.Rd new file mode 100644 index 0000000..ceb7314 --- /dev/null +++ b/man/dot-use_package.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/generate_pkg-setup.R +\name{.use_package} +\alias{.use_package} +\title{Add a package dependency to the DESCRIPTION file} +\usage{ +.use_package(pkg, type = "Imports", pkg_dir = ".") +} +\arguments{ +\item{pkg}{(\code{character(1)}) The package name.} + +\item{type}{(\code{character(1)}) The dependency type, one of "Imports", +"Suggests", or "Depends".} + +\item{pkg_dir}{(\code{character(1)} or \code{fs_path}) The directory containing package +files.} +} +\value{ +(\code{character(1)}, invisibly) The package name. +} +\description{ +Add a package dependency to the DESCRIPTION file +} +\keyword{internal} diff --git a/tests/testthat/test-generate_pkg-setup.R b/tests/testthat/test-generate_pkg-setup.R index cdd213a..8861ad2 100644 --- a/tests/testthat/test-generate_pkg-setup.R +++ b/tests/testthat/test-generate_pkg-setup.R @@ -38,14 +38,25 @@ test_that(".setup_r() sets up dependencies (#16)", { ) }) -test_that(".setup_r() can include stbl in imports (#69)", { - skip_on_cran() - - create_local_package() - .setup_r(".", include_stbl = TRUE) - - dependencies <- desc::desc()$get_deps() - imports <- dependencies$package[dependencies$type == "Imports"] - expect_contains(imports, "nectar") - expect_contains(imports, "stbl") +test_that(".maybe_use_stbl() can add stbl to imports (#69)", { + local_mocked_bindings( + .use_package = function(pkg, type, pkg_dir) { + if (pkg == "stbl") { + expect_equal(type, "Imports") + expect_equal(pkg_dir, "DIR") + } + return(pkg) + }, + .paths_need_stbl = function(paths, security_arg_names) { + expect_equal(security_arg_names, "SECURITY_ARG_NAMES") + return(paths == "INSTALL") + } + ) + expect_equal( + .maybe_use_stbl("DIR", "INSTALL", "SECURITY_ARG_NAMES"), + "stbl" + ) + expect_null( + .maybe_use_stbl("DIR", "DONOTINSTALL", "SECURITY_ARG_NAMES") + ) })