hghave: allow adding customized features at runtime
authorFUJIWARA Katsunori <foozy@lares.dti.ne.jp>
Fri, 03 Jul 2015 06:56:03 +0900
changeset 25732 b94df10cc3b5
parent 25731 cd1daab5d036
child 25733 f99c066f5f9a
hghave: allow adding customized features at runtime Before this patch, there is no way to add customized features to `hghave` without changing `hghave` and `hghave.py` themselves. This decreases reusability of `run-tests.py` framework for third party tools, because they may want to examine custom features at runtime (e.g. existence of some external tools). To allow adding customized features at runtime, this patch makes `hghave` import `hghaveaddon` module, only when `hghaveaddon.py` file can be found in directories below: - `TESTDIR` for invocation via `run-tests.py` - `.` for invocation via command line The path to the directory where `hghaveaddon.py` should be placed is added to `sys.path` only while importing `hghaveaddon`, because: - `.` may not be added to `PYTHONPATH` - adding additional path to `sys.path` may change behavior of subsequent `import` for other features `hghave` is terminated with exit code '2' at failure of `import hghaveaddon`, because exit code '2' terminates `run-tests.py` immediately. This is a one of preparations for issue4677.
tests/hghave
tests/test-hghave.t
--- a/tests/hghave	Fri Jul 03 06:56:03 2015 +0900
+++ b/tests/hghave	Fri Jul 03 06:56:03 2015 +0900
@@ -4,7 +4,7 @@
 prefixed with "no-", the absence of feature is tested.
 """
 import optparse
-import sys
+import os, sys
 import hghave
 
 checks = hghave.checks
@@ -33,8 +33,30 @@
 parser.add_option("-q", "--quiet", action="store_true",
                   help="check features silently")
 
+def _loadaddon(quiet):
+    if 'TESTDIR' in os.environ:
+        # loading from '.' isn't needed, because `hghave` should be
+        # running at TESTTMP in this case
+        path = os.environ['TESTDIR']
+    else:
+        path = '.'
+
+    if not os.path.exists(os.path.join(path, 'hghaveaddon.py')):
+        return
+
+    sys.path.insert(0, path)
+    try:
+        import hghaveaddon
+    except BaseException, inst:
+        if not quiet:
+            sys.stderr.write('failed to import hghaveaddon.py from %r: %s\n'
+                             % (path, inst))
+        sys.exit(2)
+    sys.path.pop(0)
+
 if __name__ == '__main__':
     options, args = parser.parse_args()
+    _loadaddon(options.quiet)
     if options.list_features:
         list_features()
         sys.exit(0)
--- a/tests/test-hghave.t	Fri Jul 03 06:56:03 2015 +0900
+++ b/tests/test-hghave.t	Fri Jul 03 06:56:03 2015 +0900
@@ -1,3 +1,39 @@
 Testing that hghave does not crash when checking features
 
   $ hghave --test-features 2>/dev/null
+
+Testing hghave extensibility for third party tools
+
+  $ cat > hghaveaddon.py <<EOF
+  > import hghave
+  > @hghave.check("custom", "custom hghave feature")
+  > def has_custom():
+  >     return True
+  > EOF
+
+(invocation via run-tests.py)
+
+  $ cat > test-hghaveaddon.t <<EOF
+  > #require custom
+  >   $ echo foo
+  >   foo
+  > EOF
+  $ run-tests.py test-hghaveaddon.t
+  .
+  # Ran 1 tests, 0 skipped, 0 warned, 0 failed.
+
+(invocation via command line)
+
+  $ unset TESTDIR
+  $ hghave custom
+
+(terminate with exit code 2 at failure of importing hghaveaddon.py)
+
+  $ rm hghaveaddon.*
+  $ cat > hghaveaddon.py <<EOF
+  > importing this file should cause syntax error
+  > EOF
+
+  $ hghave custom
+  failed to import hghaveaddon.py from '.': invalid syntax (hghaveaddon.py, line 1)
+  [2]