Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export(use_rmarkdown_template)
export(use_roxygen_md)
export(use_rstudio)
export(use_rstudio_preferences)
export(use_snapshot)
export(use_spell_check)
export(use_standalone)
export(use_template)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# usethis (development version)

* `pr_resume()` (without a specific `branch`) and `pr_fetch()` (without a specific `number`) no longer error when a branch name contains curly braces (#2107, @jonthegeek).
* `use_snapshot()` is a new function to open a [testthat snapshot file]() corresponding to a given test file or R file (#2156, @jonthegeek).

# usethis 3.2.1

Expand Down
39 changes: 38 additions & 1 deletion R/r.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,39 @@ use_test <- function(name = NULL, open = rlang::is_interactive()) {
invisible(TRUE)
}

#' Edit the snapshot file associated with an R or test file
#'
#' This function opens the snapshot file associated with an R or test file, if
#' one exists.
#'
#' @inheritParams use_r
#' @param variant An optional subdirectory within the `_snaps/` directory.
#' @export
use_snapshot <- function(
name = NULL,
variant = NULL,
open = rlang::is_interactive()
) {
if (!uses_testthat()) {
use_testthat_impl()
}

path_root <- proj_path("tests", "testthat", "_snaps")
if (!is.null(variant)) {
path_root <- path(path_root, variant)
}
# I can't pass "md" to `compute_name()` because we're fine with them giving us
# "R" as the extension here.
path <- path(path_root, fs::path_ext_set(compute_name(name), "md"))

if (!file_exists(path)) {
cli::cli_abort("No snapshot file exists for {.arg {name}}.")
}
edit_file(path, open = open)

invisible(TRUE)
}

#' Create or edit a test helper file
#'
#' This function creates (or opens) a test helper file, typically
Expand Down Expand Up @@ -183,7 +216,11 @@ compute_active_name <- function(path, ext, error_call = caller_env()) {
path <- proj_path_prep(path_expand_r(path))

dir <- path_dir(proj_rel_path(path))
if (!dir %in% c("R", "src", "tests/testthat", "tests/testthat/_snaps")) {
if (
!dir %in% c("R", "src", "tests/testthat", "tests/testthat/_snaps") &&
# This makes sure variants are also supported.
!grepl("tests/testthat/_snaps", dir)
) {
cli::cli_abort(
"Open file must be code, test, or snapshot.",
call = error_call
Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ reference:
- use_import_from
- use_r
- use_rmarkdown_template
- use_snapshot
- use_spell_check
- use_test
- use_test_helper
Expand Down
20 changes: 20 additions & 0 deletions man/use_snapshot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/r.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# use_snapshot() errors for non-existent snapshot file

Code
use_snapshot("foo", open = FALSE)
Condition
Error in `use_snapshot()`:
! No snapshot file exists for `foo`.

# use_test_helper() creates a helper file

Code
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-r.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@ test_that("use_test() creates a test file", {
expect_proj_file("tests", "testthat", "test-foo.R")
})

test_that("use_snapshot() errors for non-existent snapshot file", {
create_local_package()
expect_snapshot(
error = TRUE,
use_snapshot("foo", open = FALSE)
)
})

test_that("use_snapshot() works for existing snapshot file", {
create_local_package()
path <- proj_path("tests", "testthat", "_snaps", "foo.md")
use_directory(path("tests", "testthat", "_snaps"))
write_utf8(path, "## Snapshot for foo")
expect_no_error(
use_snapshot("foo", open = FALSE)
)
})

test_that("use_test_helper() creates a helper file", {
create_local_package()

Expand Down
Loading