/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: Breezy landing bot
  • Author(s): Colin Watson
  • Date: 2020-11-16 21:47:08 UTC
  • mfrom: (7521.1.1 remove-lp-workaround)
  • Revision ID: breezy.the.bot@gmail.com-20201116214708-jos209mgxi41oy15
Remove breezy.git workaround for bazaar.launchpad.net.

Merged from https://code.launchpad.net/~cjwatson/brz/remove-lp-workaround/+merge/393710

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 Canonical Ltd
 
1
# Copyright (C) 2006-2012, 2016 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
"""Tests for handling of ignore files"""
18
18
 
19
 
from cStringIO import StringIO
 
19
from io import BytesIO
 
20
import os
20
21
 
21
 
from bzrlib import (
22
 
    config,
 
22
from .. import (
 
23
    bedding,
23
24
    ignores,
24
25
    )
25
 
from bzrlib.tests import (
 
26
from . import (
26
27
    TestCase,
27
28
    TestCaseInTempDir,
28
29
    TestCaseWithTransport,
32
33
class TestParseIgnoreFile(TestCase):
33
34
 
34
35
    def test_parse_fancy(self):
35
 
        ignored = ignores.parse_ignore_file(StringIO(
36
 
                './rootdir\n'
37
 
                'randomfile*\n'
38
 
                'path/from/ro?t\n'
39
 
                'unicode\xc2\xb5\n' # u'\xb5'.encode('utf8')
40
 
                'dos\r\n'
41
 
                '\n' # empty line
42
 
                '#comment\n'
43
 
                ' xx \n' # whitespace
44
 
                '!RE:^\.z.*\n'
45
 
                '!!./.zcompdump\n'
46
 
                ))
47
 
        self.assertEqual(set(['./rootdir',
48
 
                          'randomfile*',
49
 
                          'path/from/ro?t',
 
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',
50
51
                          u'unicode\xb5',
51
 
                          'dos',
52
 
                          ' xx ',
53
 
                          '!RE:^\.z.*',
54
 
                          '!!./.zcompdump',
55
 
                         ]), ignored)
 
52
                          u'dos',
 
53
                          u' xx ',
 
54
                          u'!RE:^\\.z.*',
 
55
                          u'!!./.zcompdump',
 
56
                          }, ignored)
56
57
 
57
58
    def test_parse_empty(self):
58
 
        ignored = ignores.parse_ignore_file(StringIO(''))
 
59
        ignored = ignores.parse_ignore_file(BytesIO(b''))
59
60
        self.assertEqual(set([]), ignored)
60
 
        
 
61
 
61
62
    def test_parse_non_utf8(self):
62
63
        """Lines with non utf 8 characters should be discarded."""
63
 
        ignored = ignores.parse_ignore_file(StringIO(
64
 
                'utf8filename_a\n'
65
 
                'invalid utf8\x80\n'
66
 
                'utf8filename_b\n'
67
 
                ))
68
 
        self.assertEqual(set([
69
 
                        'utf8filename_a',
70
 
                        'utf8filename_b',
71
 
                       ]), ignored)
 
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)
72
73
 
73
74
 
74
75
class TestUserIgnores(TestCaseInTempDir):
75
76
 
76
77
    def test_create_if_missing(self):
77
78
        # $HOME should be set to '.'
78
 
        ignore_path = config.user_ignore_config_filename()
 
79
        ignore_path = bedding.user_ignore_config_path()
79
80
        self.assertPathDoesNotExist(ignore_path)
80
81
        user_ignores = ignores.get_user_ignores()
81
82
        self.assertEqual(set(ignores.USER_DEFAULTS), user_ignores)
82
83
 
83
84
        self.assertPathExists(ignore_path)
84
 
        f = open(ignore_path, 'rb')
85
 
        try:
 
85
        with open(ignore_path, 'rb') as f:
86
86
            entries = ignores.parse_ignore_file(f)
87
 
        finally:
88
 
            f.close()
89
87
        self.assertEqual(set(ignores.USER_DEFAULTS), entries)
90
88
 
 
89
    def test_create_with_intermediate_missing(self):
 
