Skip to content

Commit

Permalink
Make some more changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jennybc committed Jul 24, 2024
1 parent f547df7 commit 6e37a77
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 23 deletions.
21 changes: 13 additions & 8 deletions R/use_standalone.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
"#"
)
Expand Down
77 changes: 66 additions & 11 deletions tests/testthat/_snaps/use_standalone.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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: <https://github.com/r-lib/usethis/blob/HEAD/R/standalone-test.R>"
[3] "# Generated by: usethis::use_standalone(\"r-lib/usethis\", \"standalone-test.R\", \"HEAD\", \"https://github.com\")"
[4] "# ----------------------------------------------------------------------"
[5] "#"

# can extract imports

Code
Expand Down
35 changes: 31 additions & 4 deletions tests/testthat/test-use_standalone.R
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 6e37a77

Please sign in to comment.