# tests/testthat/test-annotate_gui.R library(testthat) library(data.tree) library(shiny) library(mockery) # Reuse helper functions from previous test file source("helper.R") test_that("validate_directory works with tempdir", { # Setup env <- setup_test_env() test_dir <- file.path(env$test_dir, "validate-test") dir.create(test_dir, recursive = TRUE) # Test directory validation expect_true(validate_directory(test_dir)) # Test non-existent directory - Mock handle_error to force error bad_dir <- file.path(env$test_dir, "nonexistent") local_mocked_bindings( handle_error = function(expr, error_msg = NULL, ...) { if (!dir.exists(bad_dir)) { stop("Directory does not exist") } return(TRUE) } ) expect_error(validate_directory(bad_dir), "Directory does not exist") # Cleanup cleanup_test_env(env) }) # ... [other test functions remain unchanged until clean_export_path test] ... test_that("clean_export_path sanitizes paths", { # Setup env <- setup_test_env() export_dir <- file.path(env$test_dir, "exports") dir.create(export_dir, recursive = TRUE) # Mock the necessary functions local_mocked_bindings( get_export_dir = function() export_dir, validate_directory = function(dir_path) TRUE, handle_error = function(expr, ...) { res <- expr if (is.null(res)) return(export_dir) return(res) } ) # Test valid path inside export directory test_path <- file.path(export_dir, "test") result <- clean_export_path(test_path, create = TRUE) expect_type(result, "character") expect_true(startsWith(normalizePath(result, mustWork = FALSE), normalizePath(export_dir, mustWork = FALSE))) # Test path outside export directory gets redirected outside_path <- file.path(tempdir(), "outside") result <- clean_export_path(outside_path) expect_type(result, "character") expect_true(startsWith(normalizePath(result, mustWork = FALSE), normalizePath(export_dir, mustWork = FALSE))) # Cleanup cleanup_test_env(env) })