90
        # $HOME should be set to '.'
 
91
        ignore_path = bedding.user_ignore_config_path()
 
92
        self.assertPathDoesNotExist(ignore_path)
 
93
        os.mkdir('empty-home')
 
94
 
 
95
        config_path = os.path.join(
 
96
            self.test_dir, 'empty-home', 'foo', '.config')
 
97
        self.overrideEnv('BRZ_HOME', config_path)
 
98
        self.assertPathDoesNotExist(config_path)
 
99
 
 
100
        user_ignores = ignores.get_user_ignores()
 
101
        self.assertEqual(set(ignores.USER_DEFAULTS), user_ignores)
 
102
 
 
103
        ignore_path = bedding.user_ignore_config_path()
 
104
        self.assertPathDoesNotExist(ignore_path)
 
105
 
91
106
    def test_use_existing(self):
92
 
        patterns = ['*.o', '*.py[co]', u'\xe5*']
 
107
        patterns = [u'*.o', u'*.py[co]', u'\xe5*']
93
108
        ignores._set_user_ignores(patterns)
94
109
 
95
110
        user_ignores = ignores.get_user_ignores()
97
112
 
98
113
    def test_use_empty(self):
99
114
        ignores._set_user_ignores([])
100
 
        ignore_path = config.user_ignore_config_filename()
101
 
        self.check_file_contents(ignore_path, '')
 
115
        ignore_path = bedding.user_ignore_config_path()
 
116
        self.check_file_contents(ignore_path, b'')
102
117
 
103
118
        self.assertEqual(set([]), ignores.get_user_ignores())
104
119
 
129
144
 
130
145
        in_patterns = ['foo/', 'bar/', 'baz\\']
131
146
        added = ignores.add_unique_user_ignores(in_patterns)
132
 
        out_patterns = [ x.rstrip('/\\') for x in in_patterns ]
 
147
        out_patterns = [x.rstrip('/\\') for x in in_patterns]
133
148
        self.assertEqual(out_patterns, added)
134
149
        self.assertEqual(set(out_patterns), ignores.get_user_ignores())
135
150
 
141
156
        added = ignores.add_unique_user_ignores(
142
157
            ['xxx', './bar', 'xxx', 'dir1/', 'dir2/', 'dir3\\'])
143
158
        self.assertEqual(['xxx', 'dir2'], added)
