Error in `grepl(sprintf("^%s", export_dir), dir_path)`: invalid regular expression '^D:\temp\2024_11_11_18_05_02_19838\RtmpSAdkwq/textAnnotatoR-tests/exports', reason 'Invalid back reference'

# Reuse helper functions from previous test file
test_that("validate_directory works with tempdir", {
# Test non-existent directory - Mock handle_error to force error
handle_error = function(expr, error_msg = NULL, ...) {
expect_error(validate_directory(bad_dir), "Directory does not exist")
# ... [other test functions remain unchanged until clean_export_path test] ...
# Test valid path inside export directory
result <- clean_export_path(test_path, create = TRUE)
# Test path outside export directory gets redirected

Error in `grepl(sprintf("^%s", export_dir), dir_path)`: invalid regular expression '^D:\temp\2024_11_11_18_05_02_19838\RtmpSAdkwq\textAnnotatoR_test', reason 'Invalid back reference'

# Test helper to create temporary test environment
test_dir <- normalizePath(file.path(tempdir(), "textAnnotatoR_test"), mustWork = FALSE)
dir.create(test_dir, recursive = TRUE, showWarnings = FALSE)
test_that("init_data_dir creates directory only after confirmation", {
# Create and normalize test directory path
mock_session <- structure(list(), class = "ShinySession")
# Create test environment with the test directory path
mock_env <- new.env(parent = parent.env(environment()))
mock_env$test_dir <- test_env$test_dir  # Add test_dir to the environment
# Create modified version of init_data_dir that uses the environment's test_dir
current_dir <- get("test_dir", envir = parent.env(environment()))
sprintf("textAnnotatoR needs to create a directory at:\n%s", current_dir),
dir.create(test_env$test_dir, recursive = TRUE, showWarnings = FALSE)
test_that("get_project_dir creates and returns project directory", {
# Create temporary get_project_dir function
test_that("clean_project_path sanitizes paths correctly", {
# Create temporary clean_project_path function
stop("Invalid path: results in empty string after cleaning")
"Invalid path: results in empty string after cleaning"
test_that("validate_directory checks directory permissions correctly", {
# Create temporary validate_directory function that doesn't use handle_error
test_file <- tempfile(tmpdir = dir_path)
if (!file.access(dir_path, mode = 4) == 0) {
test_that("create_backup_dir manages backups correctly", {
file_path <- file.path(backup_dir, sprintf("backup%d.txt", i))
# Create temporary create_backup_dir function
temp_create_backup_dir <- function(max_backups = 3) {
backup_files <- list.files(backup_dir, full.names = TRUE)
if (length(backup_files) > max_backups) {
files_to_remove <- row.names(file_info)[1:(length(backup_files) - max_backups)]
result <- temp_create_backup_dir(max_backups = 2)
test_that("clean_export_path validates and sanitizes export paths", {
# Create temporary clean_export_path function
temp_clean_export_path <- function(dir_path, create = FALSE) {
dir_path <- normalizePath(dir_path, mustWork = FALSE)
# Ensure path is within allowed directories
if (!grepl(sprintf("^%s", export_dir), dir_path)) {
result <- temp_clean_export_path(test_path, create = TRUE)