2017-03-16 22:37:56 +00:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
2018-01-31 20:18:31 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2017-03-16 22:37:56 +00:00
|
|
|
"testing"
|
2018-01-31 20:18:31 +00:00
|
|
|
"time"
|
2017-03-16 22:37:56 +00:00
|
|
|
|
2018-01-31 20:18:31 +00:00
|
|
|
"github.com/ncw/rclone/fs/hash"
|
|
|
|
"github.com/ncw/rclone/fstest"
|
|
|
|
"github.com/ncw/rclone/lib/readers"
|
2017-03-16 22:37:56 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-01-31 20:18:31 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-03-16 22:37:56 +00:00
|
|
|
)
|
|
|
|
|
2018-01-31 20:18:31 +00:00
|
|
|
// TestMain drives the tests
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
fstest.TestMain(m)
|
|
|
|
}
|
|
|
|
|
2017-03-16 22:37:56 +00:00
|
|
|
func TestMapper(t *testing.T) {
|
|
|
|
m := newMapper()
|
|
|
|
assert.Equal(t, m.m, map[string]string{})
|
|
|
|
assert.Equal(t, "potato", m.Save("potato", "potato"))
|
|
|
|
assert.Equal(t, m.m, map[string]string{})
|
|
|
|
assert.Equal(t, "-r'áö", m.Save("-r?'a´o¨", "-r'áö"))
|
|
|
|
assert.Equal(t, m.m, map[string]string{
|
|
|
|
"-r'áö": "-r?'a´o¨",
|
|
|
|
})
|
|
|
|
assert.Equal(t, "potato", m.Load("potato"))
|
|
|
|
assert.Equal(t, "-r?'a´o¨", m.Load("-r'áö"))
|
2018-04-09 14:27:58 +00:00
|
|
|
}
|
2018-01-31 20:18:31 +00:00
|
|
|
|
2018-04-09 14:27:58 +00:00
|
|
|
// Test copy with source file that's updating
|
|
|
|
func TestUpdatingCheck(t *testing.T) {
|
2018-01-31 20:18:31 +00:00
|
|
|
r := fstest.NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
filePath := "sub dir/local test"
|
|
|
|
r.WriteFile(filePath, "content", time.Now())
|
|
|
|
|
|
|
|
fd, err := os.Open(path.Join(r.LocalName, filePath))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed opening file %q: %v", filePath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := fd.Stat()
|
2018-05-04 14:19:50 +00:00
|
|
|
require.NoError(t, err)
|
2018-05-14 17:06:57 +00:00
|
|
|
o := &Object{size: fi.Size(), modTime: fi.ModTime(), fs: &Fs{}}
|
2018-01-31 20:18:31 +00:00
|
|
|
wrappedFd := readers.NewLimitedReadCloser(fd, -1)
|
|
|
|
hash, err := hash.NewMultiHasherTypes(hash.Supported)
|
2018-05-04 14:19:50 +00:00
|
|
|
require.NoError(t, err)
|
2018-01-31 20:18:31 +00:00
|
|
|
in := localOpenFile{
|
|
|
|
o: o,
|
|
|
|
in: wrappedFd,
|
|
|
|
hash: hash,
|
|
|
|
fd: fd,
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, 1)
|
|
|
|
_, err = in.Read(buf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
r.WriteFile(filePath, "content updated", time.Now())
|
|
|
|
_, err = in.Read(buf)
|
|
|
|
require.Errorf(t, err, "can't copy - source file is being updated")
|
2018-04-09 14:27:58 +00:00
|
|
|
|
|
|
|
// turn the checking off and try again
|
2018-05-14 17:06:57 +00:00
|
|
|
in.o.fs.opt.NoCheckUpdated = true
|
2018-04-09 14:27:58 +00:00
|
|
|
|
|
|
|
r.WriteFile(filePath, "content updated", time.Now())
|
|
|
|
_, err = in.Read(buf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2017-03-16 22:37:56 +00:00
|
|
|
}
|