From f2d92365653421c0ebc3151afcccf8d1406dbe1a Mon Sep 17 00:00:00 2001 From: Sandor Kertesz Date: Fri, 23 Feb 2024 14:44:20 +0000 Subject: [PATCH] from_source should fail when try to load empty file --- earthkit/data/readers/__init__.py | 6 ++++++ tests/data/empty_file.grib | 0 tests/readers/test_empty_file.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 tests/data/empty_file.grib create mode 100644 tests/readers/test_empty_file.py diff --git a/earthkit/data/readers/__init__.py b/earthkit/data/readers/__init__.py index 5871b999..51a19b21 100644 --- a/earthkit/data/readers/__init__.py +++ b/earthkit/data/readers/__init__.py @@ -169,6 +169,12 @@ def reader(source, path, **kwargs): return DirectoryReader(source, path).mutate() LOG.debug("Reader for %s", path) + if not os.path.exists(path): + raise FileExistsError(f"No such file exists: '{path}'") + + if os.path.getsize(path) == 0: + raise Exception(f"File is empty: '{path}'") + n_bytes = SETTINGS.get("reader-type-check-bytes") with open(path, "rb") as f: magic = f.read(n_bytes) diff --git a/tests/data/empty_file.grib b/tests/data/empty_file.grib new file mode 100644 index 00000000..e69de29b diff --git a/tests/readers/test_empty_file.py b/tests/readers/test_empty_file.py new file mode 100644 index 00000000..4895cd1f --- /dev/null +++ b/tests/readers/test_empty_file.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +# (C) Copyright 2020 ECMWF. +# +# This software is licensed under the terms of the Apache Licence Version 2.0 +# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. +# In applying this licence, ECMWF does not waive the privileges and immunities +# granted to it by virtue of its status as an intergovernmental organisation +# nor does it submit to any jurisdiction. +# + +import pytest + +import earthkit.data +from earthkit.data.testing import earthkit_test_data_file + + +def test_empty_file_reader(): + with pytest.raises(Exception): + earthkit.data.from_source("file", earthkit_test_data_file("empty_file.grib")) + + +def test_nonexisting_file_reader(): + with pytest.raises(FileExistsError): + earthkit.data.from_source("file", "__nonexistingfile__") + + +if __name__ == "__main__": + from earthkit.data.testing import main + + main(__file__)