From a590f875d57be1155ad16c37127eb263a80172b2 Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Mon, 10 Jan 2022 17:05:58 -0800 Subject: [PATCH] run_c: check return code of compiled program before returning Every call to `run_c` was immediately checking that the compiled program succeeded. So by moving the check inside `run_c` itself we can write more concise test cases. --- rtest/gvtest.py | 11 +++++++++-- rtest/test_examples.py | 8 +------- rtest/test_itos.py | 4 +--- rtest/test_regression.py | 20 ++++++-------------- rtest/test_sprint.py | 4 +--- rtest/test_vmalloc.py | 4 +--- 6 files changed, 19 insertions(+), 32 deletions(-) diff --git a/rtest/gvtest.py b/rtest/gvtest.py index c6c699258..383cb8275 100644 --- a/rtest/gvtest.py +++ b/rtest/gvtest.py @@ -4,6 +4,7 @@ import os from pathlib import Path import platform import subprocess +import sys import sysconfig import tempfile from typing import List, Optional, Tuple, Union @@ -94,7 +95,7 @@ def dot(T: str, source_file: Optional[Path] = None, source: Optional[str] = None def run_c(src: Path, args: List[str] = None, input: str = "", cflags: List[str] = None, link: List[str] = None - ) -> Tuple[int, str, str]: + ) -> Tuple[str, str]: """compile and run a C program""" if args is None: @@ -120,4 +121,10 @@ def run_c(src: Path, args: List[str] = None, input: str = "", p = subprocess.run([exe] + args, input=input, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) - return p.returncode, p.stdout, p.stderr + # check it succeeded + if p.returncode != 0: + sys.stdout.write(p.stdout) + sys.stderr.write(p.stderr) + p.check_returncode() + + return p.stdout, p.stderr diff --git a/rtest/test_examples.py b/rtest/test_examples.py index 5395d431a..ca91a002f 100644 --- a/rtest/test_examples.py +++ b/rtest/test_examples.py @@ -40,13 +40,7 @@ def test_compile_example(src): else: cflags = None - ret, out, err = run_c(filepath, args, "graph {a -- b}", cflags=cflags, link=libs) - - print(f"returncode: {ret} = 0x{ret:08x}") - if ret != 0: - print(out) - print(err) - assert ret == 0 + _, _ = run_c(filepath, args, "graph {a -- b}", cflags=cflags, link=libs) @pytest.mark.parametrize("src", ["addrings", "attr", "bbox", "bipart", "chkedges", "clustg", "collapse", "cycle", "deghist", "delmulti", "depath", diff --git a/rtest/test_itos.py b/rtest/test_itos.py index 7e47bee79..459258722 100644 --- a/rtest/test_itos.py +++ b/rtest/test_itos.py @@ -20,6 +20,4 @@ def test_itos(): # extra C flags this compilation needs cflags = ['-I', lib] - ret, _, _ = run_c(src, cflags=cflags) - - assert ret == 0 + _, _ = run_c(src, cflags=cflags) diff --git a/rtest/test_regression.py b/rtest/test_regression.py index 4f3e588b0..ff2e9330c 100644 --- a/rtest/test_regression.py +++ b/rtest/test_regression.py @@ -571,8 +571,7 @@ def test_1767(): src = (Path(__file__).parent / "1767.dot").resolve() assert src.exists(), "missing test case" - ret, _, _ = run_c(c_src, [src], link=["cgraph", "gvc"]) - assert ret == 0 + _, _ = run_c(c_src, [src], link=["cgraph", "gvc"]) # FIXME: uncomment this when #1767 is fixed # assert stdout == "Loaded graph:clusters\n" \ @@ -972,8 +971,7 @@ def test_1910(): assert c_src.exists(), "missing test case" # run the test - ret, _, _ = run_c(c_src, link=["cgraph", "gvc"]) - assert ret == 0 + _, _ = run_c(c_src, link=["cgraph", "gvc"]) def test_1913(): """ @@ -1122,8 +1120,7 @@ def test_2057(): assert c_src.exists(), "missing test case" # run the test - ret, _, _ = run_c(c_src, link=["gvc"]) - assert ret == 0 + _, _ = run_c(c_src, link=["gvc"]) def test_2078(): """ @@ -1221,10 +1218,7 @@ def test_2089_2(): assert c_src.exists(), "missing test case" # run it - ret, stdout, stderr = run_c(c_src, link=["cgraph"]) - sys.stdout.write(stdout) - sys.stderr.write(stderr) - assert ret == 0 + _, _ = run_c(c_src, link=["cgraph"]) @pytest.mark.skipif(os.environ.get("build_system") == "msbuild" and os.environ.get("configuration") == "Debug", @@ -1314,8 +1308,7 @@ def test_package_version(): assert c_src.exists(), "missing test case" # run the test - ret, _, _ = run_c(c_src) - assert ret == 0 + _, _ = run_c(c_src) def test_user_shapes(): """ @@ -1347,7 +1340,7 @@ def test_xdot_json(): # ask our C helper to process this try: - ret, output, err = run_c(c_src, input=input, link=["xdot"]) + output, err = run_c(c_src, input=input, link=["xdot"]) except subprocess.CalledProcessError: # FIXME: Remove this try-catch when # https://gitlab.com/graphviz/graphviz/-/issues/1777 is fixed @@ -1355,7 +1348,6 @@ def test_xdot_json(): pytest.skip("Windows MSBuild release does not contain any header " "files (#1777)") raise - assert ret == 0 assert err == "" if os.getenv("build_system") == "msbuild": diff --git a/rtest/test_sprint.py b/rtest/test_sprint.py index e8c00d725..8b5d52de5 100644 --- a/rtest/test_sprint.py +++ b/rtest/test_sprint.py @@ -20,6 +20,4 @@ def test_sprint(): # extra C flags this compilation needs cflags = ['-I', lib] - ret, _, _ = run_c(src, cflags=cflags) - - assert ret == 0 + _, _ = run_c(src, cflags=cflags) diff --git a/rtest/test_vmalloc.py b/rtest/test_vmalloc.py index 829166f03..98de900ab 100644 --- a/rtest/test_vmalloc.py +++ b/rtest/test_vmalloc.py @@ -23,6 +23,4 @@ def test_vmalloc(): if platform.system() != "Windows": cflags += ["-std=gnu99", "-Wall", "-Wextra", "-Werror"] - ret, _, _ = run_c(src, cflags=cflags) - - assert ret == 0 + _, _ = run_c(src, cflags=cflags) -- 2.40.0