Debian Codegraph @

tests/fsmonitor-run-tests.py

raw
1 #!/usr/bin/env python3
2
3 # fsmonitor-run-tests.py - Run Mercurial tests with fsmonitor enabled
4 #
5 # Copyright 2017 Facebook, Inc.
6 #
7 # This software may be used and distributed according to the terms of the
8 # GNU General Public License version 2 or any later version.
9 #
10 # This is a wrapper around run-tests.py that spins up an isolated instance of
11 # Watchman and runs the Mercurial tests against it. This ensures that the global
12 # version of Watchman isn't affected by anything this test does.
13
14
15 import argparse
16 import contextlib
17 import json
18 import os
19 import shutil
20 import subprocess
21 import sys
22 import tempfile
23 import uuid
24
25 osenvironb = getattr(os, 'environb', os.environ)
26
27 # Don't compare sys.version_info directly, to prevent pyupgrade from dropping
28 # the conditional.
29 sys_version_info = sys.version_info
30
31 if sys_version_info >= (3, 8, 0):
32
33 def _sys2bytes(p):
34 return p.encode('utf-8')
35
36 else:
37 print(
38 '%s is only supported on Python 3.8+, not %s'
39 % (sys.argv[0], '.'.join(str(v) for v in sys.version_info[:3]))
40 )
41 sys.exit(70) # EX_SOFTWARE from `man 3 sysexit`
42
43
44 def getparser():
45 """Obtain the argument parser used by the CLI."""
46 parser = argparse.ArgumentParser(
47 description='Run tests with fsmonitor enabled.',
48 epilog='Unrecognized options are passed to run-tests.py.',
49 )
50 # - keep these sorted
51 # - none of these options should conflict with any in run-tests.py
52 parser.add_argument(
53 '--keep-fsmonitor-tmpdir',
54 action='store_true',
55 help='keep temporary directory with fsmonitor state',
56 )
57 parser.add_argument(
58 '--watchman',
59 help='location of watchman binary (default: watchman in PATH)',
60 default='watchman',
61 )
62
63 return parser
64
65
66 @contextlib.contextmanager
67 def watchman(args):
68 basedir = tempfile.mkdtemp(prefix='hg-fsmonitor')
69 try:
70 # Much of this configuration is borrowed from Watchman's test harness.
71 cfgfile = os.path.join(basedir, 'config.json')
72 # TODO: allow setting a config
73 with open(cfgfile, 'w') as f:
74 f.write(json.dumps({}))
75
76 logfile = os.path.join(basedir, 'log')
77 clilogfile = os.path.join(basedir, 'cli-log')
78 if os.name == 'nt':
79 sockfile = '\\\\.\\pipe\\watchman-test-%s' % uuid.uuid4().hex
80 else:
81 sockfile = os.path.join(basedir, 'sock')
82 pidfile = os.path.join(basedir, 'pid')
83 statefile = os.path.join(basedir, 'state')
84
85 argv = [
86 args.watchman,
87 '--sockname',
88 sockfile,
89 '--logfile',
90 logfile,
91 '--pidfile',
92 pidfile,
93 '--statefile',
94 statefile,
95 '--foreground',
96 '--log-level=2', # debug logging for watchman
97 ]
98
99 envb = osenvironb.copy()
100 envb[b'WATCHMAN_CONFIG_FILE'] = _sys2bytes(cfgfile)
101 with open(clilogfile, 'wb') as f:
102 proc = subprocess.Popen(
103 argv, env=envb, stdin=None, stdout=f, stderr=f
104 )
105 try:
106 yield sockfile
107 finally:
108 proc.terminate()
109 proc.kill()
110 finally:
111 if args.keep_fsmonitor_tmpdir:
112 print('fsmonitor dir available at %s' % basedir)
113 else:
114 shutil.rmtree(basedir, ignore_errors=True)
115
116
117 def run():
118 parser = getparser()
119 args, runtestsargv = parser.parse_known_args()
120
121 with watchman(args) as sockfile:
122 osenvironb[b'WATCHMAN_SOCK'] = _sys2bytes(sockfile)
123 # Indicate to hghave that we're running with fsmonitor enabled.
124 osenvironb[b'HGFSMONITOR_TESTS'] = b'1'
125
126 runtestdir = os.path.dirname(__file__)
127 runtests = os.path.join(runtestdir, 'run-tests.py')
128 blacklist = os.path.join(runtestdir, 'blacklists', 'fsmonitor')
129
130 runtestsargv.insert(0, runtests)
131 runtestsargv.extend(
132 [
133 '--extra-config',
134 'extensions.fsmonitor=',
135 # specify fsmonitor.mode=paranoid always in order to force
136 # fsmonitor extension execute "paranoid" code path
137 #
138 # TODO: make fsmonitor-run-tests.py accept specific options
139 '--extra-config',
140 'fsmonitor.mode=paranoid',
141 '--blacklist',
142 blacklist,
143 ]
144 )
145
146 return subprocess.call(runtestsargv)
147
148
149 if __name__ == '__main__':
150 sys.exit(run())