diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 5209aa1..1445eed 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -20,6 +20,13 @@ Possible types of changes are: Unreleased ---------- +0.4.1 - 18.12.2018 +------------------ + +Fixed +''''' +- Fixed a bug where ``create=True`` in combination with an "empty-ish ``root_path`` like ``""``, ``"."`` or ``"/"`` would create a directory marker. + 0.4.0 - 11.12.2018 ------------------ @@ -30,7 +37,6 @@ Added which means they will raise a ``CreateFailed`` exception if ``root_path`` does not exist - 0.3.0 - 20.11.2018 ------------------ diff --git a/fs_gcsfs/_gcsfs.py b/fs_gcsfs/_gcsfs.py index c87ac8a..f9647c9 100644 --- a/fs_gcsfs/_gcsfs.py +++ b/fs_gcsfs/_gcsfs.py @@ -78,13 +78,14 @@ def __init__(self, except google.api_core.exceptions.Forbidden as err: raise CreateFailed("You don't have access to the bucket \"{}\"".format(self._bucket_name)) from err - if create: - root_marker = self._get_blob(forcedir(root_path)) - if root_marker is None: - blob = self.bucket.blob(forcedir(root_path)) - blob.upload_from_string(b"") - elif strict and self._get_blob(forcedir(root_path)) is None: - raise errors.CreateFailed("Root path \"{}\" does not exist".format(root_path)) + if self._prefix != "": + if create: + root_marker = self._get_blob(self._prefix + GCSFS.DELIMITER) + if root_marker is None: + blob = self.bucket.blob(self._prefix + GCSFS.DELIMITER) + blob.upload_from_string(b"") + elif strict and self._get_blob(self._prefix + GCSFS.DELIMITER) is None: + raise errors.CreateFailed("Root path \"{}\" does not exist".format(root_path)) def __repr__(self) -> str: return _make_repr( diff --git a/fs_gcsfs/_version.py b/fs_gcsfs/_version.py index 6a9beea..3d26edf 100644 --- a/fs_gcsfs/_version.py +++ b/fs_gcsfs/_version.py @@ -1 +1 @@ -__version__ = "0.4.0" +__version__ = "0.4.1" diff --git a/fs_gcsfs/tests/test_gcsfs.py b/fs_gcsfs/tests/test_gcsfs.py index 776d9d8..ac461d6 100644 --- a/fs_gcsfs/tests/test_gcsfs.py +++ b/fs_gcsfs/tests/test_gcsfs.py @@ -117,6 +117,13 @@ def test_listdir_works_on_bucket_as_root_directory(client): assert directory in result +@pytest.mark.parametrize("root_path", ["", ".", "/"]) +def test_create_property_does_not_create_file_if_emptyish_root_path(root_path, client): + """Regression test for a bug fixed in 0.4.1""" + gcs_fs = GCSFS(bucket_name=TEST_BUCKET, root_path=root_path, client=client, create=True) + assert gcs_fs.bucket.get_blob(root_path + GCSFS.DELIMITER) is None + + def test_fix_storage_adds_binary_blobs_with_empty_string_as_directory_marker(bucket, tmp_gcsfs): # Creating a 'nested' hierarchy of blobs without directory marker for path in ["foo/test", "foo/bar/test", "foo/baz/test", "foo/bar/egg/test"]: