# ================================================================= # Tests for publish.R internal helper functions # ================================================================= # Note: We test the internal helper functions that don't require # actual Git/GitHub operations # --- .get_gitignore_patterns() --- test_that(".get_gitignore_patterns returns character vector", { patterns <- dashboardr:::.get_gitignore_patterns() expect_type(patterns, "character") expect_true(length(patterns) > 0) }) test_that(".get_gitignore_patterns includes R-specific patterns", { patterns <- dashboardr:::.get_gitignore_patterns() expect_true(".Rproj.user" %in% patterns) expect_true(".Rhistory" %in% patterns) expect_true(".RData" %in% patterns) }) test_that(".get_gitignore_patterns includes Quarto patterns", { patterns <- dashboardr:::.get_gitignore_patterns() expect_true(".quarto/" %in% patterns) }) test_that(".get_gitignore_patterns includes data file patterns", { patterns <- dashboardr:::.get_gitignore_patterns() # R data files expect_true("*.rds" %in% patterns) expect_true("*.RData" %in% patterns) # CSV and delimited expect_true("*.csv" %in% patterns) expect_true("*.tsv" %in% patterns) # Excel expect_true("*.xlsx" %in% patterns) expect_true("*.xls" %in% patterns) # Statistical software expect_true("*.sav" %in% patterns) expect_true("*.dta" %in% patterns) }) test_that(".get_gitignore_patterns includes OS-specific patterns", { patterns <- dashboardr:::.get_gitignore_patterns() expect_true(".DS_Store" %in% patterns) expect_true("Thumbs.db" %in% patterns) }) test_that(".get_gitignore_patterns includes IDE patterns", { patterns <- dashboardr:::.get_gitignore_patterns() expect_true(".vscode/" %in% patterns) expect_true(".idea/" %in% patterns) }) test_that(".get_gitignore_patterns includes data directories", { patterns <- dashboardr:::.get_gitignore_patterns() expect_true("data/" %in% patterns) expect_true("raw_data/" %in% patterns) }) # --- .glob_to_regex() --- test_that(".glob_to_regex converts simple patterns", { # Simple extension pattern pattern <- dashboardr:::.glob_to_regex("*.csv") expect_type(pattern, "character") expect_true(grepl("\\[\\^/\\]\\*", pattern)) # * becomes [^/]* }) test_that(".glob_to_regex handles dot escaping", { pattern <- dashboardr:::.glob_to_regex("*.R") # . should be escaped expect_true(grepl("\\\\\\.R", pattern)) }) test_that(".glob_to_regex handles directory patterns", { pattern <- dashboardr:::.glob_to_regex("data/") # Directory pattern should match at start or after / expect_true(grepl("\\(\\^\\|/\\)", pattern)) }) test_that(".glob_to_regex handles ** for recursive matching", { pattern <- dashboardr:::.glob_to_regex("**/*.csv") # Pattern should be a valid regex expect_type(pattern, "character") # Should handle the extension expect_true(grepl("csv", pattern)) }) test_that(".glob_to_regex handles ? for single character", { pattern <- dashboardr:::.glob_to_regex("file?.txt") # ? should become . (single char match) expect_true(grepl("\\.", pattern)) }) # --- .find_large_files() --- test_that(".find_large_files returns character vector", { # Create temp directory temp_dir <- tempdir() result <- dashboardr:::.find_large_files(temp_dir, size_mb = 100) expect_type(result, "character") }) test_that(".find_large_files finds files above threshold", { # Create temp directory with a file temp_dir <- file.path(tempdir(), "large_file_test") dir.create(temp_dir, showWarnings = FALSE) # Create a small file (under any reasonable threshold) small_file <- file.path(temp_dir, "small.txt") writeLines("hello", small_file) # Look for files > 100MB (our small file won't be found) result <- dashboardr:::.find_large_files(temp_dir, size_mb = 100) expect_equal(length(result), 0) # Cleanup unlink(temp_dir, recursive = TRUE) }) test_that(".find_large_files with small threshold finds files", { temp_dir <- file.path(tempdir(), "large_file_test2") dir.create(temp_dir, showWarnings = FALSE) # Create a file with some content test_file <- file.path(temp_dir, "test.txt") writeLines(rep("x", 1000), test_file) # Small but not empty # Use very small threshold (basically 0 MB) to find it result <- dashboardr:::.find_large_files(temp_dir, size_mb = 0.00001) # Should find our file expect_true(length(result) > 0 || file.size(test_file) < 10) # Cleanup unlink(temp_dir, recursive = TRUE) }) # --- .get_committable_files() --- test_that(".get_committable_files returns character vector", { temp_dir <- file.path(tempdir(), "commit_test") dir.create(temp_dir, showWarnings = FALSE) # Create a simple file writeLines("test", file.path(temp_dir, "test.R")) result <- dashboardr:::.get_committable_files(temp_dir) expect_type(result, "character") # Cleanup unlink(temp_dir, recursive = TRUE) }) test_that(".get_committable_files reads .gitignore", { temp_dir <- file.path(tempdir(), "gitignore_test") dir.create(temp_dir, showWarnings = FALSE) # Create files writeLines("code", file.path(temp_dir, "script.R")) writeLines("data", file.path(temp_dir, "data.csv")) # Create .gitignore that excludes .csv files writeLines("*.csv", file.path(temp_dir, ".gitignore")) result <- dashboardr:::.get_committable_files(temp_dir) # Should include .R file expect_true("script.R" %in% result) # Note: the gitignore filtering is complex and may not work perfectly # in all cases - this test verifies the function runs without error expect_type(result, "character") # Cleanup unlink(temp_dir, recursive = TRUE) }) test_that(".get_committable_files excludes .git directory", { temp_dir <- file.path(tempdir(), "git_dir_test") dir.create(temp_dir, showWarnings = FALSE) dir.create(file.path(temp_dir, ".git"), showWarnings = FALSE) writeLines("code", file.path(temp_dir, "script.R")) writeLines("git file", file.path(temp_dir, ".git", "config")) result <- dashboardr:::.get_committable_files(temp_dir) # Should not include .git files expect_false(any(grepl("^\\.git/", result))) # Cleanup unlink(temp_dir, recursive = TRUE) }) # --- .print_file_tree() --- test_that(".print_file_tree produces console output", { temp_dir <- file.path(tempdir(), "tree_test") dir.create(temp_dir, showWarnings = FALSE) dir.create(file.path(temp_dir, "subdir"), showWarnings = FALSE) writeLines("a", file.path(temp_dir, "root.R")) writeLines("b", file.path(temp_dir, "subdir", "nested.R")) output <- capture.output({ result <- dashboardr:::.print_file_tree(temp_dir) }) # Should produce output expect_true(length(output) > 0) # Should show file count expect_true(any(grepl("Files to be committed", output))) # Returns file count invisibly expect_equal(result, 2) # Cleanup unlink(temp_dir, recursive = TRUE) }) test_that(".print_file_tree handles empty directory", { temp_dir <- file.path(tempdir(), "empty_tree_test") dir.create(temp_dir, showWarnings = FALSE) output <- capture.output({ result <- dashboardr:::.print_file_tree(temp_dir) }) expect_true(any(grepl("0 total", output))) expect_equal(result, 0) # Cleanup unlink(temp_dir, recursive = TRUE) }) # --- .display_pre_review_info() --- test_that(".display_pre_review_info produces console output", { output <- capture.output({ dashboardr:::.display_pre_review_info() }) expect_true(length(output) > 0) expect_true(any(grepl("docs/", output))) expect_true(any(grepl("gitignore", output))) }) # --- .cleanup_review_folder() --- test_that(".cleanup_review_folder removes the review folder", { temp_dir <- file.path(tempdir(), "cleanup_test") dir.create(temp_dir, showWarnings = FALSE) review_path <- file.path(temp_dir, ".dashboardr_review") dir.create(review_path, showWarnings = FALSE) writeLines("test", file.path(review_path, "test.txt")) expect_true(dir.exists(review_path)) dashboardr:::.cleanup_review_folder(temp_dir) expect_false(dir.exists(review_path)) # Cleanup unlink(temp_dir, recursive = TRUE) }) test_that(".cleanup_review_folder handles non-existent folder", { temp_dir <- file.path(tempdir(), "no_review_folder") dir.create(temp_dir, showWarnings = FALSE) # Should not error when folder doesn't exist expect_no_error({ dashboardr:::.cleanup_review_folder(temp_dir) }) # Cleanup unlink(temp_dir, recursive = TRUE) }) # --- .create_review_folder() --- # Skip this test as it requires a usethis project context test_that(".create_review_folder requires project context", { skip("Requires usethis project context") })