~bzr-pqm/bzr/bzr.dev

1755.3.6 by John Arbash Meinel
Add a test suite for Atomic File, and clean it up so that it really does set the mode properly.
1
# Copyright (C) 2006 Canonical Ltd
2
#
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.
7
#
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.
12
#
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
16
17
"""Basic tests for AtomicFile"""
18
19
import os
20
import stat
21
import sys
22
23
from bzrlib import (
24
    atomicfile,
25
    errors,
1755.3.9 by John Arbash Meinel
Make AtomicFile not do anything if not supplied a mode, clean up LocalTransport now that we do the right thing for None
26
    osutils,
1755.3.11 by John Arbash Meinel
Test that AtomicFile.closed is supported, but deprecated
27
    symbol_versioning,
1755.3.6 by John Arbash Meinel
Add a test suite for Atomic File, and clean it up so that it really does set the mode properly.
28
    )
29
from bzrlib.tests import TestCaseInTempDir
30
31
32
class TestAtomicFile(TestCaseInTempDir):
33
34
    def test_commit(self):
35
        f = atomicfile.AtomicFile('test')
36
        self.failIfExists('test')
37
        f.write('foo\n')
38
        f.commit()
39
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
45
        f.close()
46
47
    def test_abort(self):
48
        f = atomicfile.AtomicFile('test')
49
        f.write('foo\n')
50
        f.abort()
51
        self.assertEqual([], os.listdir('.'))
52
53
        self.assertRaises(errors.AtomicFileAlreadyClosed, f.abort)
54
        self.assertRaises(errors.AtomicFileAlreadyClosed, f.commit)
55
56
        # close is re-entrant safe
57
        f.close()
58
59
    def test_close(self):
60
        f = atomicfile.AtomicFile('test')
61
        f.write('foo\n')
62
        # close on an open file is an abort
63
        f.close()
64
        self.assertEqual([], os.listdir('.'))
65
66
        self.assertRaises(errors.AtomicFileAlreadyClosed, f.abort)
67
        self.assertRaises(errors.AtomicFileAlreadyClosed, f.commit)
68
69
        # close is re-entrant safe
70
        f.close()
71
        
72
    def test_text_mode(self):
73
        f = atomicfile.AtomicFile('test', mode='wt')
74
        f.write('foo\n')
75
        f.commit()
76
77
        contents = open('test', 'rb').read()
78
        if sys.platform == 'win32':
79
            self.assertEqual('foo\r\n', contents)
80
        else:
81
            self.assertEqual('foo\n', contents)
82
83
    def can_sys_preserve_mode(self):
84
        # PLATFORM DEFICIENCY/ TestSkipped
85
        return sys.platform not in ('win32',)
86
87
    def _test_mode(self, mode):
88
        if not self.can_sys_preserve_mode():
89
            return
1755.3.9 by John Arbash Meinel
Make AtomicFile not do anything if not supplied a mode, clean up LocalTransport now that we do the right thing for None
90
        f = atomicfile.AtomicFile('test', mode='wb', new_mode=mode)
1755.3.6 by John Arbash Meinel
Add a test suite for Atomic File, and clean it up so that it really does set the mode properly.
91
        f.write('foo\n')
92
        f.commit()
93
        st = os.lstat('test')
94
        self.assertEqualMode(mode, stat.S_IMODE(st.st_mode))
95
96
    def test_mode_02666(self):
97
        self._test_mode(02666)
98
99
    def test_mode_0666(self):
100
        self._test_mode(0666)
101
102
    def test_mode_0664(self):
103
        self._test_mode(0664)
104
105
    def test_mode_0660(self):
106
        self._test_mode(0660)
107
108
    def test_mode_0660(self):
109
        self._test_mode(0660)
110
111
    def test_mode_0640(self):
112
        self._test_mode(0640)
113
114
    def test_mode_0600(self):
115
        self._test_mode(0600)
116
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)
1755.3.9 by John Arbash Meinel
Make AtomicFile not do anything if not supplied a mode, clean up LocalTransport now that we do the right thing for None
121
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')
126
        f.write('foo\n')
127
        f.commit()
128
        st = os.lstat('test')
129
        self.assertEqualMode(0666 & ~umask, stat.S_IMODE(st.st_mode))
1755.3.11 by John Arbash Meinel
Test that AtomicFile.closed is supported, but deprecated
130
131
    def test_closed(self):
132
        local_warnings = []
133
        def capture_warnings(msg, cls, stacklevel=None):
134
            self.assertEqual(cls, DeprecationWarning)
135
            local_warnings.append(msg)
136
137
        method = symbol_versioning.warn
138
        try:
139
            symbol_versioning.set_warning_method(capture_warnings)
140
            f = atomicfile.AtomicFile('test', mode='wb')
141
            self.assertEqual(False, f.closed)
142
            f.abort()
143
            self.assertEqual(True, f.closed)
144
145
            f = atomicfile.AtomicFile('test', mode='wb')
146
            f.close()
147
            self.assertEqual(True, f.closed)
148
149
            f = atomicfile.AtomicFile('test', mode='wb')
150
            f.commit()
151
            self.assertEqual(True, f.closed)
152
        finally:
153
            symbol_versioning.set_warning_method(method)
154
155
        txt = 'AtomicFile.closed deprecated in bzr 0.10'
156
        self.assertEqual([txt]*4, local_warnings)