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

tests: clean up, simplify tests #716

Merged
merged 17 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 13 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
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,13 @@ Suggests:
httptest2,
jsonlite,
knitr,
mockery,
parsedate,
pkgbuild,
pkgload,
rmarkdown,
rstudioapi,
rversions,
testthat (>= 3.0.0),
testthat (>= 3.1.10),
vctrs,
withr
Config/Needs/readme: r-lib/downlit@f-readme-document
Expand Down
16 changes: 8 additions & 8 deletions R/auto.R
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ update_cran_comments <- function() {
}

get_crp_date <- function() {
cmt <- gh::gh("/repos/eddelbuettel/crp/commits")[[1]]
cmt <- gh("/repos/eddelbuettel/crp/commits")[[1]]
Copy link
Member Author

Choose a reason for hiding this comment

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

this change is for being able to mock it while respecting the guidance in https://testthat.r-lib.org/reference/local_mocked_bindings.html#namespaced-calls

date <- cmt$commit$committer$date
as.Date(date)
}
Expand Down Expand Up @@ -396,7 +396,7 @@ create_github_release <- function() {
slug <- github_slug()
tag <- get_tag_info()

out <- gh::gh(
out <- gh(
glue("POST /repos/{slug}/releases"),
tag_name = tag$name,
name = tag$header,
Expand Down Expand Up @@ -453,7 +453,7 @@ is_ignored <- function(path) {
}

create_pull_request <- function(release_branch, main_branch, remote_name, force) {
# FIXME: Use gh::gh() to determine if we need to create the pull request
# FIXME: Use gh() to determine if we need to create the pull request
create <- TRUE

if (create) {
Expand All @@ -462,7 +462,7 @@ create_pull_request <- function(release_branch, main_branch, remote_name, force)
template_path <- system.file("templates", "pr.md", package = "fledge")
body <- glue_collapse(readLines(template_path), sep = "\n")

pr <- gh::gh(
pr <- gh(
"POST /repos/:owner/:repo/pulls",
owner = info$owner$login,
repo = info$name,
Expand All @@ -479,7 +479,7 @@ create_pull_request <- function(release_branch, main_branch, remote_name, force)
)

## ensure that label exists ----
labels <- gh::gh(
labels <- gh(
"GET /repos/:owner/:repo/labels",
owner = info$owner$login,
repo = info$name
Expand All @@ -489,7 +489,7 @@ create_pull_request <- function(release_branch, main_branch, remote_name, force)
no_cran_release_label <- (length(cran_release_label) == 0)
if (no_cran_release_label) {
cran_release_label <- "CRAN release :station:"
gh::gh(
gh(
"POST /repos/:owner/:repo/labels",
owner = info[["owner"]][["login"]],
repo = info[["name"]],
Expand All @@ -499,7 +499,7 @@ create_pull_request <- function(release_branch, main_branch, remote_name, force)
}

## add label to PR ----
gh::gh(
gh(
"PATCH /repos/:owner/:repo/issues/:issue_number",
owner = info[["owner"]][["login"]],
repo = info[["name"]],
Expand All @@ -517,7 +517,7 @@ release_after_cran_built_binaries <- function() {
remote <- "origin"
github_info <- github_info(remote)

prs <- gh::gh(
prs <- gh(
"GET /repos/:owner/:repo/pulls",
owner = github_info[["owner"]][["login"]],
repo = github_info[["name"]],
Expand Down
2 changes: 1 addition & 1 deletion R/gh-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ extract_repo <- function(url) {
}

get_repo_data <- function(repo) {
req <- gh::gh("/repos/:repo", repo = repo)
req <- gh("/repos/:repo", repo = repo)
return(req)
}
6 changes: 3 additions & 3 deletions R/parse-news-items.R
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ harvest_pr_data <- function(message) {
{
# suppressMessages() for quiet mocking
suppressMessages(
gh::gh(glue("GET /repos/{slug}/pulls/{pr_number}"))
gh(glue("GET /repos/{slug}/pulls/{pr_number}"))
)
},
error = function(e) {
Expand Down Expand Up @@ -360,10 +360,10 @@ has_internet <- function() {
if (!rlang::is_installed("curl")) {
return(FALSE)
}
if (nzchar(Sys.getenv("YES_INTERNET_TEST_FLEDGE"))) {
if (nzchar(Sys.getenv("FLEDGE_YES_INTERNET_TEST"))) {
Copy link
Member Author

Choose a reason for hiding this comment

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

these were the only test switches not starting with FLEDGE_ 😬

return(TRUE)
}
if (nzchar(Sys.getenv("NO_INTERNET_TEST_FLEDGE"))) {
if (nzchar(Sys.getenv("FLEDGE_NO_INTERNET_TEST"))) {
return(FALSE)
}
curl::has_internet()
Expand Down
57 changes: 20 additions & 37 deletions R/repo-helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#' @param date String of time for DESCRIPTION and git.
#' @param dir Directory within which to create the mock package folder.
#' @param news If TRUE, create a NEWS.md file.
#' @param dev_md Whether to use "(development version)" (with `dev_md = TRUE`) or the current version
#' number, in the first NEWS.md header.
#'
#' @return The path to the newly created mock package.
#' @export
Expand All @@ -19,8 +17,7 @@ create_demo_project <- function(open = rlang::is_interactive(),
email = NULL,
date = "2021-09-27",
dir = file.path(tempdir(), "fledge"),
news = FALSE,
dev_md = FALSE) {
news = FALSE) {
if (is.null(maintainer)) {
maintainer <- whoami::fullname(fallback = "Kirill M\u00fcller")
}
Expand Down Expand Up @@ -65,35 +62,17 @@ create_demo_project <- function(open = rlang::is_interactive(),
gert::git_config_set(name = "init.defaultbranch", value = "main")

if (news) {
usethis::with_project(
path = pkg,
{
rlang::with_interactive(
{
# we now have to create a demo project with a preambled NEWS.md for tests to pass
withr::with_options(
list(repos = c("CRAN" = "https://cloud.r-project.org")),
{
usethis::use_news_md()
}
)

news_lines <- readLines("NEWS.md")
if (!dev_md) {
news_lines <- sub("\\(development version\\)", desc::desc_get_version(), news_lines)
}
news_lines <- c(news_preamble(), "", news_lines)
writeLines(news_lines, "NEWS.md")
},
value = FALSE
)
gert::git_add("NEWS.md")
gert::git_commit(
"Add NEWS.md to track changes.",
author = default_gert_author(),
committer = default_gert_committer()
)
}
news_lines <- c(
news_preamble(), "",
sprintf("# %s %s", name, as.character(desc::desc_get_version())), "",
"* Added a `NEWS.md` file to track changes to the package."
)
brio::write_lines(news_lines, "NEWS.md")
gert::git_add("NEWS.md")
gert::git_commit(
"Add NEWS.md to track changes.",
author = default_gert_author(),
committer = default_gert_committer()
)
}

Expand Down Expand Up @@ -142,16 +121,20 @@ with_demo_project <- function(code, dir = NULL, news = TRUE, quiet = FALSE) {
#' @return `local_demo_project()` is called for its side effect and returns `NULL`, invisibly.
#' @rdname with_demo_project
#' @export
local_demo_project <- function(dir = NULL, news = TRUE, quiet = FALSE, .local_envir = parent.frame()) {
if (is.null(dir)) {
dir <- withr::local_tempdir(pattern = "fledge", .local_envir = .local_envir)
}
local_demo_project <- function(dir = NULL,
news = TRUE,
quiet = FALSE,
.local_envir = parent.frame()) {

dir <- dir %||%
withr::local_tempdir(pattern = "fledge", .local_envir = .local_envir)

if (!dir.exists(dir)) {
cli::cli_abort(c(x = "Can't find the directory {.file {dir}}."))
}

repo <- create_demo_project(dir = dir, news = news)

usethis::local_project(
path = repo,
quiet = quiet,
Expand Down
3 changes: 3 additions & 0 deletions R/utils-gh.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
gh <- function(...) {
gh::gh(...)
}
6 changes: 5 additions & 1 deletion tests/testthat/_snaps/with_demo_project.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# with_demo_project errors informatively

x Can't find the directory 'unexisting-dir'.
Code
with_demo_project(1 + 1, dir = "unexisting-dir")
Condition
Error in `local_demo_project()`:
x Can't find the directory 'unexisting-dir'.

2 changes: 1 addition & 1 deletion tests/testthat/helper-conventional-commits.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Also tweak the CI workflow accordingly. :sweat_smile:",
)
}

sort_of_commit <- function(commit_message, repo) {
sort_of_commit <- function(commit_message, repo = ".") {
file <- digest::sha1(commit_message)
file.create(file.path(repo, file))
gert::git_add(file, repo = repo)
Expand Down
36 changes: 25 additions & 11 deletions tests/testthat/helper-remote.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
create_remote <- function() {
# assuming this is a temporary directory :-)
parent_dir <- dirname(getwd())
dir.create(file.path(parent_dir, "remote"))
gert::git_init(file.path(parent_dir, "remote"), bare = TRUE)
remote_url <- file.path(parent_dir, "remote")
gert::git_remote_add(remote_url, name = "origin")
create_remote <- function(tempdir_remote) {

gert::git_init(tempdir_remote, bare = TRUE)

current_remotes <- gert::git_remote_list()
origin_exists <- "origin" %in% current_remotes[["name"]]
if (origin_exists) {
cli::cli_abort(c(
"Found an origin, we're in {getwd()}, was this expected?",
i = "Did you forget to set up a local toy repo?"
))
}

gert::git_remote_add(tempdir_remote, name = "origin")
gert::git_push(remote = "origin")
remote_url

tempdir_remote
}

show_tags <- function(remote_url) {

tempdir_remote <- withr::local_tempdir(pattern = "remote")

withr::with_dir(tempdir_remote, {
gert::git_clone(remote_url)
gert::git_clone(remote_url, path = "remote")
gert::git_tag_list(repo = "remote")[, c("name", "ref")]
})
}

show_files <- function(remote_url) {
if (!gert::user_is_configured()) {
usethis::use_git_config(user.name = "Jane Doe", user.email = "[email protected]")
usethis::use_git_config(
user.name = "Jane Doe",
user.email = "[email protected]"
)
}

git_config <- gert::git_config_global()
Expand All @@ -28,7 +42,7 @@ show_files <- function(remote_url) {

tempdir_remote <- withr::local_tempdir(pattern = "remote")
withr::with_dir(tempdir_remote, {
gert::git_clone(remote_url)
gert::git_clone(remote_url, path = "remote")
suppressMessages(gert::git_branch_checkout("main", force = TRUE, repo = "remote"))
fs::dir_ls("remote", recurse = TRUE)
})
Expand Down
Loading