From d313e51ea481d1f5509bab8dbd680d906595c0e8 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Thu, 5 Dec 2024 12:16:41 -0800 Subject: [PATCH] grepl --> cli::ansi_grepl in test-cache.R --- tests/testthat/test-cache.R | 68 ++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/tests/testthat/test-cache.R b/tests/testthat/test-cache.R index 259e2011a..7b61ea91f 100644 --- a/tests/testthat/test-cache.R +++ b/tests/testthat/test-cache.R @@ -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")) ))) @@ -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)) } @@ -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") @@ -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") @@ -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) @@ -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) @@ -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) @@ -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)) @@ -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 @@ -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) @@ -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()) @@ -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))) }) @@ -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)) @@ -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) @@ -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") } } @@ -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", { @@ -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", { @@ -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)) }) @@ -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", { @@ -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({ @@ -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) @@ -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))) })