1
mirror of https://github.com/rclone/rclone synced 2024-12-22 13:03:02 +01:00

Convert more tests to use assert/require

This commit is contained in:
Nick Craig-Wood 2016-06-29 17:59:31 +01:00
parent f6a053df6e
commit 70dc97231e
7 changed files with 248 additions and 695 deletions

View File

@ -8,122 +8,81 @@ import (
"strings"
"testing"
"testing/iotest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAsyncReader(t *testing.T) {
buf := ioutil.NopCloser(bytes.NewBufferString("Testbuffer"))
ar, err := newAsyncReader(buf, 4, 10000)
if err != nil {
t.Fatal("error when creating:", err)
}
require.NoError(t, err)
var dst = make([]byte, 100)
n, err := ar.Read(dst)
if err != nil {
t.Fatal("error when reading:", err)
}
if n != 10 {
t.Fatal("unexpected length, expected 10, got ", n)
}
require.NoError(t, err)
assert.Equal(t, 10, n)
n, err = ar.Read(dst)
if err != io.EOF {
t.Fatal("expected io.EOF, got", err)
}
if n != 0 {
t.Fatal("unexpected length, expected 0, got ", n)
}
assert.Equal(t, io.EOF, err)
assert.Equal(t, 0, n)
// Test read after error
n, err = ar.Read(dst)
if err != io.EOF {
t.Fatal("expected io.EOF, got", err)
}
if n != 0 {
t.Fatal("unexpected length, expected 0, got ", n)
}
assert.Equal(t, io.EOF, err)
assert.Equal(t, 0, n)
err = ar.Close()
if err != nil {
t.Fatal("error when closing:", err)
}
require.NoError(t, err)
// Test double close
err = ar.Close()
if err != nil {
t.Fatal("error when closing:", err)
}
require.NoError(t, err)
// Test Close without reading everything
buf = ioutil.NopCloser(bytes.NewBuffer(make([]byte, 50000)))
ar, err = newAsyncReader(buf, 4, 100)
if err != nil {
t.Fatal("error when creating:", err)
}
require.NoError(t, err)
err = ar.Close()
if err != nil {
t.Fatal("error when closing, noread:", err)
}
require.NoError(t, err)
}
func TestAsyncWriteTo(t *testing.T) {
buf := ioutil.NopCloser(bytes.NewBufferString("Testbuffer"))
ar, err := newAsyncReader(buf, 4, 10000)
if err != nil {
t.Fatal("error when creating:", err)
}
require.NoError(t, err)
var dst = &bytes.Buffer{}
n, err := io.Copy(dst, ar)
if err != io.EOF {
t.Fatal("error when reading:", err)
}
if n != 10 {
t.Fatal("unexpected length, expected 10, got ", n)
}
assert.Equal(t, io.EOF, err)
assert.Equal(t, int64(10), n)
// Should still return EOF
n, err = io.Copy(dst, ar)
if err != io.EOF {
t.Fatal("expected io.EOF, got", err)
}
if n != 0 {
t.Fatal("unexpected length, expected 0, got ", n)
}
assert.Equal(t, io.EOF, err)
assert.Equal(t, int64(0), n)
err = ar.Close()
if err != nil {
t.Fatal("error when closing:", err)
}
require.NoError(t, err)
}
func TestAsyncReaderErrors(t *testing.T) {
// test nil reader
_, err := newAsyncReader(nil, 4, 10000)
if err == nil {
t.Fatal("expected error when creating, but got nil")
}
require.Error(t, err)
// invalid buffer number
buf := ioutil.NopCloser(bytes.NewBufferString("Testbuffer"))
_, err = newAsyncReader(buf, 0, 10000)
if err == nil {
t.Fatal("expected error when creating, but got nil")
}
require.Error(t, err)
_, err = newAsyncReader(buf, -1, 10000)
if err == nil {
t.Fatal("expected error when creating, but got nil")
}
require.Error(t, err)
// invalid buffer size
_, err = newAsyncReader(buf, 4, 0)
if err == nil {
t.Fatal("expected error when creating, but got nil")
}
require.Error(t, err)
_, err = newAsyncReader(buf, 4, -1)
if err == nil {
t.Fatal("expected error when creating, but got nil")
}
require.Error(t, err)
}
// Complex read tests, leveraged from "bufio".
@ -210,9 +169,7 @@ func TestAsyncReaderSizes(t *testing.T) {
readmaker.name, bufreader.name, bufsize, text, s)
}
err := ar.Close()
if err != nil {
t.Fatal("Unexpected close error:", err)
}
require.NoError(t, err)
}
}
}
@ -257,9 +214,7 @@ func TestAsyncReaderWriteTo(t *testing.T) {
readmaker.name, bufreader.name, bufsize, text, s)
}
err = ar.Close()
if err != nil {
t.Fatal("Unexpected close error:", err)
}
require.NoError(t, err)
}
}
}

View File

