github.com/ncw/rclone@v1.48.1-0.20190724201158-a35aa1360e3e/cmd/touch/touch_test.go (about) 1 package touch 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 _ "github.com/ncw/rclone/backend/local" 9 "github.com/ncw/rclone/fs" 10 "github.com/ncw/rclone/fstest" 11 "github.com/stretchr/testify/require" 12 ) 13 14 var ( 15 t1 = fstest.Time("2017-02-03T04:05:06.499999999Z") 16 ) 17 18 func checkFile(t *testing.T, r fs.Fs, path string, content string) { 19 layout := defaultLayout 20 if len(timeAsArgument) == len(layoutDateWithTime) { 21 layout = layoutDateWithTime 22 } 23 timeAtrFromFlags, err := time.Parse(layout, timeAsArgument) 24 require.NoError(t, err) 25 file1 := fstest.NewItem(path, content, timeAtrFromFlags) 26 fstest.CheckItems(t, r, file1) 27 } 28 29 // TestMain drives the tests 30 func TestMain(m *testing.M) { 31 fstest.TestMain(m) 32 } 33 34 func TestTouchOneFile(t *testing.T) { 35 r := fstest.NewRun(t) 36 defer r.Finalise() 37 38 err := Touch(context.Background(), r.Fremote, "newFile") 39 require.NoError(t, err) 40 _, err = r.Fremote.NewObject(context.Background(), "newFile") 41 require.NoError(t, err) 42 } 43 44 func TestTouchWithNoCreateFlag(t *testing.T) { 45 r := fstest.NewRun(t) 46 defer r.Finalise() 47 48 notCreateNewFile = true 49 err := Touch(context.Background(), r.Fremote, "newFile") 50 require.NoError(t, err) 51 _, err = r.Fremote.NewObject(context.Background(), "newFile") 52 require.Error(t, err) 53 notCreateNewFile = false 54 } 55 56 func TestTouchWithTimestamp(t *testing.T) { 57 r := fstest.NewRun(t) 58 defer r.Finalise() 59 60 timeAsArgument = "060102" 61 srcFileName := "oldFile" 62 err := Touch(context.Background(), r.Fremote, srcFileName) 63 require.NoError(t, err) 64 checkFile(t, r.Fremote, srcFileName, "") 65 } 66 67 func TestTouchWithLognerTimestamp(t *testing.T) { 68 r := fstest.NewRun(t) 69 defer r.Finalise() 70 71 timeAsArgument = "2006-01-02T15:04:05" 72 srcFileName := "oldFile" 73 err := Touch(context.Background(), r.Fremote, srcFileName) 74 require.NoError(t, err) 75 checkFile(t, r.Fremote, srcFileName, "") 76 } 77 78 func TestTouchUpdateTimestamp(t *testing.T) { 79 r := fstest.NewRun(t) 80 defer r.Finalise() 81 82 srcFileName := "a" 83 content := "aaa" 84 file1 := r.WriteObject(context.Background(), srcFileName, content, t1) 85 fstest.CheckItems(t, r.Fremote, file1) 86 87 timeAsArgument = "121212" 88 err := Touch(context.Background(), r.Fremote, "a") 89 require.NoError(t, err) 90 checkFile(t, r.Fremote, srcFileName, content) 91 } 92 93 func TestTouchUpdateTimestampWithCFlag(t *testing.T) { 94 r := fstest.NewRun(t) 95 defer r.Finalise() 96 97 srcFileName := "a" 98 content := "aaa" 99 file1 := r.WriteObject(context.Background(), srcFileName, content, t1) 100 fstest.CheckItems(t, r.Fremote, file1) 101 102 notCreateNewFile = true 103 timeAsArgument = "121212" 104 err := Touch(context.Background(), r.Fremote, "a") 105 require.NoError(t, err) 106 checkFile(t, r.Fremote, srcFileName, content) 107 notCreateNewFile = false 108 } 109 110 func TestTouchCreateMultipleDirAndFile(t *testing.T) { 111 r := fstest.NewRun(t) 112 defer r.Finalise() 113 114 longPath := "a/b/c.txt" 115 err := Touch(context.Background(), r.Fremote, longPath) 116 require.NoError(t, err) 117 file1 := fstest.NewItem("a/b/c.txt", "", t1) 118 fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{file1}, []string{"a", "a/b"}, fs.ModTimeNotSupported) 119 }