Esempio n. 1
0
 def testAppend(self):
     lines = ['line1', 'line2', 'line3']
     open(self.name, 'w').write(lines[0])
     with atomic_file.AtomicFile(self.name, mode='a',
                                 dir=self.directory) as af:
         for line in lines[1:]:
             af.write(line + '\n')
     self.assertEqual(open(self.name).read(), '\n'.join(lines) + '\n')
Esempio n. 2
0
 def testDifferentDirectory(self):
     tmpdir = tempfile.gettempdir()
     af = atomic_file.AtomicFile(name=self.name, dir=tmpdir)
     self.assert_(not path.exists(self.name))
     self.assert_(path.exists(path.join(tmpdir, af.tempfile.name)))
     af.swap()
     self.assert_(path.exists(self.name))
     self.assert_(not path.exists(path.join(tmpdir, af.tempfile.name)))
Esempio n. 3
0
 def testContext(self):
     with atomic_file.AtomicFile(name=self.name, dir=self.directory) as af:
         self.assert_(not path.exists(self.name))
         self.assert_(
             path.exists(path.join(self.directory, af.tempfile.name)))
     self.assert_(path.exists(self.name))
     self.assert_(
         not path.exists(path.join(self.directory, af.tempfile.name)))
Esempio n. 4
0
 def testClose(self):
     af = atomic_file.AtomicFile(name=self.name, dir=self.directory)
     self.assert_(not path.exists(self.name))
     self.assert_(path.exists(path.join(self.directory, af.tempfile.name)))
     af.close()
     self.assert_(path.exists(self.name))
     self.assert_(
         not path.exists(path.join(self.directory, af.tempfile.name)))
Esempio n. 5
0
 def testDifferentDirectory2(self):
     self.name = path.join('..', 'here' + str(time.time))
     af = atomic_file.AtomicFile(name=self.name, dir=self.directory)
     self.assert_(not path.exists(self.name))
     self.assert_(path.exists(path.join(self.directory, af.tempfile.name)))
     af.swap()
     self.assert_(path.exists(self.name))
     self.assert_(
         not path.exists(path.join(self.directory, af.tempfile.name)))
Esempio n. 6
0
 def hasExplosion(self):
     with atomic_file.AtomicFile(name=self.name, dir=self.directory) as af:
         raise RuntimeError()
     self.assert_(not path.exists(self.name))
     self.assert_(
         not path.exists(path.join(self.directory, af.tempfile.name)))
Esempio n. 7
0
 def testMoreWrite(self):
     with atomic_file.AtomicFile(name=self.name, dir=self.directory) as af:
         lines = ['THE TEXT', 'MORE TEXT', 'AGAIN!']
         for line in lines:
             print >> af, line
     self.assertEqual(file(self.name).read(), '\n'.join(lines) + '\n')
Esempio n. 8
0
 def testWrite(self):
     with atomic_file.AtomicFile(name=self.name, dir=self.directory) as af:
         text = 'THE TEXT\n'
         af.write(text)
     self.assertEqual(file(self.name).read(), text)