]> granicus.if.org Git - graphviz/commitdiff
use a cleaner pattern for temporary directory management in tests
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 23 Aug 2020 20:42:19 +0000 (13:42 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 23 Aug 2020 20:42:19 +0000 (13:42 -0700)
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.

rtest/test_regression.py
rtest/test_vmalloc.py

index 8c331b1d2328ffea304741eab4058cbaf812cb47..23c74d341e789befedae952b7d77e349b2e69fbe 100644 (file)
@@ -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():
     '''
index f25134e8e541a97c76e7c2d4e151694c9aac4894..98e4041ebbf4e5a0e38bc657dd20e6702b704743 100644 (file)
@@ -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])