Mercurial > hg
annotate mercurial/testing/__init__.py @ 51723:9367571fea21
cext: correct the argument handling of `b85encode()`
The type stub indicated that this argument is `Optional`, which implies None is
allowed. I don't see in the documentation where that's the case for `i`[1], and
trying it in `hg debugshell` resulted in the method failing with a TypeError. I
guess it was typed as an `int` argument because the `p` format unit wasn't added
until Python 3.3[2].
In any event, 2 clients in core (`pvec` and `obsolete`) call this with no
argument supplied, and `mdiff` calls it with True. So I guess we've avoided the
None arg case, and when no arg is supplied, it defaults to the 0 initialization
of the `pad` variable in C. Since the `p` format unit accepts both `int` and
None, as well as `bool`, I'm not bothering to bump the module version- this code
is more permissive than it was, in addition to being more correct.
Interestingly, when I first imported the `cext` and `pure` methods in the same
manner as the previous commit, it dropped the `Optional` part of the argument
type when generating `util.pyi`. No idea why.
[1] https://docs.python.org/3/c-api/arg.html#numbers
[2] https://docs.python.org/3/c-api/arg.html#other-objects
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Sat, 20 Jul 2024 01:55:09 -0400 |
parents | 8e0d823ef182 |
children | f4733654f144 |
rev | line source |
---|---|
46984
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
1 import os |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
2 import time |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
3 |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
4 |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
5 # work around check-code complains |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
6 # |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
7 # This is a simple log level module doing simple test related work, we can't |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
8 # import more things, and we do not need it. |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
9 environ = getattr(os, 'environ') |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
10 |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
11 |
50214
8e0d823ef182
testing: introduce util function to synchronize concurrent commands on files
Raphaël Gomès <rgomes@octobus.net>
parents:
48875
diff
changeset
|
12 def wait_on_cfg(ui, cfg, timeout=10): |
8e0d823ef182
testing: introduce util function to synchronize concurrent commands on files
Raphaël Gomès <rgomes@octobus.net>
parents:
48875
diff
changeset
|
13 """synchronize on the `cfg` config path |
8e0d823ef182
testing: introduce util function to synchronize concurrent commands on files
Raphaël Gomès <rgomes@octobus.net>
parents:
48875
diff
changeset
|
14 |
8e0d823ef182
testing: introduce util function to synchronize concurrent commands on files
Raphaël Gomès <rgomes@octobus.net>
parents:
48875
diff
changeset
|
15 Use this to synchronize commands during race tests. |
8e0d823ef182
testing: introduce util function to synchronize concurrent commands on files
Raphaël Gomès <rgomes@octobus.net>
parents:
48875
diff
changeset
|
16 """ |
8e0d823ef182
testing: introduce util function to synchronize concurrent commands on files
Raphaël Gomès <rgomes@octobus.net>
parents:
48875
diff
changeset
|
17 full_config = b'sync.' + cfg |
8e0d823ef182
testing: introduce util function to synchronize concurrent commands on files
Raphaël Gomès <rgomes@octobus.net>
parents:
48875
diff
changeset
|
18 wait_config = full_config + b'-timeout' |
8e0d823ef182
testing: introduce util function to synchronize concurrent commands on files
Raphaël Gomès <rgomes@octobus.net>
parents:
48875
diff
changeset
|
19 sync_path = ui.config(b'devel', full_config) |
8e0d823ef182
testing: introduce util function to synchronize concurrent commands on files
Raphaël Gomès <rgomes@octobus.net>
parents:
48875
diff
changeset
|
20 if sync_path is not None: |
8e0d823ef182
testing: introduce util function to synchronize concurrent commands on files
Raphaël Gomès <rgomes@octobus.net>
parents:
48875
diff
changeset
|
21 timeout = ui.config(b'devel', wait_config) |
8e0d823ef182
testing: introduce util function to synchronize concurrent commands on files
Raphaël Gomès <rgomes@octobus.net>
parents:
48875
diff
changeset
|
22 ready_path = sync_path + b'.waiting' |
8e0d823ef182
testing: introduce util function to synchronize concurrent commands on files
Raphaël Gomès <rgomes@octobus.net>
parents:
48875
diff
changeset
|
23 write_file(ready_path) |
8e0d823ef182
testing: introduce util function to synchronize concurrent commands on files
Raphaël Gomès <rgomes@octobus.net>
parents:
48875
diff
changeset
|
24 wait_file(sync_path, timeout=timeout) |
8e0d823ef182
testing: introduce util function to synchronize concurrent commands on files
Raphaël Gomès <rgomes@octobus.net>
parents:
48875
diff
changeset
|
25 |
8e0d823ef182
testing: introduce util function to synchronize concurrent commands on files
Raphaël Gomès <rgomes@octobus.net>
parents:
48875
diff
changeset
|
26 |
46984
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
27 def _timeout_factor(): |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
28 """return the current modification to timeout""" |
47493
2dac94edd98d
testing: fix _timeout_factor
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
46985
diff
changeset
|
29 default = int(environ.get('HGTEST_TIMEOUT_DEFAULT', 360)) |
46984
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
30 current = int(environ.get('HGTEST_TIMEOUT', default)) |
47493
2dac94edd98d
testing: fix _timeout_factor
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
46985
diff
changeset
|
31 if current == 0: |
2dac94edd98d
testing: fix _timeout_factor
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
46985
diff
changeset
|
32 return 1 |
46984
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
33 return current / float(default) |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
34 |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
35 |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
36 def wait_file(path, timeout=10): |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
37 timeout *= _timeout_factor() |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
38 start = time.time() |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
39 while not os.path.exists(path): |
47657
1bad89a67745
testing: do not stop waiting if timeout is 0 (issue6541)
Cédric Krier <ced@b2ck.com>
parents:
47493
diff
changeset
|
40 if timeout and time.time() - start > timeout: |
46984
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
41 raise RuntimeError(b"timed out waiting for file: %s" % path) |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
42 time.sleep(0.01) |
46985
52cee44aa1a0
testing: add a `write_file` function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
46984
diff
changeset
|
43 |
52cee44aa1a0
testing: add a `write_file` function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
46984
diff
changeset
|
44 |
52cee44aa1a0
testing: add a `write_file` function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
46984
diff
changeset
|
45 def write_file(path, content=b''): |
47804
5ad37164a8fe
testing: make sure write_file is "atomic"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47657
diff
changeset
|
46 if content: |
5ad37164a8fe
testing: make sure write_file is "atomic"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47657
diff
changeset
|
47 write_path = b'%s.tmp' % path |
5ad37164a8fe
testing: make sure write_file is "atomic"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47657
diff
changeset
|
48 else: |
5ad37164a8fe
testing: make sure write_file is "atomic"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47657
diff
changeset
|
49 write_path = path |
5ad37164a8fe
testing: make sure write_file is "atomic"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47657
diff
changeset
|
50 with open(write_path, 'wb') as f: |
46985
52cee44aa1a0
testing: add a `write_file` function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
46984
diff
changeset
|
51 f.write(content) |
47804
5ad37164a8fe
testing: make sure write_file is "atomic"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47657
diff
changeset
|
52 if path != write_path: |
5ad37164a8fe
testing: make sure write_file is "atomic"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47657
diff
changeset
|
53 os.rename(write_path, path) |