def test_open(gcsbucket, rmode, wmode, file_cls, expect): p = GCSPath(f"{gcsbucket.root}/file") with p.open(wmode) as fw: assert isinstance(fw, file_cls) fw.write(expect) with p.open(rmode) as fr: assert isinstance(fr, file_cls) assert fr.read() == expect
def test_touch(gcsbucket, contents, key, expect): for k, v in contents.items(): gcsbucket.put(k, v) GCSPath(f"{gcsbucket.root}/{key}").touch(exist_ok=False) for k, v in expect.items(): assert gcsbucket.get(k).download_as_string().decode("utf-8") == v
def test_touch_exist_ok(gcsbucket, contents): originals = {} for k, v in contents.items(): gcsbucket.put(k, v) originals[k] = gcsbucket.get(k) time.sleep(1) for k, v in contents.items(): GCSPath(f"{gcsbucket.root}/{k}").touch(exist_ok=True) for k, org in originals.items(): touched = gcsbucket.get(k) assert org.etag == touched.etag assert org.updated < touched.updated
def test_iterdir(gcsbucket, keys, root, expect): for k in keys: gcsbucket.put(k) it = GCSPath(f"{gcsbucket.root}/{root}").iterdir() assert isinstance(it, collections.abc.Iterable) assert set(it) == {GCSPath(f"{gcsbucket.root}/{p}") for p in expect}
def test_read_write_bytes(gcsbucket, expect): url = f"{gcsbucket.root}/file" GCSPath(url).write_bytes(expect) assert GCSPath(url).read_bytes() == expect
def test_path_predicate(api_name): assert getattr(GCSPath("gs://example/com"), api_name)() == False
def test_path_public_api_fail(api_name, args): getattr(GCSPath("gs://example/"), api_name)(*args)
def test_exists(gcsbucket, putstr, pathstr, expect): gcsbucket.put(putstr) assert GCSPath(f"{gcsbucket.root}{pathstr}").exists() == expect
def test_is_dir(gcsbucket, key, content, expect): gcsbucket.put(key, content) assert GCSPath(f"{gcsbucket.root}/{key}").is_dir() == expect
def test_mkdir_fail(gcsbucket, pathstr, expect): gcsbucket.put("parent/") with pytest.raises(expect): GCSPath(f"{gcsbucket.root}/{pathstr}").mkdir()
def test_mkdir(gcsbucket, key, args): gcsbucket.put("parent/") GCSPath(f"{gcsbucket.root}/{key}").mkdir(**args) assert gcsbucket.get(f"{key.rstrip('/')}/")
def test_touch_fail(gcsbucket, key, args, expect): gcsbucket.put("exist") with pytest.raises(expect): GCSPath(f"{gcsbucket.root}/{key}").touch(**args)
("key/", "", True), ("key/", "abc", False), ("key/file", "", False), ("key/dir/", "", True), ], ) def test_is_dir(gcsbucket, key, content, expect): gcsbucket.put(key, content) assert GCSPath(f"{gcsbucket.root}/{key}").is_dir() == expect @pytest.mark.parametrize( ["api_name", "args"], [ ("home", []), ("samefile", [GCSPath("gs://other/abc")]), ("glob", ["*.py"]), ("rglob", ["*.py"]), ("absolute", []), pytest.param( "stat", [], marks=pytest.mark.skipif( (3, 10) <= sys.version_info, reason="python 3.10 require stat implementation", ), ), ("owner", []), ("readlink", []), ("chmod", [0x666]), ("lchmod", [0x666]),
from google.auth.credentials import AnonymousCredentials from google.cloud import storage from paaaaath import GCSPath, Path OUTPUT_BUCKET = "" # fill output bucket name GCSPath.register_client( storage.Client( credentials=AnonymousCredentials(), client_options={ "api_endpoint": "http://127.0.0.1:4443", # use local gcs emulator }, ) ) def main(): p = Path("gs://local_test/abc") text = "abc" p.write_text(text) print(f"write text:{text}") print(f"read text:{p.read_text()}") if __name__ == "__main__": main()