testing: make sure write_file is "atomic" stable
authorPierre-Yves David <pierre-yves.david@octobus.net>
Tue, 03 Aug 2021 19:26:26 +0200
branchstable
changeset 47799 5ad37164a8fe
parent 47798 580bca200874
child 47800 8892f604e242
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
mercurial/testing/__init__.py
--- a/mercurial/testing/__init__.py	Wed Aug 04 19:45:13 2021 +0200
+++ b/mercurial/testing/__init__.py	Tue Aug 03 19:26:26 2021 +0200
@@ -33,5 +33,11 @@
 
 
 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)