diff --git a/.Rbuildignore b/.Rbuildignore index 8d89da3..97149ec 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -7,3 +7,5 @@ ^README.Rmd$ ^docs$ ^\.github$ +^[.]?air[.]toml$ +^\.vscode$ diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..344f76e --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,5 @@ +{ + "recommendations": [ + "Posit.air-vscode" + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a6f7e54 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,10 @@ +{ + "[r]": { + "editor.formatOnSave": true, + "editor.defaultFormatter": "Posit.air-vscode" + }, + "[quarto]": { + "editor.formatOnSave": true, + "editor.defaultFormatter": "quarto.quarto" + } +} diff --git a/R/functions.R b/R/functions.R index b18d4f3..84ee798 100644 --- a/R/functions.R +++ b/R/functions.R @@ -1,4 +1,3 @@ - number_of_files <- function(bucket_name) { chk::chk_string(bucket_name) @@ -28,6 +27,8 @@ ask_to_overwrite <- function(directory, ask) { return(TRUE) } - usethis::ui_yeah(paste0("The folder is not empty. Files may be overwritten. ", - "Proceed?")) + usethis::ui_yeah(paste0( + "The folder is not empty. Files may be overwritten. ", + "Proceed?" + )) } diff --git a/R/rwa-download-files.R b/R/rwa-download-files.R index 921a431..9d6e882 100644 --- a/R/rwa-download-files.R +++ b/R/rwa-download-files.R @@ -60,14 +60,16 @@ #' } #' @export #' -rwa_download_files <- function(file_list, - directory, - bucket_name, - silent = FALSE, - ask = TRUE, - aws_access_key_id = Sys.getenv("AWS_ACCESS_KEY_ID"), - aws_secret_access_key = Sys.getenv("AWS_SECRET_ACCESS_KEY"), - region = Sys.getenv("AWS_REGION", "ca-central-1")) { +rwa_download_files <- function( + file_list, + directory, + bucket_name, + silent = FALSE, + ask = TRUE, + aws_access_key_id = Sys.getenv("AWS_ACCESS_KEY_ID"), + aws_secret_access_key = Sys.getenv("AWS_SECRET_ACCESS_KEY"), + region = Sys.getenv("AWS_REGION", "ca-central-1") +) { chk::chk_character(file_list) chk::chk_dir(directory) chk::chk_string(bucket_name) diff --git a/R/rwa-list-files.R b/R/rwa-list-files.R index 33df27b..395ab57 100644 --- a/R/rwa-list-files.R +++ b/R/rwa-list-files.R @@ -56,13 +56,15 @@ #' ) #' } #' @export -rwa_list_files <- function(bucket_name, - max_request_size = 1000, - pattern = ".*", - silent = FALSE, - aws_access_key_id = Sys.getenv("AWS_ACCESS_KEY_ID"), - aws_secret_access_key = Sys.getenv("AWS_SECRET_ACCESS_KEY"), - region = Sys.getenv("AWS_REGION", "ca-central-1")) { +rwa_list_files <- function( + bucket_name, + max_request_size = 1000, + pattern = ".*", + silent = FALSE, + aws_access_key_id = Sys.getenv("AWS_ACCESS_KEY_ID"), + aws_secret_access_key = Sys.getenv("AWS_SECRET_ACCESS_KEY"), + region = Sys.getenv("AWS_REGION", "ca-central-1") +) { chk::chk_string(bucket_name) chk::chk_whole_number(max_request_size) chk::chk_gt(max_request_size, value = 0) @@ -74,14 +76,17 @@ rwa_list_files <- function(bucket_name, input_max_request <- max_request_size start_after <- "" all_keys <- c() - s3 <- paws::s3(config = list( - credentials = list(creds = list( - access_key_id = aws_access_key_id, - secret_access_key = aws_secret_access_key - )), - region = region - )) - + s3 <- paws::s3( + config = list( + credentials = list( + creds = list( + access_key_id = aws_access_key_id, + secret_access_key = aws_secret_access_key + ) + ), + region = region + ) + ) while (max_request_size > 0) { key_list <- s3$list_objects_v2( @@ -89,9 +94,13 @@ rwa_list_files <- function(bucket_name, MaxKeys = max_request_size, StartAfter = start_after ) - key_names <- vapply(key_list$Contents, function(x) { - x$Key - }, "") + key_names <- vapply( + key_list$Contents, + function(x) { + x$Key + }, + "" + ) all_keys <- c(all_keys, key_names) start_after <- key_names[length(key_names)] max_request_size <- max_request_size - 1000 @@ -103,19 +112,24 @@ rwa_list_files <- function(bucket_name, usethis::ui_info(msg) if (input_max_request == no_all_keys) { - usethis::ui_warn(paste("{usethis::ui_path('Max_request_size')} matches", - "retrieved files from AWS. Think about", - "increasing", - "{usethis::ui_path('Max_request_size')}", - "as there could be more files present")) + usethis::ui_warn(paste( + "{usethis::ui_path('Max_request_size')} matches", + "retrieved files from AWS. Think about", + "increasing", + "{usethis::ui_path('Max_request_size')}", + "as there could be more files present" + )) } } filter_files <- grep(pattern, all_keys, value = TRUE) if (!silent) { - msg <- paste(length(filter_files), "files returned after", - "{usethis::ui_path('pattern')} is applied") + msg <- paste( + length(filter_files), + "files returned after", + "{usethis::ui_path('pattern')} is applied" + ) usethis::ui_info(msg) } diff --git a/R/rwa-list-su-files.R b/R/rwa-list-su-files.R index 95338a1..5a67e8e 100644 --- a/R/rwa-list-su-files.R +++ b/R/rwa-list-su-files.R @@ -24,18 +24,20 @@ #' extension = "xlsx", year = 2021) #' } #' @export -rwa_list_su_files <- function(bucket_name, - data_type = NULL, - year = NULL, - month = NULL, - day = NULL, - file_name = NULL, - file_extension = NULL, - max_request_size = 1000, - silent = FALSE, - aws_access_key_id = Sys.getenv("AWS_ACCESS_KEY_ID"), - aws_secret_access_key = Sys.getenv("AWS_SECRET_ACCESS_KEY"), - region = Sys.getenv("AWS_REGION", "ca-central-1")) { +rwa_list_su_files <- function( + bucket_name, + data_type = NULL, + year = NULL, + month = NULL, + day = NULL, + file_name = NULL, + file_extension = NULL, + max_request_size = 1000, + silent = FALSE, + aws_access_key_id = Sys.getenv("AWS_ACCESS_KEY_ID"), + aws_secret_access_key = Sys.getenv("AWS_SECRET_ACCESS_KEY"), + region = Sys.getenv("AWS_REGION", "ca-central-1") +) { chk::chk_null_or(data_type, vld = chk::vld_string) chk::chk_null_or(year, vld = chk::vld_whole_number) chk::chk_range(year, range = c(1900, 2100)) @@ -59,13 +61,9 @@ rwa_list_su_files <- function(bucket_name, month <- pad_dates(month) date <- paste(year, month, day, sep = "-") - regex_pattern <- paste(data_type, - date, - file_name, - file_extension, - sep = ".*" - ) - file_list <- rwa_list_files(bucket_name, + regex_pattern <- paste(data_type, date, file_name, file_extension, sep = ".*") + file_list <- rwa_list_files( + bucket_name, pattern = regex_pattern, max_request_size = max_request_size, silent = silent, diff --git a/R/rwa-upload-files.R b/R/rwa-upload-files.R index a1c0e7a..c26a5b6 100644 --- a/R/rwa-upload-files.R +++ b/R/rwa-upload-files.R @@ -54,13 +54,15 @@ #' } #' #' @export -rwa_upload_files <- function(file_list, - directory = ".", - bucket_name, - bucket_path = "", - aws_access_key_id = Sys.getenv("AWS_ACCESS_KEY_ID"), - aws_secret_access_key = Sys.getenv("AWS_SECRET_ACCESS_KEY"), - region = Sys.getenv("AWS_REGION", "ca-central-1")) { +rwa_upload_files <- function( + file_list, + directory = ".", + bucket_name, + bucket_path = "", + aws_access_key_id = Sys.getenv("AWS_ACCESS_KEY_ID"), + aws_secret_access_key = Sys.getenv("AWS_SECRET_ACCESS_KEY"), + region = Sys.getenv("AWS_REGION", "ca-central-1") +) { chk::chk_character(file_list) chk::chk_null_or(directory, vld = chk::vld_dir) chk::chk_string(bucket_name) @@ -69,13 +71,17 @@ rwa_upload_files <- function(file_list, chk::chk_string(aws_secret_access_key) chk::chk_string(region) - s3 <- paws::s3(config = list( - credentials = list(creds = list( - access_key_id = aws_access_key_id, - secret_access_key = aws_secret_access_key - )), - region = region - )) + s3 <- paws::s3( + config = list( + credentials = list( + creds = list( + access_key_id = aws_access_key_id, + secret_access_key = aws_secret_access_key + ) + ), + region = region + ) + ) for (file in file_list) { local_path <- paste0(directory, "/", file) diff --git a/air.toml b/air.toml new file mode 100644 index 0000000..283b9be --- /dev/null +++ b/air.toml @@ -0,0 +1,7 @@ +[format] +line-width = 80 +indent-width = 2 +indent-style = "space" +line-ending = "auto" +persistent-line-breaks = true +default-exclude = true diff --git a/scripts/build.R b/scripts/build.R index dbd401a..c5206a0 100644 --- a/scripts/build.R +++ b/scripts/build.R @@ -1,10 +1,37 @@ -roxygen2md::roxygen2md() -styler::style_pkg(filetype = c("R", "Rmd")) -lintr::lint_package() +styler::style_pkg( + scope = "line_breaks", + filetype = c("R", "Rmd") +) -devtools::test() +lintr::lint_package( + linters = lintr::linters_with_defaults( + line_length_linter = lintr::line_length_linter(1000), + object_name_linter = lintr::object_name_linter(regexes = ".*") + ) +) + +roxygen2md::roxygen2md() devtools::document() +devtools::build_readme() + +# Note: Only use pkgdown to build a documentation website for public facing packages +pkgdown::build_home() +pkgdown::build_reference() pkgdown::build_site() +browseURL("docs/index.html") +devtools::test() devtools::check() + +# Test files that have less then 100% coverage +covr:::tally_coverage(covr::package_coverage()) |> + dplyr::summarize( + percent_coverage = mean(value > 0) * 100, + .by = filename + ) |> + dplyr::filter(percent_coverage < 100) |> + dplyr::arrange(percent_coverage) + +# Report for all test files +covr::report(covr::package_coverage()) diff --git a/tests/testthat/test-rwa-download-files.R b/tests/testthat/test-rwa-download-files.R index bf9f207..ac9007b 100644 --- a/tests/testthat/test-rwa-download-files.R +++ b/tests/testthat/test-rwa-download-files.R @@ -1,4 +1,3 @@ - bucket_name <- "readwriteaws-test-poissonconsulting" file_01 <- "shiny-upload/pdf/2023-08-02_09-39-40.535327_test_daddf0561fc45b4168bd38aad50bbc47/uploaded_file.pdf" @@ -44,57 +43,62 @@ files_in_list <- list(file_01, file_02) test_that("function should error if list of files instead of vector given", { directory <- withr::local_tempdir() - expect_error(rwa_download_files( - file_list = files_in_list, - directory = directory, - bucket_name = bucket_name - ), - regexp = "must be character" + expect_error( + rwa_download_files( + file_list = files_in_list, + directory = directory, + bucket_name = bucket_name + ), + regexp = "must be character" ) }) test_that("function should error when directory is not a string", { - expect_error(rwa_download_files( - file_list = multi_files, - directory = 01, - bucket_name = bucket_name - ), - regexp = "must be a string" + expect_error( + rwa_download_files( + file_list = multi_files, + directory = 01, + bucket_name = bucket_name + ), + regexp = "must be a string" ) }) test_that("function should error when bucket is not a string", { directory <- withr::local_tempdir() - expect_error(rwa_download_files( - file_list = multi_files, - directory = directory, - bucket_name = 01 - ), - regexp = "must be a string" + expect_error( + rwa_download_files( + file_list = multi_files, + directory = directory, + bucket_name = 01 + ), + regexp = "must be a string" ) }) test_that("function should error when silent is not logical", { directory <- withr::local_tempdir() - expect_error(rwa_download_files( - file_list = multi_files, - directory = directory, - bucket_name = bucket_name, - silent = "no" - ), - regexp = "must be a flag" + expect_error( + rwa_download_files( + file_list = multi_files, + directory = directory, + bucket_name = bucket_name, + silent = "no" + ), + regexp = "must be a flag" ) }) test_that("function should error when ask is not logical", { directory <- withr::local_tempdir() - expect_error(rwa_download_files( - file_list = multi_files, - directory = directory, - bucket_name = bucket_name, - ask = "no" - ), - regexp = "must be a flag" + expect_error( + rwa_download_files( + file_list = multi_files, + directory = directory, + bucket_name = bucket_name, + ask = "no" + ), + regexp = "must be a flag" ) }) @@ -114,7 +118,6 @@ test_that("function should error when fake file provided", { }) - #### Credentials #### Sys.sleep(5) diff --git a/tests/testthat/test-rwa-list-files.R b/tests/testthat/test-rwa-list-files.R index 2739ec8..af55d43 100644 --- a/tests/testthat/test-rwa-list-files.R +++ b/tests/testthat/test-rwa-list-files.R @@ -1,4 +1,3 @@ - bucket_name <- "readwriteaws-test-poissonconsulting" test_that("functions works", { @@ -186,9 +185,11 @@ test_that("Checking message that max size is same as outputed files", { bucket_name = bucket_name, max_request_size = 26 ), - regexp = paste("'Max_request_size' matches retrieved files from AWS.", - "Think about increasing 'Max_request_size' as there", - "could be more files present") + regexp = paste( + "'Max_request_size' matches retrieved files from AWS.", + "Think about increasing 'Max_request_size' as there", + "could be more files present" + ) ), regexp = "26 files were retrieved from AWS" ), diff --git a/tests/testthat/test-rwa-list-su-files.R b/tests/testthat/test-rwa-list-su-files.R index 047ea3a..5a9fbfc 100644 --- a/tests/testthat/test-rwa-list-su-files.R +++ b/tests/testthat/test-rwa-list-su-files.R @@ -1,4 +1,3 @@ - bucket_name <- "readwriteaws-test-poissonconsulting" test_that("get 6 image related files", { diff --git a/tests/testthat/test-rwa-upload-files.R b/tests/testthat/test-rwa-upload-files.R index 7e4a0ba..a8c4bdb 100644 --- a/tests/testthat/test-rwa-upload-files.R +++ b/tests/testthat/test-rwa-upload-files.R @@ -1,4 +1,3 @@ - bucket_name <- "readwriteaws-test-poissonconsulting" s3_bucket <- paws::s3()