rclone/cmd/touch/touch_test.go

164 lines
4.5 KiB
Go
Raw Normal View History

2017-11-20 19:16:05 +08:00
package touch
import (
"context"
2017-11-20 19:16:05 +08:00
"testing"
_ "github.com/rclone/rclone/backend/local"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fstest"
2017-11-20 19:16:05 +08:00
"github.com/stretchr/testify/require"
)
2018-02-02 21:46:56 +08:00
var (
t1 = fstest.Time("2017-02-03T04:05:06.499999999Z")
)
func checkFile(t *testing.T, r fs.Fs, path string, content string) {
timeAtrFromFlags, err := timeOfTouch()
2018-02-02 21:46:56 +08:00
require.NoError(t, err)
file1 := fstest.NewItem(path, content, timeAtrFromFlags)
fstest.CheckItems(t, r, file1)
}
// TestMain drives the tests
func TestMain(m *testing.M) {
fstest.TestMain(m)
}
func TestTouchOneFile(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
err := Touch(context.Background(), r.Fremote, "newFile")
2017-11-20 19:16:05 +08:00
require.NoError(t, err)
_, err = r.Fremote.NewObject(context.Background(), "newFile")
2017-11-20 19:16:05 +08:00
require.NoError(t, err)
2018-02-02 21:46:56 +08:00
}
func TestTouchWithNoCreateFlag(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
2017-11-20 19:16:05 +08:00
notCreateNewFile = true
err := Touch(context.Background(), r.Fremote, "newFile")
2017-11-20 19:16:05 +08:00
require.NoError(t, err)
_, err = r.Fremote.NewObject(context.Background(), "newFile")
2018-02-02 21:46:56 +08:00
require.Error(t, err)
2017-11-20 19:16:05 +08:00
notCreateNewFile = false
2018-02-02 21:46:56 +08:00
}
func TestTouchWithTimestamp(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
2017-11-20 19:16:05 +08:00
timeAsArgument = "060102"
2018-02-02 21:46:56 +08:00
srcFileName := "oldFile"
err := Touch(context.Background(), r.Fremote, srcFileName)
2017-11-20 19:16:05 +08:00
require.NoError(t, err)
2018-02-02 21:46:56 +08:00
checkFile(t, r.Fremote, srcFileName, "")
}
func TestTouchWithLongerTimestamp(t *testing.T) {
2018-02-02 21:46:56 +08:00
r := fstest.NewRun(t)
defer r.Finalise()
2017-11-20 19:16:05 +08:00
timeAsArgument = "2006-01-02T15:04:05"
2018-02-02 21:46:56 +08:00
srcFileName := "oldFile"
err := Touch(context.Background(), r.Fremote, srcFileName)
2017-11-20 19:16:05 +08:00
require.NoError(t, err)
2018-02-02 21:46:56 +08:00
checkFile(t, r.Fremote, srcFileName, "")
}
2017-11-20 19:16:05 +08:00
2018-02-02 21:46:56 +08:00
func TestTouchUpdateTimestamp(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
srcFileName := "a"
content := "aaa"
file1 := r.WriteObject(context.Background(), srcFileName, content, t1)
r.CheckRemoteItems(t, file1)
2018-02-02 21:46:56 +08:00
timeAsArgument = "121212"
err := Touch(context.Background(), r.Fremote, "a")
2017-11-20 19:16:05 +08:00
require.NoError(t, err)
2018-02-02 21:46:56 +08:00
checkFile(t, r.Fremote, srcFileName, content)
}
func TestTouchUpdateTimestampWithCFlag(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
srcFileName := "a"
content := "aaa"
file1 := r.WriteObject(context.Background(), srcFileName, content, t1)
r.CheckRemoteItems(t, file1)
2018-02-02 21:46:56 +08:00
notCreateNewFile = true
timeAsArgument = "121212"
err := Touch(context.Background(), r.Fremote, "a")
2017-11-20 19:16:05 +08:00
require.NoError(t, err)
2018-02-02 21:46:56 +08:00
checkFile(t, r.Fremote, srcFileName, content)
notCreateNewFile = false
}
func TestTouchCreateMultipleDirAndFile(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
longPath := "a/b/c.txt"
err := Touch(context.Background(), r.Fremote, longPath)
2017-11-20 19:16:05 +08:00
require.NoError(t, err)
2018-02-02 21:46:56 +08:00
file1 := fstest.NewItem("a/b/c.txt", "", t1)
fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{file1}, []string{"a", "a/b"}, fs.ModTimeNotSupported)
2017-11-20 19:16:05 +08:00
}
func TestTouchEmptyName(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
err := Touch(context.Background(), r.Fremote, "")
require.NoError(t, err)
fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{}, []string{}, fs.ModTimeNotSupported)
}
func TestTouchEmptyDir(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
err := r.Fremote.Mkdir(context.Background(), "a")
require.NoError(t, err)
err = Touch(context.Background(), r.Fremote, "a")
require.NoError(t, err)
fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{}, []string{"a"}, fs.ModTimeNotSupported)
}
func TestTouchDirWithFiles(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
err := r.Fremote.Mkdir(context.Background(), "a")
require.NoError(t, err)
file1 := r.WriteObject(context.Background(), "a/f1", "111", t1)
file2 := r.WriteObject(context.Background(), "a/f2", "222", t1)
err = Touch(context.Background(), r.Fremote, "a")
require.NoError(t, err)
fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{file1, file2}, []string{"a"}, fs.ModTimeNotSupported)
}
func TestRecursiveTouchDirWithFiles(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
err := r.Fremote.Mkdir(context.Background(), "a/b/c")
require.NoError(t, err)
file1 := r.WriteObject(context.Background(), "a/f1", "111", t1)
file2 := r.WriteObject(context.Background(), "a/b/f2", "222", t1)
file3 := r.WriteObject(context.Background(), "a/b/c/f3", "333", t1)
recursive = true
err = Touch(context.Background(), r.Fremote, "a")
recursive = false
require.NoError(t, err)
fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{file1, file2, file3}, []string{"a", "a/b", "a/b/c"}, fs.ModTimeNotSupported)
}