-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathtest_calfem.py
126 lines (89 loc) · 3.55 KB
/
test_calfem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# -*- coding: utf-8 -*-
"""
This is very simple test of the calfem package.
Make sure all examples run without errors.
"""
import os, sys
import example_output as eo
import difflib
from pprint import pprint
def test_examples():
examples_dir = "examples"
examples = [
"exs_bar2.py",
"exs_bar2_la.py",
"exs_bar2_lb.py",
"exs_beam1.py",
"exs_beam2.py",
"exs_beambar2.py",
"exs_flw_diff2.py",
"exs_flw_temp1.py",
"exs_flw_temp2.py",
"exs_spring.py"
]
# Remove old log files
if os.path.exists("test_examples.log"):
os.remove("test_examples.log")
if os.path.exists("test_examples_warnings.log"):
os.remove("test_examples_warnings.log")
if os.path.exists("test_examples_output.log"):
os.remove("test_examples_output.log")
# Set environment variable to avoid blocking of plots
os.environ["CFV_NO_BLOCK"] = "YES"
# Assume 0 return codes
return_codes = 0
for example in examples:
print(f"Running: {example}", end="")
echo_string = f"echo ## EXAMPLE: {example} "
os.system(echo_string + "-"*(40-len(example)) +
" >> test_examples.log 2>&1")
example_path = os.path.join(examples_dir, example)
python_executable = sys.executable
return_code = os.system(
f'"{python_executable}" {example_path} >> test_examples.log 2>&1')
if return_code == 0:
print(" --- PASSED!")
else:
print(" --- FAILED!")
return_codes += return_code
# Parse for warnings and errors
example_output = {}
with open("test_examples.log", "r") as f:
with open("test_examples_warnings.log", "w") as w:
log = f.readlines()
current_example = ""
for line in log:
if "## EXAMPLE:" in line:
current_example = line.split("## EXAMPLE: ")[1].split()[0].strip()
example_output[current_example] = ""
continue
if "Warning:" in line:
line_items = line.split(":")
if len(line_items)>2:
w.write(f"{current_example}: {line_items[-2]}: {line_items[-1]} at line: {line_items[-3]}\n")
if current_example!="":
example_output[current_example] += line.rstrip() + "\n"
# Compare output
with open("test_examples_output.log", "w") as f:
for example, output in example_output.items():
if example in eo.examples.keys():
print(f"Comparing output of {example}", end="")
f.write(f"Comparing output of {example}")
d = difflib.Differ()
lines = eo.examples[example].splitlines()
stripped_lines = [line.rstrip() for line in lines]
diff = d.compare(output.splitlines(), stripped_lines)
diff_list = list(diff)
is_identical = all(line.startswith(' ') for line in diff_list)
if not is_identical:
f.write(f"\n---------- {example} -----------\n")
f.write("\n".join(diff_list))
f.write(f"\n---------- {example} -----------\n")
return_codes += 1
print(" --- FAILED!")
else:
f.write(f" --- PASSED!\n")
print(" --- PASSED!")
assert return_codes == 0
if __name__ == "__main__":
test_examples()