Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

from_source should fail when try to load empty file #319

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions earthkit/data/readers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Empty file added tests/data/empty_file.grib
Empty file.
31 changes: 31 additions & 0 deletions tests/readers/test_empty_file.py
Original file line number Diff line number Diff line change
@@ -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__)
Loading