Skip to content

Commit

Permalink
Simplified test_python_bindings.py (#831)
Browse files Browse the repository at this point in the history
Running test_python_bindings.py when building wheels fails randomly on Windows.

Tried to simplify stdout redirections. Hopefully that will work better on Windows.
  • Loading branch information
jesper-friis authored May 1, 2024
1 parent a5fb422 commit 02080b3
Showing 1 changed file with 11 additions and 25 deletions.
36 changes: 11 additions & 25 deletions bindings/python/tests/test_python_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
from glob import glob
import unittest
from contextlib import redirect_stdout, redirect_stderr

import dlite

Expand Down Expand Up @@ -42,6 +43,7 @@ def __repr__(self):


def test(verbosity=1, stream=sys.stdout):
"""Run tests with given verbosity level."""
tests = [
test
for test in sorted(glob(os.path.join(thisdir, "test_*.py")))
Expand All @@ -54,32 +56,16 @@ def test(verbosity=1, stream=sys.stdout):
ts = unittest.TestSuite()
for test in sorted(tests):
ts.addTest(ScriptTestCase(filename=os.path.abspath(test)))
with open(os.devnull, "w") as devnull:
if not verbosity:
stream = devnull
ttr = unittest.TextTestRunner(verbosity=verbosity, stream=stream)

# Redirect stdout and stderr to devnull
# stderr is redicred at file-descriptor level to get rid of
# C-level output
dest_fd = devnull.fileno()
stderr_fd = sys.stderr.fileno()
# copy stderr_fd before it is overwritten
# NOTE: `copied` is inheritable on Windows when duplicating a
# standard stream
with os.fdopen(os.dup(stderr_fd), "wb") as copied:
sys.stdout.flush()
sys.stderr.flush()
try:
sys.stdout = devnull
os.dup2(dest_fd, stderr_fd) # $ exec >&dest
results = ttr.run(ts)
finally:
sys.stdout.flush()
sys.stderr.flush()
sys.stdout = sys.__stdout__
os.dup2(copied.fileno(), stderr_fd) # $ exec >&copied
return results
ttr = unittest.TextTestRunner(verbosity=verbosity, stream=stream)

if verbosity < 3:
with open(os.devnull, "w") as devnull:
with redirect_stderr(devnull):
with redirect_stdout(devnull):
return ttr.run(ts)
else:
return ttr.run(ts)


if __name__ == "__main__":
Expand Down

0 comments on commit 02080b3

Please sign in to comment.