Fix tests under Windows

This commit is contained in:
Nick Craig-Wood 2017-01-20 17:12:05 +00:00
parent 2abfae283c
commit 9fdeb82328
2 changed files with 21 additions and 12 deletions

View File

@ -14,7 +14,6 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime"
"sort" "sort"
"strings" "strings"
"testing" "testing"
@ -145,17 +144,23 @@ func (is *Items) Done(t *testing.T) {
} }
// makeListingFromItems returns a string representation of the items // makeListingFromItems returns a string representation of the items
func makeListingFromItems(items []Item) string { //
nameLengths := make([]string, len(items)) // it returns two possible strings, one normal and one for windows
func makeListingFromItems(items []Item) (string, string) {
nameLengths1 := make([]string, len(items))
nameLengths2 := make([]string, len(items))
for i, item := range items { for i, item := range items {
remote := item.Path remote1 := item.Path
if item.WinPath != "" && runtime.GOOS == "windows" { remote2 := item.Path
remote = item.WinPath if item.WinPath != "" {
remote2 = item.WinPath
} }
nameLengths[i] = fmt.Sprintf("%s (%d)", remote, item.Size) nameLengths1[i] = fmt.Sprintf("%s (%d)", remote1, item.Size)
nameLengths2[i] = fmt.Sprintf("%s (%d)", remote2, item.Size)
} }
sort.Strings(nameLengths) sort.Strings(nameLengths1)
return strings.Join(nameLengths, ", ") sort.Strings(nameLengths2)
return strings.Join(nameLengths1, ", "), strings.Join(nameLengths2, ", ")
} }
// makeListingFromObjects returns a string representation of the objects // makeListingFromObjects returns a string representation of the objects
@ -182,15 +187,17 @@ func CheckListingWithPrecision(t *testing.T, f fs.Fs, items []Item, expectedDirs
var err error var err error
var retries = *listRetries var retries = *listRetries
sleep := time.Second / 2 sleep := time.Second / 2
wantListing := makeListingFromItems(items) wantListing1, wantListing2 := makeListingFromItems(items)
gotListing := "<unset>" gotListing := "<unset>"
listingOK := false
for i := 1; i <= retries; i++ { for i := 1; i <= retries; i++ {
objs, dirs, err = fs.NewLister().Start(f, "").GetAll() objs, dirs, err = fs.NewLister().Start(f, "").GetAll()
if err != nil && err != fs.ErrorDirNotFound { if err != nil && err != fs.ErrorDirNotFound {
t.Fatalf("Error listing: %v", err) t.Fatalf("Error listing: %v", err)
} }
gotListing = makeListingFromObjects(objs) gotListing = makeListingFromObjects(objs)
if wantListing == gotListing && (expectedDirs == nil || len(dirs) == 0 || len(dirs) == len(expectedDirs)) { listingOK = wantListing1 == gotListing || wantListing2 == gotListing
if listingOK && (expectedDirs == nil || len(dirs) == 0 || len(dirs) == len(expectedDirs)) {
// Put an extra sleep in if we did any retries just to make sure it really // Put an extra sleep in if we did any retries just to make sure it really
// is consistent (here is looking at you Amazon Drive!) // is consistent (here is looking at you Amazon Drive!)
if i != 1 { if i != 1 {
@ -208,7 +215,7 @@ func CheckListingWithPrecision(t *testing.T, f fs.Fs, items []Item, expectedDirs
doDirCacheFlush() doDirCacheFlush()
} }
} }
assert.Equal(t, wantListing, gotListing, "listing incorrect") assert.True(t, listingOK, fmt.Sprintf("listing wrong, want\n %s or\n %s got\n %s", wantListing1, wantListing2, gotListing))
for _, obj := range objs { for _, obj := range objs {
require.NotNil(t, obj) require.NotNil(t, obj)
is.Find(t, obj, precision) is.Find(t, obj, precision)

View File

@ -432,6 +432,7 @@ func TestFsMove(t *testing.T) {
// check happy path, i.e. no naming conflicts when rename and move are two // check happy path, i.e. no naming conflicts when rename and move are two
// separate operations // separate operations
file2Move.Path = "other.txt" file2Move.Path = "other.txt"
file2Move.WinPath = ""
src := findObject(t, file2.Path) src := findObject(t, file2.Path)
dst, err := doMove(src, file2Move.Path) dst, err := doMove(src, file2Move.Path)
if err == fs.ErrorCantMove { if err == fs.ErrorCantMove {
@ -684,6 +685,7 @@ func TestFsIsFile(t *testing.T) {
remoteName := subRemoteName + "/" + file2.Path remoteName := subRemoteName + "/" + file2.Path
file2Copy := file2 file2Copy := file2
file2Copy.Path = "z.txt" file2Copy.Path = "z.txt"
file2Copy.WinPath = ""
fileRemote, err := fs.NewFs(remoteName) fileRemote, err := fs.NewFs(remoteName)
assert.Equal(t, fs.ErrorIsFile, err) assert.Equal(t, fs.ErrorIsFile, err)
fstest.CheckListing(t, fileRemote, []fstest.Item{file2Copy}) fstest.CheckListing(t, fileRemote, []fstest.Item{file2Copy})