Skip to content

Commit

Permalink
Merge pull request #9136 from gem/check-extract-newlines
Browse files Browse the repository at this point in the history
Add a test checking newlines in extracted ruptures
  • Loading branch information
micheles committed Oct 23, 2023
2 parents a6458a3 + a5c0d8f commit bda66a4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
8 changes: 7 additions & 1 deletion openquake/baselib/tests/flake8_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ def get_long_funcs(mod_or_pkg, maxlen):
@numba.njit
def check_newlines(bytes):
"""
:returns: 0 if the newlines are \r\n, 1 for \n and 2 for \r
:returns: 0 if the newlines are \r\n, 1 for \n, 2 for \r and 3 for two
consecutive \r
"""
n1 = len(bytes) - 1
for i, byte in enumerate(bytes):
Expand All @@ -84,6 +85,8 @@ def check_newlines(bytes):
elif byte == CR:
if (i < n1 and bytes[i+1] != LF) or i == n1:
return 2 # \r ending
if i > 0 and bytes[i-1] == CR:
return 3
return 0


Expand Down Expand Up @@ -155,6 +158,9 @@ def test_csv(OVERWRITE=False):
raise ValueError('Found \\n line ending in %s' % fname)
elif error == 2:
raise ValueError('Found \\r line ending in %s' % fname)
elif error == 3:
raise ValueError('Found two consecutive \\r line endings'
' in %s' % fname)


def test_forbid_long_funcs():
Expand Down
23 changes: 22 additions & 1 deletion openquake/commands/tests/commands_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from openquake.baselib.general import gettemp, chdir
from openquake.baselib import parallel, sap
from openquake.baselib.hdf5 import read_csv
from openquake.baselib.tests.flake8_test import check_newlines
from openquake.hazardlib import tests
from openquake import commonlib
from openquake.commonlib.datastore import read
Expand All @@ -46,7 +47,8 @@
from openquake.qa_tests_data.classical import case_01, case_18
from openquake.qa_tests_data.classical_risk import case_3
from openquake.qa_tests_data.scenario import case_4
from openquake.qa_tests_data.event_based import case_5, case_16, case_21
from openquake.qa_tests_data.event_based import (
case_1 as eb_case_1, case_5, case_16, case_21)
from openquake.qa_tests_data.event_based_risk import (
case_master, case_1 as case_eb)
from openquake.qa_tests_data.scenario import case_25
Expand Down Expand Up @@ -308,6 +310,25 @@ def test_extract_sitecol(self):
self.assertIn(str(fnames[0]), str(p))
shutil.rmtree(tempdir)

def test_extract_ruptures(self):
job_ini = os.path.join(
os.path.dirname(eb_case_1.__file__), 'job_ruptures.ini')
with Print.patch() as p:
calc = sap.runline(f'openquake.commands run {job_ini} -c 0')
calc_id = calc.datastore.calc_id
tempdir = tempfile.mkdtemp()
with Print.patch() as p:
sap.runline("openquake.commands extract ruptures "
f"{calc_id} --extract-dir={tempdir}")
fnames = os.listdir(tempdir)
fname = os.path.join(tempdir, fnames[0])
error = check_newlines(open(fname, 'rb').read())
if error:
raise ValueError(
f'Invalid newlines in the exported ruptures file {fname}')
else:
shutil.rmtree(tempdir)


class CompareTestCase(unittest.TestCase):
def test_med_gmv(self):
Expand Down

0 comments on commit bda66a4

Please sign in to comment.