|
| 1 | +/* |
| 2 | + * Copyright 2026 Han Li and contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package util |
| 18 | + |
| 19 | +import ( |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + "testing" |
| 23 | +) |
| 24 | + |
| 25 | +func TestRenameFile(t *testing.T) { |
| 26 | + // Create temp directory for testing |
| 27 | + tmpDir, err := os.MkdirTemp("", "vfox-rename-test-") |
| 28 | + if err != nil { |
| 29 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 30 | + } |
| 31 | + defer os.RemoveAll(tmpDir) |
| 32 | + |
| 33 | + // Create a test file |
| 34 | + srcFile := filepath.Join(tmpDir, "test-src.txt") |
| 35 | + content := "test content" |
| 36 | + if err := os.WriteFile(srcFile, []byte(content), 0644); err != nil { |
| 37 | + t.Fatalf("Failed to create test file: %v", err) |
| 38 | + } |
| 39 | + |
| 40 | + // Test renaming file |
| 41 | + dstFile := filepath.Join(tmpDir, "test-dst.txt") |
| 42 | + if err := Rename(srcFile, dstFile); err != nil { |
| 43 | + t.Fatalf("Rename failed: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + // Verify source no longer exists |
| 47 | + if FileExists(srcFile) { |
| 48 | + t.Error("Source file still exists after rename") |
| 49 | + } |
| 50 | + |
| 51 | + // Verify destination exists with correct content |
| 52 | + if !FileExists(dstFile) { |
| 53 | + t.Error("Destination file does not exist") |
| 54 | + } |
| 55 | + readContent, err := os.ReadFile(dstFile) |
| 56 | + if err != nil { |
| 57 | + t.Fatalf("Failed to read destination file: %v", err) |
| 58 | + } |
| 59 | + if string(readContent) != content { |
| 60 | + t.Errorf("Content mismatch: expected %q, got %q", content, string(readContent)) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestRenameDirectory(t *testing.T) { |
| 65 | + // Create temp directory for testing |
| 66 | + tmpDir, err := os.MkdirTemp("", "vfox-rename-test-") |
| 67 | + if err != nil { |
| 68 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 69 | + } |
| 70 | + defer os.RemoveAll(tmpDir) |
| 71 | + |
| 72 | + // Create a test directory structure |
| 73 | + srcDir := filepath.Join(tmpDir, "test-src-dir") |
| 74 | + if err := os.Mkdir(srcDir, 0755); err != nil { |
| 75 | + t.Fatalf("Failed to create test directory: %v", err) |
| 76 | + } |
| 77 | + |
| 78 | + // Create nested structure |
| 79 | + subDir := filepath.Join(srcDir, "subdir") |
| 80 | + if err := os.Mkdir(subDir, 0755); err != nil { |
| 81 | + t.Fatalf("Failed to create subdirectory: %v", err) |
| 82 | + } |
| 83 | + |
| 84 | + file1 := filepath.Join(srcDir, "file1.txt") |
| 85 | + file2 := filepath.Join(subDir, "file2.txt") |
| 86 | + content1 := "content1" |
| 87 | + content2 := "content2" |
| 88 | + |
| 89 | + if err := os.WriteFile(file1, []byte(content1), 0644); err != nil { |
| 90 | + t.Fatalf("Failed to create file1: %v", err) |
| 91 | + } |
| 92 | + if err := os.WriteFile(file2, []byte(content2), 0644); err != nil { |
| 93 | + t.Fatalf("Failed to create file2: %v", err) |
| 94 | + } |
| 95 | + |
| 96 | + // Test renaming directory |
| 97 | + dstDir := filepath.Join(tmpDir, "test-dst-dir") |
| 98 | + if err := Rename(srcDir, dstDir); err != nil { |
| 99 | + t.Fatalf("Rename directory failed: %v", err) |
| 100 | + } |
| 101 | + |
| 102 | + // Verify source no longer exists |
| 103 | + if FileExists(srcDir) { |
| 104 | + t.Error("Source directory still exists after rename") |
| 105 | + } |
| 106 | + |
| 107 | + // Verify destination exists with correct structure |
| 108 | + if !FileExists(dstDir) { |
| 109 | + t.Error("Destination directory does not exist") |
| 110 | + } |
| 111 | + |
| 112 | + // Check file1 |
| 113 | + dstFile1 := filepath.Join(dstDir, "file1.txt") |
| 114 | + if !FileExists(dstFile1) { |
| 115 | + t.Error("file1.txt does not exist in destination") |
| 116 | + } |
| 117 | + readContent1, err := os.ReadFile(dstFile1) |
| 118 | + if err != nil { |
| 119 | + t.Fatalf("Failed to read file1: %v", err) |
| 120 | + } |
| 121 | + if string(readContent1) != content1 { |
| 122 | + t.Errorf("Content mismatch in file1: expected %q, got %q", content1, string(readContent1)) |
| 123 | + } |
| 124 | + |
| 125 | + // Check file2 in subdirectory |
| 126 | + dstFile2 := filepath.Join(dstDir, "subdir", "file2.txt") |
| 127 | + if !FileExists(dstFile2) { |
| 128 | + t.Error("file2.txt does not exist in destination subdirectory") |
| 129 | + } |
| 130 | + readContent2, err := os.ReadFile(dstFile2) |
| 131 | + if err != nil { |
| 132 | + t.Fatalf("Failed to read file2: %v", err) |
| 133 | + } |
| 134 | + if string(readContent2) != content2 { |
| 135 | + t.Errorf("Content mismatch in file2: expected %q, got %q", content2, string(readContent2)) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func TestRenameNonExistent(t *testing.T) { |
| 140 | + // Create temp directory for testing |
| 141 | + tmpDir, err := os.MkdirTemp("", "vfox-rename-test-") |
| 142 | + if err != nil { |
| 143 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 144 | + } |
| 145 | + defer os.RemoveAll(tmpDir) |
| 146 | + |
| 147 | + srcFile := filepath.Join(tmpDir, "nonexistent.txt") |
| 148 | + dstFile := filepath.Join(tmpDir, "destination.txt") |
| 149 | + |
| 150 | + // Test renaming non-existent file should fail |
| 151 | + err = Rename(srcFile, dstFile) |
| 152 | + if err == nil { |
| 153 | + t.Error("Expected error when renaming non-existent file, got nil") |
| 154 | + } |
| 155 | +} |
0 commit comments