From 48552d7504d80bef2697b7e0f6b9cf465e24c504 Mon Sep 17 00:00:00 2001 From: Elio Neto Date: Thu, 26 Feb 2026 18:05:45 -0300 Subject: [PATCH 1/2] feat: add comprehensive unit tests for ParseExtToSlice and ParseExcludeToSlice - Add table-driven tests for ParseExtToSlice covering: * Empty strings * Single and multiple extensions * Extensions with/without leading dots * Mixed case handling (uppercase/lowercase) * Whitespace trimming * Duplicate extensions * Special characters and edge cases - Add table-driven tests for ParseExcludeToSlice covering: * Empty strings * Single and multiple patterns * Whitespace variations (leading/trailing/multiple) * Empty items in comma-separated lists * Special patterns and paths - Follow Go testing best practices: * Descriptive test names * Clear test case documentation * Comprehensive edge case coverage * Assertions with helpful error messages Fixes #279 --- internal/utils/utils_test.go | 467 +++++++++++++++++++++++++++-------- 1 file changed, 368 insertions(+), 99 deletions(-) diff --git a/internal/utils/utils_test.go b/internal/utils/utils_test.go index 0636964..191007e 100644 --- a/internal/utils/utils_test.go +++ b/internal/utils/utils_test.go @@ -1,60 +1,189 @@ package utils import ( - "os/user" - "path/filepath" "reflect" - "strings" "testing" ) func TestParseExtToSlice(t *testing.T) { tests := []struct { - name string - extensions string - want []string + name string + input string + expected []string + desc string }{ + // Empty and whitespace cases { - name: "Basic valid extensions", - extensions: "jpg,png,gif", - want: []string{".jpg", ".png", ".gif"}, + name: "empty_string", + input: "", + expected: []string{}, + desc: "Empty string should return empty slice", }, { - name: "Extensions with existing dot prefixes", - extensions: ".jpg,.png,.gif", - want: []string{".jpg", ".png", ".gif"}, + name: "only_whitespace", + input: " ", + expected: []string{}, + desc: "Only whitespace should return empty slice", }, { - name: "Mixed casing and extra whitespace", - extensions: " JPG , .Png , Gif ", - want: []string{".jpg", ".png", ".gif"}, + name: "only_commas", + input: ",,,", + expected: []string{}, + desc: "Only commas should return empty slice", + }, + + // Single extension cases + { + name: "single_ext_without_dot", + input: "txt", + expected: []string{".txt"}, + desc: "Single extension without dot should add dot prefix and lowercase", + }, + { + name: "single_ext_with_dot", + input: ".txt", + expected: []string{".txt"}, + desc: "Single extension with dot should keep dot and lowercase", + }, + { + name: "single_ext_uppercase", + input: "TXT", + expected: []string{".txt"}, + desc: "Uppercase extension should be converted to lowercase", + }, + { + name: "single_ext_mixed_case", + input: "JpG", + expected: []string{".jpg"}, + desc: "Mixed case extension should be converted to lowercase", + }, + { + name: "single_ext_with_spaces", + input: " txt ", + expected: []string{".txt"}, + desc: "Extension with surrounding spaces should be trimmed", + }, + + // Multiple extension cases + { + name: "multiple_exts_without_dots", + input: "txt,jpg,pdf", + expected: []string{".txt", ".jpg", ".pdf"}, + desc: "Multiple extensions without dots should all get dot prefix", + }, + { + name: "multiple_exts_with_dots", + input: ".txt,.jpg,.pdf", + expected: []string{".txt", ".jpg", ".pdf"}, + desc: "Multiple extensions with dots should keep dots", + }, + { + name: "multiple_exts_mixed_dots", + input: "txt,.jpg,pdf", + expected: []string{".txt", ".jpg", ".pdf"}, + desc: "Mix of extensions with and without dots should normalize all", + }, + { + name: "multiple_exts_with_spaces", + input: "txt, jpg, pdf", + expected: []string{".txt", ".jpg", ".pdf"}, + desc: "Extensions with spaces after commas should be trimmed", + }, + { + name: "multiple_exts_mixed_case", + input: "TXT,JpG,PDF", + expected: []string{".txt", ".jpg", ".pdf"}, + desc: "Mixed case extensions should all be lowercased", + }, + + // Edge cases with empty items + { + name: "trailing_comma", + input: "txt,jpg,", + expected: []string{".txt", ".jpg"}, + desc: "Trailing comma should be ignored", + }, + { + name: "leading_comma", + input: ",txt,jpg", + expected: []string{".txt", ".jpg"}, + desc: "Leading comma should be ignored", + }, + { + name: "multiple_consecutive_commas", + input: "txt,,jpg", + expected: []string{".txt", ".jpg"}, + desc: "Multiple consecutive commas should be treated as empty items and ignored", + }, + { + name: "comma_with_spaces", + input: "txt, , jpg", + expected: []string{".txt", ".jpg"}, + desc: "Comma with only spaces should be ignored", + }, + + // Special characters and realistic scenarios + { + name: "programming_languages", + input: "go,js,ts,py,java", + expected: []string{".go", ".js", ".ts", ".py", ".java"}, + desc: "Common programming language extensions", + }, + { + name: "compressed_files", + input: "zip,tar,gz,7z,rar", + expected: []string{".zip", ".tar", ".gz", ".7z", ".rar"}, + desc: "Common compressed file extensions including numbers", + }, + { + name: "media_files", + input: "mp4,mp3,avi,mkv,jpg,png", + expected: []string{".mp4", ".mp3", ".avi", ".mkv", ".jpg", ".png"}, + desc: "Common media file extensions", }, { - name: "Empty segments", - extensions: "jpg,,png", - want: []string{".jpg", ".png"}, + name: "double_extensions", + input: "tar.gz,min.js", + expected: []string{".tar.gz", ".min.js"}, + desc: "Double extensions should be preserved", }, { - name: "Empty string", - extensions: "", - want: []string{}, + name: "excessive_whitespace", + input: " txt , jpg , pdf ", + expected: []string{".txt", ".jpg", ".pdf"}, + desc: "Excessive whitespace around items should be trimmed", }, { - name: "Whitespace-only string", - extensions: " ", - want: []string{}, + name: "mixed_case_and_dots", + input: "TXT, .JpG, Pdf, .DOC", + expected: []string{".txt", ".jpg", ".pdf", ".doc"}, + desc: "Mixed case and dot prefixes should be normalized", }, + + // Duplicate handling (note: function doesn't deduplicate, so duplicates are preserved) + { + name: "duplicate_extensions", + input: "txt,jpg,txt,pdf,jpg", + expected: []string{".txt", ".jpg", ".txt", ".pdf", ".jpg"}, + desc: "Duplicate extensions are preserved (no deduplication)", + }, + + // Single character extensions { - name: "Multiple dots and special chars", - extensions: "..jpg, .tar.gz, txt ", - want: []string{"..jpg", ".tar.gz", ".txt"}, + name: "single_char_extension", + input: "c,h,o", + expected: []string{".c", ".h", ".o"}, + desc: "Single character extensions should work", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := ParseExtToSlice(tt.extensions); !reflect.DeepEqual(got, tt.want) { - t.Errorf("ParseExtToSlice() = %v, want %v", got, tt.want) + result := ParseExtToSlice(tt.input) + + if !reflect.DeepEqual(result, tt.expected) { + t.Errorf("\nTest: %s\nDescription: %s\nInput: %q\nExpected: %v\nGot: %v", + tt.name, tt.desc, tt.input, tt.expected, result) } }) } @@ -62,111 +191,251 @@ func TestParseExtToSlice(t *testing.T) { func TestParseExcludeToSlice(t *testing.T) { tests := []struct { - name string - exclude string - want []string + name string + input string + expected []string + desc string }{ + // Empty and whitespace cases { - name: "Basic valid patterns", - exclude: "node_modules,vendor,temp", - want: []string{"node_modules", "vendor", "temp"}, + name: "empty_string", + input: "", + expected: []string{}, + desc: "Empty string should return empty slice", }, { - name: "Mixed casing and extra whitespace", - exclude: " node_modules , Vendor , TEMP ", - want: []string{"node_modules", "Vendor", "TEMP"}, + name: "only_whitespace", + input: " ", + expected: []string{}, + desc: "Only whitespace should return empty slice", }, { - name: "Empty segments", - exclude: "a,,b", - want: []string{"a", "b"}, + name: "only_commas", + input: ",,,", + expected: []string{}, + desc: "Only commas should return empty slice", }, + + // Single pattern cases { - name: "Empty string", - exclude: "", - want: []string{}, + name: "single_pattern", + input: "data", + expected: []string{"data"}, + desc: "Single pattern should be returned as-is", }, { - name: "Whitespace-only string", - exclude: " ", - want: []string{}, + name: "single_pattern_with_spaces", + input: " data ", + expected: []string{"data"}, + desc: "Pattern with surrounding spaces should be trimmed", + }, + { + name: "single_path_pattern", + input: "node_modules", + expected: []string{"node_modules"}, + desc: "Path-like pattern should be preserved", }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := ParseExcludeToSlice(tt.exclude); !reflect.DeepEqual(got, tt.want) { - t.Errorf("ParseExcludeToSlice() = %v, want %v", got, tt.want) - } - }) - } -} + // Multiple pattern cases + { + name: "multiple_patterns", + input: "data,backup,temp", + expected: []string{"data", "backup", "temp"}, + desc: "Multiple patterns should be split correctly", + }, + { + name: "multiple_patterns_with_spaces", + input: "data, backup, temp", + expected: []string{"data", "backup", "temp"}, + desc: "Patterns with spaces after commas should be trimmed", + }, + { + name: "patterns_with_varied_spacing", + input: " data ,backup, temp ", + expected: []string{"data", "backup", "temp"}, + desc: "Varied spacing around patterns should all be trimmed", + }, -func TestExpandTilde(t *testing.T) { - currentUser, err := user.Current() - if err != nil { - t.Fatal("Failed to get current user:", err) - } - homeDir := currentUser.HomeDir - fullUsername := currentUser.Username + // Edge cases with empty items + { + name: "trailing_comma", + input: "data,backup,", + expected: []string{"data", "backup"}, + desc: "Trailing comma should be ignored", + }, + { + name: "leading_comma", + input: ",data,backup", + expected: []string{"data", "backup"}, + desc: "Leading comma should be ignored", + }, + { + name: "multiple_consecutive_commas", + input: "data,,backup", + expected: []string{"data", "backup"}, + desc: "Multiple consecutive commas should be ignored", + }, + { + name: "comma_with_spaces_only", + input: "data, , backup", + expected: []string{"data", "backup"}, + desc: "Comma-separated whitespace should be ignored", + }, - // Извлекаем "чистое" имя пользователя (без домена) для Lookup на Windows - // На Linux это просто вернёт исходное имя - usernameParts := strings.Split(fullUsername, "\\") - cleanUsername := usernameParts[len(usernameParts)-1] + // Realistic patterns + { + name: "directory_names", + input: "node_modules,.git,dist,build", + expected: []string{"node_modules", ".git", "dist", "build"}, + desc: "Common directory names to exclude", + }, + { + name: "file_patterns", + input: "*.log,*.tmp,*.cache", + expected: []string{"*.log", "*.tmp", "*.cache"}, + desc: "Wildcard patterns should be preserved", + }, + { + name: "path_patterns", + input: "src/test,build/output,temp/cache", + expected: []string{"src/test", "build/output", "temp/cache"}, + desc: "Path patterns with slashes should be preserved", + }, + { + name: "windows_path_patterns", + input: "C:\\\\temp,D:\\\\backup", + expected: []string{"C:\\\\temp", "D:\\\\backup"}, + desc: "Windows-style paths should be preserved", + }, + { + name: "hidden_files_and_folders", + input: ".env,.DS_Store,.vscode", + expected: []string{".env", ".DS_Store", ".vscode"}, + desc: "Hidden files and folders (starting with dot) should be preserved", + }, - tests := []struct { - name string - input string - want string - }{ + // Special characters + { + name: "patterns_with_underscores", + input: "__pycache__,node_modules,test_data", + expected: []string{"__pycache__", "node_modules", "test_data"}, + desc: "Patterns with underscores should be preserved", + }, { - name: "No tilde", - input: "/absolute/path", - want: "/absolute/path", + name: "patterns_with_hyphens", + input: "test-data,my-backup,old-files", + expected: []string{"test-data", "my-backup", "old-files"}, + desc: "Patterns with hyphens should be preserved", }, { - name: "Just tilde", - input: "~", - want: homeDir, + name: "patterns_with_numbers", + input: "temp1,backup2,old3", + expected: []string{"temp1", "backup2", "old3"}, + desc: "Patterns with numbers should be preserved", }, { - name: "Tilde with subpath", - input: "~/documents", - want: filepath.Join(homeDir, "documents"), + name: "patterns_with_mixed_special_chars", + input: "test_data-v1,backup.old,temp@2024", + expected: []string{"test_data-v1", "backup.old", "temp@2024"}, + desc: "Patterns with various special characters should be preserved", }, + + // Case sensitivity (note: function preserves case) { - name: "Tilde with nested subpath", - input: "~/projects/go/src", - want: filepath.Join(homeDir, "projects/go/src"), + name: "mixed_case_patterns", + input: "Data,BACKUP,TeMp", + expected: []string{"Data", "BACKUP", "TeMp"}, + desc: "Case should be preserved exactly as input", }, + + // Duplicate handling { - name: "Tilde with current username", - input: "~" + cleanUsername + "/test", - want: filepath.Join(homeDir, "test"), + name: "duplicate_patterns", + input: "data,backup,data,temp,backup", + expected: []string{"data", "backup", "data", "temp", "backup"}, + desc: "Duplicate patterns are preserved (no deduplication)", }, + + // Excessive whitespace { - name: "Tilde with non-existing user", - input: "~nonexistentuser/file", - want: "~nonexistentuser/file", + name: "excessive_whitespace_between_items", + input: "data , backup , temp", + expected: []string{"data", "backup", "temp"}, + desc: "Excessive whitespace should be trimmed from each item", }, { - name: "Tilde not at start", - input: "/home/~/file", - want: "/home/~/file", + name: "tabs_and_spaces", + input: "data\t,\tbackup\t,\ttemp", + expected: []string{"data", "backup", "temp"}, + desc: "Tabs should be treated as whitespace and trimmed", }, + + // Real-world exclusion scenarios { - name: "Empty string", - input: "", - want: "", + name: "common_dev_directories", + input: "node_modules,vendor,target,.gradle,build", + expected: []string{"node_modules", "vendor", "target", ".gradle", "build"}, + desc: "Common development directories to exclude", + }, + { + name: "temp_and_cache_patterns", + input: "tmp,temp,cache,.cache,*.tmp,*.log", + expected: []string{"tmp", "temp", "cache", ".cache", "*.tmp", "*.log"}, + desc: "Common temporary and cache patterns", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := ExpandTilde(tt.input) - if got != tt.want { - t.Errorf("ExpandTilde(%q) = %q, want %q", tt.input, got, tt.want) + result := ParseExcludeToSlice(tt.input) + + if !reflect.DeepEqual(result, tt.expected) { + t.Errorf("\nTest: %s\nDescription: %s\nInput: %q\nExpected: %v\nGot: %v", + tt.name, tt.desc, tt.input, tt.expected, result) + } + }) + } +} + +// Benchmark tests to measure performance + +func BenchmarkParseExtToSlice(b *testing.B) { + testCases := []struct { + name string + input string + }{ + {"empty", ""}, + {"single", "txt"}, + {"multiple_small", "txt,jpg,pdf"}, + {"multiple_large", "go,js,ts,py,java,cpp,rs,php,rb,swift,kt,scala,hs,lua"}, + {"with_spaces", "txt, jpg, pdf, doc, docx, xls, xlsx, ppt, pptx"}, + } + + for _, tc := range testCases { + b.Run(tc.name, func(b *testing.B) { + for i := 0; i < b.N; i++ { + ParseExtToSlice(tc.input) + } + }) + } +} + +func BenchmarkParseExcludeToSlice(b *testing.B) { + testCases := []struct { + name string + input string + }{ + {"empty", ""}, + {"single", "data"}, + {"multiple_small", "data,backup,temp"}, + {"multiple_large", "node_modules,.git,dist,build,target,vendor,.gradle,.idea,.vscode"}, + {"with_paths", "src/test,build/output,temp/cache,data/backup,logs/old"}, + } + + for _, tc := range testCases { + b.Run(tc.name, func(b *testing.B) { + for i := 0; i < b.N; i++ { + ParseExcludeToSlice(tc.input) } }) } From a29aba396d2388b7bfe3ad974eca85a3f38d6020 Mon Sep 17 00:00:00 2001 From: Elio Neto Date: Thu, 26 Feb 2026 18:12:14 -0300 Subject: [PATCH 2/2] feat: add comprehensive unit tests for path package constants - Add tests for AppDirName constant validation - Add tests for RuleFileName constant validation - Add tests for LogFileName constant validation - Verify constants are non-empty - Verify constants match expected values - Add tests for path construction scenarios - Add validation for file extensions - Follow Go testing best practices with descriptive names Fixes #285 --- internal/path/path_test.go | 357 +++++++++++++++++++++++++++++++++++++ 1 file changed, 357 insertions(+) create mode 100644 internal/path/path_test.go diff --git a/internal/path/path_test.go b/internal/path/path_test.go new file mode 100644 index 0000000..cfdf230 --- /dev/null +++ b/internal/path/path_test.go @@ -0,0 +1,357 @@ +package path + +import ( + "path/filepath" + "strings" + "testing" +) + +// TestAppDirName verifies the AppDirName constant is properly defined +func TestAppDirName(t *testing.T) { + tests := []struct { + name string + checkFunc func(t *testing.T) + description string + }{ + { + name: "not_empty", + checkFunc: func(t *testing.T) { + if AppDirName == "" { + t.Error("AppDirName should not be empty") + } + }, + description: "AppDirName constant must not be empty", + }, + { + name: "expected_value", + checkFunc: func(t *testing.T) { + expected := "deletor" + if AppDirName != expected { + t.Errorf("AppDirName = %q, expected %q", AppDirName, expected) + } + }, + description: "AppDirName should equal 'deletor'", + }, + { + name: "no_whitespace", + checkFunc: func(t *testing.T) { + if strings.TrimSpace(AppDirName) != AppDirName { + t.Error("AppDirName should not contain leading or trailing whitespace") + } + }, + description: "AppDirName should not have leading/trailing whitespace", + }, + { + name: "no_path_separators", + checkFunc: func(t *testing.T) { + if strings.Contains(AppDirName, string(filepath.Separator)) { + t.Error("AppDirName should not contain path separators") + } + }, + description: "AppDirName should be a simple directory name without path separators", + }, + { + name: "valid_directory_name", + checkFunc: func(t *testing.T) { + // Check for invalid characters in directory names (common across OS) + invalidChars := []string{"<", ">", ":", "\"", "|", "?", "*"} + for _, char := range invalidChars { + if strings.Contains(AppDirName, char) { + t.Errorf("AppDirName should not contain invalid character: %q", char) + } + } + }, + description: "AppDirName should not contain characters invalid for directory names", + }, + { + name: "lowercase", + checkFunc: func(t *testing.T) { + if AppDirName != strings.ToLower(AppDirName) { + t.Error("AppDirName should be lowercase for cross-platform consistency") + } + }, + description: "AppDirName should be lowercase", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.checkFunc(t) + }) + } +} + +// TestRuleFileName verifies the RuleFileName constant is properly defined +func TestRuleFileName(t *testing.T) { + tests := []struct { + name string + checkFunc func(t *testing.T) + description string + }{ + { + name: "not_empty", + checkFunc: func(t *testing.T) { + if RuleFileName == "" { + t.Error("RuleFileName should not be empty") + } + }, + description: "RuleFileName constant must not be empty", + }, + { + name: "expected_value", + checkFunc: func(t *testing.T) { + expected := "rule.json" + if RuleFileName != expected { + t.Errorf("RuleFileName = %q, expected %q", RuleFileName, expected) + } + }, + description: "RuleFileName should equal 'rule.json'", + }, + { + name: "has_json_extension", + checkFunc: func(t *testing.T) { + if !strings.HasSuffix(RuleFileName, ".json") { + t.Error("RuleFileName should have .json extension") + } + }, + description: "RuleFileName should have JSON extension", + }, + { + name: "no_whitespace", + checkFunc: func(t *testing.T) { + if strings.TrimSpace(RuleFileName) != RuleFileName { + t.Error("RuleFileName should not contain leading or trailing whitespace") + } + }, + description: "RuleFileName should not have leading/trailing whitespace", + }, + { + name: "no_path_separators", + checkFunc: func(t *testing.T) { + if strings.Contains(RuleFileName, string(filepath.Separator)) { + t.Error("RuleFileName should not contain path separators") + } + }, + description: "RuleFileName should be a simple filename without path", + }, + { + name: "valid_filename", + checkFunc: func(t *testing.T) { + // Check for invalid characters in filenames + invalidChars := []string{"<", ">", ":", "\"", "|", "?", "*"} + for _, char := range invalidChars { + if strings.Contains(RuleFileName, char) { + t.Errorf("RuleFileName should not contain invalid character: %q", char) + } + } + }, + description: "RuleFileName should not contain characters invalid for filenames", + }, + { + name: "valid_json_structure", + checkFunc: func(t *testing.T) { + ext := filepath.Ext(RuleFileName) + if ext != ".json" { + t.Errorf("RuleFileName extension = %q, expected '.json'", ext) + } + }, + description: "RuleFileName extension should be exactly '.json'", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.checkFunc(t) + }) + } +} + +// TestLogFileName verifies the LogFileName constant is properly defined +func TestLogFileName(t *testing.T) { + tests := []struct { + name string + checkFunc func(t *testing.T) + description string + }{ + { + name: "not_empty", + checkFunc: func(t *testing.T) { + if LogFileName == "" { + t.Error("LogFileName should not be empty") + } + }, + description: "LogFileName constant must not be empty", + }, + { + name: "expected_value", + checkFunc: func(t *testing.T) { + expected := "deletor.log" + if LogFileName != expected { + t.Errorf("LogFileName = %q, expected %q", LogFileName, expected) + } + }, + description: "LogFileName should equal 'deletor.log'", + }, + { + name: "has_log_extension", + checkFunc: func(t *testing.T) { + if !strings.HasSuffix(LogFileName, ".log") { + t.Error("LogFileName should have .log extension") + } + }, + description: "LogFileName should have .log extension", + }, + { + name: "no_whitespace", + checkFunc: func(t *testing.T) { + if strings.TrimSpace(LogFileName) != LogFileName { + t.Error("LogFileName should not contain leading or trailing whitespace") + } + }, + description: "LogFileName should not have leading/trailing whitespace", + }, + { + name: "no_path_separators", + checkFunc: func(t *testing.T) { + if strings.Contains(LogFileName, string(filepath.Separator)) { + t.Error("LogFileName should not contain path separators") + } + }, + description: "LogFileName should be a simple filename without path", + }, + { + name: "valid_filename", + checkFunc: func(t *testing.T) { + // Check for invalid characters in filenames + invalidChars := []string{"<", ">", ":", "\"", "|", "?", "*"} + for _, char := range invalidChars { + if strings.Contains(LogFileName, char) { + t.Errorf("LogFileName should not contain invalid character: %q", char) + } + } + }, + description: "LogFileName should not contain characters invalid for filenames", + }, + { + name: "valid_log_structure", + checkFunc: func(t *testing.T) { + ext := filepath.Ext(LogFileName) + if ext != ".log" { + t.Errorf("LogFileName extension = %q, expected '.log'", ext) + } + }, + description: "LogFileName extension should be exactly '.log'", + }, + { + name: "matches_app_name", + checkFunc: func(t *testing.T) { + // Log filename should start with app name for consistency + if !strings.HasPrefix(LogFileName, AppDirName) { + t.Errorf("LogFileName should start with AppDirName (%q), got %q", AppDirName, LogFileName) + } + }, + description: "LogFileName should start with application name for consistency", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.checkFunc(t) + }) + } +} + +// TestPathConstantsIntegration verifies that all constants work together correctly +func TestPathConstantsIntegration(t *testing.T) { + tests := []struct { + name string + checkFunc func(t *testing.T) + description string + }{ + { + name: "all_constants_defined", + checkFunc: func(t *testing.T) { + if AppDirName == "" || RuleFileName == "" || LogFileName == "" { + t.Error("All path constants must be defined") + } + }, + description: "All constants should be non-empty", + }, + { + name: "constants_are_unique", + checkFunc: func(t *testing.T) { + if AppDirName == RuleFileName || AppDirName == LogFileName || RuleFileName == LogFileName { + t.Error("All constants should have unique values") + } + }, + description: "All constants should be distinct from each other", + }, + { + name: "can_construct_rule_path", + checkFunc: func(t *testing.T) { + // Simulate constructing a typical rule file path + path := filepath.Join("config", AppDirName, RuleFileName) + if !strings.Contains(path, AppDirName) || !strings.Contains(path, RuleFileName) { + t.Error("Should be able to construct valid rule file path") + } + }, + description: "Constants should work together to construct valid paths", + }, + { + name: "can_construct_log_path", + checkFunc: func(t *testing.T) { + // Simulate constructing a typical log file path + path := filepath.Join("config", AppDirName, LogFileName) + if !strings.Contains(path, AppDirName) || !strings.Contains(path, LogFileName) { + t.Error("Should be able to construct valid log file path") + } + }, + description: "Constants should work together to construct valid log paths", + }, + { + name: "filenames_have_different_extensions", + checkFunc: func(t *testing.T) { + ruleExt := filepath.Ext(RuleFileName) + logExt := filepath.Ext(LogFileName) + if ruleExt == logExt { + t.Error("RuleFileName and LogFileName should have different extensions") + } + }, + description: "Rule and log files should have different extensions", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.checkFunc(t) + }) + } +} + +// TestConstantsImmutability ensures constants maintain expected values +// This test acts as a safety net against accidental changes +func TestConstantsImmutability(t *testing.T) { + // These values are critical for the application's file structure + // Any change should be intentional and well-documented + expectedValues := map[string]string{ + "AppDirName": "deletor", + "RuleFileName": "rule.json", + "LogFileName": "deletor.log", + } + + actualValues := map[string]string{ + "AppDirName": AppDirName, + "RuleFileName": RuleFileName, + "LogFileName": LogFileName, + } + + for name, expected := range expectedValues { + t.Run(name, func(t *testing.T) { + actual := actualValues[name] + if actual != expected { + t.Errorf("%s changed from %q to %q - this may break existing installations", + name, expected, actual) + } + }) + } +}