Skip to content

Commit d000d4f

Browse files
committed
feat: simplify use of vcr in examples
1 parent 42fad40 commit d000d4f

File tree

10 files changed

+212
-5
lines changed

10 files changed

+212
-5
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Config/testthat/parallel: true
5656
Config/testthat/start-first: ause_cassette_re_record
5757
Encoding: UTF-8
5858
Language: en-US
59-
Roxygen: list(markdown = TRUE)
59+
Roxygen: list(markdown = TRUE, roclets = c("collate", "rd", "namespace", "vcr::examplesVCR_roclet"), packages = "vcr")
6060
RoxygenNote: 7.3.2
6161
X-schema.org-applicationCategory: Web
6262
X-schema.org-isPartOf: https://ropensci.org

NAMESPACE

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method(roclet_output,roclet_examplesVCR)
4+
S3method(roclet_process,roclet_examplesVCR)
5+
S3method(roxy_tag_parse,roxy_tag_examplesVCR)
6+
S3method(roxy_tag_rd,roxy_tag_examplesVCR)
37
export(Cassette)
48
export(cassette_path)
59
export(cassettes)
@@ -8,6 +12,7 @@ export(current_cassette)
812
export(current_cassette_recording)
913
export(current_cassette_replaying)
1014
export(eject_cassette)
15+
export(examplesVCR_roclet)
1116
export(insert_cassette)
1217
export(insert_example_cassette)
1318
export(is_recording)
@@ -23,6 +28,7 @@ export(turned_off)
2328
export(turned_on)
2429
export(use_cassette)
2530
export(use_vcr)
31+
export(use_vcr_examples)
2632
export(vcr_config_defaults)
2733
export(vcr_configuration)
2834
export(vcr_configure)
@@ -33,3 +39,8 @@ export(vcr_last_response)
3339
export(vcr_test_path)
3440
import(rlang)
3541
importFrom(lifecycle,deprecated)
42+
importFrom(roxygen2,block_get_tags)
43+
importFrom(roxygen2,roclet_output)
44+
importFrom(roxygen2,roclet_process)
45+
importFrom(roxygen2,roxy_tag_parse)
46+
importFrom(roxygen2,roxy_tag_rd)

R/examples.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
#'
33
#' @description
44
#' `insert_example_cassette()` is a wrapper around [insert_cassette()] that
5-
#' stores cassettes in `inst/_vcr/`. Call it in the first line of your examples
5+
#' stores cassettes in `inst/_vcr/`. Either:
6+
#' - Use the vcr-specific `@examplesVCR` tag instead of the `@examples` tag.
7+
#' For this to work you need to register vcr in your DESCRIPTION by running
8+
#' `vcr::use_vcr_examples()` once per package.
9+
#' - Call it in the first line of your examples
610
#' (typically wrapped in `\dontshow{}`), and call `eject_cassette()` on the
711
#' last line.
812
#'

R/roxygen-example.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#' @rdname vcr-example
2+
#' @name vcr-example
3+
#' @title Trying out our custom tag
4+
#' @keywords internal
5+
#' @examplesVCR bla
6+
#' 1 + 1
7+
NULL

R/roxygen.R

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#' @importFrom roxygen2 roxy_tag_parse
2+
#' @importFrom roxygen2 roxy_tag_rd
3+
NULL
4+
5+
#' @export
6+
roxy_tag_parse.roxy_tag_examplesVCR <- function(x) {
7+
roxygen2::tag_markdown(x)
8+
}
9+
10+
#' @export
11+
roxy_tag_rd.roxy_tag_examplesVCR <- function(x, base_path, env) {
12+
roxygen2::rd_section("examples", x$val)
13+
}
14+
15+
#' @export
16+
roxy_tag_parse.roxy_tag_examplesVCR <- function(x, ...) {
17+
lines <- unlist(strsplit(x$raw, "\r?\n"))
18+
19+
cassette_name <- trimws(lines[1])
20+
package_name <- roxygen2::roxy_meta_get("current_package")
21+
22+
x$raw <- paste(
23+
c(
24+
sprintf(
25+
"\\dontshow{vcr::insert_example_cassette('%s', package = '%s')}",
26+
cassette_name,
27+
package_name
28+
),
29+
lines[-1],
30+
"\\dontshow{vcr::eject_example_cassette()}"
31+
),
32+
collapse = "\n"
33+
)
34+
x$value <- ""
35+
roxygen2::tag_examples(x)
36+
}
37+
38+
39+
#' @export
40+
# https://github.com/shahronak47/informationtag
41+
examplesVCR_roclet <- function() {
42+
roxygen2::roclet("examplesVCR")
43+
}
44+
45+
#' @importFrom roxygen2 block_get_tags roclet_process
46+
#' @method roclet_process roclet_examplesVCR
47+
#' @export
48+
# https://github.com/shahronak47/informationtag
49+
roclet_process.roclet_examplesVCR <- function(x, blocks, env, base_path) {
50+
x
51+
}
52+
53+
54+
#' @export
55+
#' @importFrom roxygen2 block_get_tags roclet_output
56+
roclet_output.roclet_examplesVCR <- function(x, results, base_path, ...) {
57+
x
58+
}
59+
60+
#' Use 'vcr' for examples in your package
61+
#'
62+
#' This functions amends DESCRIPTION to set up vcr's roxygen2 machinery.
63+
#' Call it once when you're setting a new package.
64+
#'
65+
#' @param path character path to the package
66+
#'
67+
#' @return Nothing: called for file system side effects.
68+
#' @export
69+
#'
70+
use_vcr_examples <- function(path = ".") {
71+
rlang::check_installed("desc")
72+
73+
cli::cli_alert_info("Registering vcr's roxygen2 tag usage in DESCRIPTION")
74+
75+
current_roxy <- desc::desc_get("Roxygen", file = path)[[1]]
76+
if (is.na(current_roxy)) {
77+
desc::desc_set(
78+
"Roxygen",
79+
'list(markdown = TRUE, roclets = c("collate", "rd", "namespace", "vcr::examplesVCR_roclet", packages = "vcr"))'
80+
)
81+
} else {
82+
current <- eval(parse(text = current_roxy))
83+
new <- current
84+
new[["roclets"]] <- union(
85+
current[["roclets"]],
86+
c("collate", "rd", "namespace", "vcr::examplesVCR_roclet")
87+
)
88+
new[["packages"]] <- union(current[["packages"]], "vcr")
89+
90+
new_string <- paste(deparse(new), collapse = "")
91+
desc::desc_set("Roxygen", new_string, file = path)
92+
}
93+
94+
cli::cli_alert_info("Registering vcr build-time dependency in DESCRIPTION")
95+
desc::desc_set(
96+
"Config/Needs/build",
97+
paste_desc(
98+
desc::desc_get("Config/Needs/build", file = path),
99+
"vcr"
100+
),
101+
file = path
102+
)
103+
}
104+
105+
#' Append value if needed
106+
#'
107+
#' @param x Existing DESCRIPTION field value
108+
#' @param y Value to append
109+
#'
110+
#' @return A character string
111+
#' @dev
112+
#'
113+
#' @examples
114+
#' paste_desc(NA, 1)
115+
#' paste_desc(1, 1)
116+
#' paste_desc(2, 1)
117+
paste_desc <- function(x, y) {
118+
if (is.na(x)) {
119+
return(y)
120+
}
121+
122+
# ensure this is idempotent
123+
toString(unique(c(x, y)))
124+
}

man/insert_example_cassette.Rd

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/paste_desc.Rd

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/use_vcr_examples.Rd

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/vcr-example.Rd

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/vcr-package.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)