/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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.
16
17
"""Basic tests for AtomicFile"""
18
19
import os
20
import stat
21
import sys
22
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
23
from .. import (
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.
24
    atomicfile,
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
25
    osutils,
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.
26
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
27
from . import TestCaseInTempDir, TestSkipped
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
30
class TestAtomicFile(TestCaseInTempDir):
31
32
    def test_commit(self):
33
        f = atomicfile.AtomicFile('test')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
34
        self.assertPathDoesNotExist('test')
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
35
        f.write(b'foo\n')
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.
36
        f.commit()
37
38
        self.assertEqual(['test'], os.listdir('.'))
6973.10.4 by Jelmer Vernooij
Update python3.passing.
39
        self.check_file_contents('test', b'foo\n')
7067.12.1 by Jelmer Vernooij
Fix an ignore test. Make AtomicFile a contextmanager.
40
        self.assertRaises(atomicfile.AtomicFileAlreadyClosed, f.commit)
41
        self.assertRaises(atomicfile.AtomicFileAlreadyClosed, f.abort)
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.
42
        # close is re-entrant safe
43
        f.close()
44
45
    def test_abort(self):
46
        f = atomicfile.AtomicFile('test')
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
47
        f.write(b'foo\n')
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.
48
        f.abort()
49
        self.assertEqual([], os.listdir('.'))
50
7067.12.1 by Jelmer Vernooij
Fix an ignore test. Make AtomicFile a contextmanager.
51
        self.assertRaises(atomicfile.AtomicFileAlreadyClosed, f.abort)
52
        self.assertRaises(atomicfile.AtomicFileAlreadyClosed, f.commit)
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.
53
54
        # close is re-entrant safe
55
        f.close()
56
57
    def test_close(self):
58
        f = atomicfile.AtomicFile('test')
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
59
        f.write(b'foo\n')
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.
60
        # close on an open file is an abort
61
        f.close()
62
        self.assertEqual([], os.listdir('.'))
63
7067.12.1 by Jelmer Vernooij
Fix an ignore test. Make AtomicFile a contextmanager.
64
        self.assertRaises(atomicfile.AtomicFileAlreadyClosed, f.abort)
65
        self.assertRaises(atomicfile.AtomicFileAlreadyClosed, f.commit)
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.
66
67
        # close is re-entrant safe
68
        f.close()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
69
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.
70
    def test_text_mode(self):
71
        f = atomicfile.AtomicFile('test', mode='wt')
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
72
        f.write(b'foo\n')
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.
73
        f.commit()
74
75
        contents = open('test', 'rb').read()
76
        if sys.platform == 'win32':
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
77
            self.assertEqual(b'foo\r\n', contents)
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.
78
        else:
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
79
            self.assertEqual(b'foo\n', contents)
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.
80
81
    def can_sys_preserve_mode(self):
82
        # PLATFORM DEFICIENCY/ TestSkipped
83
        return sys.platform not in ('win32',)
84
85
    def _test_mode(self, mode):
86
        if not self.can_sys_preserve_mode():
1982.1.1 by Alexander Belchenko
Explicitly skip tests with file attribute manipulations instead of silently pass
87
            raise TestSkipped("This test cannot be run on your platform")
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
88
        f = atomicfile.AtomicFile('test', mode='wb', new_mode=mode)
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
89
        f.write(b'foo\n')
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.
90
        f.commit()
91
        st = os.lstat('test')
92
        self.assertEqualMode(mode, stat.S_IMODE(st.st_mode))
93
94
    def test_mode_0666(self):
6619.3.14 by Jelmer Vernooij
Convert some octal numbers to new notations.
95
        self._test_mode(0o666)
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.
96
97
    def test_mode_0664(self):
6619.3.14 by Jelmer Vernooij
Convert some octal numbers to new notations.
98
        self._test_mode(0o664)
99
100
    def test_mode_0660(self):
101
        self._test_mode(0o660)
102
103
    def test_mode_0660(self):
104
        self._test_mode(0o660)
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.
105
106
    def test_mode_0640(self):
6619.3.14 by Jelmer Vernooij
Convert some octal numbers to new notations.
107
        self._test_mode(0o640)
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.
108
109
    def test_mode_0600(self):
6619.3.14 by Jelmer Vernooij
Convert some octal numbers to new notations.
110
        self._test_mode(0o600)
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.
111
112
    def test_mode_0400(self):
6619.3.14 by Jelmer Vernooij
Convert some octal numbers to new notations.
113
        self._test_mode(0o400)
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.
114
        # Make it read-write again so cleanup doesn't complain
6619.3.14 by Jelmer Vernooij
Convert some octal numbers to new notations.
115
        os.chmod('test', 0o600)
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
116
117
    def test_no_mode(self):
118
        # The default file permissions should be based on umask
119
        umask = osutils.get_umask()
120
        f = atomicfile.AtomicFile('test', mode='wb')
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
121
        f.write(b'foo\n')
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
122
        f.commit()
123
        st = os.lstat('test')
6619.3.14 by Jelmer Vernooij
Convert some octal numbers to new notations.
124
        self.assertEqualMode(0o666 & ~umask, stat.S_IMODE(st.st_mode))
7067.12.1 by Jelmer Vernooij
Fix an ignore test. Make AtomicFile a contextmanager.
125
126
    def test_context_manager_commit(self):
127
        with atomicfile.AtomicFile('test') as f:
128
            self.assertPathDoesNotExist('test')
129
            f.write(b'foo\n')
130
131
        self.assertEqual(['test'], os.listdir('.'))
132
        self.check_file_contents('test', b'foo\n')
133
134
    def test_context_manager_abort(self):
135
        def abort():
136
            with atomicfile.AtomicFile('test') as f:
137
                f.write(b'foo\n')
138
                raise AssertionError
139
        self.assertRaises(AssertionError, abort)
140
        self.assertEqual([], os.listdir('.'))