-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprompt_test.go
199 lines (179 loc) · 4.08 KB
/
prompt_test.go
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"os"
"path/filepath"
"testing"
)
func TestHandleDeletions(t *testing.T) {
oldStdin := os.Stdin
defer func() { os.Stdin = oldStdin }()
tmpDir := t.TempDir()
fileNames := []string{"file1.txt", "file2.txt", "file3.txt"}
filePaths := make([]string, len(fileNames))
for i, name := range fileNames {
path := filepath.Join(tmpDir, name)
if err := os.WriteFile(path, []byte("test"), 0644); err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
filePaths[i] = path
}
input := "1 3\n"
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Pipe creation failed: %v", err)
}
defer r.Close()
_, err = w.WriteString(input)
if err != nil {
t.Fatalf("Failed to write to pipe: %v", err)
}
w.Close()
os.Stdin = r
handleDeletions([][]string{filePaths})
assertFileExists(t, filePaths[0], false)
assertFileExists(t, filePaths[1], true)
assertFileExists(t, filePaths[2], false)
}
func assertFileExists(t *testing.T, path string, shouldExist bool) {
t.Helper()
_, err := os.Stat(path)
if shouldExist && os.IsNotExist(err) {
t.Errorf("File %s should exist but was deleted", path)
} else if !shouldExist && err == nil {
t.Errorf("File %s should be deleted but exists", path)
}
}
func TestParseDeleteInput(t *testing.T) {
tests := []struct {
name string
input string
max int
expected []int
expectedErr bool
}{
{
name: "valid input with single number",
input: "1",
max: 3,
expected: []int{0},
expectedErr: false,
},
{
name: "valid input with multiple numbers",
input: "1 2 3",
max: 3,
expected: []int{0, 1, 2},
expectedErr: false,
},
{
name: "input with duplicates",
input: "1 2 2 3",
max: 3,
expected: []int{0, 1, 2},
expectedErr: false,
},
{
name: "input with invalid number",
input: "1 4",
max: 3,
expected: nil,
expectedErr: true,
},
{
name: "empty input",
input: "",
max: 3,
expected: nil,
expectedErr: false,
},
{
name: "input with out-of-range number",
input: "0 2",
max: 3,
expected: nil,
expectedErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := parseDeleteInput(tt.input, tt.max)
if tt.expectedErr && err == nil {
t.Errorf("Expected error but got nil")
}
if !tt.expectedErr && err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !equal(result, tt.expected) {
t.Errorf("Expected %v but got %v", tt.expected, result)
}
})
}
}
func equal(a, b []int) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func TestDeleteFiles(t *testing.T) {
tempDir := t.TempDir()
tests := []struct {
name string
files []string
indices []int
expectedErr bool
}{
{
name: "delete single file",
files: []string{"file1.txt"},
indices: []int{0},
expectedErr: false,
},
{
name: "delete multiple files",
files: []string{"file1.txt", "file2.txt"},
indices: []int{0, 1},
expectedErr: false,
},
{
name: "delete some files",
files: []string{"file1.txt", "file2.txt", "file3.txt", "file4.txt"},
indices: []int{1, 3},
expectedErr: false,
},
{
name: "delete non-existent file",
files: []string{"file1.txt"},
indices: []int{0},
expectedErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var group []string
for _, file := range tt.files {
filePath := filepath.Join(tempDir, file)
group = append(group, filePath)
if !tt.expectedErr {
_, err := os.Create(filePath)
if err != nil {
t.Fatalf("Failed to create file: %v", err)
}
}
}
deleteFiles(group, tt.indices)
for _, idx := range tt.indices {
path := group[idx]
_, err := os.Stat(path)
if err == nil {
t.Errorf("Failed to delete file %s: %v", path, err)
}
}
})
}
}