Patchwork D11250: testing: make sure write_file is "atomic"

login
register
mail settings
Submitter phabricator
Date Aug. 4, 2021, 8:42 p.m.
Message ID <differential-rev-PHID-DREV-yw3iccvyginu6rid3tmm-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/49562/
State Superseded
Headers show

Comments

phabricator - Aug. 4, 2021, 8:42 p.m.
marmoute created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.
marmoute added a comment.
Alphare accepted this revision.
This revision is now accepted and ready to land.


  This does not seems to be all of the issue we have with nointerrupt.t … https://foss.heptapod.net/octobus/mercurial-devel/-/jobs/233269

REVISION SUMMARY
  This make sure viewer cannot see the new file with partial content.
  
  This was likely the cause of some flakiness in `test-nointerrupt.t`

REPOSITORY
  rHG Mercurial

BRANCH
  stable

REVISION DETAIL
  https://phab.mercurial-scm.org/D11250

AFFECTED FILES
  mercurial/testing/__init__.py

CHANGE DETAILS




To: marmoute, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

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 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)