/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_atomicfile.py

  • Committer: Martin
  • Date: 2017-06-10 01:57:00 UTC
  • mto: This revision was merged to the branch mainline in revision 6679.
  • Revision ID: gzlist@googlemail.com-20170610015700-o3xeuyaqry2obiay
Go back to native str for urls and many other py3 changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Basic tests for AtomicFile"""
 
18
 
 
19
import os
 
20
import stat
 
21
import sys
 
22
 
 
23
from .. import (
 
24
    atomicfile,
 
25
    errors,
 
26
    osutils,
 
27
    )
 
28
from . import TestCaseInTempDir, TestSkipped
 
29
 
 
30
 
 
31
class TestAtomicFile(TestCaseInTempDir):
 
32
 
 
33
    def test_commit(self):
 
34
        f = atomicfile.AtomicFile('test')
 
35
        self.assertPathDoesNotExist('test')
 
36
        f.write('foo\n')
 
37
        f.commit()
 
38
 
 
39
        self.assertEqual(['test'], os.listdir('.'))
 
40
        self.check_file_contents('test', 'foo\n')
 
41
        self.assertRaises(errors.AtomicFileAlreadyClosed, f.commit)
 
42
        self.assertRaises(errors.AtomicFileAlreadyClosed, f.abort)
 
43
        # close is re-entrant safe
 
44
        f.close()
 
45
 
 
46
    def test_abort(self):
 
47
        f = atomicfile.AtomicFile('test')
 
48
        f.write('foo\n')
 
49
        f.abort()
 
50
        self.assertEqual([], os.listdir('.'))
 
51
 
 
52
        self.assertRaises(errors.AtomicFileAlreadyClosed, f.abort)
 
53
        self.assertRaises(errors.AtomicFileAlreadyClosed, f.commit)
 
54
 
 
55
        # close is re-entrant safe
 
56
        f.close()
 
57
 
 
58
    def test_close(self):
 
59
        f = atomicfile.AtomicFile('test')
 
60
        f.write('foo\n')
 
61
        # close on an open file is an abort
 
62
        f.close()
 
63
        self.assertEqual([], os.listdir('.'))
 
64
 
 
65
        self.assertRaises(errors.AtomicFileAlreadyClosed, f.abort)
 
66
        self.assertRaises(errors.AtomicFileAlreadyClosed, f.commit)
 
67
 
 
68
        # close is re-entrant safe
 
69
        f.close()
 
70
 
 
71
    def test_text_mode(self):
 
72
        f = atomicfile.AtomicFile('test', mode='wt')
 
73
        f.write('foo\n')
 
74
        f.commit()
 
75
 
 
76
        contents = open('test', 'rb').read()
 
77
        if sys.platform == 'win32':
 
78
            self.assertEqual('foo\r\n', contents)
 
79
        else:
 
80
            self.assertEqual('foo\n', contents)
 
81
 
 
82
    def can_sys_preserve_mode(self):
 
83
        # PLATFORM DEFICIENCY/ TestSkipped
 
84
        return sys.platform not in ('win32',)
 
85
 
 
86
    def _test_mode(self, mode):
 
87
        if not self.can_sys_preserve_mode():
 
88
            raise TestSkipped("This test cannot be run on your platform")
 
89
        f = atomicfile.AtomicFile('test', mode='wb', new_mode=mode)
 
90
        f.write('foo\n')
 
91
        f.commit()
 
92
        st = os.lstat('test')
 
93
        self.assertEqualMode(mode, stat.S_IMODE(st.st_mode))
 
94
 
 
95
    def test_mode_0666(self):
 
96
        self._test_mode(0o666)
 
97
 
 
98
    def test_mode_0664(self):
 
99
        self._test_mode(0o664)
 
100
 
 
101
    def test_mode_0660(self):
 
102
        self._test_mode(0o660)
 
103
 
 
104
    def test_mode_0660(self):
 
105
        self._test_mode(0o660)
 
106
 
 
107
    def test_mode_0640(self):
 
108
        self._test_mode(0o640)
 
109
 
 
110
    def test_mode_0600(self):
 
111
        self._test_mode(0o600)
 
112
 
 
113
    def test_mode_0400(self):
 
114
        self._test_mode(0o400)
 
115
        # Make it read-write again so cleanup doesn't complain
 
116
        os.chmod('test', 0o600)
 
117
 
 
118
    def test_no_mode(self):
 
119
        # The default file permissions should be based on umask
 
120
        umask = osutils.get_umask()
 
121
        f = atomicfile.AtomicFile('test', mode='wb')
 
122
        f.write('foo\n')
 
123
        f.commit()
 
124
        st = os.lstat('test')
 
125
        self.assertEqualMode(0o666 & ~umask, stat.S_IMODE(st.st_mode))