Skip to content

Commit

Permalink
grepl --> cli::ansi_grepl in test-cache.R
Browse files Browse the repository at this point in the history
  • Loading branch information
Eliot McIntire committed Dec 5, 2024
1 parent 5c5aaee commit d313e51
Showing 1 changed file with 34 additions and 34 deletions.
68 changes: 34 additions & 34 deletions tests/testthat/test-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ test_that("test file-backed raster caching", {
basename(tmpfile[1])
), split = "[\\/]")
))
expect_true(any(grepl(
expect_true(any(cli::ansi_grepl(
pattern = basename(tmpfile[1]),
dir(file.path(tmpCache, "rasters"))
)))
Expand Down Expand Up @@ -91,7 +91,7 @@ test_that("test file-backed raster caching", {
# ._prepareOutputs_1 <<- ._prepareOutputs_2 <<- ._getFromRepo <<- 1
# Will silently update the filename of the RasterLayer, and recover it
type <- gsub("Connection", "", class(getOption("reproducible.conn")))
isSQLite <- grepl(type, "NULL")
isSQLite <- cli::ansi_grepl(type, "NULL")
if (!isSQLite) {
warn1 <- capture_warnings(movedCache(tmpdir, old = tmpCache))
}
Expand Down Expand Up @@ -349,7 +349,7 @@ test_that("test 'quick' argument", {
out1c <- Cache(quickFun, thePath, cachePath = tmpdir, quick = TRUE)
})

expect_true(sum(grepl(
expect_true(sum(cli::ansi_grepl(
paste0(
paste(.message$LoadedCache(.message$LoadedCacheResult(), "quickFun"), .message$AddingToMemoised), "|",
.message$LoadedCache(.message$LoadedCacheResult("Memoised"), "quickFun")
Expand All @@ -375,7 +375,7 @@ test_that("test 'quick' argument", {
mess1 <- capture_messages({
out1c <- Cache(quickFun, r1, cachePath = tmpdir, quick = TRUE)
})
expect_true(sum(grepl(
expect_true(sum(cli::ansi_grepl(
paste0(
paste(.message$LoadedCache(.message$LoadedCacheResult(), "quickFun"), .message$AddingToMemoised), "|",
paste(.message$LoadedCacheResult("Memoised"), "quickFun call")
Expand Down Expand Up @@ -540,7 +540,7 @@ test_that("test asPath", {

expect_equal(length(a1), 1)
expect_equal(length(a2), 1)
expect_true(sum(grepl(paste(
expect_true(sum(cli::ansi_grepl(paste(
.message$LoadedCacheResult("Memoised"), "|",
.message$LoadedCacheResult()
), a3)) == 1)
Expand All @@ -560,11 +560,11 @@ test_that("test asPath", {
quick = TRUE, cachePath = tmpdir
))
expect_equal(length(a1), 1)
expect_true(sum(grepl(paste(
expect_true(sum(cli::ansi_grepl(paste(
.message$LoadedCacheResult("Memoised"), "|",
.message$LoadedCacheResult()
), a2)) == 1)
expect_true(sum(grepl(paste(.message$LoadedCacheResult("Memoised"), ".*saveRDS.*call"), a3)) == 1)
expect_true(sum(cli::ansi_grepl(paste(.message$LoadedCacheResult("Memoised"), "saveRDS call"), a3)) == 1)

unlink("filename.RData")
try(clearCache(tmpdir, ask = FALSE), silent = TRUE)
Expand All @@ -581,7 +581,7 @@ test_that("test asPath", {
quick = TRUE, cachePath = tmpdir
))
expect_equal(length(a1), 1)
expect_true(sum(grepl(paste(
expect_true(sum(cli::ansi_grepl(paste(
.message$LoadedCacheResult("Memoised"), "|",
.message$LoadedCacheResult()
), a2)) == 1)
Expand Down Expand Up @@ -633,23 +633,23 @@ test_that("test Cache argument inheritance to inner functions", {
}

mess <- capture_messages(Cache(outer, n = 2))
expect_equal(sum(grepl(.message$NoCacheRepoSuppliedGrep, mess)), 2)
expect_equal(sum(cli::ansi_grepl(.message$NoCacheRepoSuppliedGrep, mess)), 2)
clearCache(ask = FALSE, x = tmpdir)

# options(reproducible.cachePath = tmpCache)
out <- capture_messages(Cache(outer, n = 2))
expect_true(all(unlist(lapply(
c(.message$NoCacheRepoSuppliedGrep, .message$NoCacheRepoSuppliedGrep),
function(mess) any(grepl(mess, out))
function(mess) any(cli::ansi_grepl(mess, out))
))))

# does Sys.time() propagate to outer ones
out <- capture_messages(Cache(outer(n = 2, not = Sys.time() + 1), notOlderThan = Sys.time() + 1))
expect_equal(sum(grepl(.message$NoCacheRepoSuppliedGrep, out)), 2)
expect_equal(sum(cli::ansi_grepl(.message$NoCacheRepoSuppliedGrep, out)), 2)

# does Sys.time() propagate to outer ones -- no message about cachePath being tempdir()
mess <- capture_messages(Cache(outer(n = 2, not = Sys.time()), notOlderThan = Sys.time(), cachePath = tmpdir))
expect_equal(sum(grepl(.message$NoCacheRepoSuppliedGrep, mess)), 1)
expect_equal(sum(cli::ansi_grepl(.message$NoCacheRepoSuppliedGrep, mess)), 1)

# does cachePath propagate to outer ones -- no message about cachePath being tempdir()
out <- capture_messages(Cache(outer, n = 2, cachePath = tmpdir))
Expand All @@ -675,7 +675,7 @@ test_that("test Cache argument inheritance to inner functions", {
Cache(rnorm, n, notOlderThan = Sys.time() + 1)
}
out <- capture_messages(Cache(outer, n = 2, cachePath = tmpdir))
expect_equal(sum(grepl(.message$NoCacheRepoSuppliedGrep, out)), 1)
expect_equal(sum(cli::ansi_grepl(.message$NoCacheRepoSuppliedGrep, out)), 1)

# change the outer function, so no cache on that, & have notOlderThan on rnorm,
# so no Cache on that
Expand All @@ -684,9 +684,9 @@ test_that("test Cache argument inheritance to inner functions", {
Cache(rnorm, n, notOlderThan = Sys.time() + 1)
}
out <- capture_messages(Cache(outer, n = 2, cachePath = tmpdir))
expect_equal(sum(grepl(.message$NoCacheRepoSuppliedGrep, out)), 1)
expect_equal(sum(cli::ansi_grepl(.message$NoCacheRepoSuppliedGrep, out)), 1)

# expect_true(all(grepl("There is no similar item in the cachePath", out)))
# expect_true(all(cli::ansi_grepl("There is no similar item in the cachePath", out)))
# Second time will get a cache on outer
out <- capture_messages(Cache(outer, n = 2, cachePath = tmpdir))
expect_true(length(out) == 2)
Expand All @@ -711,9 +711,9 @@ test_that("test Cache argument inheritance to inner functions", {
"There is no similar item in the cachePath",
sep = "|"
)
expect_true(sum(grepl(.message$NoCacheRepoSuppliedGrep, out)) == 1)
expect_true(sum(cli::ansi_grepl(.message$NoCacheRepoSuppliedGrep, out)) == 1)

# expect_true(sum(grepl(msgGrep, out)) == 1)
# expect_true(sum(cli::ansi_grepl(msgGrep, out)) == 1)

outer <- function(n) {
Cache(inner, 0.1, notOlderThan = Sys.time())
Expand Down Expand Up @@ -843,7 +843,7 @@ test_that("test mergeCache", {
mess <- capture_messages({
d1 <- mergeCache(tmpCache, tmpdir)
})
expect_true(any(grepl("Skipping", mess)))
expect_true(any(cli::ansi_grepl("Skipping", mess)))
expect_true(identical(showCache(d), showCache(d1)))
})

Expand Down Expand Up @@ -907,7 +907,7 @@ test_that("test cache-helpers", {
bnfn3 <- basename(fn3[actualFiles])
bnfn2 <- unique(filePathSansExt(bnfn2))
bnfn3 <- unique(filePathSansExt(bnfn3))
sameFileBase <- grepl(pattern = bnfn2, x = bnfn3)
sameFileBase <- cli::ansi_grepl(pattern = bnfn2, x = bnfn3)
expect_true(sameFileBase)

unlink(Filenames(s2))
Expand All @@ -927,7 +927,7 @@ test_that("test useCache = 'overwrite'", {
b <- Cache(rnorm, 1, useCache = "overwrite", cachePath = tmpCache)
})
expect_true(!identical(a, b))
expect_true(any(grepl(pattern = "Overwriting", mess)))
expect_true(any(cli::ansi_grepl(pattern = "Overwriting", mess)))

clearCache(x = tmpCache, ask = FALSE)

Expand All @@ -942,14 +942,14 @@ test_that("test useCache = 'overwrite'", {
b <- Cache(rnorm, 1, cachePath = tmpCache)
})
expect_true(!identical(a, b))
expect_true(any(grepl(pattern = "Overwriting", mess)))
expect_true(any(cli::ansi_grepl(pattern = "Overwriting", mess)))
})

test_that("test rm large non-file-backed rasters", {
## This is a large object test!
skip_on_cran()
if (!is.null(getOption("reproducible.conn", NULL))) {
if (!grepl("SQLite", class(getOption("reproducible.conn", NULL)))) {
if (!cli::ansi_grepl("SQLite", class(getOption("reproducible.conn", NULL)))) {
skip("This is not for non-SQLite")
}
}
Expand Down Expand Up @@ -996,7 +996,7 @@ test_that("test cc", {
expect_true(length(unique(b1[[.cacheTableHashColName()]])) == 0)

mess <- capture_messages(cc(ask = FALSE, x = tmpCache)) # Cache is already empty
expect_true(any(grepl("Cache already empty", mess)))
expect_true(any(cli::ansi_grepl("Cache already empty", mess)))
})

test_that("test pre-creating conn", {
Expand All @@ -1016,8 +1016,8 @@ test_that("test pre-creating conn", {
)
expect_true(file.exists(Filenames(r1)))
expect_true(file.exists(Filenames(r2)))
expect_false(grepl(basename(dirname(Filenames(r1))), "rasters")) # changed behaviour as of reproducible 1.2.0.9020
expect_false(grepl(basename(dirname(Filenames(r2))), "rasters")) # changed behaviour as of reproducible 1.2.0.9020
expect_false(cli::ansi_grepl(basename(dirname(Filenames(r1))), "rasters")) # changed behaviour as of reproducible 1.2.0.9020
expect_false(cli::ansi_grepl(basename(dirname(Filenames(r2))), "rasters")) # changed behaviour as of reproducible 1.2.0.9020
})

test_that("test .defaultUserTags", {
Expand Down Expand Up @@ -1049,9 +1049,9 @@ test_that("test failed Cache recovery -- message to delete cacheId", {
})
})
})
expect_true(sum(grepl(paste0("(trying to recover).*(", ci, ")"), mess)) == 1)
expect_true(sum(grepl(paste0("(trying to recover).*(", ci, ")"), err)) == 0)
expect_true(any(grepl(paste0("[cannot|failed to] open"), paste(warn, err, mess))))
expect_true(sum(cli::ansi_grepl(paste0("(trying to recover).*(", ci, ")"), mess)) == 1)
expect_true(sum(cli::ansi_grepl(paste0("(trying to recover).*(", ci, ")"), err)) == 0)
expect_true(any(cli::ansi_grepl(paste0("[cannot|failed to] open"), paste(warn, err, mess))))
expect_true(is.numeric(d))
})

Expand All @@ -1072,14 +1072,14 @@ test_that("test changing reproducible.cacheSaveFormat midstream", {
b <- Cache(rnorm, 1, cachePath = tmpdir)
})
expect_false(attr(b, ".Cache")$newCache)
expect_true(sum(grepl("Changing format of Cache entry from rds to qs", mess)) == 1)
expect_true(sum(cli::ansi_grepl("Changing format of Cache entry from rds to qs", mess)) == 1)

opts <- options(reproducible.cacheSaveFormat = "rds")
mess <- capture_messages({
b <- Cache(rnorm, 1, cachePath = tmpdir)
})
expect_false(attr(b, ".Cache")$newCache)
expect_true(sum(grepl("Changing format of Cache entry from qs to rds", mess)) == 1)
expect_true(sum(cli::ansi_grepl("Changing format of Cache entry from qs to rds", mess)) == 1)
})

test_that("test file link with duplicate Cache", {
Expand Down Expand Up @@ -1114,7 +1114,7 @@ test_that("test file link with duplicate Cache", {
g <- Cache(sam1, N, cachePath = tmpCache)
})

expect_true(sum(grepl("A file with identical", mess3)) == 1)
expect_true(sum(cli::ansi_grepl("A file with identical", mess3)) == 1)

set.seed(123)
mess1 <- capture_messages({
Expand All @@ -1130,8 +1130,8 @@ test_that("test file link with duplicate Cache", {
mess2 <- capture_messages({
d <- Cache(sam1, N, cachePath = tmpCache)
})
expect_true(any(grepl(.message$LoadedCacheResult(), mess2)))
expect_true(any(grepl(.message$LoadedCacheResult(), mess1)))
expect_true(any(cli::ansi_grepl(.message$LoadedCacheResult(), mess2)))
expect_true(any(cli::ansi_grepl(.message$LoadedCacheResult(), mess1)))
# There are intermittent "status 5" warnings on next line on Windows -- not relevant here
warns <- capture_warnings({
out1 <- try(system2("du", paste0("\"", tmpCache, "\""), stdout = TRUE), silent = TRUE)
Expand Down Expand Up @@ -1793,7 +1793,7 @@ test_that("terra files were creating file.link", {
ras
}
mess <- capture_messages(Map(f = func, fn = tmpfile, ras = rasts))
expect_false(any(grepl("file.link", mess)))
expect_false(any(cli::ansi_grepl("file.link", mess)))

})

Expand Down

0 comments on commit d313e51

Please sign in to comment.