Spaces:
Runtime error
Runtime error
File size: 9,994 Bytes
63deadc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
import os
from pathlib import PurePosixPath, PureWindowsPath
import pytest
from fsspec.implementations.local import LocalFileSystem, make_path_posix
def test_1(m):
m.touch("/somefile") # NB: is found with or without initial /
m.touch("afiles/and/another")
files = m.find("")
assert files == ["/afiles/and/another", "/somefile"]
files = sorted(m.get_mapper())
assert files == ["afiles/and/another", "somefile"]
def test_strip(m):
assert m._strip_protocol("") == ""
assert m._strip_protocol("memory://") == ""
assert m._strip_protocol("afile") == "/afile"
assert m._strip_protocol("/b/c") == "/b/c"
assert m._strip_protocol("/b/c/") == "/b/c"
def test_ls(m):
m.mkdir("/dir")
m.mkdir("/dir/dir1")
m.touch("/dir/afile")
m.touch("/dir/dir1/bfile")
m.touch("/dir/dir1/cfile")
assert m.ls("/", False) == ["/dir"]
assert m.ls("/dir", False) == ["/dir/afile", "/dir/dir1"]
assert m.ls("/dir", True)[0]["type"] == "file"
assert m.ls("/dir", True)[1]["type"] == "directory"
assert m.ls("/dir/afile", False) == ["/dir/afile"]
assert m.ls("/dir/afile", True)[0]["type"] == "file"
assert len(m.ls("/dir/dir1")) == 2
assert len(m.ls("/dir/afile")) == 1
def test_directories(m):
m.mkdir("outer/inner")
assert m.info("outer/inner")["type"] == "directory"
assert m.ls("outer")
assert m.ls("outer/inner") == []
with pytest.raises(OSError):
m.rmdir("outer")
m.rmdir("outer/inner")
m.rmdir("outer")
assert not m.store
def test_exists_isdir_isfile(m):
m.mkdir("/root")
m.touch("/root/a")
assert m.exists("/root")
assert m.isdir("/root")
assert not m.isfile("/root")
assert m.exists("/root/a")
assert m.isfile("/root/a")
assert not m.isdir("/root/a")
assert not m.exists("/root/not-exists")
assert not m.isfile("/root/not-exists")
assert not m.isdir("/root/not-exists")
m.rm("/root/a")
m.rmdir("/root")
assert not m.exists("/root")
m.touch("/a/b")
assert m.isfile("/a/b")
assert m.exists("/a")
assert m.isdir("/a")
assert not m.isfile("/a")
def test_touch(m):
m.touch("/root/a")
with pytest.raises(FileExistsError):
m.touch("/root/a/b")
with pytest.raises(FileExistsError):
m.touch("/root/a/b/c")
assert not m.exists("/root/a/b/")
def test_mv_recursive(m):
m.mkdir("src")
m.touch("src/file.txt")
m.mv("src", "dest", recursive=True)
assert m.exists("dest/file.txt")
assert not m.exists("src")
def test_mv_same_paths(m):
m.mkdir("src")
m.touch("src/file.txt")
m.mv("src", "src", recursive=True)
assert m.exists("src/file.txt")
def test_rm_no_pseudo_dir(m):
m.touch("/dir1/dir2/file")
m.rm("/dir1", recursive=True)
assert not m.exists("/dir1/dir2/file")
assert not m.exists("/dir1/dir2")
assert not m.exists("/dir1")
with pytest.raises(FileNotFoundError):
m.rm("/dir1", recursive=True)
def test_rewind(m):
# https://github.com/fsspec/filesystem_spec/issues/349
with m.open("src/file.txt", "w") as f:
f.write("content")
with m.open("src/file.txt") as f:
assert f.tell() == 0
def test_empty_raises(m):
with pytest.raises(FileNotFoundError):
m.ls("nonexistent")
with pytest.raises(FileNotFoundError):
m.info("nonexistent")
def test_dir_errors(m):
m.mkdir("/first")
with pytest.raises(FileExistsError):
m.mkdir("/first")
with pytest.raises(FileExistsError):
m.makedirs("/first", exist_ok=False)
m.makedirs("/first", exist_ok=True)
m.makedirs("/first/second/third")
assert "/first/second" in m.pseudo_dirs
m.touch("/afile")
with pytest.raises(NotADirectoryError):
m.mkdir("/afile/nodir")
def test_no_rewind_append_mode(m):
# https://github.com/fsspec/filesystem_spec/issues/349
with m.open("src/file.txt", "w") as f:
f.write("content")
with m.open("src/file.txt", "a") as f:
assert f.tell() == 7
def test_moves(m):
m.touch("source.txt")
m.mv("source.txt", "target.txt")
m.touch("source2.txt")
m.mv("source2.txt", "target2.txt", recursive=True)
assert m.find("") == ["/target.txt", "/target2.txt"]
def test_rm_reursive_empty_subdir(m):
# https://github.com/fsspec/filesystem_spec/issues/500
m.mkdir("recdir")
m.mkdir("recdir/subdir2")
m.rm("recdir/", recursive=True)
assert not m.exists("dir")
def test_seekable(m):
fn0 = "foo.txt"
with m.open(fn0, "wb") as f:
f.write(b"data")
f = m.open(fn0, "rt")
assert f.seekable(), "file is not seekable"
f.seek(1)
assert f.read(1) == "a"
assert f.tell() == 2
# https://github.com/fsspec/filesystem_spec/issues/1425
@pytest.mark.parametrize("mode", ["r", "rb", "w", "wb", "ab", "r+b"])
def test_open_mode(m, mode):
filename = "mode.txt"
m.touch(filename)
with m.open(filename, mode=mode) as _:
pass
def test_remove_all(m):
m.touch("afile")
m.rm("/", recursive=True)
assert not m.ls("/")
def test_cp_directory_recursive(m):
# https://github.com/fsspec/filesystem_spec/issues/1062
# Recursive cp/get/put of source directory into non-existent target directory.
src = "/src"
src_file = src + "/file"
m.mkdir(src)
m.touch(src_file)
target = "/target"
# cp without slash
assert not m.exists(target)
for loop in range(2):
m.cp(src, target, recursive=True)
assert m.isdir(target)
if loop == 0:
correct = [target + "/file"]
assert m.find(target) == correct
else:
correct = [target + "/file", target + "/src/file"]
assert sorted(m.find(target)) == correct
m.rm(target, recursive=True)
# cp with slash
assert not m.exists(target)
for loop in range(2):
m.cp(src + "/", target, recursive=True)
assert m.isdir(target)
correct = [target + "/file"]
assert m.find(target) == correct
def test_get_directory_recursive(m, tmpdir):
# https://github.com/fsspec/filesystem_spec/issues/1062
# Recursive cp/get/put of source directory into non-existent target directory.
src = "/src"
src_file = src + "/file"
m.mkdir(src)
m.touch(src_file)
target = os.path.join(tmpdir, "target")
target_fs = LocalFileSystem()
# get without slash
assert not target_fs.exists(target)
for loop in range(2):
m.get(src, target, recursive=True)
assert target_fs.isdir(target)
if loop == 0:
correct = [make_path_posix(os.path.join(target, "file"))]
assert target_fs.find(target) == correct
else:
correct = [
make_path_posix(os.path.join(target, "file")),
make_path_posix(os.path.join(target, "src", "file")),
]
assert sorted(target_fs.find(target)) == correct
target_fs.rm(target, recursive=True)
# get with slash
assert not target_fs.exists(target)
for loop in range(2):
m.get(src + "/", target, recursive=True)
assert target_fs.isdir(target)
correct = [make_path_posix(os.path.join(target, "file"))]
assert target_fs.find(target) == correct
def test_put_directory_recursive(m, tmpdir):
# https://github.com/fsspec/filesystem_spec/issues/1062
# Recursive cp/get/put of source directory into non-existent target directory.
src = os.path.join(tmpdir, "src")
src_file = os.path.join(src, "file")
source_fs = LocalFileSystem()
source_fs.mkdir(src)
source_fs.touch(src_file)
target = "/target"
# put without slash
assert not m.exists(target)
for loop in range(2):
m.put(src, target, recursive=True)
assert m.isdir(target)
if loop == 0:
correct = [target + "/file"]
assert m.find(target) == correct
else:
correct = [target + "/file", target + "/src/file"]
assert sorted(m.find(target)) == correct
m.rm(target, recursive=True)
# put with slash
assert not m.exists(target)
for loop in range(2):
m.put(src + "/", target, recursive=True)
assert m.isdir(target)
correct = [target + "/file"]
assert m.find(target) == correct
def test_cp_empty_directory(m):
# https://github.com/fsspec/filesystem_spec/issues/1198
# cp/get/put of empty directory.
empty = "/src/empty"
m.mkdir(empty)
target = "/target"
m.mkdir(target)
# cp without slash, target directory exists
assert m.isdir(target)
m.cp(empty, target)
assert m.find(target, withdirs=True) == [target]
# cp with slash, target directory exists
assert m.isdir(target)
m.cp(empty + "/", target)
assert m.find(target, withdirs=True) == [target]
m.rmdir(target)
# cp without slash, target directory doesn't exist
assert not m.isdir(target)
m.cp(empty, target)
assert not m.isdir(target)
# cp with slash, target directory doesn't exist
assert not m.isdir(target)
m.cp(empty + "/", target)
assert not m.isdir(target)
def test_cp_two_files(m):
src = "/src"
file0 = src + "/file0"
file1 = src + "/file1"
m.mkdir(src)
m.touch(file0)
m.touch(file1)
target = "/target"
assert not m.exists(target)
m.cp([file0, file1], target)
assert m.isdir(target)
assert sorted(m.find(target)) == [
"/target/file0",
"/target/file1",
]
def test_open_path_posix(m):
path = PurePosixPath("/myfile/foo/bar")
with m.open(path, "wb") as f:
f.write(b"some\nlines\nof\ntext")
assert m.read_text(path) == "some\nlines\nof\ntext"
def test_open_path_windows(m):
path = PureWindowsPath("C:\\myfile\\foo\\bar")
with m.open(path, "wb") as f:
f.write(b"some\nlines\nof\ntext")
assert m.read_text(path) == "some\nlines\nof\ntext"
|