summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Gamari <ben@smart-cactus.org>2020-05-27 15:07:47 -0400
committerMarge Bot <ben+marge-bot@smart-cactus.org>2020-06-01 06:38:26 -0400
commit4a7229ef361307e16574b85f71f473f936728890 (patch)
tree4ea56b14d558231fca3388a55cb7890805a5e978
parent15857ad856b6072d2c6a34b2bf7aa7316d7e2b12 (diff)
downloadhaskell-4a7229ef361307e16574b85f71f473f936728890.tar.gz
testsuite: Refactor ghostscript detection
Tamar reported that he saw crashes due to unhandled exceptions.
-rw-r--r--testsuite/driver/testglobals.py2
-rw-r--r--testsuite/driver/testlib.py55
-rw-r--r--testsuite/driver/testutil.py13
3 files changed, 45 insertions, 25 deletions
diff --git a/testsuite/driver/testglobals.py b/testsuite/driver/testglobals.py
index ceee5df9a8..e20a8329d7 100644
--- a/testsuite/driver/testglobals.py
+++ b/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
diff --git a/testsuite/driver/testlib.py b/testsuite/driver/testlib.py
index ad9c0852e9..95b0983ba0 100644
--- a/testsuite/driver/testlib.py
+++ b/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();
+@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 == '':
diff --git a/testsuite/driver/testutil.py b/testsuite/driver/testutil.py
index 34d78cda99..683941c65c 100644
--- a/testsuite/driver/testutil.py
+++ b/testsuite/driver/testutil.py
@@ -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