1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
package archive
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func TestArchiveCards(t *testing.T) {
// Create temp directory
tmpDir := t.TempDir()
// Create cards directory with some test files
cardsDir := filepath.Join(tmpDir, "cards")
if err := os.MkdirAll(cardsDir, 0755); err != nil {
t.Fatalf("Failed to create cards directory: %v", err)
}
// Create some test files in cards directory
testFile := filepath.Join(cardsDir, "test.txt")
if err := os.WriteFile(testFile, []byte("test content"), 0644); err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
// Create a subdirectory with a file
subDir := filepath.Join(cardsDir, "subdir")
if err := os.MkdirAll(subDir, 0755); err != nil {
t.Fatalf("Failed to create subdirectory: %v", err)
}
subFile := filepath.Join(subDir, "subfile.txt")
if err := os.WriteFile(subFile, []byte("sub content"), 0644); err != nil {
t.Fatalf("Failed to create sub file: %v", err)
}
// Archive the cards directory
if err := ArchiveCards(cardsDir); err != nil {
t.Fatalf("ArchiveCards failed: %v", err)
}
// Check that cards directory no longer exists
if _, err := os.Stat(cardsDir); !os.IsNotExist(err) {
t.Error("Cards directory still exists after archiving")
}
// Check that archive directory was created
archiveDir := filepath.Join(tmpDir, "archive")
if _, err := os.Stat(archiveDir); os.IsNotExist(err) {
t.Error("Archive directory was not created")
}
// Check that archived directory exists with timestamp
entries, err := os.ReadDir(archiveDir)
if err != nil {
t.Fatalf("Failed to read archive directory: %v", err)
}
if len(entries) != 1 {
t.Fatalf("Expected 1 entry in archive directory, got %d", len(entries))
}
// Verify the archived directory name starts with "cards-"
archivedName := entries[0].Name()
if !strings.HasPrefix(archivedName, "cards-") {
t.Errorf("Archived directory name doesn't start with 'cards-': %s", archivedName)
}
// Verify timestamp format (should be cards-YYYYMMDD-HHMMSS)
parts := strings.Split(archivedName, "-")
if len(parts) < 3 {
t.Errorf("Invalid archive name format: %s", archivedName)
}
// Check that archived files exist
archivedPath := filepath.Join(archiveDir, archivedName)
archivedTestFile := filepath.Join(archivedPath, "test.txt")
if _, err := os.Stat(archivedTestFile); os.IsNotExist(err) {
t.Error("Test file not found in archive")
}
archivedSubFile := filepath.Join(archivedPath, "subdir", "subfile.txt")
if _, err := os.Stat(archivedSubFile); os.IsNotExist(err) {
t.Error("Sub file not found in archive")
}
}
func TestArchiveCards_NonExistentDirectory(t *testing.T) {
tmpDir := t.TempDir()
nonExistentDir := filepath.Join(tmpDir, "nonexistent")
err := ArchiveCards(nonExistentDir)
if err == nil {
t.Error("Expected error for non-existent directory")
}
if !strings.Contains(err.Error(), "does not exist") {
t.Errorf("Expected 'does not exist' error, got: %v", err)
}
}
func TestArchiveCards_MultipleArchives(t *testing.T) {
// Create temp directory
tmpDir := t.TempDir()
// Archive twice to ensure unique timestamps
for i := 0; i < 2; i++ {
// Create cards directory
cardsDir := filepath.Join(tmpDir, "cards")
if err := os.MkdirAll(cardsDir, 0755); err != nil {
t.Fatalf("Failed to create cards directory: %v", err)
}
// Create a test file
testFile := filepath.Join(cardsDir, "test.txt")
content := []byte("test content " + string(rune(i)))
if err := os.WriteFile(testFile, content, 0644); err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
// Small delay to ensure different timestamps
if i == 1 {
time.Sleep(10 * time.Millisecond)
}
// Archive
if err := ArchiveCards(cardsDir); err != nil {
t.Fatalf("ArchiveCards failed on iteration %d: %v", i, err)
}
}
// Check that we have 2 archives
archiveDir := filepath.Join(tmpDir, "archive")
entries, err := os.ReadDir(archiveDir)
if err != nil {
t.Fatalf("Failed to read archive directory: %v", err)
}
if len(entries) != 2 {
t.Fatalf("Expected 2 entries in archive directory, got %d", len(entries))
}
// Verify both archives have different names
if entries[0].Name() == entries[1].Name() {
t.Error("Archive names are not unique")
}
}
|