def test_get_put(tmpdir): tmpdir = str(tmpdir) fn = os.path.join(tmpdir, "one") open(fn, "wb").write(b"one") os.mkdir(os.path.join(tmpdir, "dir")) fn2 = os.path.join(tmpdir, "dir", "two") open(fn2, "wb").write(b"two") fs = MemoryFileSystem() fs.put(fn, "/afile") assert fs.cat("/afile") == b"one" fs.store["/bfile"] = MemoryFile(fs, "/bfile", b"data") fn3 = os.path.join(tmpdir, "three") fs.get("/bfile", fn3) assert open(fn3, "rb").read() == b"data" fs.put(tmpdir, "/more", recursive=True) assert fs.find("/more") == ["/more/dir/two", "/more/one", "/more/three"] for f in [fn, fn2, fn3]: os.remove(f) os.rmdir(os.path.join(tmpdir, "dir")) fs.get("/more/", tmpdir + "/", recursive=True) assert open(fn3, "rb").read() == b"data" assert open(fn, "rb").read() == b"one"
def test_get_put(tmpdir): tmpdir = str(tmpdir) fn = os.path.join(tmpdir, 'one') open(fn, 'wb').write(b'one') os.mkdir(os.path.join(tmpdir, 'dir')) fn2 = os.path.join(tmpdir, 'dir', 'two') open(fn2, 'wb').write(b'two') fs = MemoryFileSystem() fs.put(fn, '/afile') assert fs.cat('/afile') == b'one' fs.store['/bfile'] = MemoryFile(fs, '/bfile', b'data') fn3 = os.path.join(tmpdir, 'three') fs.get('/bfile', fn3) assert open(fn3, 'rb').read() == b'data' fs.put(tmpdir, '/more', recursive=True) assert fs.find('/more') == ['/more/dir/two', '/more/one', '/more/three'] for f in [fn, fn2, fn3]: os.remove(f) os.rmdir(os.path.join(tmpdir, 'dir')) fs.get('/more/', tmpdir + '/', recursive=True) assert open(fn3, 'rb').read() == b'data' assert open(fn, 'rb').read() == b'one'
def test_get_put(tmpdir): tmpdir = str(tmpdir) fn = os.path.join(tmpdir, "one") open(fn, "wb").write(b"one") os.mkdir(os.path.join(tmpdir, "dir")) fn2 = os.path.join(tmpdir, "dir", "two") open(fn2, "wb").write(b"two") fs = MemoryFileSystem() fs.put(fn, "/afile") assert fs.cat("/afile") == b"one" fs.store["/bfile"] = MemoryFile(fs, "/bfile", b"data") fn3 = os.path.join(tmpdir, "three") fs.get("/bfile", fn3) assert open(fn3, "rb").read() == b"data" fs.put(tmpdir, "/more", recursive=True) assert fs.find("/more") == ["/more/dir/two", "/more/one", "/more/three"] @contextlib.contextmanager def tmp_chdir(path): curdir = os.getcwd() os.chdir(path) try: yield finally: os.chdir(curdir) with tmp_chdir(os.path.join(tmpdir, os.path.pardir)): fs.put(os.path.basename(tmpdir), "/moretwo", recursive=True) assert fs.find("/moretwo") == [ "/moretwo/dir/two", "/moretwo/one", "/moretwo/three", ] with tmp_chdir(tmpdir): fs.put(os.path.curdir, "/morethree", recursive=True) assert fs.find("/morethree") == [ "/morethree/dir/two", "/morethree/one", "/morethree/three", ] for f in [fn, fn2, fn3]: os.remove(f) os.rmdir(os.path.join(tmpdir, "dir")) fs.get("/more/", tmpdir + "/", recursive=True) assert open(fn3, "rb").read() == b"data" assert open(fn, "rb").read() == b"one"
def test_recursive_get_put(tmpdir): fs = MemoryFileSystem() os.makedirs(f"{tmpdir}/nest") for file in ["one", "two", "nest/other"]: with open(f"{tmpdir}/{file}", "wb") as f: f.write(b"data") fs.put(str(tmpdir), "test", recursive=True) d = tempfile.mkdtemp() fs.get("test", d, recursive=True) for file in ["one", "two", "nest/other"]: with open(f"{d}/{file}", "rb") as f: f.read() == b"data"