Skip to content

Commit

Permalink
handle no prompts case in directory_list() (closes #78)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpcouch committed Jan 24, 2025
1 parent ae6dace commit e85ef35
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
11 changes: 11 additions & 0 deletions R/directory.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ directory_load <- function(dir = directory_path()) {
directory_list <- function() {
prompt_dir <- directory_path()
prompt_base_names <- directory_base_names(prompt_dir)

if (identical(prompt_base_names, character(0))) {
if (interactive()) {
cli::cli_h3("No custom prompts.")
cli::cli_bullets(c(
"i" = "Create a new prompt with {.help [{.fun prompt_new}](pal::prompt_new)}."
))
}
return(invisible(character(0)))
}

prompt_paths <- paste0(prompt_dir, "/", prompt_base_names)
if (interactive()) {
cli::cli_h3("Prompts: ")
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/_snaps/directory.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,12 @@
! `dir` doesn't exist.
i If desired, create it with `dir.create("some/nonexistent/path", recursive = TRUE)`.

# directory_list returns empty and messages informatively when no files

Code
res <- directory_list()
Message
-- No custom prompts.
i Create a new prompt with `prompt_new()` (`?pal::prompt_new()`).

10 changes: 10 additions & 0 deletions tests/testthat/test-directory.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,13 @@ test_that("directory_load() doesn't warn with no trailing newline (#75)", {
expect_no_warning(directory_load(tmp_dir))
expect_true("test" %in% list_pals())
})

test_that("directory_list returns empty and messages informatively when no files", {
tmp_dir <- withr::local_tempdir()
withr::local_options(.pal_dir = tmp_dir)
testthat::local_mocked_bindings(interactive = function(...) {TRUE})

expect_snapshot(res <- directory_list())
expect_equal(res, character(0))
})

0 comments on commit e85ef35

Please sign in to comment.