Skip to content

Commit

Permalink
Add run_file checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanzomorrodi committed Dec 30, 2024
1 parent 39d2b32 commit eb08f47
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
2 changes: 1 addition & 1 deletion R/engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ sas_engine <- function (options) {
paste0(
"ods html3 file='", sas_temp_html_path,
"' gpath = '", sas_temp_dir,
"' (no_top_matter no_bottom_matter) style=journal;\n"
"' (no_top_matter no_bottom_matter);\n"
),
sep = "\n"
)
Expand Down
40 changes: 18 additions & 22 deletions R/run-file.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,47 +24,43 @@ sas_run_file <- function(input_path, output_path, overwrite = FALSE) {
chk::chk_not_missing(input_path)
chk::chk_string(input_path)
chk::chk_file(input_path)

input <- read_file(input_path)
if (missing(output_path)) {
return(sas_run_string(input))
} else {
chk::chk_string(output_path)
chk::chk_ext(output_path, "html")
chk::chk_logical(overwrite)
}

output_dir <- dirname(output_path)
output_file <- basename(output_path)
output_file_name <- substr(output_file, 0, nchar(output_file) - 5)
output_file_ext <- substr(output_file, nchar(output_file) - 4, nchar(output_file))
output_log_path <- file.path(output_dir, paste0(output_file_name, ".log"))

if (output_file_ext != ".html") {
chk::err("Output file extension must end in .html")
if (!overwrite) {
chk_no_file(output_path)
chk_no_file(output_log_path)
}

results <- .pkgenv$session$submit(input)

write_file(
output = paste(results$LST, collapse = "\n"),
path = output_path,
overwrite
)
write_file(
output = paste(results$LOG, collapse = "\n"),
path = file.path(output_dir, paste0(output_file_name, ".log")),
overwrite
)
cat(paste(results$LST, collapse = "\n"), file = output_path)
cat(paste(results$LOG, collapse = "\n"), file = output_log_path)

invisible()
}

read_file <- function(path) {
readChar(path, file.info(path)$size)
}

write_file <- function(output, path, overwrite) {
if (file.exists(path) && !overwrite) {
chk::err("Output file already exists. If you would like to overwrite the file, use overwrite = TRUE.")
chk_no_file <- function(x, x_name = NULL) {
if (vld_no_file(x)) {
return(invisible(x))
}
if (is.null(x_name)) x_name <- chk::deparse_backtick_chk(substitute(x))
chk::abort_chk(x_name, " already exists. If you would like to overwrite the file, use overwrite = TRUE.")
}
vld_no_file <- function(x) !file.exists(x)

cat(output, file = path)
read_file <- function(path) {
readChar(path, file.info(path)$size)
}
2 changes: 1 addition & 1 deletion tests/testthat/test-run-file.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ test_that("overwrite output html and log from file", {
"don't overwrite"
cat("PROC MEANS DATA = sashelp.cars; RUN;", file = local_path)
cat("test", file = local_html)
expect_error(sas_run_file(local_path, local_html), "Output file already exists. If you would like to overwrite the file, use overwrite = TRUE.", fixed = TRUE)
expect_error(sas_run_file(local_path, local_html), "already exists. If you would like to overwrite the file, use overwrite = TRUE.", fixed = TRUE)

"overwrite output"
sas_run_file(local_path, local_html, overwrite = TRUE)
Expand Down

0 comments on commit eb08f47

Please sign in to comment.