Skip to content

Commit

Permalink
Add support for 'gs' as alias for 'gcs' in pytest servers (#182)
Browse files Browse the repository at this point in the history
The commit extends pytest_servers to recognize 'gs' as an alias for 'gcs'. This allows users to use 'gs' as a parameter in pytest. Corresponding test cases have also been updated to reflect this change.
  • Loading branch information
amritghimire authored Jun 17, 2024
1 parent 21b6fbe commit e42491d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ The `tmp_upath` fixture can be used for parametrizing paths with pytest's indire

.. code:: python
@pytest.mark.parametrize("tmp_upath", ["local", "s3", "gcs"], indirect=True)
@pytest.mark.parametrize("tmp_upath", ["local", "s3", "gcs", "gs"], indirect=True)
def test_something(tmp_upath):
pass
Expand Down
44 changes: 43 additions & 1 deletion src/pytest_servers/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class TempUPathFactory:
"_gcs_endpoint_url",
requires_docker=True,
),
"gs": MockRemote(
"fake_gcs_server",
"_gcs_endpoint_url",
requires_docker=True,
),
"s3": MockRemote(
"s3_server",
"_s3_client_kwargs",
Expand Down Expand Up @@ -103,6 +108,7 @@ def mktemp( # noqa: C901 # complex-structure
- s3
- azure
- gcs
- gs (alias for gcs)
:param mock:
Set to False to use real remotes
Expand Down Expand Up @@ -146,13 +152,20 @@ def mktemp( # noqa: C901 # complex-structure
msg = "missing connection string"
raise RemoteUnavailable(msg)
return self.azure(connection_string=self._azure_connection_string, **kwargs)

if fs == "gcs":
return self.gcs(
endpoint_url=self._gcs_endpoint_url,
version_aware=version_aware,
**kwargs,
)

if fs == "gs":
return self.gs(
endpoint_url=self._gcs_endpoint_url,
version_aware=version_aware,
**kwargs,
)
raise ValueError(fs)

def local(self) -> LocalPath:
Expand Down Expand Up @@ -237,12 +250,41 @@ def memory(
path.mkdir()
return path

def gs(
self,
endpoint_url: str | None = None,
*,
version_aware: bool = False,
**kwargs,
) -> UPath:
return self._gs(
"gs",
endpoint_url=endpoint_url,
version_aware=version_aware,
**kwargs,
)

def gcs(
self,
endpoint_url: str | None = None,
*,
version_aware: bool = False,
**kwargs,
) -> UPath:
return self._gs(
"gcs",
endpoint_url=endpoint_url,
version_aware=version_aware,
**kwargs,
)

def _gs(
self,
scheme: str,
endpoint_url: str | None = None,
*,
version_aware: bool = False,
**kwargs,
) -> UPath:
"""Create a new gcs bucket and return an UPath instance.
Expand All @@ -256,7 +298,7 @@ def gcs(
bucket_name = f"pytest-servers-{random_string()}"

path = UPath(
f"gcs://{bucket_name}",
f"{scheme}://{bucket_name}",
version_aware=version_aware,
**client_kwargs,
**kwargs,
Expand Down
2 changes: 2 additions & 0 deletions src/pytest_servers/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,7 @@ def tmp_upath(
return tmp_upath_factory.mktemp("azure")
if param == "gcs":
return tmp_upath_factory.mktemp("gcs", version_aware=version_aware)
if param == "gs":
return tmp_upath_factory.mktemp("gs", version_aware=version_aware)
msg = f"unknown {param=}"
raise ValueError(msg)
8 changes: 6 additions & 2 deletions tests/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@
"gcs",
upath.implementations.cloud.GCSPath,
),
pytest.param(
"gs",
upath.implementations.cloud.GCSPath,
),
]


with_versioning = [
param for param in implementations if param.values[0] in ("s3", "gcs")
param for param in implementations if param.values[0] in ("s3", "gcs", "gs")
]


Expand All @@ -43,7 +47,7 @@
)
class TestTmpUPathFactory:
def test_init(self, tmp_upath_factory, fs, cls):
if fs == "gcs":
if fs in ("gcs", "gs"):
pytest.skip("gcsfs does not support .exists() on a bucket")
path = tmp_upath_factory.mktemp(fs)
assert isinstance(path, cls)
Expand Down

0 comments on commit e42491d

Please sign in to comment.