Skip to content

Commit

Permalink
Test invalid file
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnoe committed Nov 27, 2023
1 parent 2e7d1e0 commit 8ade117
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
14 changes: 9 additions & 5 deletions corsikaio/longitudinal.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,18 @@ def read_longitudinal_distributions(path):
"""
first = True
with open(path, "r") as f:
while line := f.readline().strip():
try:
line = f.readline().strip()
except UnicodeDecodeError:
raise IOError(f"Inputfile {path} does not seem to be a longitudinal file")

while line:
match = PARTICLE_HEADER_RE.match(line)
if not match:
if first:
raise ValueError(f"Inputfile {path} does not seem to be a longitudinal file")
raise IOError(f"Inputfile {path} does not seem to be a longitudinal file")

Check warning on line 71 in corsikaio/longitudinal.py

View check run for this annotation

Codecov / codecov/patch

corsikaio/longitudinal.py#L70-L71

Added lines #L70 - L71 were not covered by tests
else:
raise ValueError(f"Error reading file, expected header line, got: {line}")
raise IOError(f"Error reading file, expected header line, got: {line}")

Check warning on line 73 in corsikaio/longitudinal.py

View check run for this annotation

Codecov / codecov/patch

corsikaio/longitudinal.py#L73

Added line #L73 was not covered by tests
first = False

n_steps = int(match.group(1))
Expand All @@ -86,7 +90,7 @@ def read_longitudinal_distributions(path):
line = f.readline().strip()
match = ENERGY_HEADER_RE.match(line)
if not match:
raise ValueError(f"Error reading file, expected energy deposition header line, got: {line}")
raise IOError(f"Error reading file, expected energy deposition header line, got: {line}")

Check warning on line 93 in corsikaio/longitudinal.py

View check run for this annotation

Codecov / codecov/patch

corsikaio/longitudinal.py#L93

Added line #L93 was not covered by tests
n_steps = int(match.group(1))

# skip
Expand All @@ -106,5 +110,5 @@ def read_longitudinal_distributions(path):
longi["average_deviation"] = float(deviation)

f.readline()

yield longi
line = f.readline().strip()
7 changes: 7 additions & 0 deletions tests/test_longitudinal.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,10 @@ def test_longitudinal(slant):
assert n_showers == 5
else:
assert n_showers == 1


def test_invalid(tmp_path):
from corsikaio import read_longitudinal_distributions

with pytest.raises(IOError, match="does not seem to be"):
next(read_longitudinal_distributions("tests/resources/corsika_77500_particle"))

0 comments on commit 8ade117

Please sign in to comment.