1
mirror of https://github.com/rclone/rclone synced 2024-11-14 13:36:24 +01:00

Clean empty directories between test runs

This commit is contained in:
Nick Craig-Wood 2017-01-24 17:15:56 +00:00
parent 16d91246c4
commit 85f05c57d1

View File

@ -29,6 +29,7 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"regexp" "regexp"
"sort"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -128,6 +129,13 @@ func newRun() *Run {
return r return r
} }
// dirsToRemove sorts by string length
type dirsToRemove []string
func (d dirsToRemove) Len() int { return len(d) }
func (d dirsToRemove) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
func (d dirsToRemove) Less(i, j int) bool { return len(d[i]) > len(d[j]) }
// NewRun initialise the remote and local for testing and returns a // NewRun initialise the remote and local for testing and returns a
// run object. Call this from the tests. // run object. Call this from the tests.
// //
@ -144,22 +152,31 @@ func NewRun(t *testing.T) *Run {
r = new(Run) r = new(Run)
*r = *oneRun *r = *oneRun
r.cleanRemote = func() { r.cleanRemote = func() {
var toDelete dirsToRemove
list := fs.NewLister().Start(r.fremote, "") list := fs.NewLister().Start(r.fremote, "")
for { for {
o, err := list.GetObject() o, dir, err := list.Get()
if err != nil { if err != nil {
if err == fs.ErrorDirNotFound { if err == fs.ErrorDirNotFound {
break break
} }
t.Fatalf("Error listing: %v", err) t.Fatalf("Error listing: %v", err)
} } else if o != nil {
// Check if we are finished err = o.Remove()
if o == nil { if err != nil {
t.Errorf("Error removing file %q: %v", o.Remote(), err)
}
} else if dir != nil {
toDelete = append(toDelete, dir.Remote())
} else {
break break
} }
err = o.Remove() }
sort.Sort(toDelete)
for _, dir := range toDelete {
err := r.fremote.Rmdir(dir)
if err != nil { if err != nil {
t.Errorf("Error removing file: %v", err) t.Errorf("Error removing dir %q: %v", dir, err)
} }
} }
// Check remote is empty // Check remote is empty