Skip to content

Commit

Permalink
create prompt directory if it doesn't exist yet
Browse files Browse the repository at this point in the history
closes #47. thank you, hannah!
  • Loading branch information
simonpcouch committed Oct 18, 2024
1 parent bd1634b commit 026b2f0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion R/prompt.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ prompt_new <- function(role, interface, contents = NULL) {
))
}

path <- paste0(directory_path(), "/", role, "-", interface, ".md")
dir_path <- directory_path()
if (!dir.exists(dir_path)) {
dir.create(dir_path, recursive = TRUE)
}
path <- paste0(dir_path, "/", role, "-", interface, ".md")

# TODO: should this message "Register with `directory_load()`" or
# something as it creates the file?
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-prompt.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ test_that("prompt_new errors informatively with redundant role", {
expect_snapshot(error = TRUE, prompt_new("cli", "replace"))
})

test_that("prompt_new works when directory doesn't exist yet (#47)", {
subdir <- "test-prompt-dir/subdir"
withr::local_options(.pal_dir = subdir)
testthat::local_mocked_bindings(interactive = function(...) {FALSE})

if (dir.exists(subdir)) {unlink(subdir, recursive = TRUE)}
if ("floop" %in% list_pals()) {.pal_remove("floop")}

withr::defer({
if (dir.exists(subdir)) {unlink(subdir, recursive = TRUE)}
if ("floop" %in% list_pals()) {.pal_remove("floop")}
})

.res <- prompt_new("floop", "replace")
expect_equal(.res, paste0(subdir, "/floop-replace.md"))
expect_true(file.exists(.res))
})

test_that("prompt_remove errors informatively with bad role", {
# contains two prompts, `boop-replace` and `wop-prefix`
withr::local_options(.pal_dir = "test-prompt-dir")
Expand Down

0 comments on commit 026b2f0

Please sign in to comment.