-
Notifications
You must be signed in to change notification settings - Fork 1
/
filediff_test.go
302 lines (266 loc) · 7.58 KB
/
filediff_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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package main
import (
"errors"
"io/ioutil"
"log"
"os"
"reflect"
"testing"
)
func updateStdInContent(tmpfile *os.File, input string) {
content := []byte(input)
if _, err := tmpfile.Write(content); err != nil {
log.Fatal(err)
}
if _, err := tmpfile.Seek(0, 0); err != nil {
log.Fatal(err)
}
}
func loadTestFile(fileName string) fileInfoExtended {
fileStat, _ := os.Stat(fileName)
testFileExt := fileInfoExtended{
osPathname: fileName,
fileInfo: fileStat,
}
return testFileExt
}
func Test_askForConfirmation(t *testing.T) {
oldStdin := os.Stdin
defer func() { os.Stdin = oldStdin }() // Restore original Stdin
tests := []struct {
name string
want bool
err error
}{
{"y", true, nil},
{"Y", true, nil},
{"yes", true, nil},
{"n", false, nil},
{"N", false, nil},
{"no", false, nil},
{"q", false, ErrorCanceled},
{"blabla", false, nil},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "utesttmp.txt")
if err != nil {
log.Fatal(err)
}
os.Stdin = tmpfile
updateStdInContent(tmpfile, tt.name)
got, err := askForConfirmation()
if got != tt.want {
t.Errorf("askForConfirmation() = %v, want %v", got, tt.want)
}
if err != nil && tt.err == nil {
t.Errorf("askForConfirmation() = %v, want %v", err, tt.err)
}
if err == nil && tt.err != nil {
t.Errorf("askForConfirmation() = %v, want %v", err, tt.err)
}
if err != nil && tt.err != nil && !errors.Is(err, tt.err) {
t.Errorf("askForConfirmation() = %v, want %v", err, tt.err)
}
os.Remove(tmpfile.Name())
if err := tmpfile.Close(); err != nil {
log.Fatal(err)
}
})
}
}
func Test_reviewDiff(t *testing.T) {
type args struct {
mydiffString string
fileAName string
fileBName string
autoApply bool
}
tests := []struct {
name string
args args
want bool
wantErr bool
}{
{"SimpleTest1", args{mydiffString: "Test1", fileAName: "FileA", fileBName: "FileB", autoApply: true}, true, false},
{"SimpleTest2", args{mydiffString: "Test2", fileAName: "FileA", fileBName: "FileB", autoApply: false}, false, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := reviewDiff(tt.args.mydiffString, tt.args.fileAName, tt.args.fileBName, tt.args.autoApply)
if (err != nil) != tt.wantErr {
t.Errorf("reviewDiff() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("reviewDiff() = %v, want %v", got, tt.want)
}
})
}
}
func Test_reviewPatchDetailed(t *testing.T) {
type args struct {
patchString string
fileAName string
autoApply bool
}
tests := []struct {
name string
args args
want bool
wantErr bool
}{
{"SimpleTest1", args{patchString: "Test1", fileAName: "FileA", autoApply: true}, true, false},
{"SimpleTest2", args{patchString: "Test2", fileAName: "FileA", autoApply: false}, false, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := reviewPatchDetailed(tt.args.patchString, tt.args.fileAName, tt.args.autoApply)
if (err != nil) != tt.wantErr {
t.Errorf("reviewPatchDetailed() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("reviewPatchDetailed() = %v, want %v", got, tt.want)
}
})
}
}
func Test_loadFileContent(t *testing.T) {
fileA := loadTestFile("testdata/same/a/t1.txt")
type args struct {
fileX *fileInfoExtended
}
tests := []struct {
name string
args args
}{
{"RealFile", args{fileX: &fileA}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
loadFileContent(tt.args.fileX)
})
}
}
func Test_createDiffs(t *testing.T) {
fileA := loadTestFile("testdata/same/a/t1.txt")
loadFileContent(&fileA)
fileC := loadTestFile("testdata/smalldiff/t2.txt")
loadFileContent(&fileC)
fileSource := loadTestFile("testdata/smalldiff/t2.txt")
loadFileContent(&fileSource)
tmpfile, err := ioutil.TempFile("", "example")
if err != nil {
log.Fatal(err)
}
data, _ := ioutil.ReadFile("testdata/smalldiff/t1.txt")
err = ioutil.WriteFile(tmpfile.Name(), data, 0644)
if err != nil {
log.Fatal(err)
}
filePatch := loadTestFile(tmpfile.Name())
loadFileContent(&filePatch)
filePatch.autoPatch = true
defer os.Remove(tmpfile.Name()) // clean up
noChangesDiff := fileDiffInfo{
diffCount: 13,
patchesTotal: 0,
patchesApplied: 0,
patchesFailed: 0,
patched: false,
}
withChangesDiff := noChangesDiff
withChangesDiff.diffCount = 13
withChangesDiff.patchesTotal = 4
withChangesDiff.patchesApplied = 4
withChangesDiff.patched = true
withChangesDiff.newContent = fileSource.fileContent
type args struct {
fileAExt fileInfoExtended
fileBExt fileInfoExtended
}
tests := []struct {
name string
args args
want fileDiffInfo
wantErr bool
}{
{"FilesDifer", args{fileAExt: fileA, fileBExt: fileC}, noChangesDiff, false},
{"RealPatch", args{fileAExt: filePatch, fileBExt: fileSource}, withChangesDiff, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := createDiffs(tt.args.fileAExt, tt.args.fileBExt)
if (err != nil) != tt.wantErr {
t.Errorf("createDiffs() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("createDiffs() = %v, want %v", got, tt.want)
}
})
}
}
func Test_compareFiles(t *testing.T) {
fileA := loadTestFile("testdata/same/a/t1.txt")
fileB := loadTestFile("testdata/same/b/t1.txt")
fileC := loadTestFile("testdata/smalldiff/t2.txt")
fileFake := loadTestFile("testdata/fakefile")
fileSource := loadTestFile("testdata/smalldiff/t2.txt")
tmpfile, err := ioutil.TempFile("", "example")
if err != nil {
log.Fatal(err)
}
data, _ := ioutil.ReadFile("testdata/smalldiff/t1.txt")
err = ioutil.WriteFile(tmpfile.Name(), data, 0644)
if err != nil {
log.Fatal(err)
}
filePatch := loadTestFile(tmpfile.Name())
filePatch.autoPatch = true
tmpfile2, err := ioutil.TempFile("", "example")
if err != nil {
log.Fatal(err)
}
data, _ = ioutil.ReadFile("testdata/smalldiff/t1.txt")
err = ioutil.WriteFile(tmpfile2.Name(), data, 0644)
if err != nil {
log.Fatal(err)
}
filePatch2 := loadTestFile(tmpfile2.Name())
filePatch2.autoPatch = true
defer os.Remove(tmpfile2.Name()) // clean up
type args struct {
fileAExt fileInfoExtended
fileBExt fileInfoExtended
dryRun bool
reportOnly bool
}
tests := []struct {
name string
args args
want bool
wantErr bool
}{
{"FilesMatch", args{fileAExt: fileA, fileBExt: fileB, dryRun: true, reportOnly: false}, true, false},
{"FilesDifer", args{fileAExt: fileA, fileBExt: fileC, dryRun: true, reportOnly: false}, false, false},
{"FilesSame", args{fileAExt: fileA, fileBExt: fileA, dryRun: true, reportOnly: false}, true, false},
{"FakeFile", args{fileAExt: fileA, fileBExt: fileFake, dryRun: true, reportOnly: false}, false, true},
{"RealPatch", args{fileAExt: filePatch, fileBExt: fileSource, dryRun: false, reportOnly: false}, false, false},
{"PatchNewFile", args{fileAExt: fileFake, fileBExt: fileA, dryRun: true, reportOnly: false}, false, true},
{"RealPatchReport", args{fileAExt: filePatch2, fileBExt: fileSource, dryRun: false, reportOnly: true}, false, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := compareFiles(tt.args.fileAExt, tt.args.fileBExt, tt.args.dryRun, tt.args.reportOnly)
if (err != nil) != tt.wantErr {
t.Errorf("compareFiles() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("compareFiles() = %v, want %v", got, tt.want)
}
})
}
}