Skip to content

Commit

Permalink
rename function, add test and docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Abel committed Oct 24, 2024
1 parent 762df2b commit d0c72c6
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 3 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ S3method(sendObject,default)
S3method(syncAllFiles,awss3)
S3method(syncAllFiles,default)
export(awss3)
export(checkSystemResult)
export(getData)
export(getFile)
export(listFiles)
Expand Down
2 changes: 1 addition & 1 deletion R/awscli.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ awscli <- function(src, dest, includes = NULL, excludes = NULL, args = "", profi
# cat(command, "\n")

status <- system(command, intern = intern, wait = TRUE, ignore.stdout = FALSE, ignore.stderr = FALSE)
checkForStatus(status)
checkSystemResult(status)
}
28 changes: 26 additions & 2 deletions R/rsynccli.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,34 @@ rsynccli <- function(file, to, includes = NULL, excludes = NULL, args = "-rltvx"
)

status <- system(command, intern = intern, wait = TRUE, ignore.stdout = FALSE, ignore.stderr = FALSE)
checkForStatus(status)
checkSystemResult(status)
}

checkForStatus <- function(result) {

#' Check System Command Result for Exit Code
#'
#' Evaluates the result from R's \code{system()} function and checks for a
#' non-zero exit status. If the system command failed (i.e., returned a non-zero
#' exit status), the function throws an error. If the result contains the
#' command's output, it is returned.
#'
#' @param result The result of a \code{system()} function execution. This can be:
#' \itemize{
#' \item A character vector containing the command's output.
#' \item A numeric value representing the exit status code.
#' \item An object with a \code{status} attribute.
#' }
#' @return
#' \itemize{
#' \item If \code{result} is a character vector (command output) and has no
#' \code{status} attribute, returns the output.
#' \item If \code{result} is numeric (exit status code), returns the status
#' code.
#' \item If the command failed (non-zero exit status), the function stops
#' with an error message.
#' }
#' @export
checkSystemResult <- function(result) {
if (is.character(result) && is.null(attr(result, "status"))) {
sys_output <- result
return(sys_output)
Expand Down
32 changes: 32 additions & 0 deletions man/checkSystemResult.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions tests/testthat/test-checkForStatus.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
testthat::context("checkSystemResult")

test_that("checkSystemResult with intern = TRUE and successful command", {
command <- "true"
intern <- TRUE
status <- system(command, intern = intern, wait = TRUE)
result <- checkSystemResult(status)

expect_is(status, "character")
expect_is(result, "character")
expect_equal(result, status)
})

test_that("checkSystemResult with intern = FALSE and successful command", {
command <- "true"
intern <- FALSE
status <- system(command, intern = intern, wait = TRUE)
result <- checkSystemResult(status)

expect_is(status, "integer")
expect_is(result, "integer")
expect_equal(result, status)
})

test_that("checkSystemResult with intern = TRUE and failing command", {
command <- "false"
intern <- TRUE

expect_warning(
status <- system(command, intern = intern, wait = TRUE)
)
expect_error(checkSystemResult(status))
expect_is(status, "character")
expect_equal(attr(status, "status"), 1)
})

test_that("checkSystemResult with intern = FALSE and failing command", {
command <- "false"
intern <- FALSE

status <- system(command, intern = intern, wait = TRUE)
expect_error(checkSystemResult(status))
expect_is(status, "integer")
expect_equal(status, 1)
})

0 comments on commit d0c72c6

Please sign in to comment.