comparison tests/hghave @ 1106:cca56bbea143

tests: copy hg test infra from hg repo @5cfdf6137af8
author Tony Tung <ttung@chanzuckerberg.com <mailto:ttung@chanzuckerberg.com>>
date Tue, 06 Feb 2018 16:34:28 -0800
parents 8c6dc6a6f5d8
children
comparison
equal deleted inserted replaced
1105:bb255eaf3d14 1106:cca56bbea143
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 """Test the running system for features availability. Exit with zero 2 """Test the running system for features availability. Exit with zero
3 if all features are there, non-zero otherwise. If a feature name is 3 if all features are there, non-zero otherwise. If a feature name is
4 prefixed with "no-", the absence of feature is tested. 4 prefixed with "no-", the absence of feature is tested.
5 """ 5 """
6
7 from __future__ import absolute_import, print_function
8
9 import hghave
6 import optparse 10 import optparse
11 import os
7 import sys 12 import sys
8 import hghave
9 13
10 checks = hghave.checks 14 checks = hghave.checks
11 15
12 def list_features(): 16 def list_features():
13 for name, feature in checks.iteritems(): 17 for name, feature in sorted(checks.items()):
14 desc = feature[1] 18 desc = feature[1]
15 print name + ':', desc 19 print(name + ':', desc)
16 20
17 def test_features(): 21 def test_features():
18 failed = 0 22 failed = 0
19 for name, feature in checks.iteritems(): 23 for name, feature in checks.items():
20 check, _ = feature 24 check, _ = feature
21 try: 25 try:
22 check() 26 check()
23 except Exception, e: 27 except Exception as e:
24 print "feature %s failed: %s" % (name, e) 28 print("feature %s failed: %s" % (name, e))
25 failed += 1 29 failed += 1
26 return failed 30 return failed
27 31
28 parser = optparse.OptionParser("%prog [options] [features]") 32 parser = optparse.OptionParser("%prog [options] [features]")
29 parser.add_option("--test-features", action="store_true", 33 parser.add_option("--test-features", action="store_true",
30 help="test available features") 34 help="test available features")
31 parser.add_option("--list-features", action="store_true", 35 parser.add_option("--list-features", action="store_true",
32 help="list available features") 36 help="list available features")
33 parser.add_option("-q", "--quiet", action="store_true", 37
34 help="check features silently") 38 def _loadaddon():
39 if 'TESTDIR' in os.environ:
40 # loading from '.' isn't needed, because `hghave` should be
41 # running at TESTTMP in this case
42 path = os.environ['TESTDIR']
43 else:
44 path = '.'
45
46 if not os.path.exists(os.path.join(path, 'hghaveaddon.py')):
47 return
48
49 sys.path.insert(0, path)
50 try:
51 import hghaveaddon
52 assert hghaveaddon # silence pyflakes
53 except BaseException as inst:
54 sys.stderr.write('failed to import hghaveaddon.py from %r: %s\n'
55 % (path, inst))
56 sys.exit(2)
57 sys.path.pop(0)
35 58
36 if __name__ == '__main__': 59 if __name__ == '__main__':
37 options, args = parser.parse_args() 60 options, args = parser.parse_args()
61 _loadaddon()
38 if options.list_features: 62 if options.list_features:
39 list_features() 63 list_features()
40 sys.exit(0) 64 sys.exit(0)
41 65
42 if options.test_features: 66 if options.test_features:
43 sys.exit(test_features()) 67 sys.exit(test_features())
44 68
45 quiet = options.quiet 69 hghave.require(args)
46
47 failures = 0
48
49 def error(msg):
50 global failures
51 if not quiet:
52 sys.stderr.write(msg + '\n')
53 failures += 1
54
55 for feature in args:
56 negate = feature.startswith('no-')
57 if negate:
58 feature = feature[3:]
59
60 if feature not in checks:
61 error('skipped: unknown feature: ' + feature)
62 continue
63
64 check, desc = checks[feature]
65 try:
66 available = check()
67 except Exception, e:
68 error('hghave check failed: ' + feature)
69 continue
70
71 if not negate and not available:
72 error('skipped: missing feature: ' + desc)
73 elif negate and available:
74 error('skipped: system supports %s' % desc)
75
76 if failures != 0:
77 sys.exit(1)