55 "os"
66 "path"
77 "path/filepath"
8+ "regexp"
89 "runtime"
910 "sort"
1011 "strings"
@@ -159,6 +160,14 @@ func TestNormalSync(t *testing.T) {
159160 filesToCheck , foldersToCheck , err = removeSomeTestFilesAndFolders (local , remote , filesToCheck , foldersToCheck , "_Remove" )
160161 if err != nil {
161162 t .Error (err )
163+ return
164+ }
165+ checkFilesAndFolders (t , filesToCheck , foldersToCheck , local , remote , 10 * time .Second )
166+
167+ filesToCheck , foldersToCheck , err = renameSomeTestFilesAndFolders (local , remote , outside , filesToCheck , foldersToCheck )
168+ if err != nil {
169+ t .Error (err )
170+ return
162171 }
163172 checkFilesAndFolders (t , filesToCheck , foldersToCheck , local , remote , 10 * time .Second )
164173
@@ -185,6 +194,12 @@ func setExcludePaths(syncClient *SyncConfig, testCases testCaseList) {
185194 syncClient .DownloadExcludePaths = append (syncClient .DownloadExcludePaths , testCase .path )
186195 } else if strings .Contains (testCase .path , "noUpload" ) {
187196 syncClient .UploadExcludePaths = append (syncClient .UploadExcludePaths , testCase .path )
197+ } else if strings .HasSuffix (testCase .path , "_RenameToIgnore" ) {
198+ syncClient .ExcludePaths = append (syncClient .ExcludePaths , testCase .path + "After" )
199+ } else if strings .HasSuffix (testCase .path , "_RenameToNoDownload" ) {
200+ syncClient .DownloadExcludePaths = append (syncClient .DownloadExcludePaths , testCase .path + "After" )
201+ } else if strings .HasSuffix (testCase .path , "_RenameToNoUpload" ) {
202+ syncClient .UploadExcludePaths = append (syncClient .UploadExcludePaths , testCase .path + "After" )
188203 }
189204 }
190205
@@ -254,12 +269,12 @@ func makeBasicTestCases() (testCaseList, testCaseList) {
254269 }
255270
256271 //Add Files and Folders that are edited in Remote
257- filesToCheck = addRemoteTestCases (filesToCheck )
258- foldersToCheck = addRemoteTestCases (foldersToCheck )
272+ filesToCheck = makeRemoteTestCases (filesToCheck )
273+ foldersToCheck = makeRemoteTestCases (foldersToCheck )
259274
260275 //Add Files and Folders that are inside a shared testFolder
261- filesToCheck = addDeepTestCases (filesToCheck )
262- foldersToCheck = addDeepTestCases (foldersToCheck )
276+ filesToCheck = makeDeepTestCases (filesToCheck )
277+ foldersToCheck = makeDeepTestCases (foldersToCheck )
263278
264279 return filesToCheck , foldersToCheck
265280}
@@ -279,45 +294,47 @@ func makeRemoveAndRenameTestCases(filesToCheck testCaseList, foldersToCheck test
279294 shouldExistInRemote : f .shouldExistInRemote ,
280295 editLocation : f .editLocation ,
281296 }
282- filesToCheck = append (filesToCheck , removeEquivalent )
297+ array = append (array , removeEquivalent )
283298
284299 renameEquivalent := checkedFileOrFolder {
285300 path : f .path + "_RenameToFullContext" ,
286301 shouldExistInLocal : f .shouldExistInLocal ,
287302 shouldExistInRemote : f .shouldExistInRemote ,
288303 editLocation : f .editLocation ,
289304 }
290- filesToCheck = append (filesToCheck , renameEquivalent )
305+ array = append (array , renameEquivalent )
306+
307+ isFullyIncluded , _ := regexp .Compile ("(testFolder\\ /)?(testFile|testFolder)(Local|Remote)$" )
291308
292- if strings . Contains (f .path , "testFile" ) {
309+ if isFullyIncluded . MatchString (f .path ) {
293310 renameEquivalent = checkedFileOrFolder {
294311 path : f .path + "_RenameToOutside" ,
295312 shouldExistInLocal : f .shouldExistInLocal ,
296313 shouldExistInRemote : f .shouldExistInRemote ,
297314 editLocation : f .editLocation ,
298315 }
299- filesToCheck = append (filesToCheck , renameEquivalent )
316+ array = append (array , renameEquivalent )
300317 renameEquivalent = checkedFileOrFolder {
301318 path : f .path + "_RenameToIgnore" ,
302319 shouldExistInLocal : f .shouldExistInLocal ,
303320 shouldExistInRemote : f .shouldExistInRemote ,
304321 editLocation : f .editLocation ,
305322 }
306- filesToCheck = append (filesToCheck , renameEquivalent )
323+ array = append (array , renameEquivalent )
307324 renameEquivalent = checkedFileOrFolder {
308325 path : f .path + "_RenameToNoDownload" ,
309326 shouldExistInLocal : f .shouldExistInLocal ,
310327 shouldExistInRemote : f .shouldExistInRemote ,
311328 editLocation : f .editLocation ,
312329 }
313- filesToCheck = append (filesToCheck , renameEquivalent )
330+ array = append (array , renameEquivalent )
314331 renameEquivalent = checkedFileOrFolder {
315332 path : f .path + "_RenameToNoUpload" ,
316333 shouldExistInLocal : f .shouldExistInLocal ,
317334 shouldExistInRemote : f .shouldExistInRemote ,
318335 editLocation : f .editLocation ,
319336 }
320- filesToCheck = append (filesToCheck , renameEquivalent )
337+ array = append (array , renameEquivalent )
321338 }
322339 }
323340
@@ -364,8 +381,8 @@ func makeRemoveAndRenameTestCases(filesToCheck testCaseList, foldersToCheck test
364381 },
365382 }
366383
367- renameFilesFromOutside = addDeepTestCases (renameFilesFromOutside )
368- renameFolderFromOutside = addDeepTestCases (renameFolderFromOutside )
384+ renameFilesFromOutside = makeDeepTestCases (renameFilesFromOutside )
385+ renameFolderFromOutside = makeDeepTestCases (renameFolderFromOutside )
369386
370387 filesToCheck = append (filesToCheck , renameFilesFromOutside ... )
371388 foldersToCheck = append (foldersToCheck , renameFolderFromOutside ... )
@@ -374,7 +391,7 @@ func makeRemoveAndRenameTestCases(filesToCheck testCaseList, foldersToCheck test
374391
375392}
376393
377- func addRemoteTestCases (testCases testCaseList ) testCaseList {
394+ func makeRemoteTestCases (testCases testCaseList ) testCaseList {
378395
379396 for _ , f := range testCases {
380397
@@ -396,7 +413,7 @@ func addRemoteTestCases(testCases testCaseList) testCaseList {
396413 return testCases
397414}
398415
399- func addDeepTestCases (testCases testCaseList ) testCaseList {
416+ func makeDeepTestCases (testCases testCaseList ) testCaseList {
400417
401418 for _ , f := range testCases {
402419
@@ -419,34 +436,24 @@ func addDeepTestCases(testCases testCaseList) testCaseList {
419436func createTestFilesAndFolders (local string , remote string , outside string , filesToCheck testCaseList , foldersToCheck testCaseList ) error {
420437
421438 for _ , f := range foldersToCheck {
422- var parentDir string
423- if f .editLocation == editInLocal {
424- parentDir = local
425- } else if f .editLocation == editInRemote {
426- parentDir = remote
427- } else if f .editLocation == editOutside {
428- parentDir = outside
429- } else {
430- return errors .New ("CreateLocation " + string (f .editLocation ) + " unknown" )
439+ parentDir , err := getParentDir (local , remote , outside , f .editLocation )
440+ if err != nil {
441+ return errors .Trace (err )
431442 }
432- err := os .Mkdir (path .Join (parentDir , f .path ), 0755 )
443+
444+ err = os .Mkdir (path .Join (parentDir , f .path ), 0755 )
433445 if err != nil {
434446 return errors .Trace (err )
435447 }
436448 }
437449
438450 for _ , f := range filesToCheck {
439- var parentDir string
440- if f .editLocation == editInLocal {
441- parentDir = local
442- } else if f .editLocation == editInRemote {
443- parentDir = remote
444- } else if f .editLocation == editOutside {
445- parentDir = outside
446- } else {
447- return errors .New ("CreateLocation " + string (f .editLocation ) + " unknown" )
451+ parentDir , err := getParentDir (local , remote , outside , f .editLocation )
452+ if err != nil {
453+ return errors .Trace (err )
448454 }
449- err := ioutil .WriteFile (path .Join (parentDir , f .path ), []byte (fileContents ), 0666 )
455+
456+ err = ioutil .WriteFile (path .Join (parentDir , f .path ), []byte (fileContents ), 0666 )
450457 if err != nil {
451458 return errors .Trace (err )
452459 }
@@ -494,6 +501,72 @@ func removeSomeTestFilesAndFolders(local string, remote string, filesToCheck tes
494501 return filesToCheck , foldersToCheck , nil
495502}
496503
504+ func renameSomeTestFilesAndFolders (local string , remote string , outside string , filesToCheck testCaseList , foldersToCheck testCaseList ) (testCaseList , testCaseList , error ) {
505+
506+ for n , array := range [2 ]testCaseList {filesToCheck , foldersToCheck } {
507+
508+ for n , f := range array {
509+
510+ if ! strings .Contains (f .path , "_Rename" ) {
511+ continue
512+ }
513+
514+ fromParentDir , err := getParentDir (local , remote , outside , f .editLocation )
515+ if err != nil {
516+ return nil , nil , errors .Trace (err )
517+ }
518+ fromPath := path .Join (fromParentDir , f .path )
519+
520+ var toParentDir string
521+ if strings .HasSuffix (f .path , "_RenameToOutside" ) {
522+ toParentDir = outside
523+ } else if strings .Contains (f .path , "Local_Rename" ) {
524+ toParentDir = local
525+ } else if strings .Contains (f .path , "Remote_Rename" ) {
526+ toParentDir = remote
527+ }
528+
529+ f .path = f .path + "After"
530+ toPath := path .Join (toParentDir , f .path )
531+
532+ err = os .Rename (fromPath , toPath )
533+ if err != nil {
534+ return nil , nil , errors .Trace (err )
535+ }
536+
537+ if strings .HasSuffix (f .path , "_RenameToFullContextAfter" ) {
538+ f .shouldExistInLocal = true
539+ f .shouldExistInRemote = true
540+ } else if strings .HasSuffix (f .path , "_RenameToNoDownloadAfter" ) {
541+ f .shouldExistInRemote = true
542+ f .shouldExistInLocal = f .editLocation == editInLocal
543+ } else if strings .HasSuffix (f .path , "_RenameToNoUploadAfter" ) {
544+ f .shouldExistInLocal = true
545+ f .shouldExistInRemote = f .editLocation == editInRemote
546+ } else if strings .HasSuffix (f .path , "_RenameToIgnoreAfter" ) {
547+ f .shouldExistInLocal = f .editLocation == editInLocal
548+ f .shouldExistInRemote = f .editLocation == editInRemote
549+ } else if strings .HasSuffix (f .path , "_RenameToOutsideAfter" ) {
550+ f .shouldExistInLocal = false
551+ f .shouldExistInRemote = false
552+ } else {
553+ return nil , nil , errors .New ("Bad rename suffix of " + f .path )
554+ }
555+
556+ array [n ] = f
557+
558+ }
559+
560+ if n == 0 {
561+ filesToCheck = array
562+ } else {
563+ foldersToCheck = array
564+ }
565+ }
566+
567+ return filesToCheck , foldersToCheck , nil
568+ }
569+
497570func TestCreateDirInFileMap (t * testing.T ) {
498571 sync := SyncConfig {
499572 fileIndex : newFileIndex (),
0 commit comments