Skip to content

Commit

Permalink
fix: parsing error messages with curly braces
Browse files Browse the repository at this point in the history
  • Loading branch information
luciorq committed Jan 29, 2025
1 parent 137e959 commit 9d381ae
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: condathis
Title: Run Any CLI Tool on a 'Conda' Environment
Version: 0.1.1
Version: 0.1.1.9001
Authors@R: c(
person("Lucio", "Queiroz", , "luciorqueiroz@gmail.com", role = c("aut", "cre", "cph"),
comment = c(ORCID = "0000-0002-6090-1834")),
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# condathis 0.1.2 (dev)

## Minor improvements and fixes

* Fix parsing of error messages with curly braces in `run()` and `run_bin()`,
in the rethrown error, when `error = "cancel"`.

# condathis 0.1.1

## Minor improvements and fixes
Expand Down
11 changes: 10 additions & 1 deletion R/rethrow_error_cmd.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@ rethrow_error_cmd <- function(expr, env = parent.frame()) {
if (isFALSE(is.null(err_cnd))) {
additional_lines <- NULL
if (isTRUE("stderr" %in% names(err_cnd))) {
err_vector <- stringr::str_replace_all(
stringr::str_replace_all(
string = err_cnd[["stderr"]],
pattern = stringr::fixed("{"),
replacement = stringr::fixed("{{")
),
pattern = stringr::fixed("}"),
replacement = stringr::fixed("}}")
)
additional_lines <- stringr::str_split(
string = stringr::str_trim(err_cnd[["stderr"]]),
string = stringr::str_trim(err_vector),
pattern = stringr::regex("\\R"),
simplify = FALSE
)[[1]]
Expand Down
11 changes: 10 additions & 1 deletion R/rethrow_error_run.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@ rethrow_error_run <- function(expr, env = parent.frame()) {
if (isFALSE(is.null(err_cnd)) && !isFALSE(env[["error_var"]])) {
additional_lines <- NULL
if (isTRUE("stderr" %in% names(err_cnd))) {
err_vector <- stringr::str_replace_all(
stringr::str_replace_all(
string = err_cnd[["stderr"]],
pattern = stringr::fixed("{"),
replacement = stringr::fixed("{{")
),
pattern = stringr::fixed("}"),
replacement = stringr::fixed("}}")
)
additional_lines <- stringr::str_split(
string = stringr::str_trim(err_cnd[["stderr"]]),
string = stringr::str_trim(err_vector),
pattern = stringr::regex("\\R"),
simplify = FALSE
)[[1]]
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,16 @@ For example, the system’s `curl` is of a specific version:

``` r
libcurlVersion()
#> [1] "8.7.1"
#> [1] "8.11.1"
#> attr(,"ssl_version")
#> [1] "(SecureTransport) LibreSSL/3.3.6"
#> [1] "OpenSSL/3.4.0 (SecureTransport)"
#> attr(,"libssh_version")
#> [1] ""
#> [1] "libssh2/1.11.1"
#> attr(,"protocols")
#> [1] "dict" "file" "ftp" "ftps" "gopher" "gophers" "http"
#> [8] "https" "imap" "imaps" "ldap" "ldaps" "mqtt" "pop3"
#> [15] "pop3s" "rtsp" "smb" "smbs" "smtp" "smtps" "telnet"
#> [22] "tftp"
#> [8] "https" "imap" "imaps" "mqtt" "pop3" "pop3s" "rtsp"
#> [15] "scp" "sftp" "smb" "smbs" "smtp" "smtps" "telnet"
#> [22] "tftp" "ws" "wss"
```

However, we can choose to use a different version of `curl` run in a
Expand Down
46 changes: 46 additions & 0 deletions tests/testthat/test-rethrow_error.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
testthat::test_that("Do not execute code in curl braces", {
testthat::skip_if_offline()
testthat::skip_on_cran()

px_res <- create_env(
packages = c("r-base=4.1.3", "r-devtools"),
env_name = "condathis-test-env"
)

testthat::expect_error(
object = {
run(
"R", "-q", "-s", "-e", "stop(\"{ 5 + x }\")",
env_name = "condathis-test-env",
verbose = "silent",
error = "cancel"
)
},
class = "condathis_run_status_error"
)

testthat::expect_error(
object = {
run(
"R", "-q", "-s", "-e", "stop(\"{{{ 5 + x }}}\")",
env_name = "condathis-test-env",
verbose = "silent",
error = "cancel"
)
},
class = "condathis_run_status_error"
)

px_res <- rethrow_error_run(
expr = {
run(
"R", "-q", "-s", "-e", "stop(\"{{{ 5 + x }}}\")",
env_name = "condathis-test-env",
verbose = "silent",
error = "continue"
)
}
)

testthat::expect_true(px_res$status != 0L)
})

0 comments on commit 9d381ae

Please sign in to comment.