From 2d941e011be552fcb1bda222227b5bbd0b91a308 Mon Sep 17 00:00:00 2001 From: Etienne Pot Date: Wed, 16 Dec 2020 06:31:02 -0800 Subject: [PATCH] Fix `path.touch()` when file already exists PiperOrigin-RevId: 347811114 --- tensorflow_datasets/core/utils/gpath_test.py | 24 +++++++++++++++++++- tensorflow_datasets/core/utils/type_utils.py | 7 ++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/tensorflow_datasets/core/utils/gpath_test.py b/tensorflow_datasets/core/utils/gpath_test.py index 6609cd7c4f6..980fcfd1ffb 100644 --- a/tensorflow_datasets/core/utils/gpath_test.py +++ b/tensorflow_datasets/core/utils/gpath_test.py @@ -211,7 +211,29 @@ def test_open(gcs_mocked_path: pathlib.Path): assert len(list(gcs_mocked_path.joinpath('bucket/dataset').iterdir())) == 6 -def test_read_write(gcs_mocked_path: pathlib.Path): # pylint: disable=unused-argument +@pytest.mark.usefixtures('gcs_mocked_path') +def test_touch(): + root_path = gpathlib.PosixGPath('gs://bucket/') + root_path.mkdir(parents=True) + assert root_path.exists() + + # File don't exists, touch create it + file_path = root_path / 'test.txt' + assert not file_path.exists() + file_path.touch() + assert file_path.exists() + assert file_path.read_text() == '' # File content is empty # pylint: disable=g-explicit-bool-comparison + + file_path.write_text('Some content') + file_path.touch() # Should be a no-op + assert file_path.read_text() == 'Some content' # Content still exists + + with pytest.raises(FileExistsError): + file_path.touch(exist_ok=False) + + +@pytest.mark.usefixtures('gcs_mocked_path') +def test_read_write(): gpath = gpathlib.PosixGPath('gs://file.txt') diff --git a/tensorflow_datasets/core/utils/type_utils.py b/tensorflow_datasets/core/utils/type_utils.py index 03cca2b476e..087ff4e900e 100644 --- a/tensorflow_datasets/core/utils/type_utils.py +++ b/tensorflow_datasets/core/utils/type_utils.py @@ -213,8 +213,11 @@ def write_text( def touch(self, mode: int = 0o666, exist_ok: bool = True) -> None: """Create a file at this given path.""" del mode # Unused - if self.exists() and not exist_ok: - raise FileExistsError(f'{self} already exists.') + if self.exists(): + if exist_ok: + return + else: + raise FileExistsError(f'{self} already exists.') self.write_text('') @abc.abstractmethod