[Git][ghc/ghc][wip/testsuite-fixes] 3 commits: testsuite: Refactor ghostscript detection

Ben Gamari gitlab at gitlab.haskell.org
Wed May 27 19:12:33 UTC 2020



Ben Gamari pushed to branch wip/testsuite-fixes at Glasgow Haskell Compiler / GHC


Commits:
71504a8d by Ben Gamari at 2020-05-27T15:07:47-04:00
testsuite: Refactor ghostscript detection

Tamar reported that he saw crashes due to unhandled exceptions.

- - - - -
8d5f51ec by Ben Gamari at 2020-05-27T15:10:52-04:00
testsuite/perf_notes: Fix ill-typed assignments

- - - - -
ad599da0 by Ben Gamari at 2020-05-27T15:12:12-04:00
testsuite/testutil: Fix bytes/str mismatch

- - - - -


4 changed files:

- testsuite/driver/perf_notes.py
- testsuite/driver/testglobals.py
- testsuite/driver/testlib.py
- testsuite/driver/testutil.py


Changes:

=====================================
testsuite/driver/perf_notes.py
=====================================
@@ -126,10 +126,11 @@ def get_perf_stats(commit: Union[GitRef, GitHash]=GitRef('HEAD'),
     except subprocess.CalledProcessError:
         return []
 
-    log = log.strip('\n').split('\n')
-    log = list(filter(None, log))
-    log = [parse_perf_stat(stat_str) for stat_str in log]
-    return log
+    return \
+        [ parse_perf_stat(stat_str)
+          for stat_str in log.strip('\n').split('\n')
+          if stat_str != ''
+        ]
 
 # Check if a str is in a 40 character git commit hash.
 _commit_hash_re = re.compile('[0-9a-f]' * 40)


=====================================
testsuite/driver/testglobals.py
=====================================
@@ -43,7 +43,7 @@ class TestConfig:
         self.summary_file = ''
 
         # Path to Ghostscript
-        self.gs = ''
+        self.gs = None # type: Optional[Path]
 
         # Run tests requiring Haddock
         self.haddock = False


=====================================
testsuite/driver/testlib.py
=====================================
@@ -22,7 +22,7 @@ from testglobals import config, ghc_env, default_testopts, brokens, t, \
                         TestRun, TestResult, TestOptions, PerfMetric
 from testutil import strip_quotes, lndir, link_or_copy_file, passed, \
                      failBecause, testing_metrics, \
-                     PassFail
+                     PassFail, memoize
 from term_color import Color, colored
 import testutil
 from cpu_features import have_cpu_feature
@@ -1895,7 +1895,7 @@ def check_hp_ok(name: TestName) -> bool:
 
     if hp2psResult == 0:
         if actual_ps_path.exists():
-            if gs_working:
+            if does_ghostscript_work():
                 gsResult = runCmd(genGSCmd(actual_ps_path))
                 if (gsResult == 0):
                     return True
@@ -2335,31 +2335,38 @@ def runCmd(cmd: str,
 
 # -----------------------------------------------------------------------------
 # checking if ghostscript is available for checking the output of hp2ps
-
 def genGSCmd(psfile: Path) -> str:
     return '{{gs}} -dNODISPLAY -dBATCH -dQUIET -dNOPAUSE "{0}"'.format(psfile)
 
-def gsNotWorking() -> None:
-    global gs_working
-    print("GhostScript not available for hp2ps tests")
-
-global gs_working
-gs_working = False
-if config.have_profiling:
-  if config.gs != '':
-    resultGood = runCmd(genGSCmd(config.top + '/config/good.ps'));
-    if resultGood == 0:
-        resultBad = runCmd(genGSCmd(config.top + '/config/bad.ps') +
-                                   ' >/dev/null 2>&1')
-        if resultBad != 0:
-            print("GhostScript available for hp2ps tests")
-            gs_working = True
-        else:
-            gsNotWorking();
-    else:
-        gsNotWorking();
-  else:
-    gsNotWorking();
+ at memoize
+def does_ghostscript_work() -> bool:
+    """
+    Detect whether Ghostscript is functional.
+    """
+    def gsNotWorking(reason: str) -> None:
+        print("GhostScript not available for hp2ps tests:", reason)
+
+    if config.gs is None:
+        return False
+
+    try:
+        if runCmd(genGSCmd(config.top / 'config' / 'good.ps')) != 0:
+            gsNotWorking("gs can't process good input")
+            return False
+    except Exception as e:
+        gsNotWorking('error invoking gs on bad input: %s' % e)
+        return False
+
+    try:
+        cmd = genGSCmd(config.top / 'config' / 'bad.ps') + ' >/dev/null 2>&1'
+        if runCmd(cmd) == 0:
+            gsNotWorking('gs accepts bad input')
+            return False
+    except Exception as e:
+        gsNotWorking('error invoking gs on bad input: %s' % e)
+        return False
+
+    return True
 
 def add_suffix( name: Union[str, Path], suffix: str ) -> Path:
     if suffix == '':


=====================================
testsuite/driver/testutil.py
=====================================
@@ -55,7 +55,7 @@ def getStdout(cmd_and_args: List[str]):
     if r != 0:
         raise Exception("Command failed: " + str(cmd_and_args))
     if stderr:
-        raise Exception("stderr from command: %s\nOutput:\n%s\n" % (cmd_and_args, stderr))
+        raise Exception("stderr from command: %s\nOutput:\n%s\n" % (cmd_and_args, stderr.decode('utf-8')))
     return stdout.decode('utf-8')
 
 def lndir(srcdir: Path, dstdir: Path):
@@ -131,3 +131,16 @@ class Watcher(object):
         if self.pool <= 0:
             self.evt.set()
         self.sync_lock.release()
+
+def memoize(f):
+    """
+    A decorator to memoize a nullary function.
+    """
+    def cached():
+        if cached._cache is None:
+            cached._cache = f()
+
+        return cached._cache
+
+    cached._cache = None
+    return cached



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/d60270c120ad060218bd3336c529753d09a9464d...ad599da053fae69172ba44710e560abebc0e6b4d

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/d60270c120ad060218bd3336c529753d09a9464d...ad599da053fae69172ba44710e560abebc0e6b4d
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20200527/bdd39fce/attachment-0001.html>


More information about the ghc-commits mailing list