# HG changeset patch # User Pierre-Yves David # Date 2021-08-03 17:26:26 # Node ID 5ad37164a8fe8c11d270431e75cdad78494fb100 # Parent 580bca200874aecd9214ffaad1f3a75d8e1ff4c4 testing: make sure write_file is "atomic" This make sure viewer cannot see the new file with partial content. This was likely the cause of some flakiness in `test-nointerrupt.t` Differential Revision: https://phab.mercurial-scm.org/D11250 diff --git a/mercurial/testing/__init__.py b/mercurial/testing/__init__.py --- a/mercurial/testing/__init__.py +++ b/mercurial/testing/__init__.py @@ -33,5 +33,11 @@ def wait_file(path, timeout=10): def write_file(path, content=b''): - with open(path, 'wb') as f: + if content: + write_path = b'%s.tmp' % path + else: + write_path = path + with open(write_path, 'wb') as f: f.write(content) + if path != write_path: + os.rename(write_path, path)