Skip to content

Commit

Permalink
Merge pull request #120 from UrbanAnalyst/fs
Browse files Browse the repository at this point in the history
fs for #119
  • Loading branch information
mpadge authored Oct 28, 2024
2 parents 6761fda + 080c0a1 commit a1e766d
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repos:
args: [--style_pkg=spaceout, --style_fun=spaceout_style]
additional_dependencies:
- ropensci-review-tools/spaceout
- id: roxygenize
# - id: roxygenize
# codemeta must be above use-tidy-description when both are used
# - id: codemeta-description-updated
- id: use-tidy-description
Expand Down
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: gtfsrouter
Title: Routing with 'GTFS' (General Transit Feed Specification) Data
Version: 0.1.2.014
Version: 0.1.2.018
Authors@R: c(
person("Mark", "Padgham", , "[email protected]", role = c("aut", "cre")),
person("Marcin", "Stepniak", , "[email protected]", role = "aut",
Expand All @@ -18,6 +18,7 @@ Depends:
Imports:
cli,
data.table,
fs,
geodist,
methods,
Rcpp (>= 0.12.6)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Minor changes:

- Fix bug with return values of traveltimes (#116; thanks to @FlxPo)
- Use 'fs' package for all file path manipulations.

---

Expand Down
6 changes: 3 additions & 3 deletions R/extract-gtfs.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ unzip_gtfs <- function (filename, quiet = FALSE) {
)
}

flist <- utils::unzip (filename, exdir = tempdir ())
flist <- utils::unzip (filename, exdir = fs::path_temp ())

if (!quiet) {
message ("\r", cli::col_green (
Expand All @@ -106,7 +106,7 @@ check_extract_pars <- function (filename, stn_suffixes) {
if (is.null (filename)) {
stop ("filename must be given")
}
if (!file.exists (filename)) {
if (!fs::file_exists (filename)) {
stop ("filename ", filename, " does not exist")
}
if (!(is.null (stn_suffixes) || is.character (stn_suffixes))) {
Expand Down Expand Up @@ -191,7 +191,7 @@ extract_objs_into_env <- function (flist, quiet = FALSE) {
value = fout,
envir = e
)
chk <- file.remove (flist [f]) # nolint
chk <- fs::file_delete (flist [f]) # nolint
}

if (!quiet) {
Expand Down
4 changes: 2 additions & 2 deletions R/go_home_work.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ go_home_work <- function (home = TRUE, wait, start_time) {

vars <- get_envvars ()
fname <- get_rds_name (vars$file)
if (!file.exists (fname)) {
if (!fs::file_exists (fname)) {
stop (
"This function requires the GTFS data to be pre-processed ",
"with 'process_gtfs_local'."
Expand Down Expand Up @@ -145,7 +145,7 @@ get_envvars <- function () {
}

f <- (Sys.getenv ("gtfs_data"))
if (!file.exists (f)) {
if (!fs::file_exists (f)) {
stop (
"File ", f, " specified by environmental variable ",
"'gtfs_data' does not exist"
Expand Down
4 changes: 2 additions & 2 deletions R/transfers.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ dl_net <- function (gtfs) {
stops <- gtfs$stops [, c ("stop_lon", "stop_lat")]
requireNamespace ("digest")
hash <- digest::digest (stops)
net_name <- file.path (tempdir (), paste0 ("net", hash, ".Rds"))
net_name <- fs::path (fs::path_temp (), paste0 ("net", hash, ".Rds"))

if (!file.exists (net_name)) {
if (!fs::file_exists (net_name)) {
requireNamespace ("dodgr")
net <- dodgr::dodgr_streetnet_sc (pts = stops)
saveRDS (net, file = net_name)
Expand Down
8 changes: 4 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ berlin_gtfs_to_zip <- function () {
si <- strsplit (i, ".txt") [[1]]
res <- tryCatch (
data.table::fwrite (f [[si]],
file.path (tempdir (), i),
fs::path (fs::path_temp (), i),
quote = TRUE
),
error = function (e) e
Expand All @@ -33,10 +33,10 @@ berlin_gtfs_to_zip <- function () {
stop ("Unable to write files to tempdir()")
} # nocov

flist <- file.path (tempdir (), flist)
zipname <- file.path (tempdir (), "vbb.zip")
flist <- fs::path (fs::path_temp (), flist)
zipname <- fs::path (fs::path_temp (), "vbb.zip")
utils::zip (zipname, files = flist, flags = "-q")
chk <- file.remove (flist)
chk <- fs::file_delete (flist)

return (zipname)
}
Expand Down
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"codeRepository": "https://github.com/UrbanAnalyst/gtfsrouter",
"issueTracker": "https://github.com/UrbanAnalyst/gtfsrouter/issues",
"license": "https://spdx.org/licenses/GPL-3.0",
"version": "0.1.2.014",
"version": "0.1.2.018",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down
46 changes: 23 additions & 23 deletions tests/testthat/test-extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,41 @@ test_all <- (identical (Sys.getenv ("MPADGE_LOCAL"), "true") |
nthr <- data.table::setDTthreads (1L)

test_that ("extract non gtfs", {
f <- file.path (tempdir (), "junk.txt")
f <- fs::path (fs::path_temp (), "junk.txt")
con <- file (f)
writeLines ("blah", con)
close (con)
fz <- file.path (tempdir (), "vbb.zip")
if (file.exists (fz)) {
chk <- file.remove (fz)
fz <- fs::path (fs::path_temp (), "vbb.zip")
if (fs::file_exists (fz)) {
chk <- fs::file_delete (fz)
}
chk <- zip (fz, file = f)

msg <- paste0 (fz, " does not appear to be a GTFS file")
if (test_all) { # windows paths get mucked up
expect_error (g <- extract_gtfs (fz), msg)
}
invisible (file.remove (fz))
invisible (fs::file_delete (fz))
})

test_that ("extract", {
berlin_gtfs_to_zip ()
f <- file.path (tempdir (), "vbb.zip")
expect_true (file.exists (f))
f <- fs::path (fs::path_temp (), "vbb.zip")
expect_true (fs::file_exists (f))
expect_message (g <- extract_gtfs (f, quiet = FALSE))

# remove calendar and transfers from feed:
unzip (file.path (tempdir (), "vbb.zip"),
exdir = tempdir (),
unzip (fs::path (fs::path_temp (), "vbb.zip"),
exdir = fs::path_temp (),
junkpaths = TRUE
)
froutes <- file.path (tempdir (), "routes.txt")
ftrips <- file.path (tempdir (), "trips.txt")
fstop_times <- file.path (tempdir (), "stop_times.txt")
fstops <- file.path (tempdir (), "stops.txt")
ftransfers <- file.path (tempdir (), "transfers.txt")
fcalendar <- file.path (tempdir (), "calendar.txt")
f_cut <- file.path (tempdir (), "vbb_cut.zip")
froutes <- fs::path (fs::path_temp (), "routes.txt")
ftrips <- fs::path (fs::path_temp (), "trips.txt")
fstop_times <- fs::path (fs::path_temp (), "stop_times.txt")
fstops <- fs::path (fs::path_temp (), "stops.txt")
ftransfers <- fs::path (fs::path_temp (), "transfers.txt")
fcalendar <- fs::path (fs::path_temp (), "calendar.txt")
f_cut <- fs::path (fs::path_temp (), "vbb_cut.zip")

chk <- zip (f_cut,
file = c (
Expand All @@ -62,7 +62,7 @@ test_that ("extract", {
names (vals) <- nms
expect_identical (vals, vapply (g, nrow, numeric (1)))
expect_true (!"transfers" %in% names (vals))
invisible (file.remove (f_cut))
invisible (fs::file_delete (f_cut))

chk <- zip (f_cut,
file = c (
Expand All @@ -82,13 +82,13 @@ test_that ("extract", {
names (vals) <- nms
expect_identical (vals, vapply (g, nrow, numeric (1)))
expect_true (!"calendar" %in% names (vals))
invisible (file.remove (f_cut))
invisible (fs::file_delete (f_cut))
})

test_that ("summary", {
berlin_gtfs_to_zip ()
f <- file.path (tempdir (), "vbb.zip")
expect_true (file.exists (f))
f <- fs::path (fs::path_temp (), "vbb.zip")
expect_true (fs::file_exists (f))
expect_silent (g <- extract_gtfs (f, quiet = TRUE))

x <- capture.output (summary (g))
Expand All @@ -103,8 +103,8 @@ test_that ("summary", {

test_that ("timetable summary", {
berlin_gtfs_to_zip ()
f <- file.path (tempdir (), "vbb.zip")
expect_true (file.exists (f))
f <- fs::path (fs::path_temp (), "vbb.zip")
expect_true (fs::file_exists (f))
expect_silent (g <- extract_gtfs (f, quiet = TRUE))
gt <- gtfs_timetable (g, day = 3)

Expand All @@ -131,7 +131,7 @@ test_that ("cpu time", {

test <- function () {
berlin_gtfs_to_zip ()
f <- file.path (tempdir (), "vbb.zip")
f <- fs::path (fs::path_temp (), "vbb.zip")
gtfs <- extract_gtfs (f)
}
st <- system.time (test ())
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test-frequencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test_that ("not gtfs", {

test_that ("gtfs without frequencies", {
berlin_gtfs_to_zip ()
tempfiles <- list.files (tempdir (), full.names = TRUE)
tempfiles <- list.files (fs::path_temp (), full.names = TRUE)
filename <- tempfiles [grep ("vbb.zip", tempfiles)]
gtfs <- extract_gtfs (filename)

Expand All @@ -21,7 +21,7 @@ test_that ("gtfs without frequencies", {

test_that ("gtfs with empty frequencies", {
berlin_gtfs_to_zip ()
tempfiles <- list.files (tempdir (), full.names = TRUE)
tempfiles <- list.files (fs::path_temp (), full.names = TRUE)
filename <- tempfiles [grep ("vbb.zip", tempfiles)]
gtfs <- extract_gtfs (filename)

Expand All @@ -41,7 +41,7 @@ test_that ("gtfs with empty frequencies", {

test_that ("frequencies with missing columns", {
berlin_gtfs_to_zip ()
tempfiles <- list.files (tempdir (), full.names = TRUE)
tempfiles <- list.files (fs::path_temp (), full.names = TRUE)
filename <- tempfiles [grep ("vbb.zip", tempfiles)]
gtfs <- extract_gtfs (filename)

Expand All @@ -63,7 +63,7 @@ test_that ("frequencies with missing columns", {

test_that ("only routes with frequencies to stop_times", {
f <- berlin_gtfs_to_zip ()
expect_true (file.exists (f))
expect_true (fs::file_exists (f))
gtfs <- extract_gtfs (f)

# filter only one route from gtfs
Expand Down Expand Up @@ -158,8 +158,8 @@ test_that ("only routes with frequencies to stop_times", {

test_that ("gtfs with mixed frequencies", {
berlin_gtfs_to_zip ()
f <- file.path (tempdir (), "vbb.zip")
expect_true (file.exists (f))
f <- fs::path (fs::path_temp (), "vbb.zip")
expect_true (fs::file_exists (f))
gtfs <- extract_gtfs (f)

# filter two routes: U1 - only one trip and U3 with all trips
Expand Down Expand Up @@ -215,7 +215,7 @@ test_that ("gtfs with mixed frequencies", {

test_that ("gtfs frequencies in gtfs_route", {
f <- berlin_gtfs_to_zip ()
expect_true (file.exists (f))
expect_true (fs::file_exists (f))
gtfs <- extract_gtfs (f)

gtfs$routes <- gtfs$routes [route_short_name %in% c ("U1", "U6")]
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-go-home.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test_that ("go home set up", {
"This function requires environmental variables"
)
}
f <- file.path (tempdir (), "doesnotexist.zip")
f <- fs::path (fs::path_temp (), "doesnotexist.zip")
Sys.setenv ("gtfs_home" = "Innsbrucker Platz")
Sys.setenv ("gtfs_work" = "Alexanderplatz")
Sys.setenv ("gtfs_data" = f)
Expand All @@ -29,7 +29,7 @@ test_that ("go home set up", {
)
}
berlin_gtfs_to_zip ()
f <- file.path (tempdir (), "vbb.zip")
f <- fs::path (fs::path_temp (), "vbb.zip")
Sys.setenv ("gtfs_data" = f)

expect_error (
Expand Down
Loading

0 comments on commit a1e766d

Please sign in to comment.