Skip to content

Commit 62ae9da

Browse files
author
Steve Goldhaber
committed
Add unit tests for SDF reading
Fix xmllint checking (some xmllint versions do not return error codes) Update regex usage for fortran_conditional
1 parent e0d375b commit 62ae9da

35 files changed

Lines changed: 947 additions & 12 deletions

scripts/parse_tools/fortran_conditional.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
import re
99

10-
FORTRAN_CONDITIONAL_REGEX_WORDS = [' ', '(', ')', '==', '/=', '<=', '>=', '<', '>', '.eqv.', '.neqv.',
10+
fortran_conditional_regex_tokens = ['==', '/=', '<=', '>=', '<', '>', '.eqv.', '.neqv.',
1111
'.true.', '.false.', '.lt.', '.le.', '.eq.', '.ge.', '.gt.', '.ne.',
1212
'.not.', '.and.', '.or.', '.xor.']
13-
FORTRAN_CONDITIONAL_REGEX = re.compile(r"[\w']+|" + "|".join([word.replace('(','\(').replace(')', '\)') for word in FORTRAN_CONDITIONAL_REGEX_WORDS]))
13+
14+
FORTRAN_CONDITIONAL_REGEX_WORDS = [' ', '[(]', '[)]'] + fortran_conditional_regex_tokens
15+
16+
FORTRAN_CONDITIONAL_REGEX = re.compile(r"[\w']+|[ ()]|" + "|".join([word for word in FORTRAN_CONDITIONAL_REGEX_WORDS]))

scripts/parse_tools/parse_checkers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
########################################################################
1414

1515
_UNITLESS_REGEX = "1"
16-
_NON_LEADING_ZERO_NUM = "[1-9]\d*"
16+
_NON_LEADING_ZERO_NUM = r"[1-9]\d*"
1717
_CHAR_WITH_UNDERSCORE = "([a-zA-Z]+_[a-zA-Z]+)+"
1818
_NEGATIVE_NON_LEADING_ZERO_NUM = f"[-]{_NON_LEADING_ZERO_NUM}"
1919
_POSITIVE_NON_LEADING_ZERO_NUM = f"[+]{_NON_LEADING_ZERO_NUM}"
2020
_UNIT_EXPONENT = f"({_NEGATIVE_NON_LEADING_ZERO_NUM}|{_POSITIVE_NON_LEADING_ZERO_NUM}|{_NON_LEADING_ZERO_NUM})"
2121
_UNIT_REGEX = f"[a-zA-Z]+{_UNIT_EXPONENT}?"
22-
_UNITS_REGEX = f"^({_CHAR_WITH_UNDERSCORE}|{_UNIT_REGEX}(\s{_UNIT_REGEX})*|{_UNITLESS_REGEX})$"
22+
_UNIT_REGEXES = f"{_UNIT_REGEX}(" r"\s" f"{_UNIT_REGEX})"
23+
_UNITS_REGEX = f"^({_CHAR_WITH_UNDERSCORE}|{_UNIT_REGEXES}*|{_UNITLESS_REGEX})$"
2324
_UNITS_RE = re.compile(_UNITS_REGEX)
2425
_MAX_MOLAR_MASS = 10000.0
2526

scripts/parse_tools/xml_tools.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,14 @@ def __init__(self, message):
3838
super().__init__(message)
3939

4040
###############################################################################
41-
def call_command(commands, logger, silent=False):
41+
def call_command(commands, logger, silent=False, return_proc=False):
4242
###############################################################################
4343
"""
44-
Try a command line and return the output on success (None on failure)
44+
Try a command line and return True only for a zero return code
45+
If silent==True, do not log output and simply return False on an exception
46+
If return_proc==True, return the CompletedProcess instance instead of a boolean
47+
If silent==True and return_proc==True, return None in the case of an exception
48+
4549
>>> _LOGGER = init_log('xml_tools')
4650
>>> set_log_to_null(_LOGGER)
4751
>>> call_command(['ls', 'really__improbable_fffilename.foo'], _LOGGER) #doctest: +IGNORE_EXCEPTION_DETAIL
@@ -50,6 +54,8 @@ def call_command(commands, logger, silent=False):
5054
[Errno 2] No such file or directory
5155
>>> call_command(['ls', 'really__improbable_fffilename.foo'], _LOGGER, silent=True)
5256
False
57+
>>> call_command(['ls'], _LOGGER, silent=True, return_proc=True).returncode
58+
0
5359
>>> call_command(['ls'], _LOGGER)
5460
True
5561
>>> try:
@@ -75,11 +81,22 @@ def call_command(commands, logger, silent=False):
7581
capture_output=True)
7682
if not silent:
7783
logger.debug(cproc.stdout)
84+
if cproc.stderr:
85+
logger.warning(cproc.stderr)
86+
# end if
87+
# end if
88+
if return_proc:
89+
result = cproc
90+
else:
91+
result = cproc.returncode == 0
7892
# end if
79-
result = cproc.returncode == 0
8093
except (OSError, CCPPError, subprocess.CalledProcessError) as err:
8194
if silent:
82-
result = False
95+
if return_proc:
96+
result = None
97+
else:
98+
result = False
99+
# end if
83100
else:
84101
cmd = ' '.join(commands)
85102
outstr = f"Execution of '{cmd}' failed with code: {err.returncode}\n"
@@ -213,7 +230,15 @@ def validate_xml_file(filename, schema_root, version, logger,
213230
logger.debug("Checking file {} against schema {}".format(filename,
214231
schema_file))
215232
cmd = [_XMLLINT, '--noout', '--schema', schema_file, filename]
216-
result = call_command(cmd, logger)
233+
result = call_command(cmd, logger, return_proc=True)
234+
if result.returncode == 0:
235+
## We got a pass return code but some versions of xmllint do not
236+
## correctly return an error code on non-validation so double check
237+
## the result
238+
result = b'validates' in result.stdout or b'validates' in result.stderr
239+
else:
240+
result = True
241+
# end if
217242
return result
218243
# end if
219244
lmsg = "xmllint not found, could not validate file {}"
@@ -411,11 +436,14 @@ def replace_nested_suite(element, nested_suite, default_path, logger):
411436
suite_name = nested_suite.attrib.get("name")
412437
group_name = nested_suite.attrib.get("group")
413438
file = nested_suite.attrib.get("file")
439+
if not file:
440+
raise CCPPError("file attribute required for nested_suite tag")
441+
# end if
414442
if not os.path.isabs(file):
415443
file = os.path.join(default_path, file)
416444
referenced_suite = load_suite_by_name(suite_name, group_name, file,
417445
logger=logger)
418-
imported_content = [ET.fromstring(ET.tostring(child))
446+
imported_content = [ET.fromstring(ET.tostring(child))
419447
for child in referenced_suite]
420448
# Swap nested suite with imported content
421449
for item in imported_content:
@@ -581,7 +609,7 @@ def expand_nested_suites(suite, default_path, logger=None):
581609
return
582610
raise CCPPError("Exceeded number of iterations while expanding nested suites:" + \
583611
"check for inifite recursion or adjust limit max_iterations")
584-
612+
585613
###############################################################################
586614
def write_xml_file(root, file_path, logger=None):
587615
###############################################################################
@@ -598,7 +626,7 @@ def remove_whitespace_nodes(node):
598626

599627
# Convert ElementTree to a byte string
600628
byte_string = ET.tostring(root, 'us-ascii')
601-
629+
602630
# Parse string using minidom for pretty printing
603631
reparsed = xml.dom.minidom.parseString(byte_string)
604632

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<suite name="another_suite" version="2.0">
4+
<group name="another_group">
5+
<subcycle loop="num_subcycles_for_ag">
6+
<scheme>another_scheme</scheme>
7+
</subcycle>
8+
<scheme>more_scheme</scheme>
9+
</group>
10+
</suite>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<suite name="another_suite2" version="2.0">
4+
<group name="another_group2">
5+
<subcycle loop="num_subcycles_for_ag2">
6+
<scheme>another_scheme</scheme>
7+
</subcycle>
8+
<scheme>more_scheme</scheme>
9+
</group>
10+
<group name="another_group3">
11+
<subcycle loop="num_subcycles_for_ag3">
12+
<scheme>another_scheme</scheme>
13+
</subcycle>
14+
<scheme>more_scheme</scheme>
15+
</group>
16+
</suite>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<suite name="nested_suite" version="2.0" >
4+
<group name="nested_group1">
5+
<scheme>g1_scheme1</scheme>
6+
</group>
7+
<group name="nested_group2">
8+
<nested_suite name="subsuite_1" group="group1" file="subsuite1.xml"/>
9+
</group>
10+
</suite>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<suite name="subsuite_1" version="2.0">
4+
<group name="group1">
5+
<scheme>scheme_subsuite1</scheme>
6+
</group>
7+
</suite>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<suite name="subsuite_inline" version="1.0">
4+
<group name="group2">
5+
<scheme>scheme1i</scheme>
6+
<scheme>scheme2i</scheme>
7+
<scheme>scheme1i</scheme>
8+
</group>
9+
</suite>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<suite name="main_suite" version="2.0">
4+
<group name="group1">
5+
<subcycle loop="num_subcycles_for_scheme9">
6+
<scheme>effr_pre</scheme>
7+
<subcycle loop="2">
8+
<subcycle loop="2">
9+
<scheme>scheme9</scheme>
10+
</subcycle>
11+
</subcycle>
12+
<scheme>scheme3</scheme>
13+
</subcycle>
14+
</group>
15+
<nested_suite name="subsuite_1" file="subsuite1.xml"/>
16+
</suite>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<suite name="ver_test_suite" version="2.0">
4+
<group name="group1">
5+
<suite>subsuite_inline</suite>
6+
</group>
7+
</suite>

0 commit comments

Comments
 (0)