Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli tweaks #1959

Merged
merged 13 commits into from Mar 7, 2024
2 changes: 1 addition & 1 deletion R/badge.R
Expand Up @@ -45,7 +45,7 @@ use_badge <- function(badge_name, href, src) {
if (is.null(path)) {
ui_bullets(c(
"!" = "Can't find a README for the current project.",
"i" = "See {.fun usethis::use_readme_rmd} for help creating this file.",
"i" = "See {.help usethis::use_readme_rmd} for help creating this file.",
"i" = "Badge link will only be printed to screen."
))
path <- "README"
Expand Down
2 changes: 1 addition & 1 deletion R/cpp11.R
Expand Up @@ -9,8 +9,8 @@
#'
#' @export
use_cpp11 <- function() {
check_installed("cpp11")
check_is_package("use_cpp11()")
check_installed("cpp11")
check_uses_roxygen("use_cpp11()")
use_src()

Expand Down
2 changes: 1 addition & 1 deletion R/github-actions.R
Expand Up @@ -250,7 +250,7 @@ check_uses_github_actions <- function() {

ui_abort(c(
"Cannot detect that package {.pkg {project_name()}} already uses GitHub Actions.",
"Do you need to run {.fun use_github_action}?"
"Do you need to run {.run [use_github_action()](usethis::use_github_action())}?"
))
}

Expand Down
4 changes: 2 additions & 2 deletions R/github_token.R
Expand Up @@ -199,7 +199,7 @@ pat_sitrep <- function(host = "https://github.com",
git_user_check(user)
if (!is.null(user$email) && !any(grepl(user$email, addresses))) {
ui_bullets(c(
"x" = "Git user's email (.val {user$email}}) doesn't appear to be
"x" = "Git user's email ({.val {user$email}}) doesn't appear to be
registered with GitHub host."
))
}
Expand Down Expand Up @@ -233,7 +233,7 @@ scold_for_renviron <- function() {
old, invalid PAT defined in {.path {pth(renviron_path)}}.",
"i" = "For most use cases, it is better to NOT define the PAT in
{.file .Renviron}.",
"_" = "Call {.fun edit_r_environ} to edit that file.",
"_" = "Call {.run usethis::edit_r_environ()} to edit that file.",
"_" = "Then call {.run gitcreds::gitcreds_set()} to put the PAT into
the Git credential store."
))
Expand Down
10 changes: 9 additions & 1 deletion R/logo.R
Expand Up @@ -48,7 +48,15 @@ use_logo <- function(img, geometry = "240x278", retina = TRUE) {
height <- round(height / 2)
}

ui_bullets(c("_" = "Add logo to your README with the following html:"))
# Have a clickable hyperlink to jump to README if exists.
readme_path <- find_readme()
if (is.null(readme_path)) {
readme_show <- "your README"
} else {
readme_show <- cli::format_inline("{.path {pth(readme_path)}}")
}

ui_bullets(c("_" = "Add logo to {readme_show} with the following html:"))
pd_link <- pkgdown_url(pedantic = TRUE)
if (is.null(pd_link)) {
ui_code_snippet(
Expand Down
2 changes: 1 addition & 1 deletion R/package.R
Expand Up @@ -180,7 +180,7 @@ suggests_usage_hint <- function(package) {
ui_bullets(c("_" = "Then directly refer to functions with {.code {code}}."))
} else {
code <- glue('requireNamespace("{package}", quietly = TRUE)')
ui_bullets(c("_" = "Use {.code {code}} to test if package is installed."))
ui_bullets(c("_" = "Use {.code {code}} to test if {.pkg {package}} is installed."))
code <- glue("{package}::fun()")
ui_bullets(c("_" = "Then directly refer to functions with {.code {code}}."))
}
Expand Down
15 changes: 8 additions & 7 deletions R/pr.R
Expand Up @@ -224,7 +224,7 @@ pr_init <- function(branch) {
config_key <- glue("branch.{branch}.created-by")
gert::git_config_set(config_key, value = "usethis::pr_init", repo = repo)

ui_bullets(c("_" = "Use {.run usethis::pr_push} to create a PR."))
ui_bullets(c("_" = "Use {.run usethis::pr_push()} to create a PR."))
invisible()
}

Expand All @@ -251,10 +251,10 @@ pr_resume <- function(branch = NULL) {
check_string(branch)

if (!gert::git_branch_exists(branch, local = TRUE, repo = repo)) {
code <- glue('pr_init("{branch}")')
code <- glue('usethis::pr_init("{branch}")')
ui_abort(c(
"x" = "No branch named {.val {branch}} exists.",
"_" = "Call {.code {code}} to create a new PR branch."
"_" = "Call {.run {code}} to create a new PR branch."
))
}

Expand All @@ -264,7 +264,7 @@ pr_resume <- function(branch = NULL) {
gert::git_branch_checkout(branch, repo = repo)
git_pull()

ui_bullets(c("_" = "Use {.fun usethis::pr_push} to create or update PR."))
ui_bullets(c("_" = "Use {.run usethis::pr_push()} to create or update PR."))
invisible()
}

Expand Down Expand Up @@ -302,7 +302,7 @@ pr_fetch <- function(number = NULL, target = c("source", "primary")) {

if (is.na(pr$pr_repo_owner)) {
ui_abort("
The repo or branch where PR #{pr$pr_number} originates seems to have been
The repo or branch where {.href [PR #{pr$pr_number}]({pr$pr_https_url})} originates seems to have been
deleted.")
}

Expand Down Expand Up @@ -1001,8 +1001,9 @@ check_pr_branch <- function(default_branch = git_default_branch()) {
"i" = "The {.code pr_*()} functions facilitate pull requests.",
"i" = "The current branch ({.val {gb}}) is this repo's default branch, but
pull requests should NOT come from the default branch.",
"i" = "Do you need to call {.fun pr_init} (new PR)? Or {.fun pr_resume} or
{.fun pr_fetch} (existing PR)?"
"i" = "Do you need to call {.fun usethis::pr_init} (new PR)?
Or {.fun usethis::pr_resume} or
{.fun usethis::pr_fetch} (existing PR)?"
)
)
}
3 changes: 2 additions & 1 deletion R/proj.R
Expand Up @@ -214,8 +214,9 @@ check_is_package <- function(whos_asking = NULL) {

message <- "Project {.val {project_name()}} is not an R package."
if (!is.null(whos_asking)) {
whos_asking_fn <- sub("()", "", whos_asking, fixed = TRUE)
message <- c(
"i" = "{.code {whos_asking}} is designed to work with packages.",
"i" = "{.topic [{whos_asking}](usethis::{whos_asking_fn})} is designed to work with packages.",
"x" = message
)
}
Expand Down
18 changes: 9 additions & 9 deletions R/tidyverse.R
Expand Up @@ -85,15 +85,15 @@ create_tidy_package <- function(path, copyright_holder = NULL) {

use_cran_comments(open = FALSE)

ui_bullets(c(
"i" = "In the new package, remember to do:",
"_" = "{.run usethis::use_git()}",
"_" = "{.run usethis::use_github()}",
"_" = "{.run usethis::use_tidy_github()}",
"_" = "{.run usethis::use_tidy_github_actions()}",
"_" = "{.run usethis::use_tidy_github_labels()}",
"_" = "{.run usethis::use_pkgdown_github_pages()}"
))
ui_bullets(c("i" = "In the new package, remember to do:"))
ui_code_snippet("
usethis::use_git()
usethis::use_github()
usethis::use_tidy_github()
usethis::use_tidy_github_actions()
usethis::use_tidy_github_labels()
usethis::use_pkgdown_github_pages()
")

proj_activate(path)
}
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/_snaps/logo.md
@@ -0,0 +1,10 @@
# use_logo() shows a clickable path with README

Code
use_logo("logo.png")
Message
v Creating 'man/figures/'.
v Resized 'logo.png' to 240x278.
[ ] Add logo to 'README.md' with the following html:
# {TESTPKG} <img src="man/figures/logo.png" align="right" height="90" alt="" />

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/package.md
Expand Up @@ -47,7 +47,7 @@
use_package("withr", "Suggests")
Message
v Adding withr to 'Suggests' field in DESCRIPTION.
[ ] Use `requireNamespace("withr", quietly = TRUE)` to test if package is
[ ] Use `requireNamespace("withr", quietly = TRUE)` to test if withr is
installed.
[ ] Then directly refer to functions with `withr::fun()`.

Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/_snaps/proj.md
@@ -1,3 +1,12 @@
# check_is_package() can reveal who's asking

Code
check_is_package("foo()")
Condition
Error in `check_is_package()`:
i foo() (`?usethis::foo`) is designed to work with packages.
x Project "{TESTPROJ}" is not an R package.

# proj_path() errors with absolute paths

Code
Expand Down
3 changes: 1 addition & 2 deletions tests/testthat/_snaps/tidyverse.md
Expand Up @@ -10,8 +10,7 @@
v Adding withr to 'Imports' field in DESCRIPTION.
v Adding "@import rlang" to 'R/{TESTPKG}-package.R'.
v Adding "@importFrom glue glue" to 'R/{TESTPKG}-package.R'.
v Adding "@importFrom lifecycle deprecated" to
'R/{TESTPKG}-package.R'.
v Adding "@importFrom lifecycle deprecated" to 'R/{TESTPKG}-package.R'.
v Writing 'NAMESPACE'.
v Writing 'R/import-standalone-purrr.R'.

4 changes: 4 additions & 0 deletions tests/testthat/helper.R
Expand Up @@ -82,6 +82,10 @@ scrub_testpkg <- function(message) {
gsub("testpkg[a-zA-Z0-9]+", "{TESTPKG}", message, perl = TRUE)
}

scrub_testproj <- function(message) {
gsub("testproj[a-zA-Z0-9]+", "{TESTPROJ}", message, perl = TRUE)
}

skip_if_not_ci <- function() {
ci_providers <- c("GITHUB_ACTIONS", "TRAVIS", "APPVEYOR")
ci <- any(toupper(Sys.getenv(ci_providers)) == "TRUE")
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-github-actions.R
Expand Up @@ -78,7 +78,7 @@ test_that("uses_github_action() reports usage of GitHub Actions", {

test_that("check_uses_github_actions() can throw error", {
create_local_package()
withr::local_options(list(crayon.enabled = FALSE))
withr::local_options(list(crayon.enabled = FALSE, cli.width = Inf))
expect_snapshot(
check_uses_github_actions(),
error = TRUE,
Expand Down
13 changes: 12 additions & 1 deletion tests/testthat/test-logo.R
@@ -1,8 +1,19 @@
test_that("use_logo() doesn't error", {
test_that("use_logo() doesn't error with no README", {
skip_if_not_installed("magick")
skip_on_os("solaris")

create_local_package()
img <- magick::image_write(magick::image_read("logo:"), "logo.png")
expect_no_error(use_logo("logo.png"))
})

test_that("use_logo() shows a clickable path with README", {
skip_if_not_installed("magick")
skip_on_os("solaris")

create_local_package()
use_readme_md()
img <- magick::image_write(magick::image_read("logo:"), "logo.png")
withr::local_options(usethis.quiet = FALSE)
expect_snapshot(use_logo("logo.png"), transform = scrub_testpkg)
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to actually test for "clickable path"? Because it doesn't feel perceivable from the current snapshot.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well the single quotes in the snapshot indicates that some markup was applied (and that it could actually find README.md) 'README.md'.

6 changes: 5 additions & 1 deletion tests/testthat/test-proj.R
Expand Up @@ -36,7 +36,11 @@ test_that("check_is_package() errors for non-package", {

test_that("check_is_package() can reveal who's asking", {
create_local_project()
expect_usethis_error(check_is_package("foo"), "foo")
expect_snapshot(
error = TRUE,
check_is_package("foo()"),
transform = scrub_testproj
)
})

test_that("proj_path() appends to the project path", {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-tidyverse.R
Expand Up @@ -17,7 +17,7 @@ test_that("use_tidy_dependencies() isn't overly informative", {

create_local_package()
use_package_doc()
withr::local_options(usethis.quiet = FALSE)
withr::local_options(usethis.quiet = FALSE, cli.width = Inf)

expect_snapshot(
use_tidy_dependencies(),
Expand Down