]> granicus.if.org Git - graphviz/commitdiff
helper test function for running C code
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 27 Feb 2021 03:12:30 +0000 (19:12 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 6 Mar 2021 01:30:19 +0000 (17:30 -0800)
rtest/test_regression.py

index a18888f2ebd77d6eb7a511c638de6a0278eefb63..2161194b3cfdb5646a5e40ac10b2b19cd960d01e 100644 (file)
@@ -8,7 +8,7 @@ import os
 import re
 import stat
 import tempfile
-from typing import List, Optional
+from typing import List, Optional, Tuple
 
 # The terminology used in rtest.py is a little inconsistent. At the
 # end it reports the total number of tests, the number of "failures"
@@ -74,6 +74,27 @@ def compile_c(src: Path, link: List[str] = [], dst: Optional[Path] = None) \
 
     return dst
 
+def run_c(src: Path, args: [str] = [], input: str = '', link: List[str] = []) \
+  -> Tuple[int, str, str]:
+    '''
+    compile and run a C program
+    '''
+
+    # create some temporary space to work in
+    with tempfile.TemporaryDirectory() as tmp:
+
+        # output filename to write our compiled code to
+        exe = Path(tmp) / 'a.exe'
+
+        # compile the program
+        compile_c(src, link, exe)
+
+        # run it
+        p = subprocess.run([exe] + args, input=input, stdout=subprocess.PIPE,
+          stderr=subprocess.PIPE, universal_newlines=True)
+
+        return p.returncode, p.stdout, p.stderr
+
 def test_165():
     '''
     dot should be able to produce properly escaped xdot output