Skip to content

Commit

Permalink
don't allow editing/remove default prompts (closes #59)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpcouch committed Nov 7, 2024
1 parent 63bf547 commit cf6d469
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
2 changes: 2 additions & 0 deletions R/prompt.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ prompt_new <- function(role, interface, contents = NULL) {
#' @rdname prompt
#' @export
prompt_remove <- function(role) {
check_role(role)
path <- prompt_locate(role)
file.remove(path)

Expand All @@ -121,6 +122,7 @@ prompt_remove <- function(role) {
#' @rdname prompt
#' @export
prompt_edit <- function(role) {
check_role(role)
path <- prompt_locate(role)
if (interactive()) {
file.edit(path)
Expand Down
7 changes: 7 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ check_role <- function(role, call = caller_env()) {
)
}

if (role %in% default_roles & is.null(getOption(".pal_on_load"))) {
cli::cli_abort(
"Default roles cannot be edited or removed.",
call = call
)
}

invisible(role)
}

Expand Down
1 change: 1 addition & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

.onLoad <- function(libname, pkgname) {
pal_env <- pal_env()
withr::local_options(.pal_on_load = TRUE)

directory_load(system.file("prompts", package = "pal"))

Expand Down
32 changes: 24 additions & 8 deletions tests/testthat/_snaps/prompt.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@
! There's already a pal with role "boop".
i You can edit it with `prompt_edit("boop")`

---

Code
prompt_new("cli", "replace")
Condition
Error in `prompt_new()`:
! There's already a pal with role "cli".

# prompt_remove errors informatively with bad role

Code
Expand All @@ -47,3 +39,27 @@
Error in `prompt_new()`:
! `contents` must be a connection to a markdown file.

# default roles can't be overwritten or deleted (#59)

Code
prompt_new("cli", "replace")
Condition
Error in `prompt_new()`:
! Default roles cannot be edited or removed.

---

Code
prompt_edit("cli")
Condition
Error in `prompt_edit()`:
! Default roles cannot be edited or removed.

---

Code
prompt_remove("cli")
Condition
Error in `prompt_remove()`:
! Default roles cannot be edited or removed.

8 changes: 6 additions & 2 deletions tests/testthat/test-prompt.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ test_that("prompt_new errors informatively with redundant role", {

expect_snapshot(error = TRUE, prompt_new("boop", "replace"))
expect_snapshot(error = TRUE, prompt_new("boop", "prefix"))

expect_snapshot(error = TRUE, prompt_new("cli", "replace"))
})

test_that("prompt_new works when directory doesn't exist yet (#47)", {
Expand Down Expand Up @@ -130,6 +128,12 @@ test_that("prompts can be added, removed, and added again without restart (#58)"
expect_true(file.exists(path))
})

test_that("default roles can't be overwritten or deleted (#59)", {
expect_snapshot(error = TRUE, prompt_new("cli", "replace"))
expect_snapshot(error = TRUE, prompt_edit("cli"))
expect_snapshot(error = TRUE, prompt_remove("cli"))
})

test_that("is_markdown_file works", {
expect_true(is_markdown_file("some_file.md"))
expect_true(is_markdown_file("some_file.Md"))
Expand Down

0 comments on commit cf6d469

Please sign in to comment.