Skip to content

Commit

Permalink
rotate(), backup() and co no longer fail on filenames that contai…
Browse files Browse the repository at this point in the history
…n special regex characters (such as `*` or `+`)
  • Loading branch information
Stefan Fleck committed Jan 27, 2021
1 parent fb6917d commit 33a69c3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: rotor
Title: Log Rotation and Conditional Backups
Version: 0.3.5
Version: 0.3.5.9000
Authors@R:
person(given = "Stefan",
family = "Fleck",
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# rotor (dev)

* `rotate()`, `backup()` and co no longer fail on filenames that
contain special regex characters (such as `*` or `+`)


# rotor 0.3.5

* Backups now retain their original timestamp (created, last modified) where
Expand Down
6 changes: 4 additions & 2 deletions R/BackupQueue.R
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ filenames_as_matrix <- function(
back_names <- basename(backups)

filename_end <-
attr(gregexpr(file_name, back_names[[1]])[[1]], "match.length") + 1L
attr(gregexpr(file_name, back_names[[1]], fixed = TRUE)[[1]], "match.length") + 1L

a <- strsplit_at_seperator_pos(back_names, filename_end)
assert(
Expand Down Expand Up @@ -890,7 +890,9 @@ get_backups <- function(
)
back_names <- basename(potential_backups)

sel <- grepl(paste0("^", file_name), back_names)
sel <- grepl(file_name, back_names, fixed = TRUE)
sel[regexpr(file_name, back_names, fixed = TRUE) != 1] <- FALSE

backups <- potential_backups[sel]
back_names <- basename(backups)

Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test_BackupQueue.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,28 @@ test_that("BackupQueue finding backups works as expected for files without exten



test_that("BackupQueue finding backups works on files with regex characters", {
dir.create(td, recursive = TRUE)
on.exit(unlink(td, recursive = TRUE))

tf <- file.path(td, "test+-[(")
file.create(tf)

expect_identical(n_backups(tf), 0L)
bq <- BackupQueue$new(tf)

sfxs <-c(1:12, "2019-12-31")
bus <- paste0(tools::file_path_sans_ext(tf), ".", sfxs)
file.create(bus)

expect_path_setequal(bq$files$path, bus)
expect_setequal(bq$files$sfx, sfxs)
expect_setequal(bq$files$ext, "")
})




test_that("dryrun/verbose prune", {
dir.create(td, recursive = TRUE)
on.exit(unlink(td, recursive = TRUE))
Expand Down

0 comments on commit 33a69c3

Please sign in to comment.