From: Matthew Fernandez Date: Sun, 23 Aug 2020 20:42:19 +0000 (-0700) Subject: use a cleaner pattern for temporary directory management in tests X-Git-Tag: 2.46.0~20^2^2~113^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5c950ae0ced9344cb2b6004901f05d2c54fad795;p=graphviz use a cleaner pattern for temporary directory management in tests This switches a pattern of: tmp = tempfile.mkdtemp() atexist.register(shutil.rmtree, tmp) ... to: with tempfile.TemporaryDirectory() as tmp: ... Apart from being less error prone, this also unconditionally cleans up the temporary directory, even when an uncaught exception is thrown. --- diff --git a/rtest/test_regression.py b/rtest/test_regression.py index 8c331b1d2..23c74d341 100644 --- a/rtest/test_regression.py +++ b/rtest/test_regression.py @@ -1,7 +1,5 @@ -import atexit import pytest import platform -import shutil import signal import subprocess import os @@ -265,37 +263,36 @@ def test_1767(): assert os.path.exists(c_src), 'missing test case' # create some scratch space to work in - tmp = tempfile.mkdtemp() - atexit.register(shutil.rmtree, tmp) - - # compile our test code - exe = os.path.join(tmp, 'a.exe') - rt_lib_option = '-MDd' if os.environ.get('configuration') == 'Debug' else '-MD' - - if platform.system() == 'Windows': - subprocess.check_call(['cl', c_src, '-Fe:', exe, '-nologo', - rt_lib_option, '-link', 'cgraph.lib', 'gvc.lib']) - else: - cc = os.environ.get('CC', 'cc') - subprocess.check_call([cc, c_src, '-o', exe, '-lcgraph', '-lgvc']) - - # find our co-located dot input - dot = os.path.abspath(os.path.join(os.path.dirname(__file__), '1767.dot')) - assert os.path.exists(dot), 'missing test case' - - # run the test - stdout = subprocess.check_output([exe, dot], universal_newlines=True) - - assert stdout == 'Loaded graph:clusters\n' \ - 'cluster_0 contains 5 nodes\n' \ - 'cluster_1 contains 1 nodes\n' \ - 'cluster_2 contains 3 nodes\n' \ - 'cluster_3 contains 3 nodes\n' \ - 'Loaded graph:clusters\n' \ - 'cluster_0 contains 5 nodes\n' \ - 'cluster_1 contains 1 nodes\n' \ - 'cluster_2 contains 3 nodes\n' \ - 'cluster_3 contains 3 nodes\n' + with tempfile.TemporaryDirectory() as tmp: + + # compile our test code + exe = os.path.join(tmp, 'a.exe') + rt_lib_option = '-MDd' if os.environ.get('configuration') == 'Debug' else '-MD' + + if platform.system() == 'Windows': + subprocess.check_call(['cl', c_src, '-Fe:', exe, '-nologo', + rt_lib_option, '-link', 'cgraph.lib', 'gvc.lib']) + else: + cc = os.environ.get('CC', 'cc') + subprocess.check_call([cc, c_src, '-o', exe, '-lcgraph', '-lgvc']) + + # find our co-located dot input + dot = os.path.abspath(os.path.join(os.path.dirname(__file__), '1767.dot')) + assert os.path.exists(dot), 'missing test case' + + # run the test + stdout = subprocess.check_output([exe, dot], universal_newlines=True) + + assert stdout == 'Loaded graph:clusters\n' \ + 'cluster_0 contains 5 nodes\n' \ + 'cluster_1 contains 1 nodes\n' \ + 'cluster_2 contains 3 nodes\n' \ + 'cluster_3 contains 3 nodes\n' \ + 'Loaded graph:clusters\n' \ + 'cluster_0 contains 5 nodes\n' \ + 'cluster_1 contains 1 nodes\n' \ + 'cluster_2 contains 3 nodes\n' \ + 'cluster_3 contains 3 nodes\n' def test_1783(): ''' diff --git a/rtest/test_vmalloc.py b/rtest/test_vmalloc.py index f25134e8e..98e4041eb 100644 --- a/rtest/test_vmalloc.py +++ b/rtest/test_vmalloc.py @@ -1,9 +1,7 @@ # test ../lib/vmalloc -import atexit import os import platform -import shutil import subprocess import tempfile @@ -16,16 +14,15 @@ def test_vmalloc(): assert os.path.exists(src) # create a temporary directory to work in - tmp = tempfile.mkdtemp() - atexit.register(shutil.rmtree, tmp) + with tempfile.TemporaryDirectory() as tmp: - # compile the unit tests - dst = os.path.join(tmp, 'vmalloc-tests.exe') - if platform.system() == 'Windows': - subprocess.check_call(['cl', '-nologo', src, '-Fe:', dst]) - else: - subprocess.check_call([os.environ.get('CC', 'cc'), '-Wall', '-Wextra', - '-Werror', '-o', dst, src]) + # compile the unit tests + dst = os.path.join(tmp, 'vmalloc-tests.exe') + if platform.system() == 'Windows': + subprocess.check_call(['cl', '-nologo', src, '-Fe:', dst]) + else: + subprocess.check_call([os.environ.get('CC', 'cc'), '-Wall', '-Wextra', + '-Werror', '-o', dst, src]) - # run the unit tests - subprocess.check_call([dst]) + # run the unit tests + subprocess.check_call([dst])