]> granicus.if.org Git - graphviz/commitdiff
use 'run' instead of 'Popen' where more concise in test code
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 12 Dec 2021 06:23:10 +0000 (22:23 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Tue, 14 Dec 2021 01:17:49 +0000 (17:17 -0800)
Not only does this lead to more concise and efficient code, this avoids some
upcoming warnings in newer Pylint releases.

rtest/test_regression.py
rtest/test_tools.py
tests/regression_tests/shapes/shapes.py
tests/regression_tests/shapes/test_shapes.py
tests/regression_tests/vuln/vuln.py

index f20240f2c17095883ffbe8ad29dfbfd45a6e6586..8813a93828b06b1ee1c46fc76a06c0df4c6f15a1 100644 (file)
@@ -749,10 +749,8 @@ def test_1877():
   input = "graph {subgraph cluster_a {}; cluster_a -- b}"
 
   # fdp should be able to process this
-  p = subprocess.Popen(["fdp", "-o", os.devnull], stdin=subprocess.PIPE,
+  subprocess.run(["fdp", "-o", os.devnull], input=input, check=True,
     universal_newlines=True)
-  p.communicate(input)
-  assert p.returncode == 0
 
 def test_1880():
   """
@@ -1244,11 +1242,11 @@ def test_2131():
   input = "digraph { a -> b; }"
 
   # ask gv2gml what it thinks of this
-  p = subprocess.Popen(["gv2gml"], stdin=subprocess.PIPE,
-                       universal_newlines=True)
-  p.communicate(input)
-
-  assert p.returncode == 0, "gv2gml rejected a basic graph"
+  try:
+    subprocess.run(["gv2gml"], input=input, check=True,
+                   universal_newlines=True)
+  except subprocess.CalledProcessError as e:
+    raise RuntimeError("gv2gml rejected a basic graph") from e
 
 @pytest.mark.skipif(shutil.which("gvpr") is None,
                     reason="gvpr not available")
@@ -1452,7 +1450,4 @@ def test_gvmap_fclose():
           '}'
 
   # pass this through gvmap
-  p = subprocess.Popen(["gvmap"], stdin=subprocess.PIPE)
-  p.communicate(input.encode("utf-8"))
-
-  assert p.returncode == 0
+  subprocess.run(["gvmap"], input=input.encode("utf-8"), check=True)
index 3c3d13f8e3fa7cba9c27581da15025a42d961b14..39219a96d1e2d5e9f8c4d46972559bc780c6fbfc 100644 (file)
@@ -152,7 +152,7 @@ def test_edgepaint_options(arg: str):
 
   # run edgepaint on this
   args = ["edgepaint"] + arg.split(" ")
-  p = subprocess.Popen(args, stdin=subprocess.PIPE, universal_newlines=True)
-  p.communicate(input)
-
-  assert p.returncode == 0, f"edgepaint rejected command line option '{arg}'"
+  try:
+    subprocess.run(args, input=input, check=True, universal_newlines=True)
+  except subprocess.CalledProcessError as e:
+    raise RuntimeError(f"edgepaint rejected command line option '{arg}'") from e
index 7787ef656ec17f0ff8d966532cd68e84fddf58ae..e87f01e13e0b5f30625939b0c471b03a054d1d04 100644 (file)
@@ -3,7 +3,7 @@ Tests of various shape generation against reference examples.
 """
 
 from pathlib import Path
-from subprocess import Popen, PIPE
+import subprocess
 import sys
 
 # Import helper function to compare graphs from tests/regressions_tests
@@ -90,12 +90,11 @@ def generate_shape_graph(shape, output_type):
     Path("output").mkdir(parents=True)
 
   output_file = Path("output") / f"{shape}.{output_type}"
-  process = Popen(["dot", "-T" + output_type, "-o", output_file], stdin=PIPE)
-
   input_graph = f'graph G {{ a [label="" shape={shape}] }}'
-  process.communicate(input = input_graph.encode("utf_8"))
-
-  if process.wait() != 0:
+  try:
+    subprocess.run(["dot", f"-T{output_type}", "-o", output_file],
+                   input=input_graph.encode("utf_8"), check=True)
+  except subprocess.CalledProcessError:
     print(f"An error occurred while generating: {output_file}")
     sys.exit(1)
 
index 688dc3712080fa19615a7a208fe529a2239718b0..6aadea23b84ff18ea6f2d2b32390fb1528c8f6ec 100644 (file)
@@ -4,7 +4,7 @@ Tests that built-in shape types are emitted correctly.
 
 import os.path
 from pathlib import Path
-from subprocess import Popen, PIPE
+import subprocess
 import sys
 import pytest
 
@@ -92,12 +92,11 @@ def generate_shape_graph(shape, output_type):
     Path("output").mkdir(parents=True)
 
   output_file = Path("output") / f"{shape}.{output_type}"
-  process = Popen(["dot", "-T" + output_type, "-o", output_file], stdin=PIPE)
-
   input_graph = f'graph G {{ a [label="" shape={shape}] }}'
-  process.communicate(input = input_graph.encode("utf_8"))
-
-  if process.wait() != 0:
+  try:
+    subprocess.run(["dot", f"-T{output_type}", "-o", output_file],
+                   input=input_graph.encode("utf_8"), check=True)
+  except subprocess.CalledProcessError:
     print(f"An error occurred while generating: {output_file}")
     sys.exit(1)
 
index d802b68bc5a629b3e766105b89f233bcaf953f37..6f83496c751de5bda74136a5f02d2809463cbb44 100644 (file)
@@ -4,7 +4,7 @@ Some legacy tests against previous bugs.
 FIXME: this should probably be integrated into ../../../rtest/test_regression.py
 """
 
-from subprocess import Popen, PIPE
+import subprocess
 import sys
 from pathlib import Path
 
@@ -30,9 +30,10 @@ def generate_vuln_graph(vulnfile, output_type):
 
   output_file = Path("output") / f"{vulnfile}.{output_type[0]}"
   input_file = Path("input") / f"{vulnfile}.dot"
-  process = Popen(["dot", "-T" + output_type[1], "-o", output_file, input_file], stdin=PIPE)
-
-  if process.wait() < 0:
+  try:
+    subprocess.check_call(["dot", f"-T{output_type[1]}", "-o", output_file,
+                           input_file])
+  except subprocess.CalledProcessError:
     print(f"An error occurred while generating: {output_file}")
     sys.exit(1)