Skip to content

Commit 65725c0

Browse files
author
Laurent Cozic
committed
Test processFileActions()
1 parent a39ee08 commit 65725c0

File tree

7 files changed

+172
-162
lines changed

7 files changed

+172
-162
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ massren.exe
1111
deploy/releases/
1212
test.out
1313
animation_original.gif
14+
tests/

history.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func saveHistoryItems(fileActions []*FileAction) error {
6060
// Current, undo is not supported
6161
continue
6262
}
63-
tx.Exec("INSERT INTO history (source, destination, timestamp) VALUES (?, ?, ?)", normalizePath(action.oldPath), normalizePath(action.newPath), time.Now().Unix())
63+
tx.Exec("INSERT INTO history (source, destination, timestamp) VALUES (?, ?, ?)", action.FullOldPath(), action.FullNewPath(), time.Now().Unix())
6464
}
6565

6666
return tx.Commit()

history_test.go

+45-10
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ func Test_saveHistoryItems(t *testing.T) {
99
setup(t)
1010
defer teardown(t)
1111

12-
err := saveHistoryItems([]string{"one", "two"}, []string{"one"})
13-
if err == nil {
14-
t.Error("Expected error, got nil")
15-
}
16-
17-
err = saveHistoryItems([]string{}, []string{})
12+
err := saveHistoryItems([]*FileAction{})
1813
if err != nil {
1914
t.Errorf("Expected no error, got %s", err)
2015
}
@@ -24,7 +19,22 @@ func Test_saveHistoryItems(t *testing.T) {
2419
t.Errorf("Expected no items, got %d", len(items))
2520
}
2621

27-
saveHistoryItems([]string{"one", "two"}, []string{"1", "2"})
22+
var fileActions []*FileAction
23+
var fileAction *FileAction
24+
25+
fileAction = NewFileAction()
26+
27+
fileAction = NewFileAction()
28+
fileAction.oldPath = "one"
29+
fileAction.newPath = "1"
30+
fileActions = append(fileActions, fileAction)
31+
32+
fileAction = NewFileAction()
33+
fileAction.oldPath = "two"
34+
fileAction.newPath = "2"
35+
fileActions = append(fileActions, fileAction)
36+
37+
saveHistoryItems(fileActions)
2838

2939
items, _ = allHistoryItems()
3040
if len(items) != 2 {
@@ -37,15 +47,22 @@ func Test_saveHistoryItems(t *testing.T) {
3747
}
3848
}
3949

40-
saveHistoryItems([]string{"three"}, []string{"3"})
50+
fileActions = []*FileAction{}
51+
52+
fileAction = NewFileAction()
53+
fileAction.oldPath = "three"
54+
fileAction.newPath = "3"
55+
fileActions = append(fileActions, fileAction)
56+
57+
saveHistoryItems(fileActions)
4158
items, _ = allHistoryItems()
4259
if len(items) != 3 {
4360
t.Errorf("Expected 3 items, got %d", len(items))
4461
}
4562

4663
profileDb_.Close()
4764

48-
err = saveHistoryItems([]string{"un"}, []string{"dest"})
65+
err = saveHistoryItems(fileActions)
4966
if err == nil {
5067
t.Error("Expected error, got nil")
5168
}
@@ -55,7 +72,25 @@ func Test_deleteHistoryItems(t *testing.T) {
5572
setup(t)
5673
defer teardown(t)
5774

58-
saveHistoryItems([]string{"one", "two", "three"}, []string{"1", "2", "3"})
75+
var fileActions []*FileAction
76+
var fileAction *FileAction
77+
78+
fileAction = NewFileAction()
79+
fileAction.oldPath = "one"
80+
fileAction.newPath = "1"
81+
fileActions = append(fileActions, fileAction)
82+
83+
fileAction = NewFileAction()
84+
fileAction.oldPath = "two"
85+
fileAction.newPath = "2"
86+
fileActions = append(fileActions, fileAction)
87+
88+
fileAction = NewFileAction()
89+
fileAction.oldPath = "three"
90+
fileAction.newPath = "3"
91+
fileActions = append(fileActions, fileAction)
92+
93+
saveHistoryItems(fileActions)
5994

6095
items, _ := allHistoryItems()
6196
deleteHistoryItems([]HistoryItem{items[0], items[1]})

main.go

+16-83
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,17 @@ type FileAction struct {
4545
}
4646

4747
func NewFileAction() *FileAction {
48-
return new(FileAction)
48+
output := new(FileAction)
49+
output.kind = KIND_RENAME
50+
return output
51+
}
52+
53+
func (this *FileAction) FullOldPath() string {
54+
return normalizePath(this.oldPath)
55+
}
56+
57+
func (this *FileAction) FullNewPath() string {
58+
return normalizePath(filepath.Join(filepath.Dir(this.oldPath), filepath.Base(this.newPath)))
4959
}
5060

5161
func (this *FileAction) String() string {
@@ -243,27 +253,6 @@ func filePathsFromListFile(filePath string) ([]string, error) {
243253
return filePathsFromString(string(contentB)), nil
244254
}
245255

246-
func twoColumnPrint(col1 []string, col2 []string, separator string) {
247-
if len(col1) != len(col2) {
248-
panic("col1 and col2 length do not match")
249-
}
250-
251-
maxColLength1 := 0
252-
for _, d1 := range col1 {
253-
if len(d1) > maxColLength1 {
254-
maxColLength1 = len(d1)
255-
}
256-
}
257-
258-
for i, d1 := range col1 {
259-
d2 := col2[i]
260-
for len(d1) < maxColLength1 {
261-
d1 += " "
262-
}
263-
fmt.Println(d1 + separator + d2)
264-
}
265-
}
266-
267256
func printHelp() {
268257
flagParser_.WriteHelp(os.Stdout)
269258

@@ -386,8 +375,7 @@ func processFileActions(fileActions []*FileAction, dryRun bool) (bool, error) {
386375
logInfo("\"%s\" => \"%s\"", action.oldPath, action.newPath)
387376
} else {
388377
logDebug("\"%s\" => \"%s\"", action.oldPath, action.newPath)
389-
destFilePath := filepath.Join(filepath.Dir(action.oldPath), filepath.Base(action.newPath))
390-
err := os.Rename(action.oldPath, destFilePath)
378+
err := os.Rename(action.FullOldPath(), action.FullNewPath())
391379
if err != nil {
392380
return hasChanges, err
393381
}
@@ -396,11 +384,12 @@ func processFileActions(fileActions []*FileAction, dryRun bool) (bool, error) {
396384

397385
case KIND_DELETE:
398386

387+
filePath := action.FullOldPath()
399388
if dryRun {
400-
logInfo("\"%s\" => <Deleted>", action.oldPath)
389+
logInfo("\"%s\" => <Deleted>", filePath)
401390
} else {
402-
logDebug("\"%s\" => <Deleted>", action.oldPath)
403-
_, err := trash.MoveToTrash(action.oldPath)
391+
logDebug("\"%s\" => <Deleted>", filePath)
392+
_, err := trash.MoveToTrash(filePath)
404393
if err != nil {
405394
return hasChanges, err
406395
}
@@ -420,62 +409,6 @@ func processFileActions(fileActions []*FileAction, dryRun bool) (bool, error) {
420409
return hasChanges, nil
421410
}
422411

423-
func renameFiles(filePaths []string, newFilePaths []string, dryRun bool) (bool, []string, []string) {
424-
var dryRunCol1 []string
425-
var dryRunCol2 []string
426-
hasChanges := false
427-
428-
var sources []string
429-
var destinations []string
430-
431-
defer func() {
432-
// err := saveHistoryItems(sources, destinations)
433-
// if err != nil {
434-
// logError("Could not save history items: %s", err)
435-
// }
436-
}()
437-
438-
for i, sourceFilePath := range filePaths {
439-
destFilePath := newFilePaths[i]
440-
441-
if filepath.Base(sourceFilePath) == filepath.Base(destFilePath) {
442-
continue
443-
}
444-
445-
destFilePath = filepath.Join(filepath.Dir(sourceFilePath), filepath.Base(destFilePath))
446-
447-
hasChanges = true
448-
449-
if dryRun {
450-
dryRunCol1 = append(dryRunCol1, sourceFilePath)
451-
dryRunCol2 = append(dryRunCol2, destFilePath)
452-
} else {
453-
logDebug("\"%s\" => \"%s\"", sourceFilePath, destFilePath)
454-
err := os.Rename(sourceFilePath, destFilePath)
455-
if err != nil {
456-
criticalError(err)
457-
}
458-
sources = append(sources, sourceFilePath)
459-
destinations = append(destinations, destFilePath)
460-
}
461-
}
462-
463-
return hasChanges, dryRunCol1, dryRunCol2
464-
}
465-
466-
func duplicatePaths(filePaths []string) []string {
467-
var output []string
468-
for i1, p1 := range filePaths {
469-
for i2 := i1 + 1; i2 < len(filePaths); i2++ {
470-
p2 := filePaths[i2]
471-
if p1 == p2 {
472-
output = append(output, p1)
473-
}
474-
}
475-
}
476-
return output
477-
}
478-
479412
func onExit() {
480413
deleteTempFiles()
481414
deleteOldHistoryItems(time.Now().Unix() - 60*60*24*7)

main_test.go

+67-52
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,73 @@ ijkl
223223
}
224224
}
225225

226+
func Test_processFileActions(t *testing.T) {
227+
setup(t)
228+
defer teardown(t)
229+
230+
touch(filepath.Join(tempFolder(), "one"))
231+
touch(filepath.Join(tempFolder(), "two"))
232+
touch(filepath.Join(tempFolder(), "three"))
233+
234+
fileActions := []*FileAction{}
235+
236+
fileAction := NewFileAction()
237+
fileAction.oldPath = filepath.Join(tempFolder(), "one")
238+
fileAction.newPath = "one123"
239+
fileActions = append(fileActions, fileAction)
240+
241+
fileAction = NewFileAction()
242+
fileAction.oldPath = filepath.Join(tempFolder(), "two")
243+
fileAction.newPath = "two456"
244+
fileActions = append(fileActions, fileAction)
245+
246+
processFileActions(fileActions, false)
247+
248+
if !fileExists(filepath.Join(tempFolder(), "one123")) {
249+
t.Error("File not found")
250+
}
251+
252+
if !fileExists(filepath.Join(tempFolder(), "two456")) {
253+
t.Error("File not found")
254+
}
255+
256+
if !fileExists(filepath.Join(tempFolder(), "three")) {
257+
t.Error("File not found")
258+
}
259+
260+
fileActions = []*FileAction{}
261+
262+
fileAction = NewFileAction()
263+
fileAction.oldPath = filepath.Join(tempFolder(), "two456")
264+
fileAction.kind = KIND_DELETE
265+
fileActions = append(fileActions, fileAction)
266+
267+
processFileActions(fileActions, true)
268+
269+
if !fileExists(filepath.Join(tempFolder(), "two456")) {
270+
t.Error("File should not have been deleted")
271+
}
272+
273+
processFileActions(fileActions, false)
274+
275+
if fileExists(filepath.Join(tempFolder(), "two456")) {
276+
t.Error("File should have been deleted")
277+
}
278+
279+
fileActions = []*FileAction{}
280+
281+
fileAction = NewFileAction()
282+
fileAction.oldPath = filepath.Join(tempFolder(), "three")
283+
fileAction.newPath = "nochange"
284+
fileActions = append(fileActions, fileAction)
285+
286+
processFileActions(fileActions, true)
287+
288+
if !fileExists(filepath.Join(tempFolder(), "three")) {
289+
t.Error("File was renamed in dry-run mode")
290+
}
291+
}
292+
226293
func Test_stringHash(t *testing.T) {
227294
if len(stringHash("aaaa")) != 32 {
228295
t.Error("hash should be 32 characters long")
@@ -416,44 +483,6 @@ func Test_deleteTempFiles(t *testing.T) {
416483
}
417484
}
418485

419-
func Test_renameFiles(t *testing.T) {
420-
setup(t)
421-
defer teardown(t)
422-
423-
touch(filepath.Join(tempFolder(), "one"))
424-
touch(filepath.Join(tempFolder(), "two"))
425-
touch(filepath.Join(tempFolder(), "three"))
426-
427-
hasChanges, _, _ := renameFiles([]string{filepath.Join(tempFolder(), "one"), filepath.Join(tempFolder(), "two")}, []string{"one123", "two456"}, false)
428-
429-
if !hasChanges {
430-
t.Error("Expected changes.")
431-
}
432-
433-
if !fileExists(filepath.Join(tempFolder(), "one123")) {
434-
t.Error("File not found")
435-
}
436-
437-
if !fileExists(filepath.Join(tempFolder(), "two456")) {
438-
t.Error("File not found")
439-
}
440-
441-
if !fileExists(filepath.Join(tempFolder(), "three")) {
442-
t.Error("File not found")
443-
}
444-
445-
renameFiles([]string{filepath.Join(tempFolder(), "three")}, []string{"nochange"}, true)
446-
447-
if !fileExists(filepath.Join(tempFolder(), "three")) {
448-
t.Error("File was renamed in dry-run mode")
449-
}
450-
451-
hasChanges, _, _ = renameFiles([]string{filepath.Join(tempFolder(), "three")}, []string{"three"}, false)
452-
if hasChanges {
453-
t.Error("No file should have been renamed")
454-
}
455-
}
456-
457486
func Test_newline(t *testing.T) {
458487
newline_ = ""
459488
nl := newline()
@@ -468,17 +497,3 @@ func Test_guessEditorCommand(t *testing.T) {
468497
t.Fail()
469498
}
470499
}
471-
472-
func Test_duplicatePaths(t *testing.T) {
473-
if len(duplicatePaths([]string{"one", "two", "one", "three"})) != 1 {
474-
t.Error("Wrong duplicate count")
475-
}
476-
477-
if len(duplicatePaths([]string{"one", "two", "three"})) != 0 {
478-
t.Error("Wrong duplicate count")
479-
}
480-
481-
if len(duplicatePaths([]string{})) != 0 {
482-
t.Error("Wrong duplicate count")
483-
}
484-
}

0 commit comments

Comments
 (0)