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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
17
 
"""Basic tests for AtomicFile"""
 
29
 
from bzrlib.tests import TestCaseInTempDir, TestSkipped
 
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():
 
89
 
            raise TestSkipped("This test cannot be run on your platform")
 
90
 
        f = atomicfile.AtomicFile('test', mode='wb', new_mode=mode)
 
94
 
        self.assertEqualMode(mode, stat.S_IMODE(st.st_mode))
 
96
 
    def test_mode_0666(self):
 
99
 
    def test_mode_0664(self):
 
100
 
        self._test_mode(0664)
 
102
 
    def test_mode_0660(self):
 
103
 
        self._test_mode(0660)
 
105
 
    def test_mode_0660(self):
 
106
 
        self._test_mode(0660)
 
108
 
    def test_mode_0640(self):
 
109
 
        self._test_mode(0640)
 
111
 
    def test_mode_0600(self):
 
112
 
        self._test_mode(0600)
 
114
 
    def test_mode_0400(self):
 
115
 
        self._test_mode(0400)
 
116
 
        # Make it read-write again so cleanup doesn't complain
 
117
 
        os.chmod('test', 0600)
 
119
 
    def test_no_mode(self):
 
120
 
        # The default file permissions should be based on umask
 
121
 
        umask = osutils.get_umask()
 
122
 
        f = atomicfile.AtomicFile('test', mode='wb')
 
125
 
        st = os.lstat('test')
 
126
 
        self.assertEqualMode(0666 & ~umask, stat.S_IMODE(st.st_mode))
 
128
 
    def test_closed(self):
 
130
 
        def capture_warnings(msg, cls, stacklevel=None):
 
131
 
            self.assertEqual(cls, DeprecationWarning)
 
132
 
            local_warnings.append(msg)
 
134
 
        method = symbol_versioning.warn
 
136
 
            symbol_versioning.set_warning_method(capture_warnings)
 
137
 
            f = atomicfile.AtomicFile('test', mode='wb')
 
138
 
            self.assertEqual(False, f.closed)
 
140
 
            self.assertEqual(True, f.closed)
 
142
 
            f = atomicfile.AtomicFile('test', mode='wb')
 
144
 
            self.assertEqual(True, f.closed)
 
146
 
            f = atomicfile.AtomicFile('test', mode='wb')
 
148
 
            self.assertEqual(True, f.closed)
 
150
 
            symbol_versioning.set_warning_method(method)
 
152
 
        txt = 'AtomicFile.closed deprecated in bzr 0.10'
 
153
 
        self.assertEqual([txt]*4, local_warnings)