/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: 2020-02-18 01:57:45 UTC
  • mto: This revision was merged to the branch mainline in revision 7493.
  • Revision ID: jelmer@jelmer.uk-20200218015745-q2ss9tsk74h4nh61
drop use of future.

Show diffs side-by-side

added added

removed removed

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