[PATCH 1 of 4] atomictempfile: use a tempdir to keep the test environment clean

Martijn Pieters mj at zopatista.com
Thu Jun 23 16:43:58 UTC 2016


# HG changeset patch
# User Martijn Pieters <mjpieters at fb.com>
# Date 1466699743 -3600
#      Thu Jun 23 17:35:43 2016 +0100
# Node ID f06a3b48a4b3f7beed5405e4f157c5734b68e59e
# Parent  aa1d56003872cba207d908706da059141dd901a5
atomictempfile: use a tempdir to keep the test environment clean

Rather than pre-emptively delete a file, execute the test in a dedicated
temporary directory that is removed after each test.

diff --git a/tests/test-atomictempfile.py b/tests/test-atomictempfile.py
--- a/tests/test-atomictempfile.py
+++ b/tests/test-atomictempfile.py
@@ -2,6 +2,8 @@
 
 import glob
 import os
+import shutil
+import tempfile
 import unittest
 
 from mercurial import (
@@ -10,31 +12,36 @@
 atomictempfile = util.atomictempfile
 
 class testatomictempfile(unittest.TestCase):
+    def setUp(self):
+        self._testdir = tempfile.mkdtemp('atomictempfiletest')
+        self._filename = os.path.join(self._testdir, 'testfilename')
+
+    def tearDown(self):
+        shutil.rmtree(self._testdir, True)
+
     def test1_simple(self):
-        if os.path.exists('foo'):
-            os.remove('foo')
-        file = atomictempfile('foo')
-        (dir, basename) = os.path.split(file._tempname)
-        self.assertFalse(os.path.isfile('foo'))
-        self.assertTrue(basename in glob.glob('.foo-*'))
+        file = atomictempfile(self._filename)
+        self.assertFalse(os.path.isfile(self._filename))
+        tempfilename = file._tempname
+        self.assertTrue(tempfilename in glob.glob(
+            os.path.join(self._testdir, '.testfilename-*')))
 
         file.write(b'argh\n')
         file.close()
 
-        self.assertTrue(os.path.isfile('foo'))
-        self.assertTrue(basename not in glob.glob('.foo-*'))
+        self.assertTrue(os.path.isfile(self._filename))
+        self.assertTrue(tempfilename not in glob.glob(
+            os.path.join(self._testdir, '.testfilename-*')))
 
     # discard() removes the temp file without making the write permanent
     def test2_discard(self):
-        if os.path.exists('foo'):
-            os.remove('foo')
-        file = atomictempfile('foo')
+        file = atomictempfile(self._filename)
         (dir, basename) = os.path.split(file._tempname)
 
         file.write(b'yo\n')
         file.discard()
 
-        self.assertFalse(os.path.isfile('foo'))
+        self.assertFalse(os.path.isfile(self._filename))
         self.assertTrue(basename not in os.listdir('.'))
 
     # if a programmer screws up and passes bad args to atomictempfile, they
@@ -45,7 +52,7 @@
     # checkambig=True avoids ambiguity of timestamp
     def test4_checkambig(self):
         def atomicwrite(checkambig):
-            f = atomictempfile('foo', checkambig=checkambig)
+            f = atomictempfile(self._filename, checkambig=checkambig)
             f.write('FOO')
             f.close()
 
@@ -53,7 +60,7 @@
         # "filesystem time"
         for i in xrange(5):
             atomicwrite(False)
-            oldstat = os.stat('foo')
+            oldstat = os.stat(self._filename)
             if oldstat.st_ctime != oldstat.st_mtime:
                 # subsequent changing never causes ambiguity
                 continue
@@ -64,7 +71,7 @@
             # whether st_mtime is advanced multiple times as expecetd
             for j in xrange(repetition):
                 atomicwrite(True)
-            newstat = os.stat('foo')
+            newstat = os.stat(self._filename)
             if oldstat.st_ctime != newstat.st_ctime:
                 # timestamp ambiguity was naturally avoided while repetition
                 continue


More information about the Mercurial-devel mailing list