1
# Copyright (C) 2006 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Basic tests for AtomicFile"""
27
from bzrlib.tests import TestCaseInTempDir
30
class TestAtomicFile(TestCaseInTempDir):
32
def test_commit(self):
33
f = atomicfile.AtomicFile('test')
34
self.failIfExists('test')
38
self.assertEqual(['test'], os.listdir('.'))
39
self.check_file_contents('test', 'foo\n')
40
self.assertRaises(errors.AtomicFileAlreadyClosed, f.commit)
41
self.assertRaises(errors.AtomicFileAlreadyClosed, f.abort)
42
# close is re-entrant safe
46
f = atomicfile.AtomicFile('test')
49
self.assertEqual([], os.listdir('.'))
51
self.assertRaises(errors.AtomicFileAlreadyClosed, f.abort)
52
self.assertRaises(errors.AtomicFileAlreadyClosed, f.commit)
54
# close is re-entrant safe
58
f = atomicfile.AtomicFile('test')
60
# close on an open file is an abort
62
self.assertEqual([], os.listdir('.'))
64
self.assertRaises(errors.AtomicFileAlreadyClosed, f.abort)
65
self.assertRaises(errors.AtomicFileAlreadyClosed, f.commit)
67
# close is re-entrant safe
70
def test_text_mode(self):
71
f = atomicfile.AtomicFile('test', mode='wt')
75
contents = open('test', 'rb').read()
76
if sys.platform == 'win32':
77
self.assertEqual('foo\r\n', contents)
79
self.assertEqual('foo\n', contents)
81
def can_sys_preserve_mode(self):
82
# PLATFORM DEFICIENCY/ TestSkipped
83
return sys.platform not in ('win32',)
85
def _test_mode(self, mode):
86
if not self.can_sys_preserve_mode():
88
f = atomicfile.AtomicFile('test', mode='wt', new_mode=mode)
92
self.assertEqualMode(mode, stat.S_IMODE(st.st_mode))
94
def test_mode_02666(self):
95
self._test_mode(02666)
97
def test_mode_0666(self):
100
def test_mode_0664(self):
101
self._test_mode(0664)
103
def test_mode_0660(self):
104
self._test_mode(0660)
106
def test_mode_0660(self):
107
self._test_mode(0660)
109
def test_mode_0640(self):
110
self._test_mode(0640)
112
def test_mode_0600(self):
113
self._test_mode(0600)
115
def test_mode_0400(self):
116
self._test_mode(0400)
117
# Make it read-write again so cleanup doesn't complain
118
os.chmod('test', 0600)