From ecc58ea2122b9c0d95d49b859325c14434ec954f Mon Sep 17 00:00:00 2001 From: Milan Malfait Date: Thu, 26 Oct 2023 11:48:30 +0100 Subject: [PATCH 1/6] Open new episodes for editing in interactive sessions --- R/create_episode.R | 5 ++++- man/create_episode.Rd | 11 ++++++++++- man/sandpaper-package.Rd | 1 + 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/R/create_episode.R b/R/create_episode.R index 13ae1aa96..d0668e3c1 100644 --- a/R/create_episode.R +++ b/R/create_episode.R @@ -15,12 +15,14 @@ #' @param add (logical or numeric) If numeric, it represents the position the #' episode should be added. If `TRUE`, the episode is added to the end of the #' schedule. If `FALSE`, the episode is added as a draft episode. +#' @param open if interactive, the episode will open in a new editor window. #' @export #' @examples #' tmp <- tempfile() #' create_lesson(tmp, open = FALSE, rmd = FALSE) #' create_episode_md("getting-started", path = tmp) -create_episode <- function(title, ext = "Rmd", make_prefix = FALSE, add = TRUE, path = ".") { +create_episode <- function(title, ext = "Rmd", make_prefix = FALSE, add = TRUE, path = ".", + open = rlang::is_interactive()) { check_lesson(path) ext <- switch(match.arg(tolower(ext), c("rmd", "md")), rmd = ".Rmd", md = ".md") prefix <- "" @@ -37,6 +39,7 @@ create_episode <- function(title, ext = "Rmd", make_prefix = FALSE, add = TRUE, if (add) { move_episode(ename, position = add, write = TRUE, path = path) } + usethis::edit_file(fs::path(path, "episodes", ename), open = open) invisible(fs::path(path, "episodes", ename)) } diff --git a/man/create_episode.Rd b/man/create_episode.Rd index b1a4b24e1..60a28e3f5 100644 --- a/man/create_episode.Rd +++ b/man/create_episode.Rd @@ -8,7 +8,14 @@ \alias{draft_episode_rmd} \title{Create an Episode from a template} \usage{ -create_episode(title, ext = "Rmd", make_prefix = FALSE, add = TRUE, path = ".") +create_episode( + title, + ext = "Rmd", + make_prefix = FALSE, + add = TRUE, + path = ".", + open = rlang::is_interactive() +) create_episode_md(title, make_prefix = FALSE, add = TRUE, path = ".") @@ -34,6 +41,8 @@ episode should be added. If \code{TRUE}, the episode is added to the end of the schedule. If \code{FALSE}, the episode is added as a draft episode.} \item{path}{the path to the {sandpaper} lesson.} + +\item{open}{if interactive, the episode will open in a new editor window.} } \description{ These functions allow you to create an episode that will be added to the diff --git a/man/sandpaper-package.Rd b/man/sandpaper-package.Rd index 110f873ca..5c9f1f14a 100644 --- a/man/sandpaper-package.Rd +++ b/man/sandpaper-package.Rd @@ -33,6 +33,7 @@ Other contributors: \item Erin Becker \email{ebecker@carpentries.org} [contributor] \item Hugo Gruson \email{hugo.gruson+R@normalesup.org} [contributor] \item Rob Davey \email{robertdavey@carpentries.org} [contributor] + \item Milan Malfait \email{milan.malfait94@gmail.com} (\href{https://orcid.org/0000-0001-9144-3701}{ORCID}) [contributor] } } From 4ba0cf129768ecd52ce071240d64b1a248d0e91f Mon Sep 17 00:00:00 2001 From: "Zhian N. Kamvar" Date: Wed, 22 Nov 2023 15:23:49 -0800 Subject: [PATCH 2/6] use explicit return --- R/create_episode.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/create_episode.R b/R/create_episode.R index d0668e3c1..8b35e4c2b 100644 --- a/R/create_episode.R +++ b/R/create_episode.R @@ -39,8 +39,8 @@ create_episode <- function(title, ext = "Rmd", make_prefix = FALSE, add = TRUE, if (add) { move_episode(ename, position = add, write = TRUE, path = path) } - usethis::edit_file(fs::path(path, "episodes", ename), open = open) - invisible(fs::path(path, "episodes", ename)) + new_file <- usethis::edit_file(fs::path(path, "episodes", ename), open = open) + return(new_file) } From 3ff6d51e26420e0952e6fb99950d9a3c2055247c Mon Sep 17 00:00:00 2001 From: "Zhian N. Kamvar" Date: Wed, 22 Nov 2023 15:27:03 -0800 Subject: [PATCH 3/6] update NEWS --- NEWS.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 3b738b820..49e624b41 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,13 +4,16 @@ * Using `handout: true` in `config.yaml` will cause a handout to be generated for the lesson website under `/files/code-handout.R`. At the moment, this is - only relevant for R-based lessons (implemented: @froggleston, #527) and - supersedes the need for specifying `options(sandpaper.handout = TRUE)` + only relevant for R-based lessons (implemented: @froggleston, #527, + reviewed: @zkamvar) and supersedes the need for specifying + `options(sandpaper.handout = TRUE)` * Content for learners now accessible through instructor view. The instructor view "More" dropdown menu item will now have links to learner view items appended. Note that when clicking these links, the user will remain in instructor view. This behaviour may change in future iterations (reported: @karenword, #394; fixed: @ErinBecker, #530, reviewed: @zkamvar) +* `create_episode()` will now open new episodes for editing in interactive + sessions (implemented: @milanmlft, #534, reviewed: @zkamvar) ## BUG FIX From a27d2b845d2bbd6f92da1ea53b72d737bea533a8 Mon Sep 17 00:00:00 2001 From: "Zhian N. Kamvar" Date: Wed, 22 Nov 2023 15:59:19 -0800 Subject: [PATCH 4/6] sprinkle open args through create_episode family --- R/create_episode.R | 24 ++++++++++++++---------- man/create_episode.Rd | 30 ++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/R/create_episode.R b/R/create_episode.R index 8b35e4c2b..8df5018d2 100644 --- a/R/create_episode.R +++ b/R/create_episode.R @@ -24,7 +24,10 @@ create_episode <- function(title, ext = "Rmd", make_prefix = FALSE, add = TRUE, path = ".", open = rlang::is_interactive()) { check_lesson(path) - ext <- switch(match.arg(tolower(ext), c("rmd", "md")), rmd = ".Rmd", md = ".md") + ext <- switch(match.arg(tolower(ext), c("rmd", "md")), + rmd = ".Rmd", + md = ".md" + ) prefix <- "" if (make_prefix) { episodes <- fs::path_file(fs::dir_ls(path_episodes(path), regexp = "*.[Rr]?md")) @@ -35,7 +38,8 @@ create_episode <- function(title, ext = "Rmd", make_prefix = FALSE, add = TRUE, slug <- slugify(title) ename <- paste0(prefix, slug, ext) copy_template("episode", fs::path(path, "episodes"), ename, - values = list(title = siQuote(title), md = ext == ".md")) + values = list(title = siQuote(title), md = ext == ".md") + ) if (add) { move_episode(ename, position = add, write = TRUE, path = path) } @@ -46,24 +50,24 @@ create_episode <- function(title, ext = "Rmd", make_prefix = FALSE, add = TRUE, #' @export #' @rdname create_episode -create_episode_md <- function(title, make_prefix = FALSE, add = TRUE, path = ".") { - create_episode(title, ext = "md", make_prefix = make_prefix, add = add, path = path) +create_episode_md <- function(title, make_prefix = FALSE, add = TRUE, path = ".", open = rlang::is_interactive()) { + create_episode(title, ext = "md", make_prefix = make_prefix, add = add, path = path, open = open) } #' @export #' @rdname create_episode -create_episode_rmd <- function(title, make_prefix = FALSE, add = TRUE, path = ".") { - create_episode(title, ext = "Rmd", make_prefix = make_prefix, add = add, path = path) +create_episode_rmd <- function(title, make_prefix = FALSE, add = TRUE, path = ".", open = rlang::is_interactive()) { + create_episode(title, ext = "Rmd", make_prefix = make_prefix, add = add, path = path, open = open) } #' @export #' @rdname create_episode -draft_episode_md <- function(title, make_prefix = FALSE, path = ".") { - create_episode(title, ext = "md", make_prefix = make_prefix, add = FALSE, path = path) +draft_episode_md <- function(title, make_prefix = FALSE, path = ".", open = rlang::is_interactive()) { + create_episode(title, ext = "md", make_prefix = make_prefix, add = FALSE, path = path, open = open) } #' @export #' @rdname create_episode -draft_episode_rmd <- function(title, make_prefix = FALSE, path = ".") { - create_episode(title, ext = "Rmd", make_prefix = make_prefix, add = FALSE, path = path) +draft_episode_rmd <- function(title, make_prefix = FALSE, path = ".", open = rlang::is_interactive()) { + create_episode(title, ext = "Rmd", make_prefix = make_prefix, add = FALSE, path = path, open = open) } diff --git a/man/create_episode.Rd b/man/create_episode.Rd index 60a28e3f5..f0a9488d4 100644 --- a/man/create_episode.Rd +++ b/man/create_episode.Rd @@ -17,13 +17,35 @@ create_episode( open = rlang::is_interactive() ) -create_episode_md(title, make_prefix = FALSE, add = TRUE, path = ".") +create_episode_md( + title, + make_prefix = FALSE, + add = TRUE, + path = ".", + open = rlang::is_interactive() +) -create_episode_rmd(title, make_prefix = FALSE, add = TRUE, path = ".") +create_episode_rmd( + title, + make_prefix = FALSE, + add = TRUE, + path = ".", + open = rlang::is_interactive() +) -draft_episode_md(title, make_prefix = FALSE, path = ".") +draft_episode_md( + title, + make_prefix = FALSE, + path = ".", + open = rlang::is_interactive() +) -draft_episode_rmd(title, make_prefix = FALSE, path = ".") +draft_episode_rmd( + title, + make_prefix = FALSE, + path = ".", + open = rlang::is_interactive() +) } \arguments{ \item{title}{the title of the episode} From 3f5452133c086ea26bb04e809e5e70bc4bec607f Mon Sep 17 00:00:00 2001 From: "Zhian N. Kamvar" Date: Wed, 22 Nov 2023 16:00:37 -0800 Subject: [PATCH 5/6] make sure create_episode() behaves in examples That is, make sure it does not accidentally open a file when lessons are being created or when running examples. --- R/build_lesson.R | 2 +- R/create_lesson.R | 2 +- R/move_episode.R | 28 ++++++++++++++-------------- R/set_dropdown.R | 2 +- man/build_lesson.Rd | 2 +- man/move_episode.Rd | 10 +++++----- man/set_dropdown.Rd | 2 +- 7 files changed, 24 insertions(+), 24 deletions(-) diff --git a/R/build_lesson.R b/R/build_lesson.R index 293e311ab..be5c12452 100644 --- a/R/build_lesson.R +++ b/R/build_lesson.R @@ -55,7 +55,7 @@ #' #' tmp <- tempfile() #' create_lesson(tmp, open = FALSE, rmd = FALSE) -#' create_episode("first-script", path = tmp) +#' create_episode("first-script", path = tmp, open = FALSE) #' check_lesson(tmp) #' build_lesson(tmp) build_lesson <- function(path = ".", rebuild = FALSE, quiet = !interactive(), preview = TRUE, override = list()) { diff --git a/R/create_lesson.R b/R/create_lesson.R index 5c91ba819..db87fc7d4 100644 --- a/R/create_lesson.R +++ b/R/create_lesson.R @@ -75,7 +75,7 @@ create_lesson <- function(path, name = fs::path_file(path), rmd = TRUE, rstudio create_site(path) cli::cli_status_update("{cli::symbol$arrow_right} Creating first episode ...") - ep <- create_episode("introduction", ext = if (rmd) "Rmd" else "md", path = path) + ep <- create_episode("introduction", ext = if (rmd) "Rmd" else "md", path = path, open = FALSE) cli::cli_alert_success("First episode created in {.file {ep}}") if (rstudio) { diff --git a/R/move_episode.R b/R/move_episode.R index d83520934..f15c5e611 100644 --- a/R/move_episode.R +++ b/R/move_episode.R @@ -18,10 +18,10 @@ #' if (interactive() || Sys.getenv("CI") != "") { #' tmp <- tempfile() #' create_lesson(tmp) -#' create_episode_md("getting-started", path = tmp) -#' create_episode_rmd("plotting", path = tmp) -#' create_episode_md("experimental", path = tmp, add = FALSE) -#' set_episodes(tmp, c("getting-started.md", "introduction.Rmd", "plotting.Rmd"), +#' create_episode_md("getting-started", path = tmp, open = FALSE) +#' create_episode_rmd("plotting", path = tmp, open = FALSE) +#' create_episode_md("experimental", path = tmp, add = FALSE, open = FALSE) +#' set_episodes(tmp, c("getting-started.md", "introduction.Rmd", "plotting.Rmd"), #' write = TRUE) #' #' # Default episode order is alphabetical, we can use this to nudge episodes @@ -29,11 +29,11 @@ #' move_episode("introduction.Rmd", 1L, path = tmp) # by default, it shows you the change #' move_episode("introduction.Rmd", 1L, write = TRUE, path = tmp) # write the results #' get_episodes(tmp) -#' +#' #' # Add episodes from the drafts #' get_drafts(tmp) #' move_episode("experimental.md", 2L, path = tmp) # view where it will live -#' move_episode("experimental.md", 2L, write = TRUE, path = tmp) +#' move_episode("experimental.md", 2L, write = TRUE, path = tmp) #' get_episodes(tmp) #' #' # Unpublish episodes by setting position to zero @@ -92,7 +92,7 @@ move_episode <- function(ep = NULL, position = NULL, write = FALSE, path = ".") # be coerced to 0L. position <- if (isTRUE(position)) n else position } - + eps <- eps[-ins] n <- length(eps) if (n == 0) { @@ -120,11 +120,11 @@ move_episode <- function(ep = NULL, position = NULL, write = FALSE, path = ".") #' Have user select position for an episode from a list #' -#' This function is interactive at the while loop where it will check if the +#' This function is interactive at the while loop where it will check if the #' position element is finite (failing on anything that can not be coerced to #' an integer) and if it is in bounds. It will repeat until a correct choice has #' been selected. -#' +#' #' For testing, it will return -1 and trigger an error in `move_episode()` #' #' @param eps a vector of episode names @@ -136,7 +136,7 @@ user_find_position <- function(eps, draft = FALSE) { position <- -1L cli::cli_div() cli::cli_alert_info("Select a number to insert your episode") - cli::cli_text("(if an episode already occupies that position, it will be shifted down)") + cli::cli_text("(if an episode already occupies that position, it will be shifted down)") cli::cli_text() choices <- if (draft) c(eps, "[insert at end]") else eps n <- length(choices) @@ -157,14 +157,14 @@ user_find_position <- function(eps, draft = FALSE) { #' files by a two-digit number to force a specific order by filename. This #' function will strip these numbers from the filename and set the schedule #' according to the original order. -#' +#' #' @inheritParams move_episode -#' @return when `write = TRUE`, the modified list of episodes. When +#' @return when `write = TRUE`, the modified list of episodes. When #' `write = FALSE`, the modified call is returned. #' #' @note git will recognise this as deleting a file and then adding a new file #' in the stage. If you run `git add`, it should recognise that it is a rename. -#' +#' #' @export #' @seealso [create_episode()] for creating new episodes, [move_episode()] for #' moving individual episodes around. @@ -188,7 +188,7 @@ strip_prefix <- function(path = ".", write = FALSE) { scheduled_episodes <- all_episodes[all_episodes %in% episodes] moved_episodes <- trimws(sub("^[0-9]{2}(\\.[0-9]+)?[-]", "", scheduled_episodes, perl = TRUE)) if (write) { - fs::file_move(fs::path(epathodes, scheduled_episodes), + fs::file_move(fs::path(epathodes, scheduled_episodes), fs::path(epathodes, moved_episodes)) return(set_episodes(path = path, order = moved_episodes, write = TRUE)) } else { diff --git a/R/set_dropdown.R b/R/set_dropdown.R index dc92cf09d..658c03933 100644 --- a/R/set_dropdown.R +++ b/R/set_dropdown.R @@ -18,7 +18,7 @@ #' path = tmp, #' write = TRUE #' ) -#' create_episode("using-R", path = tmp) +#' create_episode("using-R", path = tmp, open = FALSE) #' print(sched <- get_episodes(tmp)) #' #' # reverse the schedule diff --git a/man/build_lesson.Rd b/man/build_lesson.Rd index fcf24d78b..67de32899 100644 --- a/man/build_lesson.Rd +++ b/man/build_lesson.Rd @@ -71,7 +71,7 @@ folders: tmp <- tempfile() create_lesson(tmp, open = FALSE, rmd = FALSE) -create_episode("first-script", path = tmp) +create_episode("first-script", path = tmp, open = FALSE) check_lesson(tmp) build_lesson(tmp) \dontshow{\}) # examplesIf} diff --git a/man/move_episode.Rd b/man/move_episode.Rd index 7fad74a64..b8b1e44a6 100644 --- a/man/move_episode.Rd +++ b/man/move_episode.Rd @@ -28,10 +28,10 @@ and episode, draft, or remove an episode from the schedule. if (interactive() || Sys.getenv("CI") != "") { tmp <- tempfile() create_lesson(tmp) - create_episode_md("getting-started", path = tmp) - create_episode_rmd("plotting", path = tmp) - create_episode_md("experimental", path = tmp, add = FALSE) - set_episodes(tmp, c("getting-started.md", "introduction.Rmd", "plotting.Rmd"), + create_episode_md("getting-started", path = tmp, open = FALSE) + create_episode_rmd("plotting", path = tmp, open = FALSE) + create_episode_md("experimental", path = tmp, add = FALSE, open = FALSE) + set_episodes(tmp, c("getting-started.md", "introduction.Rmd", "plotting.Rmd"), write = TRUE) # Default episode order is alphabetical, we can use this to nudge episodes @@ -43,7 +43,7 @@ if (interactive() || Sys.getenv("CI") != "") { # Add episodes from the drafts get_drafts(tmp) move_episode("experimental.md", 2L, path = tmp) # view where it will live - move_episode("experimental.md", 2L, write = TRUE, path = tmp) + move_episode("experimental.md", 2L, write = TRUE, path = tmp) get_episodes(tmp) # Unpublish episodes by setting position to zero diff --git a/man/set_dropdown.Rd b/man/set_dropdown.Rd index e15a16d8d..4724aba4f 100644 --- a/man/set_dropdown.Rd +++ b/man/set_dropdown.Rd @@ -41,7 +41,7 @@ set_config(c(title = "Absolutely Free Lesson", license = "CC0"), path = tmp, write = TRUE ) -create_episode("using-R", path = tmp) +create_episode("using-R", path = tmp, open = FALSE) print(sched <- get_episodes(tmp)) # reverse the schedule From 80855ae647391dab1cb6fbedc7950f5d39b86539 Mon Sep 17 00:00:00 2001 From: "Zhian N. Kamvar" Date: Wed, 22 Nov 2023 16:01:47 -0800 Subject: [PATCH 6/6] silence create_episode() msg in tests We have the tests to ensure the basic functionality works and we know that usethis is testing for the messaging funcationality, we do not need to further test it and it is better to silence the messages than test for them. --- tests/testthat/test-build_lesson.R | 7 +++-- tests/testthat/test-build_markdown.R | 4 ++- tests/testthat/test-create_episode.R | 45 +++++++++++++++------------ tests/testthat/test-get_drafts.R | 4 ++- tests/testthat/test-get_dropdown.R | 4 ++- tests/testthat/test-get_episode.R | 23 ++++++-------- tests/testthat/test-get_syllabus.R | 4 ++- tests/testthat/test-move_episode.R | 26 +++++++++------- tests/testthat/test-set_dropdown.R | 4 +-- tests/testthat/test-utils-aggregate.R | 10 +++--- 10 files changed, 73 insertions(+), 58 deletions(-) diff --git a/tests/testthat/test-build_lesson.R b/tests/testthat/test-build_lesson.R index fe41f5990..493150583 100644 --- a/tests/testthat/test-build_lesson.R +++ b/tests/testthat/test-build_lesson.R @@ -235,9 +235,10 @@ test_that("Lesson websites contains instructor metadata", { }) test_that("single files can be built", { - - create_episode("_Second_ Episode!", path = tmp) - suppressMessages(s <- get_episodes(tmp)) + suppressMessages({ + create_episode("_Second_ Episode!", path = tmp, open = FALSE) + s <- get_episodes(tmp) + }) set_episodes(tmp, s, write = TRUE) rdr <- sandpaper_site(fs::path(tmp, "episodes", "second-episode.Rmd")) diff --git a/tests/testthat/test-build_markdown.R b/tests/testthat/test-build_markdown.R index 20e3f6b98..f227f6dd6 100644 --- a/tests/testthat/test-build_markdown.R +++ b/tests/testthat/test-build_markdown.R @@ -1,7 +1,9 @@ # setup test fixture { tmp <- res <- restore_fixture() - create_episode("second-episode", path = tmp) + suppressMessages({ + create_episode("second-episode", path = tmp, open = FALSE) + }) instruct <- fs::path(tmp, "instructors", "pyramid.md") writeLines( c( diff --git a/tests/testthat/test-create_episode.R b/tests/testthat/test-create_episode.R index a05771b1f..e9dca1464 100644 --- a/tests/testthat/test-create_episode.R +++ b/tests/testthat/test-create_episode.R @@ -1,13 +1,14 @@ tmp <- res <- restore_fixture() test_that("non-prefixed episodes can be created", { - initial_episode <- fs::dir_ls(fs::path(tmp, "episodes"), glob = "*Rmd") %>% expect_length(1L) %>% expect_match("introduction.Rmd") - second_episode <- create_episode_md("First Markdown", path = tmp) %>% - expect_match("first-markdown.md", fixed = TRUE) + suppressMessages({ + second_episode <- create_episode_md("First Markdown", path = tmp, open = FALSE) %>% + expect_match("first-markdown.md", fixed = TRUE) + }) expect_equal(get_episodes(tmp), c("introduction.Rmd", "first-markdown.md")) ep1 <- readLines(initial_episode) @@ -15,43 +16,48 @@ test_that("non-prefixed episodes can be created", { expect_equal(ep1[[2]], "title: 'introduction'") expect_true(any(grepl("^```[{]r pyramid", ep1))) # first episode will have R Markdown - + expect_equal(ep2[[2]], "title: 'First Markdown'") expect_no_match(ep2, "^```[{]r pyramid") # second episode will not have R Markdown expect_no_match(ep2, "^Or you") # second episode will not have R Markdown - }) test_that("un-prefixed episodes can be created", { - skip_on_os("windows") # y'all ain't ready for this title <- "\uC548\uB155 :joy_cat: \U0001F62D KITTY" - third_episode <- create_episode_rmd(title, path = tmp) %>% - expect_match("\uC548\uB155-\U0001F62D-kitty.Rmd", fixed = TRUE) + suppressMessages({ + third_episode <- create_episode_rmd(title, path = tmp, open = FALSE) %>% + expect_match("\uC548\uB155-\U0001F62D-kitty.Rmd", fixed = TRUE) + }) expect_equal(get_episodes(tmp), c("introduction.Rmd", "first-markdown.md", "\uC548\uB155-\U0001F62D-kitty.Rmd")) - expect_equal(readLines(third_episode, n = 2)[[2]], - paste0("title: '", title, "'")) + expect_equal( + readLines(third_episode, n = 2)[[2]], + paste0("title: '", title, "'") + ) }) test_that("draft episodes can be drafted", { - skip_on_os("windows") # y'all ain't ready for this - draft_episode_md("ignore-me", path = tmp) + suppressMessages({ + draft_episode_md("ignore-me", path = tmp, open = FALSE) + }) expect_equal(get_episodes(tmp), c("introduction.Rmd", "first-markdown.md", "\uC548\uB155-\U0001F62D-kitty.Rmd")) - draft_episode_rmd("ignore-me-in-r", path = tmp) + + suppressMessages({ + draft_episode_rmd("ignore-me-in-r", path = tmp, open = FALSE) + }) expect_equal(get_episodes(tmp), c("introduction.Rmd", "first-markdown.md", "\uC548\uB155-\U0001F62D-kitty.Rmd")) expect_setequal( - as.character(fs::path_file(get_drafts(tmp, message = FALSE))), - c("ignore-me.md", "ignore-me-in-r.Rmd")) - + as.character(fs::path_file(get_drafts(tmp, message = FALSE))), + c("ignore-me.md", "ignore-me-in-r.Rmd") + ) }) test_that("prefixed episodes can be reverted", { - # setup: create episodes with prefixes and remove the schedule skip_on_os("windows") # y'all ain't ready for this episodes <- get_episodes(tmp) @@ -64,14 +70,13 @@ test_that("prefixed episodes can be reverted", { set_episodes(tmp, new_episodes, write = TRUE) expect_equal(get_episodes(tmp), new_episodes) expect_snapshot(strip_prefix(tmp, write = FALSE)) - + # check that nothing has been written and then rewrite the episodes expect_equal(get_episodes(tmp), new_episodes) strip_prefix(tmp, write = TRUE) - + # none of the draft episodes should appear here expect_equal(get_episodes(tmp), episodes) expect_snapshot(strip_prefix(tmp, write = FALSE), transform = function(x) trimws(x)) - }) diff --git a/tests/testthat/test-get_drafts.R b/tests/testthat/test-get_drafts.R index 0f1144921..77221f9ba 100644 --- a/tests/testthat/test-get_drafts.R +++ b/tests/testthat/test-get_drafts.R @@ -1,6 +1,8 @@ { res <- restore_fixture() - create_episode("new", path = res) + suppressMessages({ + create_episode("new", path = res, open = FALSE) + }) } cli::test_that_cli("Default state reports all episodes published", { diff --git a/tests/testthat/test-get_dropdown.R b/tests/testthat/test-get_dropdown.R index 1095f4d6a..af0397996 100644 --- a/tests/testthat/test-get_dropdown.R +++ b/tests/testthat/test-get_dropdown.R @@ -1,6 +1,8 @@ { tmp <- res <- restore_fixture() -create_episode("outroduction", path = res) +suppressMessages({ + create_episode("outroduction", path = res, open = FALSE) +}) outro <- fs::path(res, "episodes", "outroduction.Rmd") fs::file_move(outro, fs::path_ext_set(outro, "md")) diff --git a/tests/testthat/test-get_episode.R b/tests/testthat/test-get_episode.R index 6b6b128b2..8d6f68a45 100644 --- a/tests/testthat/test-get_episode.R +++ b/tests/testthat/test-get_episode.R @@ -1,11 +1,10 @@ { -tmp <- res <- restore_fixture() -suppressMessages(e <- get_episodes(res)) -set_episodes(res, e, write = TRUE) + tmp <- res <- restore_fixture() + suppressMessages(e <- get_episodes(res)) + set_episodes(res, e, write = TRUE) } cli::test_that_cli("set_episode() will throw an error if an episode does not exist", { - bad <- c(e, "I-do-not-exist.md") expect_snapshot(expect_error(set_episodes(res, bad, write = TRUE))) @@ -14,29 +13,28 @@ cli::test_that_cli("set_episode() will throw an error if an episode does not exi # The output equals the only episode in there expect_equal(bad_out, e) - }) cli::test_that_cli("get_episode() will throw a message about episode in draft", { - withr::local_options(list("sandpaper.show_draft" = TRUE)) - if (!fs::file_exists(fs::path(res, "episodes", "new.Rmd"))) { - create_episode("new", add = FALSE, path = res) + needs_episode <- !fs::file_exists(fs::path(res, "episodes", "new.Rmd")) + if (needs_episode) { + suppressMessages({ + create_episode("new", add = FALSE, path = res, open = FALSE) + }) } expect_snapshot(drafty_out <- get_episodes(res)) expect_equal(drafty_out, e) - }) cli::test_that_cli("get_episode() will throw a warning if an episode in config does not exist", { - # Create a new episode that does not exist cfg <- readLines(fs::path(res, "config.yaml"), encoding = "UTF-8") episode_line <- grep("^episodes", cfg) new_cfg <- c( - cfg[seq(episode_line + 1L)], - "- I-am-an-impostor.md", + cfg[seq(episode_line + 1L)], + "- I-am-an-impostor.md", cfg[seq(episode_line + 2L, length(cfg))] ) @@ -45,5 +43,4 @@ cli::test_that_cli("get_episode() will throw a warning if an episode in config d writeLines(new_cfg, fs::path(res, "config.yaml")) expect_snapshot(expect_error(get_episodes(res))) - }) diff --git a/tests/testthat/test-get_syllabus.R b/tests/testthat/test-get_syllabus.R index 1de01c42a..f6036e79d 100644 --- a/tests/testthat/test-get_syllabus.R +++ b/tests/testthat/test-get_syllabus.R @@ -20,7 +20,9 @@ test_that("syllabus can be extracted from source files", { test_that("syllabus will update with new files", { - create_episode("postroduction", path = tmp, add = TRUE) + suppressMessages({ + create_episode("postroduction", path = tmp, add = TRUE, open = FALSE) + }) res <- get_syllabus(tmp, questions = TRUE) expect_named(res, c("episode", "timings", "path", "percents", "questions")) expect_equal(nrow(res), 3) diff --git a/tests/testthat/test-move_episode.R b/tests/testthat/test-move_episode.R index 9ace118c0..55d136e4a 100644 --- a/tests/testthat/test-move_episode.R +++ b/tests/testthat/test-move_episode.R @@ -1,14 +1,16 @@ { res <- restore_fixture() - create_episode("new", add = FALSE, path = res) - create_episode("new too", add = FALSE, path = res) - create_episode("new mewtwo three", add = FALSE, path = res) + suppressMessages({ + create_episode("new", add = FALSE, path = res, open = FALSE) + create_episode("new too", add = FALSE, path = res, open = FALSE) + create_episode("new mewtwo three", add = FALSE, path = res, open = FALSE) + }) eporder <- c("introduction.Rmd", "new.Rmd", "new-mewtwo-three.Rmd", "new-too.Rmd") set_episodes(res, eporder, write = TRUE) } test_that("all episodes are present in the config file", { - + eps <- get_config(res)$episodes expect_setequal(eps, fs::path_file(get_sources(res, "episodes"))) expect_equal(eps, eporder) @@ -31,7 +33,7 @@ test_that("Errors happen with invalid position arguments", { cli::test_that_cli("no position will trigger an interactive search", { - expect_snapshot(tryCatch(move_episode(1, path = res), + expect_snapshot(tryCatch(move_episode(1, path = res), error = function(e) e$message)) }) @@ -46,20 +48,20 @@ cli::test_that_cli("Episodes can be moved to a different position", { expect_snapshot(move_episode("introduction.Rmd", 4, write = FALSE, path = res)) expect_equal(get_episodes(res), eporder) - + expect_snapshot(move_episode(4, 3, write = FALSE, path = res)) expect_equal(get_episodes(res), eporder) move_episode(4, 3, write = TRUE, path = res) - expect_equal(get_episodes(res), + expect_equal(get_episodes(res), c("introduction.Rmd", "new.Rmd", "new-too.Rmd", "new-mewtwo-three.Rmd")) move_episode("introduction.Rmd", 4, write = TRUE, path = res) - expect_equal(get_episodes(res), + expect_equal(get_episodes(res), c("new.Rmd", "new-too.Rmd", "new-mewtwo-three.Rmd", "introduction.Rmd")) move_episode("introduction.Rmd", 1, write = TRUE, path = res) - expect_equal(get_episodes(res), + expect_equal(get_episodes(res), c("introduction.Rmd", "new.Rmd", "new-too.Rmd", "new-mewtwo-three.Rmd")) }) @@ -80,14 +82,14 @@ cli::test_that_cli("Episodes can be moved out of position", { expect_equal(get_episodes(res), eporder) move_episode(3, 0, write = TRUE, path = res) - expect_equal(get_episodes(res), + expect_equal(get_episodes(res), c("introduction.Rmd", "new.Rmd", "new-too.Rmd")) }) cli::test_that_cli("no position will trigger an interactive search", { - expect_snapshot(tryCatch(move_episode("new-mewtwo-three.Rmd", path = res), + expect_snapshot(tryCatch(move_episode("new-mewtwo-three.Rmd", path = res), error = function(e) e$message)) }) @@ -123,7 +125,7 @@ cli::test_that_cli("Drafts can be added to the index", { expect_equal(get_episodes(res), eporder[-3]) expect_snapshot(move_episode("new-mewtwo-three.Rmd", 4, write = TRUE, path = res)) - expect_equal(get_episodes(res), + expect_equal(get_episodes(res), c("introduction.Rmd", "new.Rmd", "new-too.Rmd", "new-mewtwo-three.Rmd")) }) diff --git a/tests/testthat/test-set_dropdown.R b/tests/testthat/test-set_dropdown.R index e197e0063..81d445bb1 100644 --- a/tests/testthat/test-set_dropdown.R +++ b/tests/testthat/test-set_dropdown.R @@ -69,7 +69,7 @@ test_that("schedule is empty by default", { test_that("new episodes will add to the schedule by default", { set_episodes(tmp, "introduction.Rmd", write = TRUE) - create_episode("new", path = tmp) + suppressMessages(create_episode("new", path = tmp, open = FALSE)) expect_equal(get_episodes(tmp), c("introduction.Rmd", "new.Rmd"), ignore_attr = TRUE) }) @@ -114,7 +114,7 @@ test_that("adding episodes will concatenate the schedule", { set_episodes(tmp, "introduction.Rmd", write = TRUE) expect_equal(get_episodes(tmp), "introduction.Rmd") - create_episode("second-episode", add = TRUE, path = tmp) + suppressMessages(create_episode("second-episode", add = TRUE, path = tmp, open = FALSE)) expect_equal(res, tmp, ignore_attr = TRUE) expect_equal(get_episodes(tmp), c("introduction.Rmd", "second-episode.Rmd"), ignore_attr = TRUE) diff --git a/tests/testthat/test-utils-aggregate.R b/tests/testthat/test-utils-aggregate.R index ad1b81aa7..9a83cc036 100644 --- a/tests/testthat/test-utils-aggregate.R +++ b/tests/testthat/test-utils-aggregate.R @@ -28,10 +28,12 @@ test_that("aggregate pages do not exorcise self-similar slugs", { res <- restore_fixture() withr::local_options(list("sandpaper.use_renv" = FALSE)) withr::defer(clear_globals()) - create_episode_md("images and pixels", path = res, add = TRUE) - create_episode_md("keypoints and others", path = res, add = TRUE) - create_episode_md("instructor notes and things", path = res, add = TRUE) - create_episode_md("aio stands for an information overload", path = res, add = TRUE) + suppressMessages({ + create_episode_md("images and pixels", path = res, add = TRUE, open = FALSE) + create_episode_md("keypoints and others", path = res, add = TRUE, open = FALSE) + create_episode_md("instructor notes and things", path = res, add = TRUE, open = FALSE) + create_episode_md("aio stands for an information overload", path = res, add = TRUE, open = FALSE) + }) eps <- as.character(fs::path_ext_set(get_episodes(res), "html")) skip_if_not(rmarkdown::pandoc_available("2.11")) build_lesson(res, quiet = TRUE, preview = FALSE)