-
Notifications
You must be signed in to change notification settings - Fork 35
/
undo_test.go
175 lines (133 loc) · 3.94 KB
/
undo_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
package main
import (
"os"
"path/filepath"
"testing"
)
func Test_handleUndoCommand_noArgs(t *testing.T) {
setup(t)
defer teardown(t)
var opts CommandLineOptions
err := handleUndoCommand(&opts, []string{})
if err != nil {
t.Fail()
}
}
func Test_handleUndoCommand_notInHistory(t *testing.T) {
setup(t)
defer teardown(t)
var opts CommandLineOptions
err := handleUndoCommand(&opts, []string{"one", "two"})
if err != nil {
t.Fail()
}
}
func Test_handleUndoCommand(t *testing.T) {
setup(t)
defer teardown(t)
touch(filepath.Join(tempFolder(), "one"))
touch(filepath.Join(tempFolder(), "two"))
touch(filepath.Join(tempFolder(), "three"))
var fileActions []*FileAction
var fileAction *FileAction
fileAction = NewFileAction()
fileAction.oldPath = filepath.Join(tempFolder(), "one")
fileAction.newPath = "123"
fileActions = append(fileActions, fileAction)
fileAction = NewFileAction()
fileAction.oldPath = filepath.Join(tempFolder(), "two")
fileAction.newPath = "456"
fileActions = append(fileActions, fileAction)
processFileActions(fileActions, false)
var opts CommandLineOptions
err := handleUndoCommand(&opts, []string{
filepath.Join(tempFolder(), "123"), filepath.Join(tempFolder(), "456"),
})
if err != nil {
t.Errorf("Expected no error, got: %s", err)
}
if !fileExists(filepath.Join(tempFolder(), "one")) || !fileExists(filepath.Join(tempFolder(), "two")) {
t.Error("Undo operation did not restore filenames")
}
historyItems, _ := allHistoryItems()
if len(historyItems) > 0 {
t.Error("Undo operation did not delete restored history.")
}
fileActions = []*FileAction{}
fileAction = NewFileAction()
fileAction.oldPath = filepath.Join(tempFolder(), "one")
fileAction.newPath = "123"
fileActions = append(fileActions, fileAction)
fileAction = NewFileAction()
fileAction.oldPath = filepath.Join(tempFolder(), "two")
fileAction.newPath = "456"
fileActions = append(fileActions, fileAction)
processFileActions(fileActions, false)
opts = CommandLineOptions{
DryRun: true,
}
err = handleUndoCommand(&opts, []string{
filepath.Join(tempFolder(), "123"), filepath.Join(tempFolder(), "456"),
})
if !fileExists(filepath.Join(tempFolder(), "123")) || !fileExists(filepath.Join(tempFolder(), "456")) {
t.Error("Undo operation in dry run mode restored filenames.")
}
}
func Test_handleUndoCommand_fileHasBeenDeleted(t *testing.T) {
setup(t)
defer teardown(t)
var err error
touch(filepath.Join(tempFolder(), "one"))
touch(filepath.Join(tempFolder(), "two"))
touch(filepath.Join(tempFolder(), "three"))
var fileActions []*FileAction
var fileAction *FileAction
fileAction = NewFileAction()
fileAction.oldPath = filepath.Join(tempFolder(), "one")
fileAction.newPath = "123"
fileActions = append(fileActions, fileAction)
fileAction = NewFileAction()
fileAction.oldPath = filepath.Join(tempFolder(), "two")
fileAction.newPath = "456"
fileActions = append(fileActions, fileAction)
processFileActions(fileActions, false)
os.Remove(filepath.Join(tempFolder(), "123"))
var opts CommandLineOptions
err = handleUndoCommand(&opts, []string{
filepath.Join(tempFolder(), "123"),
})
if err == nil {
t.Fail()
}
}
func Test_handleUndoCommand_withIntermediateRename(t *testing.T) {
setup(t)
defer teardown(t)
p0 := filepath.Join(tempFolder(), "0")
p1 := filepath.Join(tempFolder(), "1")
filePutContent(p0, "0")
filePutContent(p1, "1")
fileActions := []*FileAction{}
fileAction := NewFileAction()
fileAction.oldPath = p0
fileAction.newPath = "1"
fileActions = append(fileActions, fileAction)
fileAction = NewFileAction()
fileAction.oldPath = p1
fileAction.newPath = "0"
fileActions = append(fileActions, fileAction)
processFileActions(fileActions, false)
var opts CommandLineOptions
err := handleUndoCommand(&opts, []string{
p0, p1,
})
if err != nil {
t.Fail()
}
if fileGetContent(p0) != "0" {
t.Error("File 0 was not restored")
}
if fileGetContent(p1) != "1" {
t.Error("File 1 was not restored")
}
}