changeset 4881:c51c9bc4579d

Add hghave utility and run-tests.py support. hghave detects system features like symlinks availability at run-time. Tests can be skipped by starting them with: "$TESTDIR/hghave" symlink || exit 80 The 80 exit status triggers hghave output handling by run-tests.py. Also, tests output can be locally patched on the fly to match reference output.
author Patrick Mezard <pmezard@gmail.com>
date Sat, 14 Jul 2007 18:34:06 +0200
parents 6403f948bd6b
children 25d753efd48e
files tests/hghave tests/run-tests.py
diffstat 2 files changed, 81 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/hghave	Sat Jul 14 18:34:06 2007 +0200
@@ -0,0 +1,55 @@
+#!/usr/bin/env python
+"""Test the running system for features availability. Exit with zero
+if all features are there, non-zero otherwise.
+"""
+import optparse
+import os
+import sys
+
+def has_symlink():
+    return hasattr(os, "symlink")
+
+checks = {
+    "symlink": (has_symlink, "symbolic links"),
+}
+
+def list_features():
+    for name, feature in checks.iteritems():
+        desc = feature[1]
+        print name + ':', desc
+
+parser = optparse.OptionParser("%prog [options] [features]")
+parser.add_option("--list-features", action="store_true",
+                  help="list available features")
+parser.add_option("-q", "--quiet", action="store_true",
+                  help="check features silently")
+
+if __name__ == '__main__':
+    options, args = parser.parse_args()
+    if options.list_features:
+        list_features()
+        sys.exit(0)
+        
+    quiet = options.quiet
+
+    failures = 0
+
+    def error(msg):
+        global failures
+        if not quiet:
+            sys.stderr.write(msg + '\n')
+        failures += 1
+    
+    for feature in args:
+        if feature not in checks:
+            error('hghave: unknown feature: ' + feature)
+            continue
+        
+        check, desc = checks[feature]       
+        if not check():
+            error('hghave: missing feature: ' + desc)
+
+    if failures != 0:
+        sys.exit(1)
+
+    
--- a/tests/run-tests.py	Sat Jul 14 14:01:11 2007 +0200
+++ b/tests/run-tests.py	Sat Jul 14 18:34:06 2007 +0200
@@ -19,6 +19,9 @@
 import tempfile
 import time
 
+# hghave reserved exit code to skip test
+SKIPPED_STATUS = 80
+
 required_tools = ["python", "diff", "grep", "unzip", "gunzip", "bunzip2", "sed"]
 
 parser = optparse.OptionParser("%prog [options] [tests]")
@@ -68,6 +71,17 @@
         lines.append(text[i:n+1])
         i = n + 1
 
+def extract_missing_features(lines):
+    '''Extract missing/unknown features log lines as a list'''
+    missing = []
+    for line in lines:
+        if not line.startswith('hghave: '):
+            continue
+        line = line.splitlines()[0]
+        missing.append(line[8:])
+
+    return missing
+
 def show_diff(expected, output):
     for line in difflib.unified_diff(expected, output,
             "Expected output", "Test output"):
@@ -283,6 +297,7 @@
     if options.timeout > 0:
         signal.alarm(0)
 
+    skipped = (ret == SKIPPED_STATUS)
     diffret = 0
     # If reference output file exists, check test output against it
     if os.path.exists(ref):
@@ -291,16 +306,22 @@
         f.close()
     else:
         ref_out = []
-    if out != ref_out:
+    if not skipped and out != ref_out:
         diffret = 1
         print "\nERROR: %s output changed" % (test)
         show_diff(ref_out, out)
-    if ret:
+    if skipped:
+        missing = extract_missing_features(out)
+        if not missing:
+            missing = ['irrelevant']
+        print '\nSkipping %s: %s' % (test, missing[-1])
+    elif ret:
         print "\nERROR: %s failed with error code %d" % (test, ret)
     elif diffret:
         ret = diffret
 
-    if ret != 0: # Save errors to a file for diagnosis
+    if ret != 0 and not skipped: 
+        # Save errors to a file for diagnosis
         f = open(err, "wb")
         for line in out:
             f.write(line)
@@ -332,6 +353,8 @@
 
     os.chdir(TESTDIR)
     shutil.rmtree(tmpd, True)
+    if skipped:
+        return None
     return ret == 0