/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_ignores.py

  • Committer: Jelmer Vernooij
  • Date: 2018-11-23 01:35:56 UTC
  • mto: (7211.10.3 git-empty-dirs)
  • mto: This revision was merged to the branch mainline in revision 7215.
  • Revision ID: jelmer@jelmer.uk-20181123013556-mu7ct9ovl7fozjc2
Update comment about ssl.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006-2012, 2016 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
"""Tests for handling of ignore files"""
 
18
 
 
19
from .. import (
 
20
    config,
 
21
    ignores,
 
22
    )
 
23
from ..sixish import (
 
24
    BytesIO,
 
25
    )
 
26
from . import (
 
27
    TestCase,
 
28
    TestCaseInTempDir,
 
29
    TestCaseWithTransport,
 
30
    )
 
31
 
 
32
 
 
33
class TestParseIgnoreFile(TestCase):
 
34
 
 
35
    def test_parse_fancy(self):
 
36
        ignored = ignores.parse_ignore_file(BytesIO(
 
37
            b'./rootdir\n'
 
38
            b'randomfile*\n'
 
39
            b'path/from/ro?t\n'
 
40
            b'unicode\xc2\xb5\n'  # u'\xb5'.encode('utf8')
 
41
            b'dos\r\n'
 
42
            b'\n'  # empty line
 
43
            b'#comment\n'
 
44
            b' xx \n'  # whitespace
 
45
            b'!RE:^\\.z.*\n'
 
46
            b'!!./.zcompdump\n'
 
47
            ))
 
48
        self.assertEqual({u'./rootdir',
 
49
                          u'randomfile*',
 
50
                          u'path/from/ro?t',
 
51
                          u'unicode\xb5',
 
52
                          u'dos',
 
53
                          u' xx ',
 
54
                          u'!RE:^\\.z.*',
 
55
                          u'!!./.zcompdump',
 
56
                          }, ignored)
 
57
 
 
58
    def test_parse_empty(self):
 
59
        ignored = ignores.parse_ignore_file(BytesIO(b''))
 
60
        self.assertEqual(set([]), ignored)
 
61
 
 
62
    def test_parse_non_utf8(self):
 
63
        """Lines with non utf 8 characters should be discarded."""
 
64
        ignored = ignores.parse_ignore_file(BytesIO(
 
65
            b'utf8filename_a\n'
 
66
            b'invalid utf8\x80\n'
 
67
            b'utf8filename_b\n'
 
68
            ))
 
69
        self.assertEqual({
 
70
            u'utf8filename_a',
 
71
            u'utf8filename_b',
 
72
            }, ignored)
 
73
 
 
74
 
 
75
class TestUserIgnores(TestCaseInTempDir):
 
76
 
 
77
    def test_create_if_missing(self):
 
78
        # $HOME should be set to '.'
 
79
        ignore_path = config.user_ignore_config_filename()
 
80
        self.assertPathDoesNotExist(ignore_path)
 
81
        user_ignores = ignores.get_user_ignores()
 
82
        self.assertEqual(set(ignores.USER_DEFAULTS), user_ignores)
 
83
 
 
84
        self.assertPathExists(ignore_path)
 
85
        with open(ignore_path, 'rb') as f:
 
86
            entries = ignores.parse_ignore_file(f)
 
87
        self.assertEqual(set(ignores.USER_DEFAULTS), entries)
 
88
 
 
89
    def test_use_existing(self):
 
90
        patterns = [u'*.o', u'*.py[co]', u'\xe5*']
 
91
        ignores._set_user_ignores(patterns)
 
92
 
 
93
        user_ignores = ignores.get_user_ignores()
 
94
        self.assertEqual(set(patterns), user_ignores)
 
95
 
 
96
    def test_use_empty(self):
 
97
        ignores._set_user_ignores([])
 
98
        ignore_path = config.user_ignore_config_filename()
 
99
        self.check_file_contents(ignore_path, b'')
 
100
 
 
101
        self.assertEqual(set([]), ignores.get_user_ignores())
 
102
 
 
103
    def test_set(self):
 
104
        patterns = ['*.py[co]', '*.py[oc]']
 
105
        ignores._set_user_ignores(patterns)
 
106
 
 
107
        self.assertEqual(set(patterns), ignores.get_user_ignores())
 
108
 
 
109
        patterns = ['vim', '*.swp']
 
110
        ignores._set_user_ignores(patterns)
 
111
        self.assertEqual(set(patterns), ignores.get_user_ignores())
 
112
 
 
113
    def test_add(self):
 
114
        """Test that adding will not duplicate ignores"""
 
115
        # Create an empty file
 
116
        ignores._set_user_ignores([])
 
117
 
 
118
        patterns = ['foo', './bar', u'b\xe5z']
 
119
        added = ignores.add_unique_user_ignores(patterns)
 
120
        self.assertEqual(patterns, added)
 
121
        self.assertEqual(set(patterns), ignores.get_user_ignores())
 
122
 
 
123
    def test_add_directory(self):
 
124
        """Test that adding a directory will strip any trailing slash"""
 
125
        # Create an empty file
 
126
        ignores._set_user_ignores([])
 
127
 
 
128
        in_patterns = ['foo/', 'bar/', 'baz\\']
 
129
        added = ignores.add_unique_user_ignores(in_patterns)
 
