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