# Skip on Windows as path handling differs

# 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)

"init_data_dir creates directory only after confirmation"
"textAnnotatoR needs to create a directory at:\n%s"
"get_project_dir creates and returns project directory"
"validate_directory checks directory permissions correctly"