144
 
        self.assertEqual(set(['foo', './bar', u'b\xe5z',
145
 
                              'xxx', 'dir1', 'dir2', 'dir3']),
 
159
        self.assertEqual({'foo', './bar', u'b\xe5z',
 
160
                          'xxx', 'dir1', 'dir2', 'dir3'},
146
161
                         ignores.get_user_ignores())
147
162
 
148
163
 
160
175
        self.assertEqual(set(), ignores.get_runtime_ignores())
161
176
 
162
177
        ignores.add_runtime_ignores(['foo'])
163
 
        self.assertEqual(set(['foo']), ignores.get_runtime_ignores())
 
178
        self.assertEqual({'foo'}, ignores.get_runtime_ignores())
164
179
 
165
180
    def test_add_duplicate(self):
166
181
        """Adding the same ignore twice shouldn't add a new entry."""
167
182
        ignores.add_runtime_ignores(['foo', 'bar'])
168
 
        self.assertEqual(set(['foo', 'bar']), ignores.get_runtime_ignores())
 
183
        self.assertEqual({'foo', 'bar'}, ignores.get_runtime_ignores())
169
184
 
170
185
        ignores.add_runtime_ignores(['bar'])
171
 
        self.assertEqual(set(['foo', 'bar']), ignores.get_runtime_ignores())
 
186
        self.assertEqual({'foo', 'bar'}, ignores.get_runtime_ignores())
172
187
 
173
188
 
174
189
class TestTreeIgnores(TestCaseWithTransport):
175
 
    
 
190
 
176
191
    def assertPatternsEquals(self, patterns):
177
 
        contents = open(".bzrignore", 'rU').read().strip().split('\n')
178
 
        self.assertEquals(sorted(patterns), sorted(contents))
 
192
        with open(".bzrignore", "rb") as f:
 
193
            contents = f.read().decode("utf-8").splitlines()
 
194
        self.assertEqual(sorted(patterns), sorted(contents))
179
195
 
180
196
    def test_new_file(self):
181
197
        tree = self.make_branch_and_tree(".")
182
 
        ignores.tree_ignores_add_patterns(tree, ["myentry"])
 
198
        ignores.tree_ignores_add_patterns(tree, [u"myentry"])
183
199
        self.assertTrue(tree.has_filename(".bzrignore"))
184
200
        self.assertPatternsEquals(["myentry"])
185
201
 
186
202
    def test_add_to_existing(self):
187
203
        tree = self.make_branch_and_tree(".")
188
 
        self.build_tree_contents([('.bzrignore', "myentry1\n")])
 
204
        self.build_tree_contents([('.bzrignore', b"myentry1\n")])
189
205
        tree.add([".bzrignore"])
190
 
        ignores.tree_ignores_add_patterns(tree, ["myentry2", "foo"])
 
206
        ignores.tree_ignores_add_patterns(tree, [u"myentry2", u"foo"])
191
207
        self.assertPatternsEquals(["myentry1", "myentry2", "foo"])
192
208
 
193
209
    def test_adds_ending_newline(self):
194
210
        tree = self.make_branch_and_tree(".")
195
 
        self.build_tree_contents([('.bzrignore', "myentry1")])
 
211
        self.build_tree_contents([('.bzrignore', b"myentry1")])
196
212
        tree.add([".bzrignore"])
197
 
        ignores.tree_ignores_add_patterns(tree, ["myentry2"])
 
213
        ignores.tree_ignores_add_patterns(tree, [u"myentry2"])
198
214
        self.assertPatternsEquals(["myentry1", "myentry2"])
199
 
        text = open(".bzrignore", 'r').read()
200
 
        self.assertTrue(text.endswith('\r\n') or
201
 
                        text.endswith('\n') or
202
 
                        text.endswith('\r'))
 
215
        with open(".bzrignore") as f:
 
216
            text = f.read()
 
217
        self.assertTrue(text.endswith(('\r\n', '\n', '\r')))
203
218
 
204
219
    def test_does_not_add_dupe(self):
205
220
        tree = self.make_branch_and_tree(".")
206
 
        self.build_tree_contents([('.bzrignore', "myentry\n")])
 
221
        self.build_tree_contents([('.bzrignore', b"myentry\n")])
207
222
        tree.add([".bzrignore"])
208
 
        ignores.tree_ignores_add_patterns(tree, ["myentry"])
 
223
        ignores.tree_ignores_add_patterns(tree, [u"myentry"])
209
224
        self.assertPatternsEquals(["myentry"])
210
225
 
211
226
    def test_non_ascii(self):
214
229
                                   u"myentry\u1234\n".encode('utf-8'))])
215
230
        tree.add([".bzrignore"])
216
231
        ignores.tree_ignores_add_patterns(tree, [u"myentry\u5678"])
217
 
        self.assertPatternsEquals([u"myentry\u1234".encode('utf-8'),
218
 
                                   u"myentry\u5678".encode('utf-8')])
 
232
        self.assertPatternsEquals([u"myentry\u1234", u"myentry\u5678"])
219
233
 
220
234
    def test_crlf(self):
221
235
        tree = self.make_branch_and_tree(".")
222
 
        self.build_tree_contents([('.bzrignore', "myentry1\r\n")])
 
236
        self.build_tree_contents([('.bzrignore', b"myentry1\r\n")])
223
237
        tree.add([".bzrignore"])
224
238
        ignores.tree_ignores_add_patterns(tree, ["myentry2", "foo"])
225
 
        self.assertEquals(open('.bzrignore', 'rb').read(), 'myentry1\r\nmyentry2\r\nfoo\r\n')
 
239
        with open('.bzrignore', 'rb') as f:
 
240
            self.assertEqual(f.read(), b'myentry1\r\nmyentry2\r\nfoo\r\n')
226
241
        self.assertPatternsEquals(["myentry1", "myentry2", "foo"])