diff --git a/cmd/touch/touch_test.go b/cmd/touch/touch_test.go index b66194cf5..32d051ab3 100644 --- a/cmd/touch/touch_test.go +++ b/cmd/touch/touch_test.go @@ -6,56 +6,114 @@ import ( "github.com/ncw/rclone/fs" "github.com/ncw/rclone/fstest" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" _ "github.com/ncw/rclone/backend/local" ) -func TestTouch(t *testing.T) { - fstest.Initialise() - f, err := fs.NewFs("testfiles") - err = Touch(f, "newFile") - require.NoError(t, err) - file, errFile := f.NewObject("newFile") - require.NoError(t, errFile) - err = file.Remove() - require.NoError(t, err) +var ( + t1 = fstest.Time("2017-02-03T04:05:06.499999999Z") +) - notCreateNewFile = true - err = Touch(f, "fileWithCflag") +func checkFile(t *testing.T, r fs.Fs, path string, content string) { + layout := defaultLayout + if len(timeAsArgument) == len(layoutDateWithTime) { + layout = layoutDateWithTime + } + timeAtrFromFlags, err := time.Parse(layout, timeAsArgument) require.NoError(t, err) - file, errFile = f.NewObject("fileWithCflag") - require.Error(t, errFile) - notCreateNewFile = false + file1 := fstest.NewItem(path, content, timeAtrFromFlags) + fstest.CheckItems(t, r, file1) +} - timeAsArgument = "060102" - err = Touch(f, "oldFile") - require.NoError(t, err) - file, err = f.NewObject("oldFile") - require.NoError(t, err) - curretTime := time.Now() - require.NoError(t, err) - print(file.ModTime().Year() < curretTime.Year()) - assert.Equal(t, true, file.ModTime().Year() < curretTime.Year()) - err = file.Remove() - require.NoError(t, err) +// TestMain drives the tests +func TestMain(m *testing.M) { + fstest.TestMain(m) +} - timeAsArgument = "2006-01-02T15:04:05" - err = Touch(f, "oldFile") - require.NoError(t, err) - file, err = f.NewObject("oldFile") - require.NoError(t, err) - assert.Equal(t, true, file.ModTime().Year() < curretTime.Year()) +func TestTouchOneFile(t *testing.T) { + r := fstest.NewRun(t) + defer r.Finalise() - timeAsArgument = "" - err = Touch(f, "oldFile") + err := Touch(r.Fremote, "newFile") require.NoError(t, err) - file, err = f.NewObject("oldFile") - require.NoError(t, err) - timeBetween2007YearAndCurrent, errTime := time.Parse("060102", "121212") - require.NoError(t, errTime) - assert.Equal(t, true, file.ModTime().Year() > timeBetween2007YearAndCurrent.Year()) - err = file.Remove() + _, err = r.Fremote.NewObject("newFile") require.NoError(t, err) } + +func TestTouchWithNoCreateFlag(t *testing.T) { + r := fstest.NewRun(t) + defer r.Finalise() + + notCreateNewFile = true + err := Touch(r.Fremote, "newFile") + require.NoError(t, err) + _, err = r.Fremote.NewObject("newFile") + require.Error(t, err) + notCreateNewFile = false +} + +func TestTouchWithTimestamp(t *testing.T) { + r := fstest.NewRun(t) + defer r.Finalise() + + timeAsArgument = "060102" + srcFileName := "oldFile" + err := Touch(r.Fremote, srcFileName) + require.NoError(t, err) + checkFile(t, r.Fremote, srcFileName, "") +} + +func TestTouchWithLognerTimestamp(t *testing.T) { + r := fstest.NewRun(t) + defer r.Finalise() + + timeAsArgument = "2006-01-02T15:04:05" + srcFileName := "oldFile" + err := Touch(r.Fremote, srcFileName) + require.NoError(t, err) + checkFile(t, r.Fremote, srcFileName, "") +} + +func TestTouchUpdateTimestamp(t *testing.T) { + r := fstest.NewRun(t) + defer r.Finalise() + + srcFileName := "a" + content := "aaa" + file1 := r.WriteObject(srcFileName, content, t1) + fstest.CheckItems(t, r.Fremote, file1) + + timeAsArgument = "121212" + err := Touch(r.Fremote, "a") + require.NoError(t, err) + 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(srcFileName, content, t1) + fstest.CheckItems(t, r.Fremote, file1) + + notCreateNewFile = true + timeAsArgument = "121212" + err := Touch(r.Fremote, "a") + require.NoError(t, err) + 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(r.Fremote, longPath) + require.NoError(t, err) + file1 := fstest.NewItem("a/b/c.txt", "", t1) + fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{file1}, []string{"a", "a/b"}, fs.ModTimeNotSupported) +}