@ -1,9 +1,10 @@
package fs
import (
"bytes"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSizeSuffixString(t *testing.T) {
@ -23,14 +24,12 @@ func TestSizeSuffixString(t *testing.T) {
} {
ss := SizeSuffix(test.in)
got := ss.String()
if test.want != got {
t.Errorf("Want %v got %v", test.want, got)
}
assert.Equal(t, test.want, got)
}
}
func TestSizeSuffixSet(t *testing.T) {
for i, test := range []struct {
for _, test := range []struct {
in string
want int64
err bool
@ -56,13 +55,12 @@ func TestSizeSuffixSet(t *testing.T) {
} {
ss := SizeSuffix(0)
err := ss.Set(test.in)
if (err != nil) != test.err {
t.Errorf("%d: Expecting error %v but got error %v", i, test.err, err)
}
got := int64(ss)
if test.want != got {
t.Errorf("%d: Want %v got %v", i, test.want, got)
if test.err {
require.Error(t, err)
} else {
require.NoError(t, err)
}
assert.Equal(t, test.want, int64(ss))
}
}
@ -75,12 +73,8 @@ func TestReveal(t *testing.T) {
{"2sTcyNrA", "potato"},
} {
got := Reveal(test.in)
if got != test.want {
t.Errorf("%q: want %q got %q", test.in, test.want, got)
}
if Obscure(got) != test.in {
t.Errorf("%q: wasn't bidirectional", test.in)
}
assert.Equal(t, test.want, got)
assert.Equal(t, test.in, Obscure(got), "not bidirectional")
}
}
@ -97,15 +91,11 @@ func TestConfigLoad(t *testing.T) {
}
sections := c.GetSectionList()
var expect = []string{"RCLONE_ENCRYPT_V0", "nounc", "unc"}
if !reflect.DeepEqual(sections, expect) {
t.Fatalf("%v != %v", sections, expect)
}
assert.Equal(t, expect, sections)
keys := c.GetKeyList("nounc")
expect = []string{"type", "nounc"}
if !reflect.DeepEqual(keys, expect) {
t.Fatalf("%v != %v", keys, expect)
}
assert.Equal(t, expect, keys)
}
func TestConfigLoadEncrypted(t *testing.T) {
@ -119,24 +109,16 @@ func TestConfigLoadEncrypted(t *testing.T) {
// Set correct password
err = setPassword("asdf")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
c, err := loadConfigFile()
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
sections := c.GetSectionList()
var expect = []string{"nounc", "unc"}
if !reflect.DeepEqual(sections, expect) {
t.Fatalf("%v != %v", sections, expect)
}
assert.Equal(t, expect, sections)
keys := c.GetKeyList("nounc")
expect = []string{"type", "nounc"}
if !reflect.DeepEqual(keys, expect) {
t.Fatalf("%v != %v", keys, expect)
}
assert.Equal(t, expect, keys)
}
func TestConfigLoadEncryptedFailures(t *testing.T) {
@ -147,36 +129,23 @@ func TestConfigLoadEncryptedFailures(t *testing.T) {
ConfigPath = "./testdata/enc-short.conf"
defer func() { ConfigPath = oldConfigPath }()
_, err = loadConfigFile()
if err == nil {
t.Fatal("expected error")
}
t.Log("Correctly got:", err)
require.Error(t, err)
// This file contains invalid base64 characters.
ConfigPath = "./testdata/enc-invalid.conf"
_, err = loadConfigFile()
if err == nil {
t.Fatal("expected error")
}
t.Log("Correctly got:", err)
require.Error(t, err)
// This file contains invalid base64 characters.
ConfigPath = "./testdata/enc-too-new.conf"
_, err = loadConfigFile()
if err == nil {
t.Fatal("expected error")
}
t.Log("Correctly got:", err)
require.Error(t, err)
// This file contains invalid base64 characters.
ConfigPath = "./testdata/filenotfound.conf"
c, err := loadConfigFile()
if err != nil {
t.Fatal(err)
}
if len(c.GetSectionList()) != 0 {
t.Fatalf("Expected 0-length section, got %d entries", len(c.GetSectionList()))
}
require.NoError(t, err)
require.Len(t, c.GetSectionList(), 0, "Expected 0-length section")
}
func TestPassword(t *testing.T) {
@ -186,15 +155,11 @@ func TestPassword(t *testing.T) {
var err error
// Empty password should give error
err = setPassword(" \t ")
if err == nil {
t.Fatal("expected error")
}
require.Error(t, err)
// Test invalid utf8 sequence
err = setPassword(string([]byte{0xff, 0xfe, 0xfd}) + "abc")
if err == nil {
t.Fatal("expected error")
}
require.Error(t, err)
// Simple check of wrong passwords
hashedKeyCompare(t, "mis", "match", false)
@ -212,21 +177,16 @@ func TestPassword(t *testing.T) {
func hashedKeyCompare(t *testing.T, a, b string, shouldMatch bool) {
err := setPassword(a)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
k1 := configKey
err = setPassword(b)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
k2 := configKey
matches := bytes.Equal(k1, k2)
if shouldMatch && !matches {
t.Fatalf("%v != %v", k1, k2)
}
if !shouldMatch && matches {
t.Fatalf("%v == %v", k1, k2)
if shouldMatch {
assert.Equal(t, k1, k2)
} else {
assert.NotEqual(t, k1, k2)
}
}

View File

@ -12,7 +12,7 @@ import (
)
func TestAgeSuffix(t *testing.T) {
for i, test := range []struct {
for _, test := range []struct {
in string
want float64
err bool
@ -33,15 +33,12 @@ func TestAgeSuffix(t *testing.T) {
{"1x", 0, true},
} {
duration, err := ParseDuration(test.in)
if (err != nil) != test.err {
t.Errorf("%d: Expecting error %v but got error %v", i, test.err, err)
continue
}
got := float64(duration)
if test.want != got {
t.Errorf("%d: Want %v got %v", i, test.want, got)
if test.err {
require.Error(t, err)
} else {
require.NoError(t, err)
}
assert.Equal(t, test.want, float64(duration))
}
}
@ -68,9 +65,7 @@ func testFile(t *testing.T, contents string) *string {
require.NoError(t, err)
defer func() {
err := out.Close()
if err != nil {
t.Error(err)
}
require.NoError(t, err)
}()
_, err = out.Write([]byte(contents))
require.NoError(t, err)
@ -182,13 +177,9 @@ func TestNewFilterIncludeFiles(t *testing.T) {
f, err := NewFilter()
require.NoError(t, err)
err = f.AddFile("file1.jpg")
if err != nil {
t.Error(err)
}
require.NoError(t, err)
err = f.AddFile("/file2.jpg")
if err != nil {
t.Error(err)
}
require.NoError(t, err)
assert.Equal(t, filesMap{
"file1.jpg": {},
"file2.jpg": {},
@ -200,9 +191,7 @@ func TestNewFilterIncludeFiles(t *testing.T) {
{"potato/file2.jpg", 2, 0, false},
{"file3.jpg", 3, 0, false},
})
if f.InActive() {
t.Errorf("want !InActive")
}
assert.False(t, f.InActive())
}
func TestNewFilterIncludeFilesDirs(t *testing.T) {
@ -215,9 +204,7 @@ func TestNewFilterIncludeFilesDirs(t *testing.T) {
"/path/to/dir2/file4.png",
} {
err = f.AddFile(path)
if err != nil {
t.Error(err)
}
require.NoError(t, err)
}
assert.Equal(t, filesMap{
"path": {},
@ -248,9 +235,7 @@ func TestNewFilterMinSize(t *testing.T) {
{"file2.jpg", 101, 0, true},
{"potato/file2.jpg", 99, 0, false},
})
if f.InActive() {
t.Errorf("want !InActive")
}
assert.False(t, f.InActive())
}
func TestNewFilterMaxSize(t *testing.T) {
@ -262,9 +247,7 @@ func TestNewFilterMaxSize(t *testing.T) {
{"file2.jpg", 101, 0, false},
{"potato/file2.jpg", 99, 0, true},
})
if f.InActive() {
t.Errorf("want !InActive")
}
assert.False(t, f.InActive())
}
func TestNewFilterMinAndMaxAge(t *testing.T) {
@ -279,9 +262,7 @@ func TestNewFilterMinAndMaxAge(t *testing.T) {
{"potato/file1.jpg", 98, 1440000003, true},
{"potato/file2.jpg", 99, 1440000004, false},
})
if f.InActive() {
t.Errorf("want !InActive")
}
assert.False(t, f.InActive())
}
func TestNewFilterMinAge(t *testing.T) {
@ -295,9 +276,7 @@ func TestNewFilterMinAge(t *testing.T) {
{"potato/file1.jpg", 98, 1440000003, false},
{"potato/file2.jpg", 99, 1440000004, false},
})
if f.InActive() {
t.Errorf("want !InActive")
}
assert.False(t, f.InActive())
}
func TestNewFilterMaxAge(t *testing.T) {
@ -311,9 +290,7 @@ func TestNewFilterMaxAge(t *testing.T) {
{"potato/file1.jpg", 98, 1440000003, true},
{"potato/file2.jpg", 99, 1440000004, true},
})
if f.InActive() {
t.Errorf("want !InActive")
}
assert.False(t, f.InActive())
}
func TestNewFilterMatches(t *testing.T) {
@ -363,9 +340,7 @@ func TestNewFilterMatches(t *testing.T) {
{"sausage4", false},
{"a", true},
})
if f.InActive() {
t.Errorf("want !InActive")
}
assert.False(t, f.InActive())
}
func TestFilterForEachLine(t *testing.T) {
@ -382,23 +357,15 @@ five
six `)
defer func() {
err := os.Remove(*file)
if err != nil {
t.Error(err)
}
require.NoError(t, err)
}()
lines := []string{}
err := forEachLine(*file, func(s string) error {
lines = append(lines, s)
return nil
})
if err != nil {
t.Error(err)
}
got := strings.Join(lines, ",")
want := "one,two,three,four,five,six"
if want != got {
t.Errorf("want %q got %q", want, got)
}
require.NoError(t, err)
assert.Equal(t, "one,two,three,four,five,six", strings.Join(lines, ","))
}
func TestFilterMatchesFromDocs(t *testing.T) {
@ -443,7 +410,7 @@ func TestFilterMatchesFromDocs(t *testing.T) {
require.NoError(t, err)
included := f.Include(test.file, 0, time.Unix(0, 0))
if included != test.included {
t.Logf("%q match %q: want %v got %v", test.glob, test.file, test.included, included)
t.Errorf("%q match %q: want %v got %v", test.glob, test.file, test.included, included)
}
}
}

View File

@ -6,95 +6,54 @@ import (
"testing"
"github.com/ncw/rclone/fs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestHashSet(t *testing.T) {
var h fs.HashSet
if h.Count() != 0 {
t.Fatalf("expected empty set to have 0 elements, got %d", h.Count())
}
assert.Equal(t, 0, h.Count())
a := h.Array()
if len(a) != 0 {
t.Fatalf("expected empty slice, got %d", len(a))
}
assert.Len(t, a, 0)
h = h.Add(fs.HashMD5)
if h.Count() != 1 {
t.Fatalf("expected 1 element, got %d", h.Count())
}
if h.GetOne() != fs.HashMD5 {
t.Fatalf("expected HashMD5, got %v", h.GetOne())
}
assert.Equal(t, 1, h.Count())
assert.Equal(t, fs.HashMD5, h.GetOne())
a = h.Array()
if len(a) != 1 {
t.Fatalf("expected 1 element, got %d", len(a))
}
if a[0] != fs.HashMD5 {
t.Fatalf("expected HashMD5, got %v", a[0])
}
assert.Len(t, a, 1)
assert.Equal(t, a[0], fs.HashMD5)
// Test overlap, with all hashes
h = h.Overlap(fs.SupportedHashes)
if h.Count() != 1 {
t.Fatalf("expected 1 element, got %d", h.Count())
}
if h.GetOne() != fs.HashMD5 {
t.Fatalf("expected HashMD5, got %v", h.GetOne())
}
if !h.SubsetOf(fs.SupportedHashes) {
t.Fatalf("expected to be subset of all hashes")
}
if !h.SubsetOf(fs.NewHashSet(fs.HashMD5)) {
t.Fatalf("expected to be subset of itself")
}
assert.Equal(t, 1, h.Count())
assert.Equal(t, fs.HashMD5, h.GetOne())
assert.True(t, h.SubsetOf(fs.SupportedHashes))
assert.True(t, h.SubsetOf(fs.NewHashSet(fs.HashMD5)))
h = h.Add(fs.HashSHA1)
if h.Count() != 2 {
t.Fatalf("expected 2 elements, got %d", h.Count())
}
assert.Equal(t, 2, h.Count())
one := h.GetOne()
if !(one == fs.HashMD5 || one == fs.HashSHA1) {
t.Fatalf("expected to be either MD5 or SHA1, got %v", one)
}
if !h.SubsetOf(fs.SupportedHashes) {
t.Fatalf("expected to be subset of all hashes")
}
if h.SubsetOf(fs.NewHashSet(fs.HashMD5)) {
t.Fatalf("did not expect to be subset of only MD5")
}
if h.SubsetOf(fs.NewHashSet(fs.HashSHA1)) {
t.Fatalf("did not expect to be subset of only SHA1")
}
if !h.SubsetOf(fs.NewHashSet(fs.HashMD5, fs.HashSHA1)) {
t.Fatalf("expected to be subset of MD5/SHA1")
}
assert.True(t, h.SubsetOf(fs.SupportedHashes))
assert.False(t, h.SubsetOf(fs.NewHashSet(fs.HashMD5)))
assert.False(t, h.SubsetOf(fs.NewHashSet(fs.HashSHA1)))
assert.True(t, h.SubsetOf(fs.NewHashSet(fs.HashMD5, fs.HashSHA1)))
a = h.Array()
if len(a) != 2 {
t.Fatalf("expected 2 elements, got %d", len(a))
}
assert.Len(t, a, 2)
ol := h.Overlap(fs.NewHashSet(fs.HashMD5))
if ol.Count() != 1 {
t.Fatalf("expected 1 element overlap, got %d", ol.Count())
}
if !ol.Contains(fs.HashMD5) {
t.Fatalf("expected overlap to be MD5, got %v", ol)
}
if ol.Contains(fs.HashSHA1) {
t.Fatalf("expected overlap NOT to contain SHA1, got %v", ol)
}
assert.Equal(t, 1, ol.Count())
assert.True(t, ol.Contains(fs.HashMD5))
assert.False(t, ol.Contains(fs.HashSHA1))
ol = h.Overlap(fs.NewHashSet(fs.HashMD5, fs.HashSHA1))
if ol.Count() != 2 {
t.Fatalf("expected 2 element overlap, got %d", ol.Count())
}
if !ol.Contains(fs.HashMD5) {
t.Fatalf("expected overlap to contain MD5, got %v", ol)
}
if !ol.Contains(fs.HashSHA1) {
t.Fatalf("expected overlap to contain SHA1, got %v", ol)
}
assert.Equal(t, 2, ol.Count())
assert.True(t, ol.Contains(fs.HashMD5))
assert.True(t, ol.Contains(fs.HashSHA1))
}
type hashTest struct {
@ -124,31 +83,19 @@ func TestMultiHasher(t *testing.T) {
for _, test := range hashTestSet {
mh := fs.NewMultiHasher()
n, err := io.Copy(mh, bytes.NewBuffer(test.input))
if err != nil {
t.Fatal(err)
}
if int(n) != len(test.input) {
t.Fatalf("copy mismatch: %d != %d", n, len(test.input))
}
require.NoError(t, err)
assert.Len(t, test.input, int(n))
sums := mh.Sums()
for k, v := range sums {
expect, ok := test.output[k]
if !ok {
t.Errorf("Unknown hash type %v, sum: %q", k, v)
}
if expect != v {
t.Errorf("hash %v mismatch %q != %q", k, v, expect)
}
require.True(t, ok)
assert.Equal(t, v, expect)
}
// Test that all are present
for k, v := range test.output {
expect, ok := sums[k]
if !ok {
t.Errorf("did not calculate hash type %v, sum: %q", k, v)
}
if expect != v {
t.Errorf("hash %d mismatch %q != %q", k, v, expect)
}
require.True(t, ok)
assert.Equal(t, v, expect)
}
}
}
@ -161,47 +108,28 @@ func TestMultiHasherTypes(t *testing.T) {
t.Fatal(err)
}
n, err := io.Copy(mh, bytes.NewBuffer(test.input))
if err != nil {
t.Fatal(err)
}
if int(n) != len(test.input) {
t.Fatalf("copy mismatch: %d != %d", n, len(test.input))
}
require.NoError(t, err)
assert.Len(t, test.input, int(n))
sums := mh.Sums()
if len(sums) != 1 {
t.Fatalf("expected 1 sum, got %d", len(sums))
}
expect := test.output[h]
if expect != sums[h] {
t.Errorf("hash %v mismatch %q != %q", h, sums[h], expect)
}
assert.Len(t, sums, 1)
assert.Equal(t, sums[h], test.output[h])
}
}
func TestHashStream(t *testing.T) {
for _, test := range hashTestSet {
sums, err := fs.HashStream(bytes.NewBuffer(test.input))
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
for k, v := range sums {
expect, ok := test.output[k]
if !ok {
t.Errorf("Unknown hash type %v, sum: %q", k, v)
}
if expect != v {
t.Errorf("hash %v mismatch %q != %q", k, v, expect)
}
require.True(t, ok)
assert.Equal(t, v, expect)
}
// Test that all are present
for k, v := range test.output {
expect, ok := sums[k]
if !ok {
t.Errorf("did not calculate hash type %v, sum: %q", k, v)
}
if expect != v {
t.Errorf("hash %v mismatch %q != %q", k, v, expect)
}
require.True(t, ok)
assert.Equal(t, v, expect)
}
}
}
@ -210,51 +138,24 @@ func TestHashStreamTypes(t *testing.T) {
h := fs.HashSHA1
for _, test := range hashTestSet {
sums, err := fs.HashStreamTypes(bytes.NewBuffer(test.input), fs.NewHashSet(h))
if err != nil {
t.Fatal(err)
}
if len(sums) != 1 {
t.Fatalf("expected 1 sum, got %d", len(sums))
}
expect := test.output[h]
if expect != sums[h] {
t.Errorf("hash %d mismatch %q != %q", h, sums[h], expect)
}
require.NoError(t, err)
assert.Len(t, sums, 1)
assert.Equal(t, sums[h], test.output[h])
}
}
func TestHashSetStringer(t *testing.T) {
h := fs.NewHashSet(fs.HashSHA1, fs.HashMD5)
s := h.String()
expect := "[MD5, SHA-1]"
if s != expect {
t.Errorf("unexpected stringer: was %q, expected %q", s, expect)
}
assert.Equal(t, h.String(), "[MD5, SHA-1]")
h = fs.NewHashSet(fs.HashSHA1)
s = h.String()
expect = "[SHA-1]"
if s != expect {
t.Errorf("unexpected stringer: was %q, expected %q", s, expect)
}
assert.Equal(t, h.String(), "[SHA-1]")
h = fs.NewHashSet()
s = h.String()
expect = "[]"
if s != expect {
t.Errorf("unexpected stringer: was %q, expected %q", s, expect)
}
assert.Equal(t, h.String(), "[]")
}
func TestHashStringer(t *testing.T) {
h := fs.HashMD5
s := h.String()
expect := "MD5"
if s != expect {
t.Errorf("unexpected stringer: was %q, expected %q", s, expect)
}
assert.Equal(t, h.String(), "MD5")
h = fs.HashNone
s = h.String()
expect = "None"
if s != expect {
t.Errorf("unexpected stringer: was %q, expected %q", s, expect)
}
assert.Equal(t, h.String(), "None")
}

View File

@ -36,6 +36,7 @@ import (
_ "github.com/ncw/rclone/fs/all" // import all fs
"github.com/ncw/rclone/fstest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Globals
@ -271,9 +272,7 @@ func TestCopyWithDryRun(t *testing.T) {
fs.Config.DryRun = true
err := fs.CopyDir(r.fremote, r.flocal)
fs.Config.DryRun = false
if err != nil {
t.Fatalf("Copy failed: %v", err)
}
require.NoError(t, err)
fstest.CheckItems(t, r.flocal, file1)
fstest.CheckItems(t, r.fremote)
@ -286,9 +285,7 @@ func TestCopy(t *testing.T) {
file1 := r.WriteFile("sub dir/hello world", "hello world", t1)
err := fs.CopyDir(r.fremote, r.flocal)
if err != nil {
t.Fatalf("Copy failed: %v", err)
}
require.NoError(t, err)
fstest.CheckItems(t, r.flocal, file1)
fstest.CheckItems(t, r.fremote, file1)
@ -305,7 +302,7 @@ func TestCopyNoTraverse(t *testing.T) {
file1 := r.WriteFile("sub dir/hello world", "hello world", t1)
err := fs.CopyDir(r.fremote, r.flocal)
assert.NoError(t, err)
require.NoError(t, err)
fstest.CheckItems(t, r.flocal, file1)
fstest.CheckItems(t, r.fremote, file1)
@ -322,7 +319,7 @@ func TestSyncNoTraverse(t *testing.T) {
file1 := r.WriteFile("sub dir/hello world", "hello world", t1)
err := fs.Sync(r.fremote, r.flocal)
assert.NoError(t, err)
require.NoError(t, err)
fstest.CheckItems(t, r.flocal, file1)
fstest.CheckItems(t, r.fremote, file1)
@ -340,9 +337,7 @@ func TestCopyWithDepth(t *testing.T) {
defer func() { fs.Config.MaxDepth = -1 }()
err := fs.CopyDir(r.fremote, r.flocal)
if err != nil {
t.Fatalf("Copy failed: %v", err)
}
require.NoError(t, err)
fstest.CheckItems(t, r.flocal, file1, file2)
fstest.CheckItems(t, r.fremote, file2)
@ -356,16 +351,12 @@ func TestServerSideCopy(t *testing.T) {
fstest.CheckItems(t, r.fremote, file1)
fremoteCopy, finaliseCopy, err := fstest.RandomRemote(*RemoteName, *SubDir)
if err != nil {
t.Fatalf("Failed to open remote copy %q: %v", *RemoteName, err)
}
require.NoError(t, err)
defer finaliseCopy()
t.Logf("Server side copy (if possible) %v -> %v", r.fremote, fremoteCopy)
err = fs.CopyDir(fremoteCopy, r.fremote)
if err != nil {
t.Fatalf("Server Side Copy failed: %v", err)
}
require.NoError(t, err)
fstest.CheckItems(t, fremoteCopy, file1)
}
@ -379,13 +370,9 @@ func TestLsd(t *testing.T) {
var buf bytes.Buffer
err := fs.ListDir(r.fremote, &buf)
if err != nil {
t.Fatalf("ListDir failed: %v", err)
}
require.NoError(t, err)
res := buf.String()
if !strings.Contains(res, "sub dir\n") {
t.Fatalf("Result wrong %q", res)
}
assert.Contains(t, res, "sub dir\n")
}
// Check that if the local file doesn't exist when we copy it up,
@ -398,14 +385,10 @@ func TestCopyAfterDelete(t *testing.T) {
fstest.CheckItems(t, r.fremote, file1)
err := fs.Mkdir(r.flocal)
if err != nil {
t.Fatalf("Mkdir failed: %v", err)
}
require.NoError(t, err)
err = fs.CopyDir(r.fremote, r.flocal)
if err != nil {
t.Fatalf("Copy failed: %v", err)
}
require.NoError(t, err)
fstest.CheckItems(t, r.flocal)
fstest.CheckItems(t, r.fremote, file1)
@ -419,9 +402,7 @@ func TestCopyRedownload(t *testing.T) {
fstest.CheckItems(t, r.fremote, file1)
err := fs.CopyDir(r.flocal, r.fremote)
if err != nil {
t.Fatalf("Copy failed: %v", err)
}
require.NoError(t, err)
fstest.CheckItems(t, r.flocal, file1)
}
@ -440,15 +421,10 @@ func TestSyncBasedOnCheckSum(t *testing.T) {
fs.Stats.ResetCounters()
err := fs.Sync(r.fremote, r.flocal)
if err != nil {
t.Fatalf("Initial sync failed: %v", err)
}
require.NoError(t, err)
// We should have transferred exactly one file.
if fs.Stats.GetTransfers() != 1 {
t.Fatalf("Sync 1: want 1 transfer, got %d", fs.Stats.GetTransfers())
}
assert.Equal(t, int64(1), fs.Stats.GetTransfers())
fstest.CheckItems(t, r.fremote, file1)
// Change last modified date only
@ -457,15 +433,10 @@ func TestSyncBasedOnCheckSum(t *testing.T) {
fs.Stats.ResetCounters()
err = fs.Sync(r.fremote, r.flocal)
if err != nil {
t.Fatalf("Sync failed: %v", err)
}
require.NoError(t, err)
// We should have transferred no files
if fs.Stats.GetTransfers() != 0 {
t.Fatalf("Sync 2: want 0 transfers, got %d", fs.Stats.GetTransfers())
}
assert.Equal(t, int64(0), fs.Stats.GetTransfers())
fstest.CheckItems(t, r.flocal, file2)
fstest.CheckItems(t, r.fremote, file1)
}
@ -484,15 +455,10 @@ func TestSyncSizeOnly(t *testing.T) {
fs.Stats.ResetCounters()
err := fs.Sync(r.fremote, r.flocal)
if err != nil {
t.Fatalf("Initial sync failed: %v", err)
}
require.NoError(t, err)
// We should have transferred exactly one file.
if fs.Stats.GetTransfers() != 1 {
t.Fatalf("Sync 1: want 1 transfer, got %d", fs.Stats.GetTransfers())
}
assert.Equal(t, int64(1), fs.Stats.GetTransfers())
fstest.CheckItems(t, r.fremote, file1)
// Update mtime, md5sum but not length of file
@ -501,15 +467,10 @@ func TestSyncSizeOnly(t *testing.T) {
fs.Stats.ResetCounters()
err = fs.Sync(r.fremote, r.flocal)
if err != nil {
t.Fatalf("Sync failed: %v", err)
}
require.NoError(t, err)
// We should have transferred no files
if fs.Stats.GetTransfers() != 0 {
t.Fatalf("Sync 2: want 0 transfers, got %d", fs.Stats.GetTransfers())
}
assert.Equal(t, int64(0), fs.Stats.GetTransfers())
fstest.CheckItems(t, r.flocal, file2)
fstest.CheckItems(t, r.fremote, file1)
}
@ -528,15 +489,10 @@ func TestSyncIgnoreSize(t *testing.T) {
fs.Stats.ResetCounters()
err := fs.Sync(r.fremote, r.flocal)
if err != nil {
t.Fatalf("Initial sync failed: %v", err)
}
require.NoError(t, err)
// We should have transferred exactly one file.
if fs.Stats.GetTransfers() != 1 {
t.Fatalf("Sync 1: want 1 transfer, got %d", fs.Stats.GetTransfers())
}
assert.Equal(t, int64(1), fs.Stats.GetTransfers())
fstest.CheckItems(t, r.fremote, file1)
// Update size but not date of file
@ -545,15 +501,10 @@ func TestSyncIgnoreSize(t *testing.T) {
fs.Stats.ResetCounters()
err = fs.Sync(r.fremote, r.flocal)
if err != nil {
t.Fatalf("Sync failed: %v", err)
}
require.NoError(t, err)
// We should have transferred no files
if fs.Stats.GetTransfers() != 0 {
t.Fatalf("Sync 2: want 0 transfers, got %d", fs.Stats.GetTransfers())
}
assert.Equal(t, int64(0), fs.Stats.GetTransfers())
fstest.CheckItems(t, r.flocal, file2)
fstest.CheckItems(t, r.fremote, file1)
}
@ -566,30 +517,22 @@ func TestSyncIgnoreTimes(t *testing.T) {
fs.Stats.ResetCounters()
err := fs.Sync(r.fremote, r.flocal)
if err != nil {
t.Fatalf("Sync failed: %v", err)
}
require.NoError(t, err)
// We should have transferred exactly 0 files because the
// files were identical.
if fs.Stats.GetTransfers() != 0 {
t.Fatalf("Sync 1: want 0 transfer, got %d", fs.Stats.GetTransfers())
}
assert.Equal(t, int64(0), fs.Stats.GetTransfers())
fs.Config.IgnoreTimes = true
defer func() { fs.Config.IgnoreTimes = false }()
fs.Stats.ResetCounters()
err = fs.Sync(r.fremote, r.flocal)
if err != nil {
t.Fatalf("Sync failed: %v", err)
}
require.NoError(t, err)
// We should have transferred exactly one file even though the
// files were identical.
if fs.Stats.GetTransfers() != 1 {
t.Fatalf("Sync 2: want 1 transfer, got %d", fs.Stats.GetTransfers())
}
assert.Equal(t, int64(1), fs.Stats.GetTransfers())
fstest.CheckItems(t, r.flocal, file1)
fstest.CheckItems(t, r.fremote, file1)
@ -605,9 +548,7 @@ func TestSyncIgnoreExisting(t *testing.T) {
fs.Stats.ResetCounters()
err := fs.Sync(r.fremote, r.flocal)
if err != nil {
t.Fatalf("Sync failed: %v", err)
}
require.NoError(t, err)
fstest.CheckItems(t, r.flocal, file1)
fstest.CheckItems(t, r.fremote, file1)
@ -615,9 +556,7 @@ func TestSyncIgnoreExisting(t *testing.T) {
r.WriteFile("existing", "newpotatoes", t2)
fs.Stats.ResetCounters()
err = fs.Sync(r.fremote, r.flocal)
if err != nil {
t.Fatalf("Sync failed: %v", err)
}
require.NoError(t, err)
// Items should not change
fstest.CheckItems(t, r.fremote, file1)
}
@ -630,9 +569,7 @@ func TestSyncAfterChangingModtimeOnly(t *testing.T) {
fs.Stats.ResetCounters()
err := fs.Sync(r.fremote, r.flocal)
if err != nil {
t.Fatalf("Sync failed: %v", err)
}
require.NoError(t, err)
fstest.CheckItems(t, r.flocal, file1)
fstest.CheckItems(t, r.fremote, file1)
@ -646,9 +583,7 @@ func TestSyncAfterAddingAFile(t *testing.T) {
fs.Stats.ResetCounters()
err := fs.Sync(r.fremote, r.flocal)
if err != nil {
t.Fatalf("Sync failed: %v", err)
}
require.NoError(t, err)
fstest.CheckItems(t, r.flocal, file1, file2)
fstest.CheckItems(t, r.fremote, file1, file2)
}
@ -663,9 +598,7 @@ func TestSyncAfterChangingFilesSizeOnly(t *testing.T) {
fs.Stats.ResetCounters()
err := fs.Sync(r.fremote, r.flocal)
if err != nil {
t.Fatalf("Sync failed: %v", err)
}
require.NoError(t, err)
fstest.CheckItems(t, r.flocal, file2)
fstest.CheckItems(t, r.fremote, file2)
}
@ -688,9 +621,7 @@ func TestSyncAfterChangingContentsOnly(t *testing.T) {
fs.Stats.ResetCounters()
err := fs.Sync(r.fremote, r.flocal)
if err != nil {
t.Fatalf("Sync failed: %v", err)
}
require.NoError(t, err)
fstest.CheckItems(t, r.flocal, file2)
fstest.CheckItems(t, r.fremote, file2)
}
@ -707,9 +638,7 @@ func TestSyncAfterRemovingAFileAndAddingAFileDryRun(t *testing.T) {
fs.Stats.ResetCounters()
err := fs.Sync(r.fremote, r.flocal)
fs.Config.DryRun = false
if err != nil {
t.Fatalf("Sync failed: %v", err)
}
require.NoError(t, err)
fstest.CheckItems(t, r.flocal, file3, file1)
fstest.CheckItems(t, r.fremote, file3, file2)
@ -727,9 +656,7 @@ func TestSyncAfterRemovingAFileAndAddingAFile(t *testing.T) {
fs.Stats.ResetCounters()
err := fs.Sync(r.fremote, r.flocal)
if err != nil {
t.Fatalf("Sync failed: %v", err)
}
require.NoError(t, err)
fstest.CheckItems(t, r.flocal, file1, file3)
fstest.CheckItems(t, r.fremote, file1, file3)
}
@ -747,9 +674,7 @@ func TestSyncAfterRemovingAFileAndAddingAFileWithErrors(t *testing.T) {
fs.Stats.ResetCounters()
fs.Stats.Error()
err := fs.Sync(r.fremote, r.flocal)
if err != fs.ErrorNotDeleting {
t.Fatalf("Unexpected error: %v", err)
}
assert.Equal(t, fs.ErrorNotDeleting, err)
fstest.CheckItems(t, r.flocal, file1, file3)
fstest.CheckItems(t, r.fremote, file1, file2, file3)
}
@ -806,18 +731,14 @@ func TestSyncWithExclude(t *testing.T) {
fs.Stats.ResetCounters()
err := fs.Sync(r.fremote, r.flocal)
if err != nil {
t.Fatalf("Sync failed: %v", err)
}
require.NoError(t, err)
fstest.CheckItems(t, r.fremote, file2, file1)
// Now sync the other way round and check enormous doesn't get
// deleted as it is excluded from the sync
fs.Stats.ResetCounters()
err = fs.Sync(r.flocal, r.fremote)
if err != nil {
t.Fatalf("Sync failed: %v", err)
}
require.NoError(t, err)
fstest.CheckItems(t, r.flocal, file2, file1, file3)
}
@ -840,18 +761,14 @@ func TestSyncWithExcludeAndDeleteExcluded(t *testing.T) {
fs.Stats.ResetCounters()
err := fs.Sync(r.fremote, r.flocal)
if err != nil {
t.Fatalf("Sync failed: %v", err)
}
require.NoError(t, err)
fstest.CheckItems(t, r.fremote, file2)
// Check sync the other way round to make sure enormous gets
// deleted even though it is excluded
fs.Stats.ResetCounters()
err = fs.Sync(r.flocal, r.fremote)
if err != nil {
t.Fatalf("Sync failed: %v", err)
}
require.NoError(t, err)
fstest.CheckItems(t, r.flocal, file2)
}
@ -886,9 +803,7 @@ func TestSyncWithUpdateOlder(t *testing.T) {
fs.Stats.ResetCounters()
err := fs.Sync(r.fremote, r.flocal)
if err != nil {
t.Fatalf("Sync failed: %v", err)
}
require.NoError(t, err)
fstest.CheckItems(t, r.fremote, oneO, twoF, threeO, fourF, fiveF)
}
@ -902,9 +817,7 @@ func TestServerSideMove(t *testing.T) {
fstest.CheckItems(t, r.fremote, file2, file1)
fremoteMove, finaliseMove, err := fstest.RandomRemote(*RemoteName, *SubDir)
if err != nil {
t.Fatalf("Failed to open remote move %q: %v", *RemoteName, err)
}
require.NoError(t, err)
defer finaliseMove()
t.Logf("Server side move (if possible) %v -> %v", r.fremote, fremoteMove)
@ -915,9 +828,7 @@ func TestServerSideMove(t *testing.T) {
// Do server side move
fs.Stats.ResetCounters()
err = fs.MoveDir(fremoteMove, r.fremote)
if err != nil {
t.Fatalf("Server Side Move failed: %v", err)
}
require.NoError(t, err)
fstest.CheckItems(t, r.fremote)
fstest.CheckItems(t, fremoteMove, file2, file1)
@ -925,9 +836,7 @@ func TestServerSideMove(t *testing.T) {
// Move it back again, dst does not exist this time
fs.Stats.ResetCounters()
err = fs.MoveDir(r.fremote, fremoteMove)
if err != nil {
t.Fatalf("Server Side Move 2 failed: %v", err)
}
require.NoError(t, err)
fstest.CheckItems(t, r.fremote, file2, file1)
fstest.CheckItems(t, fremoteMove)
@ -943,16 +852,10 @@ func TestLs(t *testing.T) {
var buf bytes.Buffer
err := fs.List(r.fremote, &buf)
if err != nil {
t.Fatalf("List failed: %v", err)
}
require.NoError(t, err)
res := buf.String()
if !strings.Contains(res, " 0 empty space\n") {
t.Errorf("empty space missing: %q", res)
}
if !strings.Contains(res, " 60 potato2\n") {
t.Errorf("potato2 missing: %q", res)
}
assert.Contains(t, res, " 0 empty space\n")
assert.Contains(t, res, " 60 potato2\n")
}
func TestLsLong(t *testing.T) {
@ -965,14 +868,10 @@ func TestLsLong(t *testing.T) {
var buf bytes.Buffer
err := fs.ListLong(r.fremote, &buf)
if err != nil {
t.Fatalf("List failed: %v", err)
}
require.NoError(t, err)
res := buf.String()
lines := strings.Split(strings.Trim(res, "\n"), "\n")
if len(lines) != 2 {
t.Fatalf("Wrong number of lines in list: %q", lines)
}
assert.Equal(t, 2, len(lines))
timeFormat := "2006-01-02 15:04:05.000000000"
precision := r.fremote.Precision()
@ -1014,9 +913,7 @@ func TestMd5sum(t *testing.T) {
var buf bytes.Buffer
err := fs.Md5sum(r.fremote, &buf)
if err != nil {
t.Fatalf("List failed: %v", err)
}
require.NoError(t, err)
res := buf.String()
if !strings.Contains(res, "d41d8cd98f00b204e9800998ecf8427e empty space\n") &&
!strings.Contains(res, " UNSUPPORTED empty space\n") &&
@ -1040,9 +937,7 @@ func TestSha1sum(t *testing.T) {
var buf bytes.Buffer
err := fs.Sha1sum(r.fremote, &buf)
if err != nil {
t.Fatalf("List failed: %v", err)
}
require.NoError(t, err)
res := buf.String()
if !strings.Contains(res, "da39a3ee5e6b4b0d3255bfef95601890afd80709 empty space\n") &&
!strings.Contains(res, " UNSUPPORTED empty space\n") &&
@ -1070,15 +965,9 @@ func TestCount(t *testing.T) {
defer func() { fs.Config.MaxDepth = -1 }()
objects, size, err := fs.Count(r.fremote)
if err != nil {
t.Fatalf("Count failed: %v", err)
}
if objects != 2 {
t.Errorf("want 2 objects got %d", objects)
}
if size != 60 {
t.Errorf("want size 60 got %d", size)
}
require.NoError(t, err)
assert.Equal(t, int64(2), objects)
assert.Equal(t, int64(60), size)
}
func TestDelete(t *testing.T) {
@ -1095,9 +984,7 @@ func TestDelete(t *testing.T) {
}()
err := fs.Delete(r.fremote)
if err != nil {
t.Fatalf("Sync failed: %v", err)
}
require.NoError(t, err)
fstest.CheckItems(t, r.fremote, file3)
}
@ -1157,19 +1044,13 @@ func TestCheckSizeOnly(t *testing.T) {
func (r *Run) checkWithDuplicates(t *testing.T, items ...fstest.Item) {
objects, size, err := fs.Count(r.fremote)
if err != nil {
t.Fatalf("Error listing: %v", err)
}
if objects != int64(len(items)) {
t.Fatalf("Error listing want %d objects, got %d", len(items), objects)
}
require.NoError(t, err)
assert.Equal(t, int64(len(items)), objects)
wantSize := int64(0)
for _, item := range items {
wantSize += item.Size
}
if wantSize != size {
t.Fatalf("Error listing want %d size, got %d", wantSize, size)
}
assert.Equal(t, wantSize, size)
}
func TestDeduplicateInteractive(t *testing.T) {
@ -1185,9 +1066,7 @@ func TestDeduplicateInteractive(t *testing.T) {
r.checkWithDuplicates(t, file1, file2, file3)
err := fs.Deduplicate(r.fremote, fs.DeduplicateInteractive)
if err != nil {
t.Fatalf("fs.Deduplicate returned error: %v", err)
}
require.NoError(t, err)
fstest.CheckItems(t, r.fremote, file1)
}
@ -1205,9 +1084,7 @@ func TestDeduplicateSkip(t *testing.T) {
r.checkWithDuplicates(t, file1, file2, file3)
err := fs.Deduplicate(r.fremote, fs.DeduplicateSkip)
if err != nil {
t.Fatalf("fs.Deduplicate returned error: %v", err)
}
require.NoError(t, err)
r.checkWithDuplicates(t, file1, file3)
}
@ -1225,17 +1102,11 @@ func TestDeduplicateFirst(t *testing.T) {
r.checkWithDuplicates(t, file1, file2, file3)
err := fs.Deduplicate(r.fremote, fs.DeduplicateFirst)
if err != nil {
t.Fatalf("fs.Deduplicate returned error: %v", err)
}
require.NoError(t, err)
objects, size, err := fs.Count(r.fremote)
if err != nil {
t.Fatalf("Error listing: %v", err)
}
if objects != 1 {
t.Errorf("Expecting 1 object got %v", objects)
}
require.NoError(t, err)
assert.Equal(t, 1, objects)
if size != file1.Size && size != file2.Size && size != file3.Size {
t.Errorf("Size not one of the object sizes %d", size)
}
@ -1254,9 +1125,7 @@ func TestDeduplicateNewest(t *testing.T) {
r.checkWithDuplicates(t, file1, file2, file3)
err := fs.Deduplicate(r.fremote, fs.DeduplicateNewest)
if err != nil {
t.Fatalf("fs.Deduplicate returned error: %v", err)
}
require.NoError(t, err)
fstest.CheckItems(t, r.fremote, file3)
}
@ -1274,9 +1143,7 @@ func TestDeduplicateOldest(t *testing.T) {
r.checkWithDuplicates(t, file1, file2, file3)
err := fs.Deduplicate(r.fremote, fs.DeduplicateOldest)
if err != nil {
t.Fatalf("fs.Deduplicate returned error: %v", err)
}
require.NoError(t, err)
fstest.CheckItems(t, r.fremote, file1)
}
@ -1294,16 +1161,12 @@ func TestDeduplicateRename(t *testing.T) {
r.checkWithDuplicates(t, file1, file2, file3)
err := fs.Deduplicate(r.fremote, fs.DeduplicateRename)
if err != nil {
t.Fatalf("fs.Deduplicate returned error: %v", err)
}
require.NoError(t, err)
list := fs.NewLister().Start(r.fremote, "")
for {
o, err := list.GetObject()
if err != nil {
t.Fatalf("Listing failed: %v", err)
}
require.NoError(t, err)
// Check if we are finished
if o == nil {
break

View File

@ -5,6 +5,7 @@ package fstest
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
@ -17,6 +18,8 @@ import (
"time"
"github.com/ncw/rclone/fs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
@ -69,35 +72,25 @@ func CheckTimeEqualWithPrecision(t0, t1 time.Time, precision time.Duration) (tim
// CheckModTime checks the mod time to the given precision
func (i *Item) CheckModTime(t *testing.T, obj fs.Object, modTime time.Time, precision time.Duration) {
dt, ok := CheckTimeEqualWithPrecision(modTime, i.ModTime, precision)
if !ok {
t.Errorf("%s: Modification time difference too big |%s| > %s (%s vs %s) (precision %s)", obj.Remote(), dt, precision, modTime, i.ModTime, precision)
}
assert.True(t, ok, fmt.Sprintf("%s: Modification time difference too big |%s| > %s (%s vs %s) (precision %s)", obj.Remote(), dt, precision, modTime, i.ModTime, precision))
}
// CheckHashes checks all the hashes the object supports are correct
func (i *Item) CheckHashes(t *testing.T, obj fs.Object) {
if obj == nil {
t.Fatalf("Object is nil")
}
require.NotNil(t, obj)
types := obj.Fs().Hashes().Array()
for _, hash := range types {
// Check attributes
sum, err := obj.Hash(hash)
if err != nil {
t.Fatalf("%s: Failed to read hash %v for %q: %v", obj.Fs().String(), hash, obj.Remote(), err)
}
if !fs.HashEquals(i.Hashes[hash], sum) {
t.Errorf("%s/%s: %v hash incorrect - expecting %q got %q", obj.Fs().String(), obj.Remote(), hash, i.Hashes[hash], sum)
}
require.NoError(t, err)
assert.True(t, fs.HashEquals(i.Hashes[hash], sum), fmt.Sprintf("%s/%s: %v hash incorrect - expecting %q got %q", obj.Fs().String(), obj.Remote(), hash, i.Hashes[hash], sum))
}
}
// Check checks all the attributes of the object are correct
func (i *Item) Check(t *testing.T, obj fs.Object, precision time.Duration) {
i.CheckHashes(t, obj)
if i.Size != obj.Size() {
t.Errorf("%s/%s: Size incorrect - expecting %d got %d", obj.Fs().String(), obj.Remote(), i.Size, obj.Size())
}
assert.Equal(t, i.Size, obj.Size())
i.CheckModTime(t, obj, obj.ModTime(), precision)
}
@ -128,10 +121,7 @@ func (is *Items) Find(t *testing.T, obj fs.Object, precision time.Duration) {
i, ok := is.byName[obj.Remote()]
if !ok {
i, ok = is.byNameAlt[obj.Remote()]
if !ok {
t.Errorf("Unexpected file %q", obj.Remote())
return
}
assert.True(t, ok, fmt.Sprintf("Unexpected file %q", obj.Remote()))
}
delete(is.byName, i.Path)
delete(is.byName, i.WinPath)
@ -177,10 +167,7 @@ func CheckListingWithPrecision(t *testing.T, f fs.Fs, items []Item, precision ti
time.Sleep(sleep)
}
for _, obj := range objs {
if obj == nil {
t.Errorf("Unexpected nil in List()")
continue
}
require.NotNil(t, obj)
is.Find(t, obj, precision)
}
is.Done(t)
@ -308,25 +295,19 @@ func RandomRemote(remoteName string, subdir bool) (fs.Fs, func(), error) {
// TestMkdir tests Mkdir works
func TestMkdir(t *testing.T, remote fs.Fs) {
err := fs.Mkdir(remote)
if err != nil {
t.Fatalf("Mkdir failed: %v", err)
}
require.NoError(t, err)
CheckListing(t, remote, []Item{})
}
// TestPurge tests Purge works
func TestPurge(t *testing.T, remote fs.Fs) {
err := fs.Purge(remote)
if err != nil {
t.Fatalf("Purge failed: %v", err)
}
require.NoError(t, err)
CheckListing(t, remote, []Item{})
}
// TestRmdir tests Rmdir works
func TestRmdir(t *testing.T, remote fs.Fs) {
err := fs.Rmdir(remote)
if err != nil {
t.Fatalf("Rmdir failed: %v", err)
}
require.NoError(t, err)
}

View File

@ -67,23 +67,17 @@ func TestInit(t *testing.T) {
t.Logf("Using remote %q", RemoteName)
if RemoteName == "" {
RemoteName, err = fstest.LocalRemote()
if err != nil {
t.Fatalf("Failed to create tmp dir: %v", err)
}
require.NoError(t, err)
}
subRemoteName, subRemoteLeaf, err = fstest.RandomRemoteName(RemoteName)
if err != nil {
t.Fatalf("Couldn't make remote name: %v", err)
}
require.NoError(t, err)
remote, err = fs.NewFs(subRemoteName)
if err == fs.ErrorNotFoundInConfigFile {
t.Logf("Didn't find %q in config file - skipping tests", RemoteName)
return
}
if err != nil {
t.Fatalf("Couldn't start FS: %v", err)
}
require.NoError(t, err)
fstest.TestMkdir(t, remote)
}
@ -97,9 +91,7 @@ func skipIfNotOk(t *testing.T) {
func TestFsString(t *testing.T) {
skipIfNotOk(t)
str := remote.String()
if str == "" {
t.Fatal("Bad fs.String()")
}
require.NotEqual(t, str, "")
}
// TestFsRmdirEmpty tests deleting an empty directory
@ -112,9 +104,7 @@ func TestFsRmdirEmpty(t *testing.T) {
func TestFsRmdirNotFound(t *testing.T) {
skipIfNotOk(t)
err := remote.Rmdir()
if err == nil {
t.Fatalf("Expecting error on Rmdir non existent")
}
assert.Error(t, err, "Expecting error on Rmdir non existent")
}
// TestFsMkdir tests tests making a directory
@ -192,9 +182,7 @@ func findObject(t *testing.T, Name string) fs.Object {
t.Logf("Sleeping for 1 second for findObject eventual consistency: %d/%d (%v)", i, eventualConsistencyRetries, err)
time.Sleep(1 * time.Second)
}
if err != nil {
t.Fatalf("Object %q not found: %v", Name, err)
}
require.NoError(t, err)
return obj
}
@ -217,7 +205,7 @@ again:
tries++
goto again
}
t.Fatal("Put error", err)
require.NoError(t, err, "Put error")
}
file.Hashes = hash.Sums()
file.Check(t, obj, remote.Precision())
@ -268,9 +256,7 @@ func TestFsListDirFile2(t *testing.T) {
func TestFsListDirRoot(t *testing.T) {
skipIfNotOk(t)
rootRemote, err := fs.NewFs(RemoteName)
if err != nil {
t.Fatalf("Failed to make remote %q: %v", RemoteName, err)
}
require.NoError(t, err)
dirs, err := fs.NewLister().SetLevel(1).Start(rootRemote, "").GetDirs()
require.NoError(t, err)
assert.Contains(t, dirsToNames(dirs), subRemoteLeaf, "Remote leaf not found")
@ -339,23 +325,17 @@ func TestFsCopy(t *testing.T) {
// do the copy
src := findObject(t, file1.Path)
dst, err := remote.(fs.Copier).Copy(src, file1Copy.Path)
if err != nil {
t.Fatalf("Copy failed: %v (%#v)", err, err)
}
require.NoError(t, err)
// check file exists in new listing
fstest.CheckListing(t, remote, []fstest.Item{file1, file2, file1Copy})
// Check dst lightly - list above has checked ModTime/Hashes
if dst.Remote() != file1Copy.Path {
t.Errorf("object path: want %q got %q", file1Copy.Path, dst.Remote())
}
assert.Equal(t, file1Copy.Path, dst.Remote())
// Delete copy
err = dst.Remove()
if err != nil {
t.Fatal("Remove copy error", err)
}
require.NoError(t, err)
}
@ -375,24 +355,18 @@ func TestFsMove(t *testing.T) {
// do the move
src := findObject(t, file1.Path)
dst, err := remote.(fs.Mover).Move(src, file1Move.Path)
if err != nil {
t.Fatalf("Move failed: %v", err)
}
require.NoError(t, err)
// check file exists in new listing
fstest.CheckListing(t, remote, []fstest.Item{file2, file1Move})
// Check dst lightly - list above has checked ModTime/Hashes
if dst.Remote() != file1Move.Path {
t.Errorf("object path: want %q got %q", file1Move.Path, dst.Remote())
}
assert.Equal(t, file1Move.Path, dst.Remote())
// move it back
src = findObject(t, file1Move.Path)
_, err = remote.(fs.Mover).Move(src, file1.Path)
if err != nil {
t.Errorf("Move failed: %v", err)
}
require.NoError(t, err)
// check file exists in new listing
fstest.CheckListing(t, remote, []fstest.Item{file2, file1})
@ -418,22 +392,16 @@ func TestFsDirMove(t *testing.T) {
// Check it can't move onto itself
err := remote.(fs.DirMover).DirMove(remote)
if err != fs.ErrorDirExists {
t.Errorf("Expecting fs.ErrorDirExists got: %v", err)
}
require.Equal(t, fs.ErrorDirExists, err)
// new remote
newRemote, removeNewRemote, err := fstest.RandomRemote(RemoteName, false)
if err != nil {
t.Fatalf("Failed to create remote: %v", err)
}
require.NoError(t, err)
defer removeNewRemote()
// try the move
err = newRemote.(fs.DirMover).DirMove(remote)
if err != nil {
t.Errorf("Failed to DirMove: %v", err)
}
require.NoError(t, err)
// check remotes
// FIXME: Prints errors.
@ -442,9 +410,7 @@ func TestFsDirMove(t *testing.T) {
// move it back
err = remote.(fs.DirMover).DirMove(newRemote)
if err != nil {
t.Errorf("Failed to DirMove: %v", err)
}
require.NoError(t, err)
// check remotes
fstest.CheckListing(t, remote, []fstest.Item{file2, file1})
@ -455,9 +421,7 @@ func TestFsDirMove(t *testing.T) {
func TestFsRmdirFull(t *testing.T) {
skipIfNotOk(t)
err := remote.Rmdir()
if err == nil {
t.Fatalf("Expecting error on RMdir on non empty remote")
}
require.Error(t, err, "Expecting error on RMdir on non empty remote")
}
// TestFsPrecision tests the Precision of the Fs
@ -477,40 +441,28 @@ func TestFsPrecision(t *testing.T) {
func TestObjectString(t *testing.T) {
skipIfNotOk(t)
obj := findObject(t, file1.Path)
s := obj.String()
if s != file1.Path {
t.Errorf("String() wrong %v != %v", s, file1.Path)
}
obj = NilObject
s = obj.String()
if s != "<nil>" {
t.Errorf("String() wrong %v != %v", s, "<nil>")
}
assert.Equal(t, file1.Path, obj.String())
assert.Equal(t, "<nil>", NilObject.String())
}
// TestObjectFs tests the object can be found
func TestObjectFs(t *testing.T) {
skipIfNotOk(t)
obj := findObject(t, file1.Path)
equal := obj.Fs() == remote
if !equal {
if obj.Fs() != remote {
// Check to see if this wraps something else
if unwrap, ok := remote.(fs.UnWrapper); ok {
equal = obj.Fs() == unwrap.UnWrap()
remote = unwrap.UnWrap()
}
}
if !equal {
t.Errorf("Fs is wrong %v != %v", obj.Fs(), remote)
}
assert.Equal(t, obj.Fs(), remote)
}
// TestObjectRemote tests the Remote is correct
func TestObjectRemote(t *testing.T) {
skipIfNotOk(t)
obj := findObject(t, file1.Path)
if obj.Remote() != file1.Path {
t.Errorf("Remote is wrong %v != %v", obj.Remote(), file1.Path)
}
assert.Equal(t, file1.Path, obj.Remote())
}
// TestObjectHashes checks all the hashes the object supports
@ -536,9 +488,8 @@ func TestObjectSetModTime(t *testing.T) {
if err == fs.ErrorCantSetModTime {
t.Log(err)
return
} else if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
file1.ModTime = newModTime
file1.CheckModTime(t, obj, obj.ModTime(), remote.Precision())
// And make a new object and read it from there too
@ -549,9 +500,7 @@ func TestObjectSetModTime(t *testing.T) {
func TestObjectSize(t *testing.T) {
skipIfNotOk(t)
obj := findObject(t, file1.Path)
if obj.Size() != file1.Size {
t.Errorf("Size is wrong %v != %v", obj.Size(), file1.Size)
}
assert.Equal(t, file1.Size, obj.Size())
}
// TestObjectOpen tests that Open works
@ -559,27 +508,16 @@ func TestObjectOpen(t *testing.T) {
skipIfNotOk(t)
obj := findObject(t, file1.Path)
in, err := obj.Open()
if err != nil {
t.Fatalf("Open() return error: %v", err)
}
require.NoError(t, err)
hasher := fs.NewMultiHasher()
n, err := io.Copy(hasher, in)
if err != nil {
t.Fatalf("io.Copy() return error: %v", err)
}
if n != file1.Size {
t.Fatalf("Read wrong number of bytes %d != %d", n, file1.Size)
}
require.NoError(t, err)
require.Equal(t, file1.Size, n, "Read wrong number of bytes")
err = in.Close()
if err != nil {
t.Fatalf("in.Close() return error: %v", err)
}
require.NoError(t, err)
// Check content of file by comparing the calculated hashes
for hashType, got := range hasher.Sums() {
want := file1.Hashes[hashType]
if want != got {
t.Errorf("%v is wrong %v != %v", hashType, want, got)
}
assert.Equal(t, file1.Hashes[hashType], got)
}
}
@ -595,9 +533,7 @@ func TestObjectUpdate(t *testing.T) {
obj := findObject(t, file1.Path)
obji := fs.NewStaticObjectInfo("", file1.ModTime, file1.Size, true, nil, obj.Fs())
err := obj.Update(in, obji)
if err != nil {
t.Fatal("Update error", err)
}
require.NoError(t, err)
file1.Hashes = hash.Sums()
file1.Check(t, obj, remote.Precision())
// Re-read the object and check again
@ -609,9 +545,7 @@ func TestObjectUpdate(t *testing.T) {
func TestObjectStorable(t *testing.T) {
skipIfNotOk(t)
obj := findObject(t, file1.Path)
if !obj.Storable() {
t.Fatalf("Expecting %v to be storable", obj)
}
require.NotNil(t, !obj.Storable(), "Expecting object to be storable")
}
// TestFsIsFile tests that an error is returned along with a valid fs
@ -631,9 +565,7 @@ func TestFsIsFileNotFound(t *testing.T) {
skipIfNotOk(t)
remoteName := subRemoteName + "/not found.txt"
fileRemote, err := fs.NewFs(remoteName)
if err != nil {
t.Fatalf("Failed to make remote %q: %v", remoteName, err)
}
require.NoError(t, err)
fstest.CheckListing(t, fileRemote, []fstest.Item{})
}
@ -642,9 +574,7 @@ func TestObjectRemove(t *testing.T) {
skipIfNotOk(t)
obj := findObject(t, file1.Path)
err := obj.Remove()
if err != nil {
t.Fatal("Remove error", err)
}
require.NoError(t, err)
fstest.CheckListing(t, remote, []fstest.Item{file2})
}
@ -653,9 +583,7 @@ func TestObjectPurge(t *testing.T) {
skipIfNotOk(t)
fstest.TestPurge(t, remote)
err := fs.Purge(remote)
if err == nil {
t.Fatal("Expecting error after on second purge")
}
assert.Error(t, err, "Expecting error after on second purge")
}
// TestFinalise tidies up after the previous tests
@ -664,8 +592,6 @@ func TestFinalise(t *testing.T) {
if strings.HasPrefix(RemoteName, "/") {
// Remove temp directory
err := os.Remove(RemoteName)
if err != nil {
t.Logf("Failed to remove %q: %v\n", RemoteName, err)
}
require.NoError(t, err)
}
}