|
| 1 | +package utils_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/TheManticoreProject/Manticore/utils" |
| 9 | +) |
| 10 | + |
| 11 | +func TestPathSafeString(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + input string |
| 15 | + expected string |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "empty string", |
| 19 | + input: "", |
| 20 | + expected: "", |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "no unsafe characters", |
| 24 | + input: "safe_filename_123", |
| 25 | + expected: "safe_filename_123", |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "common unsafe characters", |
| 29 | + input: "file/name\\with:bad*chars?\"<>|", |
| 30 | + expected: "file_name_with_bad_chars_____", |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "whitespace characters", |
| 34 | + input: "file name\nwith\rspaces\t", |
| 35 | + expected: "file_name_with_spaces_", |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "mixed safe and unsafe", |
| 39 | + input: "My Document (v1.0)/part_A.txt", |
| 40 | + expected: "My_Document_(v1.0)_part_A.txt", |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "all unsafe characters", |
| 44 | + input: "/:*?\"<>| \n\r\t", |
| 45 | + expected: "____________", |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "multiple occurrences of same unsafe char", |
| 49 | + input: "path//to\\file", |
| 50 | + expected: "path__to_file", |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "leading/trailing unsafe chars", |
| 54 | + input: " /file.txt ", |
| 55 | + expected: "__file.txt_", |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + for _, tt := range tests { |
| 60 | + t.Run(tt.name, func(t *testing.T) { |
| 61 | + got := utils.PathSafeString(tt.input) |
| 62 | + if got != tt.expected { |
| 63 | + t.Errorf("PathSafeString(%q) = %q, want %q", tt.input, got, tt.expected) |
| 64 | + } |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestEnsureDirExists(t *testing.T) { |
| 70 | + baseTempDir := filepath.Join(os.TempDir(), "test_ensure_dir_exists") |
| 71 | + // Ensure cleanup for all tests in this function |
| 72 | + defer func() { |
| 73 | + if err := os.RemoveAll(baseTempDir); err != nil { |
| 74 | + t.Logf("Failed to clean up base temp directory %q: %v", baseTempDir, err) |
| 75 | + } |
| 76 | + }() |
| 77 | + |
| 78 | + t.Run("directory does not exist", func(t *testing.T) { |
| 79 | + dirPath := filepath.Join(baseTempDir, "new_dir") |
| 80 | + // Ensure it doesn't exist before the test |
| 81 | + _ = os.RemoveAll(dirPath) |
| 82 | + |
| 83 | + err := utils.EnsureDirExists(dirPath) |
| 84 | + if err != nil { |
| 85 | + t.Fatalf("EnsureDirExists(%q) returned an error: %v", dirPath, err) |
| 86 | + } |
| 87 | + |
| 88 | + if _, err := os.Stat(dirPath); os.IsNotExist(err) { |
| 89 | + t.Errorf("EnsureDirExists(%q) failed to create directory", dirPath) |
| 90 | + } |
| 91 | + }) |
| 92 | + |
| 93 | + t.Run("directory already exists", func(t *testing.T) { |
| 94 | + dirPath := filepath.Join(baseTempDir, "existing_dir") |
| 95 | + err := os.MkdirAll(dirPath, 0755) |
| 96 | + if err != nil { |
| 97 | + t.Fatalf("Failed to create pre-existing directory: %v", err) |
| 98 | + } |
| 99 | + |
| 100 | + err = utils.EnsureDirExists(dirPath) |
| 101 | + if err != nil { |
| 102 | + t.Errorf("EnsureDirExists(%q) returned an error for existing directory: %v", dirPath, err) |
| 103 | + } |
| 104 | + }) |
| 105 | + |
| 106 | + t.Run("nested directories", func(t *testing.T) { |
| 107 | + dirPath := filepath.Join(baseTempDir, "parent", "child", "grandchild") |
| 108 | + // Ensure parent doesn't exist before the test |
| 109 | + _ = os.RemoveAll(filepath.Join(baseTempDir, "parent")) |
| 110 | + |
| 111 | + err := utils.EnsureDirExists(dirPath) |
| 112 | + if err != nil { |
| 113 | + t.Fatalf("EnsureDirExists(%q) returned an error for nested directories: %v", dirPath, err) |
| 114 | + } |
| 115 | + |
| 116 | + if _, err := os.Stat(dirPath); os.IsNotExist(err) { |
| 117 | + t.Errorf("EnsureDirExists(%q) failed to create nested directories", dirPath) |
| 118 | + } |
| 119 | + }) |
| 120 | + |
| 121 | + t.Run("path exists as file returns error", func(t *testing.T) { |
| 122 | + filePath := filepath.Join(baseTempDir, "somefile") |
| 123 | + // Ensure fresh base dir |
| 124 | + _ = os.RemoveAll(baseTempDir) |
| 125 | + if err := os.MkdirAll(baseTempDir, 0755); err != nil { |
| 126 | + t.Fatalf("failed to create base temp dir: %v", err) |
| 127 | + } |
| 128 | + // Create a file at the path |
| 129 | + f, err := os.Create(filePath) |
| 130 | + if err != nil { |
| 131 | + t.Fatalf("failed to create file for test: %v", err) |
| 132 | + } |
| 133 | + _ = f.Close() |
| 134 | + |
| 135 | + err = utils.EnsureDirExists(filePath) |
| 136 | + if err == nil { |
| 137 | + t.Fatalf("EnsureDirExists(%q) did not return error when path is an existing file", filePath) |
| 138 | + } |
| 139 | + }) |
| 140 | +} |
0 commit comments