Skip to content

Commit a23b1ac

Browse files
authored
Refactor run_tests subprocess usage (#450)
- update `__run_exec` to take argument lists - pass argument lists instead of command strings
1 parent ac009c7 commit a23b1ac

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

scripts/run_tests.py

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import shlex
23
import subprocess
34
import platform
45
from pathlib import Path
@@ -62,55 +63,76 @@ def setup_env(self, ppc_env):
6263
self.work_dir = Path(self.__get_project_path()) / "build" / "bin"
6364

6465
def __run_exec(self, command):
65-
result = subprocess.run(command, shell=True, env=self.__ppc_env)
66+
result = subprocess.run(command, shell=False, env=self.__ppc_env)
6667
if result.returncode != 0:
6768
raise Exception(f"Subprocess return {result.returncode}.")
6869

6970
@staticmethod
7071
def __get_gtest_settings(repeats_count, type_task):
71-
command = f"--gtest_repeat={repeats_count} "
72-
command += "--gtest_recreate_environments_when_repeating "
73-
command += "--gtest_color=0 "
74-
command += "--gtest_shuffle "
75-
command += f"--gtest_filter=\"*{type_task}*\" "
72+
command = [
73+
f"--gtest_repeat={repeats_count}",
74+
"--gtest_recreate_environments_when_repeating",
75+
"--gtest_color=0",
76+
"--gtest_shuffle",
77+
f"--gtest_filter=*{type_task}*",
78+
]
7679
return command
7780

7881
def run_threads(self):
7982
if platform.system() == "Linux" and not self.__ppc_env.get("PPC_ASAN_RUN"):
8083
for task_type in ["seq", "stl"]:
81-
self.__run_exec(f"{self.valgrind_cmd} {self.work_dir / 'ppc_func_tests'} "
82-
f"{self.__get_gtest_settings(1, '_' + task_type + '_')}")
84+
self.__run_exec(
85+
shlex.split(self.valgrind_cmd)
86+
+ [str(self.work_dir / 'ppc_func_tests')]
87+
+ self.__get_gtest_settings(1, '_' + task_type + '_')
88+
)
8389

8490
for task_type in ["omp", "seq", "stl", "tbb"]:
85-
self.__run_exec(f"{self.work_dir / 'ppc_func_tests'} {self.__get_gtest_settings(3, '_' + task_type + '_')}")
91+
self.__run_exec(
92+
[str(self.work_dir / 'ppc_func_tests')] + self.__get_gtest_settings(3, '_' + task_type + '_')
93+
)
8694

8795
def run_core(self):
8896
if platform.system() == "Linux" and not self.__ppc_env.get("PPC_ASAN_RUN"):
89-
self.__run_exec(f"{self.valgrind_cmd} {self.work_dir / 'core_func_tests'} "
90-
f"{self.__get_gtest_settings(1, '*')} --gtest_filter=*:-*_disabled_valgrind")
97+
self.__run_exec(
98+
shlex.split(self.valgrind_cmd)
99+
+ [str(self.work_dir / 'core_func_tests')]
100+
+ self.__get_gtest_settings(1, '*')
101+
+ ["--gtest_filter=*:-*_disabled_valgrind"]
102+
)
91103

92-
self.__run_exec(f"{self.work_dir / 'core_func_tests'} {self.__get_gtest_settings(1, '*')}")
104+
self.__run_exec(
105+
[str(self.work_dir / 'core_func_tests')] + self.__get_gtest_settings(1, '*')
106+
)
93107

94108
def run_processes(self, additional_mpi_args):
95109
ppc_num_proc = self.__ppc_env.get("PPC_NUM_PROC")
96110
if ppc_num_proc is None:
97111
raise EnvironmentError("Required environment variable 'PPC_NUM_PROC' is not set.")
98112

99-
mpi_running = f"{self.mpi_exec} {additional_mpi_args} -np {ppc_num_proc}"
113+
mpi_running = [self.mpi_exec] + shlex.split(additional_mpi_args) + ["-np", ppc_num_proc]
100114
if not self.__ppc_env.get("PPC_ASAN_RUN"):
101115
for task_type in ["all", "mpi"]:
102-
self.__run_exec(f"{mpi_running} {self.work_dir / 'ppc_func_tests'} "
103-
f"{self.__get_gtest_settings(10, '_' + task_type)}")
116+
self.__run_exec(
117+
mpi_running
118+
+ [str(self.work_dir / 'ppc_func_tests')]
119+
+ self.__get_gtest_settings(10, '_' + task_type)
120+
)
104121

105122
def run_performance(self):
106123
if not self.__ppc_env.get("PPC_ASAN_RUN"):
107-
mpi_running = f"{self.mpi_exec} -np {self.__ppc_num_proc}"
124+
mpi_running = [self.mpi_exec, "-np", self.__ppc_num_proc]
108125
for task_type in ["all", "mpi"]:
109-
self.__run_exec(f"{mpi_running} {self.work_dir / 'ppc_perf_tests'} "
110-
f"{self.__get_gtest_settings(1, '_' + task_type)}")
126+
self.__run_exec(
127+
mpi_running
128+
+ [str(self.work_dir / 'ppc_perf_tests')]
129+
+ self.__get_gtest_settings(1, '_' + task_type)
130+
)
111131

112132
for task_type in ["omp", "seq", "stl", "tbb"]:
113-
self.__run_exec(f"{self.work_dir / 'ppc_perf_tests'} {self.__get_gtest_settings(1, '_' + task_type)}")
133+
self.__run_exec(
134+
[str(self.work_dir / 'ppc_perf_tests')] + self.__get_gtest_settings(1, '_' + task_type)
135+
)
114136

115137

116138
if __name__ == "__main__":

0 commit comments

Comments
 (0)