From 6e37a778575c78485629e877fdaab210c896dd5a Mon Sep 17 00:00:00 2001 From: Jenny Bryan Date: Tue, 23 Jul 2024 22:34:07 -0700 Subject: [PATCH] Make some more changes --- R/use_standalone.R | 21 ++++--- tests/testthat/_snaps/use_standalone.md | 77 +++++++++++++++++++++---- tests/testthat/test-use_standalone.R | 35 +++++++++-- 3 files changed, 110 insertions(+), 23 deletions(-) diff --git a/R/use_standalone.R b/R/use_standalone.R index aab36a630..af1e1b9e1 100644 --- a/R/use_standalone.R +++ b/R/use_standalone.R @@ -157,16 +157,21 @@ as_standalone_dest_file <- function(file) { } standalone_header <- function(repo_spec, path, ref = NULL, host = NULL) { - if (is.null(ref)) { - ref <- "HEAD" - } - if (is.null(host)) { - host <- "https://github.com" - } + ref_string <- ref %||% "HEAD" + host_string <- host %||% "https://github.com" + source_comment <- + glue("# Source: {host_string}/{repo_spec}/blob/{ref_string}/{path}") + + path_string <- path_ext_remove(sub("^standalone-", "", basename(path))) + ref_string <- if (is.null(ref)) "" else glue(', ref = "{ref}"') + host_string <- if (is.null(host) || host == "https://github.com") "" else glue(', host = "{host}"') + code_hint <- glue('usethis::use_standalone("{repo_spec}", "{path_string}"{ref_string}{host_string})') + generated_comment <- glue('# Generated by: {code_hint}') + c( "# Standalone file: do not edit by hand", - glue("# Source: <{host}/{repo_spec}/blob/{ref}/{path}>"), - glue('# Generated by: usethis::use_standalone("{repo_spec}", "{basename(path)}", "{ref}", "{host}")'), + source_comment, + generated_comment, paste0("# ", strrep("-", 72 - 2)), "#" ) diff --git a/tests/testthat/_snaps/use_standalone.md b/tests/testthat/_snaps/use_standalone.md index 6433d5be6..9abf59eef 100644 --- a/tests/testthat/_snaps/use_standalone.md +++ b/tests/testthat/_snaps/use_standalone.md @@ -1,3 +1,69 @@ +# standalone_header() works with various inputs + + Code + standalone_header("OWNER/REPO", "R/standalone-foo.R") + Output + [1] "# Standalone file: do not edit by hand" + [2] "# Source: https://github.com/OWNER/REPO/blob/HEAD/R/standalone-foo.R" + [3] "# Generated by: usethis::use_standalone(\"OWNER/REPO\", \"foo\")" + [4] "# ----------------------------------------------------------------------" + [5] "#" + +--- + + Code + standalone_header("OWNER/REPO", "R/standalone-foo.R", ref = "blah") + Output + [1] "# Standalone file: do not edit by hand" + [2] "# Source: https://github.com/OWNER/REPO/blob/blah/R/standalone-foo.R" + [3] "# Generated by: usethis::use_standalone(\"OWNER/REPO\", \"foo\", ref = \"blah\")" + [4] "# ----------------------------------------------------------------------" + [5] "#" + +--- + + Code + standalone_header("OWNER/REPO", "R/standalone-foo.R", ref = "blah", host = "https://github.com") + Output + [1] "# Standalone file: do not edit by hand" + [2] "# Source: https://github.com/OWNER/REPO/blob/blah/R/standalone-foo.R" + [3] "# Generated by: usethis::use_standalone(\"OWNER/REPO\", \"foo\", ref = \"blah\")" + [4] "# ----------------------------------------------------------------------" + [5] "#" + +--- + + Code + standalone_header("OWNER/REPO", "R/standalone-foo.R", host = "https://github.acme.com") + Output + [1] "# Standalone file: do not edit by hand" + [2] "# Source: https://github.acme.com/OWNER/REPO/blob/HEAD/R/standalone-foo.R" + [3] "# Generated by: usethis::use_standalone(\"OWNER/REPO\", \"foo\", host = \"https://github.acme.com\")" + [4] "# ----------------------------------------------------------------------" + [5] "#" + +--- + + Code + standalone_header("OWNER/REPO", "R/standalone-foo.R", ref = "blah", host = "https://github.com") + Output + [1] "# Standalone file: do not edit by hand" + [2] "# Source: https://github.com/OWNER/REPO/blob/blah/R/standalone-foo.R" + [3] "# Generated by: usethis::use_standalone(\"OWNER/REPO\", \"foo\", ref = \"blah\")" + [4] "# ----------------------------------------------------------------------" + [5] "#" + +--- + + Code + standalone_header("OWNER/REPO", "R/standalone-foo.R", ref = "blah", host = "https://github.acme.com") + Output + [1] "# Standalone file: do not edit by hand" + [2] "# Source: https://github.acme.com/OWNER/REPO/blob/blah/R/standalone-foo.R" + [3] "# Generated by: usethis::use_standalone(\"OWNER/REPO\", \"foo\", ref = \"blah\", host = \"https://github.acme.com\")" + [4] "# ----------------------------------------------------------------------" + [5] "#" + # can offer choices Code @@ -12,17 +78,6 @@ ! `file` is absent, but must be supplied. i Possible options are cli, downstream-deps, lazyeval, lifecycle, linked-version, obj-type, purrr, rlang, s3-register, sizes, types-check, vctrs, or zeallot. -# header provides useful summary - - Code - standalone_header("r-lib/usethis", "R/standalone-test.R") - Output - [1] "# Standalone file: do not edit by hand" - [2] "# Source: " - [3] "# Generated by: usethis::use_standalone(\"r-lib/usethis\", \"standalone-test.R\", \"HEAD\", \"https://github.com\")" - [4] "# ----------------------------------------------------------------------" - [5] "#" - # can extract imports Code diff --git a/tests/testthat/test-use_standalone.R b/tests/testthat/test-use_standalone.R index 638411c27..41d9337e7 100644 --- a/tests/testthat/test-use_standalone.R +++ b/tests/testthat/test-use_standalone.R @@ -1,3 +1,34 @@ +test_that("standalone_header() works with various inputs", { + expect_snapshot( + standalone_header("OWNER/REPO", "R/standalone-foo.R") + ) + expect_snapshot( + standalone_header("OWNER/REPO", "R/standalone-foo.R", ref = "blah") + ) + expect_snapshot( + standalone_header( + "OWNER/REPO", "R/standalone-foo.R", host = "https://github.com" + ) + ) + expect_snapshot( + standalone_header( + "OWNER/REPO", "R/standalone-foo.R", host = "https://github.acme.com" + ) + ) + expect_snapshot( + standalone_header( + "OWNER/REPO", "R/standalone-foo.R", + ref = "blah", host = "https://github.com" + ) + ) + expect_snapshot( + standalone_header( + "OWNER/REPO", "R/standalone-foo.R", + ref = "blah", host = "https://github.acme.com" + ) + ) +}) + test_that("can import standalone file with dependencies", { skip_if_offline("github.com") create_local_package() @@ -40,10 +71,6 @@ test_that("can offer choices", { }) }) -test_that("header provides useful summary", { - expect_snapshot(standalone_header("r-lib/usethis", "R/standalone-test.R")) -}) - test_that("can extract dependencies", { extract_deps <- function(deps) { out <- standalone_dependencies(c("# ---", deps, "# ---"), "test.R")