github.com/wsand02/massren@v1.5.5-0.20191104203215-f721006d1e4e/undo_test.go (about) 1 package main 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 ) 8 9 func Test_handleUndoCommand_noArgs(t *testing.T) { 10 setup(t) 11 defer teardown(t) 12 13 var opts CommandLineOptions 14 err := handleUndoCommand(&opts, []string{}) 15 if err != nil { 16 t.Fail() 17 } 18 } 19 20 func Test_handleUndoCommand_notInHistory(t *testing.T) { 21 setup(t) 22 defer teardown(t) 23 24 var opts CommandLineOptions 25 err := handleUndoCommand(&opts, []string{"one", "two"}) 26 if err != nil { 27 t.Fail() 28 } 29 } 30 31 func Test_handleUndoCommand(t *testing.T) { 32 setup(t) 33 defer teardown(t) 34 35 touch(filepath.Join(tempFolder(), "one")) 36 touch(filepath.Join(tempFolder(), "two")) 37 touch(filepath.Join(tempFolder(), "three")) 38 39 var fileActions []*FileAction 40 var fileAction *FileAction 41 42 fileAction = NewFileAction() 43 fileAction.oldPath = filepath.Join(tempFolder(), "one") 44 fileAction.newPath = "123" 45 fileActions = append(fileActions, fileAction) 46 47 fileAction = NewFileAction() 48 fileAction.oldPath = filepath.Join(tempFolder(), "two") 49 fileAction.newPath = "456" 50 fileActions = append(fileActions, fileAction) 51 52 processFileActions(fileActions, false) 53 54 var opts CommandLineOptions 55 err := handleUndoCommand(&opts, []string{ 56 filepath.Join(tempFolder(), "123"), filepath.Join(tempFolder(), "456"), 57 }) 58 59 if err != nil { 60 t.Errorf("Expected no error, got: %s", err) 61 } 62 63 if !fileExists(filepath.Join(tempFolder(), "one")) || !fileExists(filepath.Join(tempFolder(), "two")) { 64 t.Error("Undo operation did not restore filenames") 65 } 66 67 historyItems, _ := allHistoryItems() 68 if len(historyItems) > 0 { 69 t.Error("Undo operation did not delete restored history.") 70 } 71 72 fileActions = []*FileAction{} 73 74 fileAction = NewFileAction() 75 fileAction.oldPath = filepath.Join(tempFolder(), "one") 76 fileAction.newPath = "123" 77 fileActions = append(fileActions, fileAction) 78 79 fileAction = NewFileAction() 80 fileAction.oldPath = filepath.Join(tempFolder(), "two") 81 fileAction.newPath = "456" 82 fileActions = append(fileActions, fileAction) 83 84 processFileActions(fileActions, false) 85 86 opts = CommandLineOptions{ 87 DryRun: true, 88 } 89 err = handleUndoCommand(&opts, []string{ 90 filepath.Join(tempFolder(), "123"), filepath.Join(tempFolder(), "456"), 91 }) 92 93 if !fileExists(filepath.Join(tempFolder(), "123")) || !fileExists(filepath.Join(tempFolder(), "456")) { 94 t.Error("Undo operation in dry run mode restored filenames.") 95 } 96 } 97 98 func Test_handleUndoCommand_fileHasBeenDeleted(t *testing.T) { 99 setup(t) 100 defer teardown(t) 101 102 var err error 103 104 touch(filepath.Join(tempFolder(), "one")) 105 touch(filepath.Join(tempFolder(), "two")) 106 touch(filepath.Join(tempFolder(), "three")) 107 108 var fileActions []*FileAction 109 var fileAction *FileAction 110 111 fileAction = NewFileAction() 112 fileAction.oldPath = filepath.Join(tempFolder(), "one") 113 fileAction.newPath = "123" 114 fileActions = append(fileActions, fileAction) 115 116 fileAction = NewFileAction() 117 fileAction.oldPath = filepath.Join(tempFolder(), "two") 118 fileAction.newPath = "456" 119 fileActions = append(fileActions, fileAction) 120 121 processFileActions(fileActions, false) 122 123 os.Remove(filepath.Join(tempFolder(), "123")) 124 125 var opts CommandLineOptions 126 err = handleUndoCommand(&opts, []string{ 127 filepath.Join(tempFolder(), "123"), 128 }) 129 130 if err == nil { 131 t.Fail() 132 } 133 } 134 135 func Test_handleUndoCommand_withIntermediateRename(t *testing.T) { 136 setup(t) 137 defer teardown(t) 138 139 p0 := filepath.Join(tempFolder(), "0") 140 p1 := filepath.Join(tempFolder(), "1") 141 142 filePutContent(p0, "0") 143 filePutContent(p1, "1") 144 145 fileActions := []*FileAction{} 146 147 fileAction := NewFileAction() 148 fileAction.oldPath = p0 149 fileAction.newPath = "1" 150 fileActions = append(fileActions, fileAction) 151 152 fileAction = NewFileAction() 153 fileAction.oldPath = p1 154 fileAction.newPath = "0" 155 fileActions = append(fileActions, fileAction) 156 157 processFileActions(fileActions, false) 158 159 var opts CommandLineOptions 160 err := handleUndoCommand(&opts, []string{ 161 p0, p1, 162 }) 163 164 if err != nil { 165 t.Fail() 166 } 167 168 if fileGetContent(p0) != "0" { 169 t.Error("File 0 was not restored") 170 } 171 172 if fileGetContent(p1) != "1" { 173 t.Error("File 1 was not restored") 174 } 175 }