touch_test.go 4.3 KB

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