Skip to content

Commit

Permalink
Added a listing function with associated tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Apr 25, 2024
1 parent 51920e7 commit a9a9a0c
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(deregister)
export(listFiles)
export(query)
export(register)
export(retrieveDirectory)
Expand Down
46 changes: 46 additions & 0 deletions R/listFiles.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#' List files in a registered directory
#'
#' List files in a registered directory or a subdirectory thereof.
#' This will call the REST API if the caller is not on the same filesystem.
#'
#' @param path String containing the path to the registered (sub)directory.
#' @param url String containing the URL to the SewerRat REST API.
#'
#' @author Aaron Lun
#'
#' @return Character vector of relative paths of files in \code{path}.
#'
#' @examples
#' # Starting up an example SewerRat service:
#' info <- startSewerRat()
#'
#' # Mocking up a directory of stuff to query.
#' mydir <- tempfile()
#' dir.create(mydir)
#' write(file=file.path(mydir, "metadata.json"), '{ "first": "Aaron", "last": "Lun" }')
#' dir.create(file.path(mydir, "diet"))
#' write(file=file.path(mydir, "diet", "metadata.json"),
#' '{ "meal": "lunch", "ingredients": "water" }')
#'
#' # Registering it:
#' register(mydir, "metadata.json", url=info$url)
#'
#' # List files:
#' listFiles(mydir, url=info$url)
#' listFiles(paste0(mydir, "/diet"), url=info$url)
#'
#' # Forcing remote access.
#' listFiles(mydir, url=info$url, forceRemote=TRUE)
#'
#' @export
#' @import httr2
listFiles <- function(path, url, forceRemote=FALSE) {
if (!forceRemote && file.exists(path)) {
list.files(path, recursive=TRUE, all.files=TRUE)
} else {
req <- request(paste0(url, "/list?path=", URLencode(path, reserved=TRUE), "&recursive=true"))
req <- handle_error(req)
res <- req_perform(req)
unlist(resp_body_json(res))
}
}
46 changes: 46 additions & 0 deletions man/listFiles.Rd

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

26 changes: 26 additions & 0 deletions tests/testthat/test-list.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Test the listing function.
# library(testthat); library(SewerRat); source("test-list.R")

# Starting up an example SewerRat service:
info <- startSewerRat()

# Mocking up a directory of stuff to query.
mydir <- tempfile()
dir.create(mydir)
write(file=file.path(mydir, "metadata.json"), '{ "first": "Aaron", "last": "Lun" }')
dir.create(file.path(mydir, "diet"))
write(file=file.path(mydir, "diet", "metadata.json"),
'{ "meal": "lunch", "ingredients": "water" }')

# Registering it:
register(mydir, "metadata.json", url=info$url)

test_that("listing works as expected", {
expect_identical(sort(listFiles(mydir, url=info$url)), sort(c("diet/metadata.json", "metadata.json")))
expect_identical(sort(listFiles(paste0(mydir, "/diet"), url=info$url)), "metadata.json")

# Forcing remote access.
expect_identical(sort(listFiles(mydir, url=info$url, forceRemote=TRUE)), sort(c("diet/metadata.json", "metadata.json")))
})

deregister(mydir, url=info$url)

0 comments on commit a9a9a0c

Please sign in to comment.