Skip to content

Commit 37fa4f4

Browse files
authored
Improve test runner script with refactoring and prerequisite checks (#509)
1 parent 740b2b9 commit 37fa4f4

1 file changed

Lines changed: 39 additions & 25 deletions

File tree

‎run-tests.py‎

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
1-
21
importglob
32
importos
3+
importshutil
44
importsubprocess
55
importsys
66

7-
defcleanup(out):
8-
ret=''
9-
forsinout.decode('utf-8').split('\n'):
10-
iflen(s) >1ands[0] =='#':
7+
8+
defcleanup(out: str) ->str:
9+
parts= []
10+
forlineinout.splitlines():
11+
iflen(line) >1andline[0] =='#':
1112
continue
12-
s="".join(s.split())
13-
ret=ret+s
14-
returnret
13+
parts.append("".join(line.split()))
14+
return"".join(parts)
15+
16+
17+
# Check for required compilers and exit if any are missing
18+
CLANG_EXE=shutil.which('clang')
19+
ifnotCLANG_EXE:
20+
sys.exit('Failed to run tests: clang compiler not found')
21+
22+
GCC_EXE=shutil.which('gcc')
23+
ifnotGCC_EXE:
24+
sys.exit('Failed to run tests: gcc compiler not found')
25+
26+
SIMPLECPP_EXE='./simplecpp'
27+
1528

1629
commands= []
1730

@@ -78,6 +91,21 @@ def cleanup(out):
7891
'pr57580.c',
7992
]
8093

94+
95+
defrun(compiler_executable: str, compiler_args: list[str]) ->tuple[int, str, str]:
96+
"""Execute a compiler command and capture its exit code, stdout, and stderr."""
97+
compiler_cmd= [compiler_executable]
98+
compiler_cmd.extend(compiler_args)
99+
100+
withsubprocess.Popen(compiler_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, encoding="utf-8") asprocess:
101+
stdout, stderr=process.communicate()
102+
exit_code=process.returncode
103+
104+
output=cleanup(stdout)
105+
error= (stderror"").strip()
106+
return (exit_code, output, error)
107+
108+
81109
numberOfSkipped=0
82110
numberOfFailed=0
83111
numberOfFixed=0
@@ -89,26 +117,12 @@ def cleanup(out):
89117
numberOfSkipped=numberOfSkipped+1
90118
continue
91119

92-
clang_cmd= ['clang']
93-
clang_cmd.extend(cmd.split(' '))
94-
p=subprocess.Popen(clang_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
95-
comm=p.communicate()
96-
clang_output=cleanup(comm[0])
120+
_, clang_output, _=run(CLANG_EXE, cmd.split(' '))
97121

98-
gcc_cmd= ['gcc']
99-
gcc_cmd.extend(cmd.split(' '))
100-
p=subprocess.Popen(gcc_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
101-
comm=p.communicate()
102-
gcc_output=cleanup(comm[0])
122+
_, gcc_output, _=run(GCC_EXE, cmd.split(' '))
103123

104-
simplecpp_cmd= ['./simplecpp']
105124
# -E is not supported and we bail out on unknown options
106-
simplecpp_cmd.extend(cmd.replace('-E ', '', 1).split(' '))
107-
p=subprocess.Popen(simplecpp_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
108-
comm=p.communicate()
109-
simplecpp_ec=p.returncode
110-
simplecpp_output=cleanup(comm[0])
111-
simplecpp_err=comm[0].decode('utf-8').strip()
125+
simplecpp_ec, simplecpp_output, simplecpp_err=run(SIMPLECPP_EXE, cmd.replace('-E ', '', 1).split(' '))
112126

113127
ifsimplecpp_output!=clang_outputandsimplecpp_output!=gcc_output:
114128
filename=cmd[cmd.rfind('/')+1:]

0 commit comments

Comments
 (0)