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"""
29
from bzrlib.tests import TestCaseInTempDir
32
class TestAtomicFile(TestCaseInTempDir):
34
def test_commit(self):
35
f = atomicfile.AtomicFile('test')
36
self.failIfExists('test')
40
self.assertEqual(['test'], os.listdir('.'))
41
self.check_file_contents('test', 'foo\n')
42
self.assertRaises(errors.AtomicFileAlreadyClosed, f.commit)
43
self.assertRaises(errors.AtomicFileAlreadyClosed, f.abort)
44
# close is re-entrant safe
48
f = atomicfile.AtomicFile('test')
51
self.assertEqual([], os.listdir('.'))
53
self.assertRaises(errors.AtomicFileAlreadyClosed, f.abort)
54
self.assertRaises(errors.AtomicFileAlreadyClosed, f.commit)
56
# close is re-entrant safe
60
f = atomicfile.AtomicFile('test')
62
# close on an open file is an abort
64
self.assertEqual([], os.listdir('.'))
66
self.assertRaises(errors.AtomicFileAlreadyClosed, f.abort)
67
self.assertRaises(errors.AtomicFileAlreadyClosed, f.commit)
69
# close is re-entrant safe
72
def test_text_mode(self):
73
f = atomicfile.AtomicFile('test', mode='wt')
77
contents = open('test', 'rb').read()
78
if sys.platform == 'win32':
79
self.assertEqual('foo\r\n', contents)
81
self.assertEqual('foo\n', contents)
83
def can_sys_preserve_mode(self):
84
# PLATFORM DEFICIENCY/ TestSkipped
85
return sys.platform not in ('win32',)
87
def _test_mode(self, mode):
88
if not self.can_sys_preserve_mode():
90
f = atomicfile.AtomicFile('test', mode='wb', new_mode=mode)
94
self.assertEqualMode(mode, stat.S_IMODE(st.st_mode))
96
def test_mode_02666(self):
97
self._test_mode(02666)
99
def test_mode_0666(self):
100
self._test_mode(0666)
102
def test_mode_0664(self):
103
self._test_mode(0664)
105
def test_mode_0660(self):
106
self._test_mode(0660)
108
def test_mode_0660(self):
109
self._test_mode(0660)
111
def test_mode_0640(self):
112
self._test_mode(0640)
114
def test_mode_0600(self):
115
self._test_mode(0600)
117
def test_mode_0400(self):
118
self._test_mode(0400)
119
# Make it read-write again so cleanup doesn't complain
120
os.chmod('test', 0600)
122
def test_no_mode(self):
123
# The default file permissions should be based on umask
124
umask = osutils.get_umask()
125
f = atomicfile.AtomicFile('test', mode='wb')
128
st = os.lstat('test')
129
self.assertEqualMode(0666 & ~umask, stat.S_IMODE(st.st_mode))
131
def test_closed(self):
133
def capture_warnings(msg, cls, stacklevel=None):
134
self.assertEqual(cls, DeprecationWarning)
135
local_warnings.append(msg)
137
method = symbol_versioning.warn
139
symbol_versioning.set_warning_method(capture_warnings)
140
f = atomicfile.AtomicFile('test', mode='wb')
141
self.assertEqual(False, f.closed)
143
self.assertEqual(True, f.closed)
145
f = atomicfile.AtomicFile('test', mode='wb')
147
self.assertEqual(True, f.closed)
149
f = atomicfile.AtomicFile('test', mode='wb')
151
self.assertEqual(True, f.closed)
153
symbol_versioning.set_warning_method(method)
155
txt = 'AtomicFile.closed deprecated in bzr 0.10'
156
self.assertEqual([txt]*4, local_warnings)