Skip to content

Commit be933f8

Browse files
committed
small test refactoring
1 parent 2b89eb4 commit be933f8

File tree

3 files changed

+41
-30
lines changed

3 files changed

+41
-30
lines changed

test/test_apps.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import platform
77
import unittest
88

9-
from .test_compatibility import all_fortran_paths
9+
from .test_compatibility import all_fortran_paths, TestsBase
1010

1111
_LOG = logging.getLogger(__name__)
1212

@@ -56,7 +56,7 @@
5656
'maprof.h', 'maprof_proc.h', 'maprof_yaml.h')]}
5757

5858

59-
class Tests(unittest.TestCase):
59+
class Tests(TestsBase):
6060

6161
maxDiff = None
6262

@@ -73,9 +73,8 @@ def _run_app_test(
7373
failure_reports_path = _HERE.joinpath('results', 'apps', app_dirname, 'failure' + _suffix)
7474
success_reports_path = _HERE.joinpath('results', 'apps', app_dirname, 'success' + _suffix)
7575

76-
from .test_compatibility import Tests as CompatibilityTests
77-
CompatibilityTests.check_cases_and_report(
78-
self, app_name, failure_reports_path, success_reports_path,
76+
self.check_cases_and_report(
77+
app_name, failure_reports_path, success_reports_path,
7978
_APPS_ROOT_PATHS[app_name], _APPS_CODE_FILEPATHS[app_name],
8079
minimum_passed_cases, fall_back_to_ofc)
8180

test/test_compatibility.py

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def all_fortran_paths(root_path: pathlib.Path):
3737
ALL_OFP_TEST_PATHS = all_fortran_paths(_OFP_TESTS_DIR)
3838

3939

40-
class Tests(unittest.TestCase):
40+
class TestsBase(unittest.TestCase):
4141

4242
maxDiff = None
4343

@@ -62,6 +62,34 @@ def _check_cases(self, cases, relative=True):
6262
self.check_cases_and_report(
6363
'OFP', failure_reports_path, success_reports_path, tests_absolute_path, cases)
6464

65+
def _check_case(self, input_path: pathlib.Path, fall_back_to_ofc: bool = False,
66+
ofc_target_path: pathlib.Path = None):
67+
result = None
68+
try:
69+
try:
70+
result = parse(input_path, verbosity=100, raise_on_error=True)
71+
self.assertIsNotNone(result)
72+
except subprocess.CalledProcessError as parser_err:
73+
if not fall_back_to_ofc:
74+
raise parser_err
75+
assert isinstance(ofc_target_path, pathlib.Path), ofc_target_path
76+
code = None
77+
try:
78+
code = transpile(input_path, raise_on_error=True)
79+
self.assertIsInstance(code, str)
80+
with ofc_target_path.open('w') as transpiled_file:
81+
transpiled_file.write(code)
82+
result = parse(ofc_target_path, verbosity=100, raise_on_error=True)
83+
self.assertIsNotNone(result)
84+
_LOG.info('OFC definitely fixed something, see %s', ofc_target_path)
85+
except subprocess.CalledProcessError as err3:
86+
if code is not None:
87+
_LOG.warning('OFC succeeded but parser failed %s', ofc_target_path)
88+
raise parser_err from err3
89+
except subprocess.CalledProcessError as err:
90+
result = err
91+
return result
92+
6593
def check_cases_and_report(
6694
self, scenario_name: str, failure_reports_path: pathlib.Path,
6795
success_reports_path: pathlib.Path, input_paths_root: pathlib.Path,
@@ -89,35 +117,13 @@ def check_cases_and_report(
89117

90118
relative_input_path = input_path.relative_to(input_paths_root)
91119
flat_relative_input_path = str(relative_input_path).replace(os.sep, '_')
120+
ofc_target_path = pathlib.Path('/tmp', flat_relative_input_path)
92121

93122
logger_level = logging.getLogger('open_fortran_parser.parser_wrapper').level
94123
logging.getLogger('open_fortran_parser.parser_wrapper').setLevel(logging.CRITICAL)
95124
ofc_logger_level = logging.getLogger('open_fortran_parser.ofc_wrapper').level
96125
logging.getLogger('open_fortran_parser.ofc_wrapper').setLevel(logging.CRITICAL)
97-
result = None
98-
try:
99-
try:
100-
result = parse(input_path, verbosity=100, raise_on_error=True)
101-
self.assertIsNotNone(result)
102-
except subprocess.CalledProcessError as parser_err:
103-
if not fall_back_to_ofc:
104-
raise parser_err
105-
transpiled_path = pathlib.Path('/tmp', flat_relative_input_path)
106-
code = None
107-
try:
108-
code = transpile(input_path, raise_on_error=True)
109-
self.assertIsInstance(code, str)
110-
with open(str(transpiled_path), 'w') as transpiled_file:
111-
transpiled_file.write(code)
112-
result = parse(transpiled_path, verbosity=100, raise_on_error=True)
113-
self.assertIsNotNone(result)
114-
_LOG.info('OFC definitely fixed something, see %s', transpiled_path)
115-
except subprocess.CalledProcessError as err3:
116-
if code is not None:
117-
_LOG.warning('OFC succeeded but parser failed %s', transpiled_path)
118-
raise parser_err from err3
119-
except subprocess.CalledProcessError as err:
120-
result = err
126+
result = self._check_case(input_path, fall_back_to_ofc, ofc_target_path)
121127
logging.getLogger('open_fortran_parser.parser_wrapper').setLevel(logger_level)
122128
logging.getLogger('open_fortran_parser.ofc_wrapper').setLevel(ofc_logger_level)
123129

@@ -191,6 +197,11 @@ def check_cases_and_report(
191197

192198
return passed_test_cases, new_passed_cases, failed_test_cases, new_failed_cases
193199

200+
201+
class Tests(TestsBase):
202+
203+
maxDiff = None
204+
194205
def test_comments(self):
195206
for suffix in ('.f', '.f90'):
196207
input_path = pathlib.Path(_HERE, 'examples', 'comments{}'.format(suffix))

test/test_parser_wrapper.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def test_unset_config(self):
7272
java_config['classpath'] = None
7373
with tempfile.NamedTemporaryFile() as output_file:
7474
execute_parser(INPUT_PATH, pathlib.Path(output_file.name))
75+
self.assertIsNone(java_config['classpath'])
7576

7677
java_config['classpath'] = classpath
7778
java_config['options'] = options

0 commit comments

Comments
 (0)