Skip to content

Commit da9f522

Browse files
committed
cleanup tests + use withr
1 parent 27d9f01 commit da9f522

2 files changed

Lines changed: 21 additions & 17 deletions

File tree

tests/testthat/test-makeClimateDEM.R

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,24 @@ test_that("makeClimateDEM works", {
33
skip_if_not(identical(tolower(.Platform$OS.type), "windows"))
44
skip_if_not_installed("geodata")
55
skip_if_not_installed("googledrive")
6+
skip_if_not_installed("withr")
67

78
googledrive::drive_deauth()
89

9-
dPath <- file.path(tempdir(), "test_makeClimateDEM")
10+
dPath <- withr::local_tempdir("test_makeClimateDEM_")
1011

1112
canProvs <- geodata::gadm(country = "CAN", level = 1, path = dPath)
1213
expect_s4_class(canProvs, "SpatVector")
1314

1415
studyArea <- canProvs[canProvs$NAME_1 == "Alberta", ]
1516

16-
climateDEM <- makeClimateDEM(studyArea, DEMdestinationPath = dPath, destinationPath = dPath,
17-
filename2 = "Alberta")
17+
climateDEM <- makeClimateDEM(
18+
studyArea,
19+
DEMdestinationPath = dPath,
20+
destinationPath = dPath,
21+
filename2 = "Alberta"
22+
)
1823
expect_s4_class(climateDEM, "SpatRaster")
1924

20-
unlink(dPath, recursive = TRUE)
25+
withr::deferred_run()
2126
})

tests/testthat/test-prepClimateLayers.R

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ test_that("prepClimateLayers works for multiple variable types", {
33
skip_on_ci() ## needs to authorize googldrive package for downloads
44
skip_if_not_installed("archive")
55
skip_if_not_installed("googledrive")
6+
skip_if_not_installed("withr")
67

7-
dPath <- file.path(tempdir(), "test_prepClimateLayers")
8+
dPath <- withr::local_tempdir("test_prepClimateLayers_")
89
climateType <- "historical"
910
climatePath <- file.path(dPath, "climate") |> reproducible::checkPath(create = TRUE)
1011
climatePathOut <- file.path(climatePath, "outputs") |> reproducible::checkPath(create = TRUE)
@@ -66,11 +67,10 @@ test_that("prepClimateLayers works for multiple variable types", {
6667
## test with all parallel backends
6768
climateRasters <- list()
6869
for (backend in .parallelBackends) {
69-
withr::with_options(
70-
list(climateData.parallel.backend = backend), {
70+
withr::with_options(list(climateData.parallel.backend = backend), {
7171
climateRasters[[backend]] <- prepClimateLayers(
7272
climateVarsList = climateVariables,
73-
srcdir = climatePath, ## 'src' is the place for raw inputs, downloaded from Google Drive
73+
srcdir = climatePath, ## 'src' is the place for raw inputs, downloaded from Google Drive
7474
dstdir = climatePathOut, ## 'dst' is the place for intermediate + final outputs
7575
tile = tileIDs,
7676
gcm = GCM,
@@ -91,20 +91,18 @@ test_that("prepClimateLayers works for multiple variable types", {
9191
skip_if_not_installed("SpaDES.tools")
9292

9393
studyArea <- SpaDES.tools::randomStudyArea(size = 1e10)
94-
rasterToMatch <- terra::rast(studyArea, resolution = 250) |>
95-
terra::rasterize(studyArea, y = _)
94+
rasterToMatch <- terra::rast(studyArea, resolution = 250) |> terra::rasterize(studyArea, y = _)
9695

9796
## test with all parallel backends
9897
climateRasters <- list()
9998
for (backend in .parallelBackends) {
100-
withr::with_options(
101-
list(climateData.parallel.backend = backend), {
99+
withr::with_options(list(climateData.parallel.backend = backend), {
102100
## spurious warning:
103101
## attribute variables are assumed to be spatially constant throughout all geometries
104102
climateRastersStudyArea <- suppressWarnings({
105103
prepClimateLayers(
106104
climateVarsList = climateVariables,
107-
srcdir = climatePath, ## 'src' is the place for raw inputs, downloaded from Google Drive
105+
srcdir = climatePath, ## 'src' is the place for raw inputs, downloaded from Google Drive
108106
dstdir = climatePathOut, ## 'dst' is the place for intermediate + final outputs
109107
# tile = tileIDs, ## when passing `studyArea`/`rasterToMatch` then `tile` isn't needed
110108
gcm = GCM,
@@ -122,16 +120,17 @@ test_that("prepClimateLayers works for multiple variable types", {
122120
})
123121
}
124122

125-
unlink(dPath, recursive = TRUE)
123+
withr::deferred_run()
126124
})
127125

128126
test_that("prepClimateLayers properly handles unordered tileIDs", {
129127
skip_on_cran()
130128
skip_on_ci() ## needs to authorize googldrive package for downloads
131129
skip_if_not_installed("archive")
132130
skip_if_not_installed("googledrive")
131+
skip_if_not_installed("withr")
133132

134-
dPath <- file.path(tempdir(), "test_prepClimateLayers")
133+
dPath <- withr::local_tempdir("test_prepClimateLayers_")
135134
climateType <- "historical"
136135
climatePath <- file.path(dPath, "climate") |> reproducible::checkPath(create = TRUE)
137136
climatePathOut <- file.path(climatePath, "outputs") |> reproducible::checkPath(create = TRUE)
@@ -153,7 +152,7 @@ test_that("prepClimateLayers properly handles unordered tileIDs", {
153152

154153
climateRasters <- prepClimateLayers(
155154
climateVarsList = climateVariables,
156-
srcdir = climatePath, ## 'src' is the place for raw inputs, downloaded from Google Drive
155+
srcdir = climatePath, ## 'src' is the place for raw inputs, downloaded from Google Drive
157156
dstdir = climatePathOut, ## 'dst' is the place for intermediate + final outputs
158157
tile = tileIDs,
159158
gcm = NULL,
@@ -168,5 +167,5 @@ test_that("prepClimateLayers properly handles unordered tileIDs", {
168167
expect_s4_class(x, "SpatRaster")
169168
})
170169

171-
unlink(dPath, recursive = TRUE)
170+
withr::deferred_run()
172171
})

0 commit comments

Comments
 (0)