def test_pipe_cat(): fs = MemoryFileSystem() fs.pipe("afile", b"contents") assert fs.cat("afile") == b"contents" data = {"bfile": b"more", "cfile": b"stuff"} fs.pipe(data) assert fs.cat(list(data)) == data
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_read_block_delimiter(): fs = MemoryFileSystem() with fs.open("/myfile", "wb") as f: f.write(b"some\n" b"lines\n" b"of\n" b"text") assert fs.read_block("/myfile", 0, 2, b"\n") == b"some\n" assert fs.read_block("/myfile", 2, 6, b"\n") == b"lines\n" assert fs.read_block("/myfile", 6, 2, b"\n") == b"" assert fs.read_block("/myfile", 2, 9, b"\n") == b"lines\nof\n" assert fs.read_block("/myfile", 12, 6, b"\n") == b"text" assert fs.read_block("/myfile", 0, None) == fs.cat("/myfile")
def test_read_block_delimiter(): fs = MemoryFileSystem() with fs.open('/myfile', 'wb') as f: f.write(b'some\n' b'lines\n' b'of\n' b'text') assert fs.read_block('/myfile', 0, 2, b'\n') == b'some\n' assert fs.read_block('/myfile', 2, 6, b'\n') == b'lines\n' assert fs.read_block('/myfile', 6, 2, b'\n') == b'' assert fs.read_block('/myfile', 2, 9, b'\n') == b'lines\nof\n' assert fs.read_block('/myfile', 12, 6, b'\n') == b'text' assert fs.read_block('/myfile', 0, None) == fs.cat('/myfile')
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"