github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/vfs/vfstest/dir.go (about) 1 package vfstest 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 // TestDirLs checks out listing 13 func TestDirLs(t *testing.T) { 14 run.skipIfNoFUSE(t) 15 16 run.checkDir(t, "") 17 18 run.mkdir(t, "a directory") 19 run.createFile(t, "a file", "hello") 20 21 run.checkDir(t, "a directory/|a file 5") 22 23 run.rmdir(t, "a directory") 24 run.rm(t, "a file") 25 26 run.checkDir(t, "") 27 } 28 29 // TestDirCreateAndRemoveDir tests creating and removing a directory 30 func TestDirCreateAndRemoveDir(t *testing.T) { 31 run.skipIfNoFUSE(t) 32 33 run.mkdir(t, "dir") 34 run.mkdir(t, "dir/subdir") 35 run.checkDir(t, "dir/|dir/subdir/") 36 37 // Check we can't delete a directory with stuff in 38 err := run.os.Remove(run.path("dir")) 39 assert.Error(t, err, "file exists") 40 41 // Now delete subdir then dir - should produce no errors 42 run.rmdir(t, "dir/subdir") 43 run.checkDir(t, "dir/") 44 run.rmdir(t, "dir") 45 run.checkDir(t, "") 46 } 47 48 // TestDirCreateAndRemoveFile tests creating and removing a file 49 func TestDirCreateAndRemoveFile(t *testing.T) { 50 run.skipIfNoFUSE(t) 51 52 run.mkdir(t, "dir") 53 run.createFile(t, "dir/file", "potato") 54 run.checkDir(t, "dir/|dir/file 6") 55 56 // Check we can't delete a directory with stuff in 57 err := run.os.Remove(run.path("dir")) 58 assert.Error(t, err, "file exists") 59 60 // Now delete file 61 run.rm(t, "dir/file") 62 63 run.checkDir(t, "dir/") 64 run.rmdir(t, "dir") 65 run.checkDir(t, "") 66 } 67 68 // TestDirRenameFile tests renaming a file 69 func TestDirRenameFile(t *testing.T) { 70 run.skipIfNoFUSE(t) 71 72 run.mkdir(t, "dir") 73 run.createFile(t, "file", "potato") 74 run.checkDir(t, "dir/|file 6") 75 76 err := run.os.Rename(run.path("file"), run.path("file2")) 77 require.NoError(t, err) 78 run.checkDir(t, "dir/|file2 6") 79 80 data := run.readFile(t, "file2") 81 assert.Equal(t, "potato", data) 82 83 err = run.os.Rename(run.path("file2"), run.path("dir/file3")) 84 require.NoError(t, err) 85 run.checkDir(t, "dir/|dir/file3 6") 86 87 data = run.readFile(t, "dir/file3") 88 require.NoError(t, err) 89 assert.Equal(t, "potato", data) 90 91 run.rm(t, "dir/file3") 92 run.rmdir(t, "dir") 93 run.checkDir(t, "") 94 } 95 96 // TestDirRenameEmptyDir tests renaming and empty directory 97 func TestDirRenameEmptyDir(t *testing.T) { 98 run.skipIfNoFUSE(t) 99 100 run.mkdir(t, "dir") 101 run.mkdir(t, "dir1") 102 run.checkDir(t, "dir/|dir1/") 103 104 err := run.os.Rename(run.path("dir1"), run.path("dir/dir2")) 105 require.NoError(t, err) 106 run.checkDir(t, "dir/|dir/dir2/") 107 108 err = run.os.Rename(run.path("dir/dir2"), run.path("dir/dir3")) 109 require.NoError(t, err) 110 run.checkDir(t, "dir/|dir/dir3/") 111 112 run.rmdir(t, "dir/dir3") 113 run.rmdir(t, "dir") 114 run.checkDir(t, "") 115 } 116 117 // TestDirRenameFullDir tests renaming a full directory 118 func TestDirRenameFullDir(t *testing.T) { 119 run.skipIfNoFUSE(t) 120 121 run.mkdir(t, "dir") 122 run.mkdir(t, "dir1") 123 run.createFile(t, "dir1/potato.txt", "maris piper") 124 run.checkDir(t, "dir/|dir1/|dir1/potato.txt 11") 125 126 err := run.os.Rename(run.path("dir1"), run.path("dir/dir2")) 127 require.NoError(t, err) 128 run.checkDir(t, "dir/|dir/dir2/|dir/dir2/potato.txt 11") 129 130 err = run.os.Rename(run.path("dir/dir2"), run.path("dir/dir3")) 131 require.NoError(t, err) 132 run.checkDir(t, "dir/|dir/dir3/|dir/dir3/potato.txt 11") 133 134 run.rm(t, "dir/dir3/potato.txt") 135 run.rmdir(t, "dir/dir3") 136 run.rmdir(t, "dir") 137 run.checkDir(t, "") 138 } 139 140 // TestDirModTime tests mod times 141 func TestDirModTime(t *testing.T) { 142 run.skipIfNoFUSE(t) 143 144 run.mkdir(t, "dir") 145 mtime := time.Date(2012, time.November, 18, 17, 32, 31, 0, time.UTC) 146 err := run.os.Chtimes(run.path("dir"), mtime, mtime) 147 require.NoError(t, err) 148 149 info, err := run.os.Stat(run.path("dir")) 150 require.NoError(t, err) 151 152 // avoid errors because of timezone differences 153 assert.Equal(t, info.ModTime().Unix(), mtime.Unix()) 154 155 run.rmdir(t, "dir") 156 } 157 158 // TestDirCacheFlush tests flushing the dir cache 159 func TestDirCacheFlush(t *testing.T) { 160 run.skipIfNoFUSE(t) 161 162 run.checkDir(t, "") 163 164 run.mkdir(t, "dir") 165 run.mkdir(t, "otherdir") 166 run.createFile(t, "dir/file", "1") 167 run.createFile(t, "otherdir/file", "1") 168 169 dm := newDirMap("otherdir/|otherdir/file 1|dir/|dir/file 1") 170 localDm := make(dirMap) 171 run.readLocal(t, localDm, "") 172 assert.Equal(t, dm, localDm, "expected vs fuse mount") 173 174 err := run.fremote.Mkdir(context.Background(), "dir/subdir") 175 require.NoError(t, err) 176 177 // expect newly created "subdir" on remote to not show up 178 run.forget("otherdir") 179 run.readLocal(t, localDm, "") 180 assert.Equal(t, dm, localDm, "expected vs fuse mount") 181 182 run.forget("dir") 183 dm = newDirMap("otherdir/|otherdir/file 1|dir/|dir/file 1|dir/subdir/") 184 run.readLocal(t, localDm, "") 185 assert.Equal(t, dm, localDm, "expected vs fuse mount") 186 187 run.rm(t, "otherdir/file") 188 run.rmdir(t, "otherdir") 189 run.rm(t, "dir/file") 190 run.rmdir(t, "dir/subdir") 191 run.rmdir(t, "dir") 192 run.checkDir(t, "") 193 } 194 195 // TestDirCacheFlushOnDirRename tests flushing the dir cache on rename 196 func TestDirCacheFlushOnDirRename(t *testing.T) { 197 run.skipIfNoFUSE(t) 198 run.mkdir(t, "dir") 199 run.createFile(t, "dir/file", "1") 200 201 dm := newDirMap("dir/|dir/file 1") 202 localDm := make(dirMap) 203 run.readLocal(t, localDm, "") 204 assert.Equal(t, dm, localDm, "expected vs fuse mount") 205 206 // expect remotely created directory to not show up 207 err := run.fremote.Mkdir(context.Background(), "dir/subdir") 208 require.NoError(t, err) 209 run.readLocal(t, localDm, "") 210 assert.Equal(t, dm, localDm, "expected vs fuse mount") 211 212 err = run.os.Rename(run.path("dir"), run.path("rid")) 213 require.NoError(t, err) 214 215 dm = newDirMap("rid/|rid/subdir/|rid/file 1") 216 localDm = make(dirMap) 217 run.readLocal(t, localDm, "") 218 assert.Equal(t, dm, localDm, "expected vs fuse mount") 219 220 run.rm(t, "rid/file") 221 run.rmdir(t, "rid/subdir") 222 run.rmdir(t, "rid") 223 run.checkDir(t, "") 224 }