hghave: move feature checking into hghave.py
authorGregory Szorc <gregory.szorc@gmail.com>
Sat, 22 Aug 2015 10:28:34 -0700
changeset 26067 8107c308ff22
parent 26066 89872688893f
child 26068 05e7f57c74ac
hghave: move feature checking into hghave.py Upcoming patches will kill hghave (the script - not hghave.py) and will move to a model where requirements checking is performed as a function call. Start diminishing the utility of hghave by moving some code to hghave.py.
tests/hghave
tests/hghave.py
--- a/tests/hghave	Sat Aug 22 10:22:12 2015 -0700
+++ b/tests/hghave	Sat Aug 22 10:28:34 2015 -0700
@@ -64,36 +64,4 @@
     if options.test_features:
         sys.exit(test_features())
 
-    quiet = options.quiet
-
-    failures = 0
-
-    def error(msg):
-        global failures
-        if not quiet:
-            sys.stderr.write(msg + '\n')
-        failures += 1
-
-    for feature in args:
-        negate = feature.startswith('no-')
-        if negate:
-            feature = feature[3:]
-
-        if feature not in checks:
-            error('skipped: unknown feature: ' + feature)
-            sys.exit(2)
-
-        check, desc = checks[feature]
-        try:
-            available = check()
-        except Exception, e:
-            error('hghave check failed: ' + feature)
-            continue
-
-        if not negate and not available:
-            error('skipped: missing feature: ' + desc)
-        elif negate and available:
-            error('skipped: system supports %s' % desc)
-
-    if failures != 0:
-        sys.exit(1)
+    hghave.require(args, options.quiet)
--- a/tests/hghave.py	Sat Aug 22 10:22:12 2015 -0700
+++ b/tests/hghave.py	Sat Aug 22 10:28:34 2015 -0700
@@ -17,6 +17,54 @@
         return func
     return decorator
 
+def checkfeatures(features):
+    result = {
+        'error': [],
+        'missing': [],
+        'skipped': [],
+    }
+
+    for feature in features:
+        negate = feature.startswith('no-')
+        if negate:
+            feature = feature[3:]
+
+        if feature not in checks:
+            result['missing'].append(feature)
+            continue
+
+        check, desc = checks[feature]
+        try:
+            available = check()
+        except Exception:
+            result['error'].append('hghave check failed: %s' % feature)
+            continue
+
+        if not negate and not available:
+            result['skipped'].append('missing feature: %s' % desc)
+        elif negate and available:
+            result['skipped'].append('system supports %s' % desc)
+
+    return result
+
+def require(features, quiet=False):
+    """Require that features are available, exiting if not."""
+    result = checkfeatures(features)
+
+    if not quiet:
+        for missing in result['missing']:
+            sys.stderr.write('skipped: unknown feature: %s\n' % missing)
+        for msg in result['skipped']:
+            sys.stderr.write('skipped: %s\n' % msg)
+        for msg in result['error']:
+            sys.stderr.write('%s\n' % msg)
+
+    if result['missing']:
+        sys.exit(2)
+
+    if result['skipped'] or result['error']:
+        sys.exit(1)
+
 def matchoutput(cmd, regexp, ignorestatus=False):
     """Return True if cmd executes successfully and its output
     is matched by the supplied regular expression.