def test_cli_rm_file(with_adapter, bucket: str): source = f"gs://{bucket}/cli_rm_file/file.txt" path = Pathy(source) path.write_text("---") assert path.exists() assert runner.invoke(app, ["rm", source]).exit_code == 0 assert not path.exists()
def test_api_unlink_path(with_adapter: str, bucket: str) -> None: path = Pathy(f"gs://{bucket}/unlink/404.txt") with pytest.raises(FileNotFoundError): path.unlink() path = Pathy(f"gs://{bucket}/unlink/foo.txt") path.write_text("---") assert path.exists() path.unlink() assert not path.exists()
def test_api_replace_folders_in_bucket(with_adapter: str, bucket: str) -> None: # Rename a folder in the same bucket Pathy(f"gs://{bucket}/replace/folder/one.txt").write_text("---") Pathy(f"gs://{bucket}/replace/folder/two.txt").write_text("---") path = Pathy(f"gs://{bucket}/replace/folder/") new_path = Pathy(f"gs://{bucket}/replace/other/") path.replace(new_path) assert not path.exists() assert new_path.exists() assert Pathy(f"gs://{bucket}/replace/other/one.txt").is_file() assert Pathy(f"gs://{bucket}/replace/other/two.txt").is_file()
def test_api_rename_folders_across_buckets(with_adapter: str, bucket: str, other_bucket: str) -> None: # Rename a folder across buckets Pathy(f"gs://{bucket}/rename/folder/one.txt").write_text("---") Pathy(f"gs://{bucket}/rename/folder/two.txt").write_text("---") path = Pathy(f"gs://{bucket}/rename/folder/") new_path = Pathy(f"gs://{other_bucket}/rename/other/") path.rename(new_path) assert not path.exists() assert new_path.exists() assert Pathy(f"gs://{other_bucket}/rename/other/one.txt").is_file() assert Pathy(f"gs://{other_bucket}/rename/other/two.txt").is_file()
def test_api_rmdir(with_adapter: str, bucket: str) -> None: Pathy(f"gs://{bucket}/rmdir/one.txt").write_text("---") Pathy(f"gs://{bucket}/rmdir/folder/two.txt").write_text("---") path = Pathy(f"gs://{bucket}/rmdir/") path.rmdir() assert not Pathy(f"gs://{bucket}/rmdir/one.txt").is_file() assert not Pathy(f"gs://{bucket}/rmdir/other/two.txt").is_file() assert not path.exists()
def test_api_exists(with_adapter: str, bucket: str) -> None: path = Pathy("./fake-key") with pytest.raises(ValueError): path.exists() # GCS buckets are globally unique, "test-bucket" exists so this # raises an access error. assert Pathy("gs://test-bucket/fake-key").exists() is False # invalid bucket name assert Pathy("gs://unknown-bucket-name-123987519875419").exists() is False # valid bucket with invalid object assert Pathy(f"gs://{bucket}/not_found_lol_nice.txt").exists() is False path = Pathy(f"gs://{bucket}/directory/foo.txt") path.write_text("---") assert path.exists() for parent in path.parents: assert parent.exists()
def test_api_mkdir(with_adapter: str, bucket: str) -> None: bucket_name = f"pathy-e2e-test-{uuid4().hex}" # Create a bucket path = Pathy(f"gs://{bucket_name}/") path.mkdir() assert path.exists() # Does not assert if it already exists path.mkdir(exist_ok=True) with pytest.raises(FileExistsError): path.mkdir(exist_ok=False) # with pytest.raises(FileNotFoundError): # Pathy("/test-second-bucket/test-directory/file.name").mkdir() # Pathy("/test-second-bucket/test-directory/file.name").mkdir(parents=True) assert path.exists() # remove the bucket # client = storage.Client() # bucket = client.lookup_bucket(bucket_name) # bucket.delete() path.rmdir() assert not path.exists()
def test_api_rglob_unlink(with_adapter: str, bucket: str) -> None: files = [f"gs://{bucket}/rglob_and_unlink/{i}.file.txt" for i in range(3)] for file in files: Pathy(file).write_text("---") path = Pathy(f"gs://{bucket}/rglob_and_unlink/") for blob in path.rglob("*"): blob.unlink() # All the files are gone for file in files: assert Pathy(file).exists() is False # The folder is gone assert not path.exists()
def test_cli_rm_invalid_file(with_adapter: str, bucket: str) -> None: source = f"gs://{bucket}/{ENV_ID}/cli_rm_file_invalid/file.txt" path = Pathy(source) assert not path.exists() assert runner.invoke(app, ["rm", source]).exit_code == 1