Skip to content

Instantly share code, notes, and snippets.

@jun66j5
Created December 10, 2020 15:53
Show Gist options
  • Save jun66j5/03f0ff742b5a08c93d699078e89a3be3 to your computer and use it in GitHub Desktop.
Save jun66j5/03f0ff742b5a08c93d699078e89a3be3 to your computer and use it in GitHub Desktop.
diff --git a/build/run_tests.py b/build/run_tests.py
index 7ae07806b..d6cf417e1 100755
--- a/build/run_tests.py
+++ b/build/run_tests.py
@@ -91,6 +91,26 @@ class TextColors:
cls.SUCCESS = ''
+if hasattr(subprocess.Popen, '__enter__'):
+ Popen = subprocess.Popen
+else:
+ class Popen(subprocess.Popen):
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, type, value, traceback):
+ if self.stdout:
+ self.stdout.close()
+ if self.stderr:
+ self.stderr.close()
+ try:
+ if self.stdin:
+ self.stdin.close()
+ finally:
+ self.wait()
+
+
def _get_term_width():
'Attempt to discern the width of the terminal'
# This may not work on all platforms, in which case the default of 80
@@ -364,10 +384,10 @@ class TestHarness:
def execute(self, harness):
start_time = datetime.now()
- with subprocess.Popen(self._command_line(harness),
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- cwd=self.progdir) as prog:
+ with Popen(self._command_line(harness),
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ cwd=self.progdir) as prog:
self.stdout_lines = prog.stdout.readlines()
self.stderr_lines = prog.stderr.readlines()
@@ -390,24 +410,20 @@ class TestHarness:
def _count_c_tests(self, progabs, progdir, progbase):
'Run a c test, escaping parameters as required.'
cmdline = [ progabs, '--list' ]
- with subprocess.Popen(cmdline, stdout=subprocess.PIPE,
- cwd=progdir) as prog:
+ with Popen(cmdline, stdout=subprocess.PIPE, cwd=progdir) as prog:
lines = prog.stdout.readlines()
self.result.append(TestHarness.Job(len(lines) - 2, False, progabs,
progdir, progbase))
- prog.wait()
def _count_py_tests(self, progabs, progdir, progbase):
'Run a c test, escaping parameters as required.'
cmdline = [ sys.executable, progabs, '--list' ]
- with subprocess.Popen(cmdline, stdout=subprocess.PIPE,
- cwd=progdir) as prog:
+ with Popen(cmdline, stdout=subprocess.PIPE, cwd=progdir) as prog:
lines = prog.stdout.readlines()
for i in range(0, len(lines) - 2):
self.result.append(TestHarness.Job(i + 1, True, progabs,
progdir, progbase))
- prog.wait()
def run(self):
"Run a single test. Return the test's exit code."
@@ -802,8 +818,8 @@ class TestHarness:
total = len(test_nums)
else:
total_cmdline = [cmdline[0], '--list']
- prog = subprocess.Popen(total_cmdline, stdout=subprocess.PIPE)
- lines = prog.stdout.readlines()
+ with Popen(total_cmdline, stdout=subprocess.PIPE) as prog:
+ lines = prog.stdout.readlines()
total = len(lines) - 2
# This has to be class-scoped for use in the progress_func()
@@ -819,23 +835,22 @@ class TestHarness:
self.dots_written = dots
tests_completed = 0
- prog = subprocess.Popen(cmdline, stdout=subprocess.PIPE,
- stderr=self.log)
- line = prog.stdout.readline()
- while line:
- line = ensure_str(line)
- if self._process_test_output_line(line):
- tests_completed += 1
- progress_func(tests_completed)
-
+ with Popen(cmdline, stdout=subprocess.PIPE, stderr=self.log) as prog:
line = prog.stdout.readline()
+ while line:
+ line = ensure_str(line)
+ if self._process_test_output_line(line):
+ tests_completed += 1
+ progress_func(tests_completed)
- # If we didn't run any tests, still print out the dots
- if not tests_completed:
- os.write(sys.stdout.fileno(), b'.' * dot_count)
+ line = prog.stdout.readline()
- prog.wait()
- return prog.returncode
+ # If we didn't run any tests, still print out the dots
+ if not tests_completed:
+ os.write(sys.stdout.fileno(), b'.' * dot_count)
+
+ prog.wait()
+ return prog.returncode
def _run_py_test(self, progabs, progdir, progbase, test_nums, dot_count):
'Run a python test, passing parameters as needed.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment