19
19
###############################################################################
20
20
21
21
22
+ import sys
22
23
import hashlib
23
24
import logging
24
25
import io
28
29
import configparser
29
30
import shutil
30
31
import shlex
32
+ from datetime import datetime
31
33
import fprettify
32
- from fprettify .tests .test_common import TEST_MAIN_DIR , TEST_EXT_DIR , BACKUP_DIR , RESULT_DIR , RESULT_FILE , FAILED_FILE , FprettifyTestCase , joinpath
34
+ from fprettify .tests .test_common import _MYPATH , FprettifyTestCase , joinpath
35
+
36
+ _TIMESTAMP = datetime .now ().strftime ('%Y-%m-%d_%H-%M-%S' )
37
+
38
+ # main directory for running tests
39
+ TEST_MAIN_DIR = joinpath (_MYPATH , r'../../fortran_tests' )
40
+
41
+ # directory for external Fortran code
42
+ TEST_EXT_DIR = joinpath (TEST_MAIN_DIR , r'test_code' )
43
+
44
+ # directory containing Fortran examples
45
+ EXAMPLE_DIR = joinpath (_MYPATH , r'../../examples/in' )
46
+
47
+ # backup directory
48
+ BACKUP_DIR = joinpath (TEST_MAIN_DIR , r'test_code_in_' + _TIMESTAMP )
49
+
50
+ # where to store summarized results
51
+ RESULT_DIR = joinpath (TEST_MAIN_DIR , r'test_results' )
52
+
53
+ # expected hash-sums
54
+ RESULT_FILE = joinpath (RESULT_DIR , r'expected_results' )
55
+
56
+ # test failures
57
+ FAILED_FILE = joinpath (RESULT_DIR , r'failed_results' )
33
58
34
59
35
60
fprettify .set_fprettify_logger (logging .ERROR )
36
61
62
+ class FprettifyIntegrationTestCase (FprettifyTestCase ):
63
+ def shortDescription (self ):
64
+ """don't print doc string of testmethod"""
65
+ return None
66
+
67
+ def setUp (self ):
68
+ """
69
+ setUp to be recognized by unittest.
70
+ We have large files to compare, raise the limit
71
+ """
72
+ self .maxDiff = None
73
+
74
+ @classmethod
75
+ def setUpClass (cls ):
76
+ """
77
+ setUpClass to be recognized by unittest.
78
+ """
79
+
80
+ cls .n_success = 0
81
+ cls .n_parsefail = 0
82
+ cls .n_internalfail = 0
83
+ cls .n_unexpectedfail = 0
84
+
85
+ FprettifyIntegrationTestCase .eprint ("-" * 70 )
86
+ FprettifyIntegrationTestCase .eprint ("recognized Fortran files" )
87
+ FprettifyIntegrationTestCase .eprint (", " .join (fprettify .FORTRAN_EXTENSIONS ))
88
+ FprettifyIntegrationTestCase .eprint ("-" * 70 )
89
+ FprettifyIntegrationTestCase .eprint ("Applying fprettify to Fortran files in " + TEST_EXT_DIR )
90
+ FprettifyIntegrationTestCase .eprint ("Writing backup of original files to " + BACKUP_DIR )
91
+ FprettifyIntegrationTestCase .eprint ("Storing expected results in " + RESULT_FILE )
92
+ FprettifyIntegrationTestCase .eprint ("Storing failed results in " + FAILED_FILE )
93
+ FprettifyIntegrationTestCase .eprint ("-" * 70 )
94
+
95
+ @classmethod
96
+ def tearDownClass (cls ):
97
+ """
98
+ tearDownClass to be recognized by unittest. Used for test summary
99
+ output.
100
+ """
101
+ if cls .n_parsefail + cls .n_internalfail > 0 :
102
+ format = "{:<20}{:<6}"
103
+ FprettifyIntegrationTestCase .eprint ('\n ' + "=" * 70 )
104
+ FprettifyIntegrationTestCase .eprint ("IGNORED errors: invalid or old Fortran" )
105
+ FprettifyIntegrationTestCase .eprint ("-" * 70 )
106
+ FprettifyIntegrationTestCase .eprint (format .format ("parse errors: " , cls .n_parsefail ))
107
+ FprettifyIntegrationTestCase .eprint (format .format ("internal errors: " , cls .n_internalfail ))
108
+
109
+ @staticmethod
110
+ def write_result (filename , content , sep_str ): # pragma: no cover
111
+ with io .open (filename , 'a' , encoding = 'utf-8' ) as outfile :
112
+ outfile .write (sep_str .join (content ) + '\n ' )
113
+
114
+ @staticmethod
115
+ def eprint (* args , ** kwargs ):
116
+ """
117
+ Print to stderr - to print output compatible with default unittest output.
118
+ """
119
+
120
+ print (* args , file = sys .stderr , flush = True , ** kwargs )
121
+
122
+
37
123
def generate_suite (suite = None , name = None ):
38
124
import git
39
125
config = configparser .ConfigParser ()
@@ -56,15 +142,15 @@ def generate_suite(suite=None, name=None):
56
142
os .chdir (orig )
57
143
58
144
addtestcode (code ['path' ], code ['options' ])
59
- return FprettifyTestCase
145
+ return FprettifyIntegrationTestCase
60
146
61
147
def addtestcode (code_path , options ):
62
148
print (f"creating test cases from { code_path } ..." )
63
149
# dynamically create test cases from fortran files in test directory
64
150
for dirpath , _ , filenames in os .walk (joinpath (TEST_EXT_DIR , code_path )):
65
151
for example in [f for f in filenames if any (f .endswith (_ ) for _ in fprettify .FORTRAN_EXTENSIONS )]:
66
152
rel_dirpath = os .path .relpath (dirpath , start = TEST_EXT_DIR )
67
- addtestmethod (FprettifyTestCase , rel_dirpath , example , options )
153
+ addtestmethod (FprettifyIntegrationTestCase , rel_dirpath , example , options )
68
154
69
155
def addtestmethod (testcase , fpath , ffile , options ):
70
156
"""add a test method for each example."""
@@ -107,19 +193,19 @@ def test_result(path, info):
107
193
test_info = "checksum"
108
194
test_content = test_result (example , m .hexdigest ())
109
195
110
- FprettifyTestCase .n_success += 1
196
+ FprettifyIntegrationTestCase .n_success += 1
111
197
except fprettify .FprettifyParseException as e :
112
198
test_info = "parse error"
113
199
fprettify .log_exception (e , test_info , level = "warning" )
114
200
test_content = test_result (example , test_info )
115
- FprettifyTestCase .n_parsefail += 1
201
+ FprettifyIntegrationTestCase .n_parsefail += 1
116
202
except fprettify .FprettifyInternalException as e :
117
203
test_info = "internal error"
118
204
fprettify .log_exception (e , test_info , level = "warning" )
119
205
test_content = test_result (example , test_info )
120
- FprettifyTestCase .n_internalfail += 1
206
+ FprettifyIntegrationTestCase .n_internalfail += 1
121
207
except : # pragma: no cover
122
- FprettifyTestCase .n_unexpectedfail += 1
208
+ FprettifyIntegrationTestCase .n_unexpectedfail += 1
123
209
raise
124
210
125
211
# overwrite example
@@ -141,7 +227,7 @@ def test_result(path, info):
141
227
line_content = line .strip ().split (sep_str )
142
228
if line_content [0 ] == test_content [0 ]:
143
229
found = True
144
- FprettifyTestCase .eprint (test_info , end = " " )
230
+ FprettifyIntegrationTestCase .eprint (test_info , end = " " )
145
231
msg = '{} (old) != {} (new)' .format (
146
232
line_content [1 ], test_content [1 ])
147
233
if test_info == "checksum" and outstring .count ('\n ' ) < 10000 :
@@ -153,14 +239,14 @@ def test_result(path, info):
153
239
testcase .assertEqual (
154
240
line_content [1 ], test_content [1 ], msg )
155
241
except AssertionError : # pragma: no cover
156
- FprettifyTestCase .write_result (
242
+ FprettifyIntegrationTestCase .write_result (
157
243
FAILED_FILE , test_content , sep_str )
158
244
raise
159
245
break
160
246
161
247
if not found : # pragma: no cover
162
- FprettifyTestCase .eprint (test_info + " new" , end = " " )
163
- FprettifyTestCase .write_result (RESULT_FILE , test_content , sep_str )
248
+ FprettifyIntegrationTestCase .eprint (test_info + " new" , end = " " )
249
+ FprettifyIntegrationTestCase .write_result (RESULT_FILE , test_content , sep_str )
164
250
165
251
# not sure why this even works, using "test something" (with a space) as function name...
166
252
# however it gives optimal test output
0 commit comments