130
        out_patterns = [x.rstrip('/\\') for x in in_patterns]
 
131
        self.assertEqual(out_patterns, added)
 
132
        self.assertEqual(set(out_patterns), ignores.get_user_ignores())
 
133
 
 
134
    def test_add_unique(self):
 
135
        """Test that adding will not duplicate ignores"""
 
136
        ignores._set_user_ignores(
 
137
            ['foo', './bar', u'b\xe5z', 'dir1/', 'dir3\\'])
 
138
 
 
139
        added = ignores.add_unique_user_ignores(
 
140
            ['xxx', './bar', 'xxx', 'dir1/', 'dir2/', 'dir3\\'])
 
141
        self.assertEqual(['xxx', 'dir2'], added)
 
142
        self.assertEqual({'foo', './bar', u'b\xe5z',
 
143
                          'xxx', 'dir1', 'dir2', 'dir3'},
 
144
                         ignores.get_user_ignores())
 
145
 
 
146
 
 
147
class TestRuntimeIgnores(TestCase):
 
148
 
 
149
    def setUp(self):
 
150
        super(TestRuntimeIgnores, self).setUp()
 
151
 
 
152
        # For the purposes of these tests, we must have no
 
153
        # runtime ignores
 
154
        self.overrideAttr(ignores, '_runtime_ignores', set())
 
155
 
 
156
    def test_add(self):
 
157
        """Test that we can add an entry to the list."""
 
158
        self.assertEqual(set(), ignores.get_runtime_ignores())
 
159
 
 
160
        ignores.add_runtime_ignores(['foo'])
 
161
        self.assertEqual({'foo'}, ignores.get_runtime_ignores())
 
162
 
 
163
    def test_add_duplicate(self):
 
164
        """Adding the same ignore twice shouldn't add a new entry."""
 
165
        ignores.add_runtime_ignores(['foo', 'bar'])
 
166
        self.assertEqual({'foo', 'bar'}, ignores.get_runtime_ignores())
 
167
 
 
168
        ignores.add_runtime_ignores(['bar'])
 
169
        self.assertEqual({'foo', 'bar'}, ignores.get_runtime_ignores())
 
170
 
 
171
 
 
172
class TestTreeIgnores(TestCaseWithTransport):
 
173
 
 
174
    def assertPatternsEquals(self, patterns):
 
175
        with open(".bzrignore", "rb") as f:
 
176
            contents = f.read().decode("utf-8").splitlines()
 
177
        self.assertEqual(sorted(patterns), sorted(contents))
 
178
 
 
179
    def test_new_file(self):
 
180
        tree = self.make_branch_and_tree(".")
 
181
        ignores.tree_ignores_add_patterns(tree, [u"myentry"])
 
182
        self.assertTrue(tree.has_filename(".bzrignore"))
 
183
        self.assertPatternsEquals(["myentry"])
 
184
 
 
185
    def test_add_to_existing(self):
 
186
        tree = self.make_branch_and_tree(".")
 
187
        self.build_tree_contents([('.bzrignore', b"myentry1\n")])
 
188
        tree.add([".bzrignore"])
 
189
        ignores.tree_ignores_add_patterns(tree, [u"myentry2", u"foo"])
 
190
        self.assertPatternsEquals(["myentry1", "myentry2", "foo"])
 
191
 
 
192
    def test_adds_ending_newline(self):
 
193
        tree = self.make_branch_and_tree(".")
 
194
        self.build_tree_contents([('.bzrignore', b"myentry1")])
 
195
        tree.add([".bzrignore"])
 
196
        ignores.tree_ignores_add_patterns(tree, [u"myentry2"])
 
197
        self.assertPatternsEquals(["myentry1", "myentry2"])
 
198
        with open(".bzrignore") as f:
 
199
            text = f.read()
 
200
        self.assertTrue(text.endswith(('\r\n', '\n', '\r')))
 
201
 
 
202
    def test_does_not_add_dupe(self):
 
203
        tree = self.make_branch_and_tree(".")
 
204
        self.build_tree_contents([('.bzrignore', b"myentry\n")])
 
205
        tree.add([".bzrignore"])
 
206
        ignores.tree_ignores_add_patterns(tree, [u"myentry"])
 
207
        self.assertPatternsEquals(["myentry"])
 
208
 
 
209
    def test_non_ascii(self):
 
210
        tree = self.make_branch_and_tree(".")
 
211
        self.build_tree_contents([('.bzrignore',
 
212
                                   u"myentry\u1234\n".encode('utf-8'))])
 
213
        tree.add([".bzrignore"])
 
214
        ignores.tree_ignores_add_patterns(tree, [u"myentry\u5678"])
 
215
        self.assertPatternsEquals([u"myentry\u1234", u"myentry\u5678"])
 
216
 
 
217
    def test_crlf(self):
 
218
        tree = self.make_branch_and_tree(".")
 
219
        self.build_tree_contents([('.bzrignore', b"myentry1\r\n")])
 
220
        tree.add([".bzrignore"])
 
221
        ignores.tree_ignores_add_patterns(tree, ["myentry2", "foo"])
 
222
        with open('.bzrignore', 'rb') as f:
 
223
            self.assertEqual(f.read(), b'myentry1\r\nmyentry2\r\nfoo\r\n')
 
224
        self.assertPatternsEquals(["myentry1", "myentry2", "foo"])