/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 bzrlib/tests/test_ignores.py

  • Committer: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2012, 2016 Canonical Ltd
 
1
# Copyright (C) 2006-2011 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 .. import (
 
19
from cStringIO import StringIO
 
20
 
 
21
from bzrlib import (
20
22
    config,
21
23
    ignores,
22
24
    )
23
 
from ..sixish import (
24
 
    BytesIO,
25
 
    )
26
 
from . import (
 
25
from bzrlib.tests import (
27
26
    TestCase,
28
27
    TestCaseInTempDir,
29
28
    TestCaseWithTransport,
33
32
class TestParseIgnoreFile(TestCase):
34
33
 
35
34
    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'
 
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'
47
46
                ))
48
 
        self.assertEqual({u'./rootdir',
49
 
                          u'randomfile*',
50
 
                          u'path/from/ro?t',
 
47
        self.assertEqual(set(['./rootdir',
 
48
                          'randomfile*',
 
49
                          'path/from/ro?t',
51
50
                          u'unicode\xb5',
52
 
                          u'dos',
53
 
                          u' xx ',
54
 
                          u'!RE:^\\.z.*',
55
 
                          u'!!./.zcompdump',
56
 
                         }, ignored)
 
51
                          'dos',
 
52
                          ' xx ',
 
53
                          '!RE:^\.z.*',
 
54
                          '!!./.zcompdump',
 
55
                         ]), ignored)
57
56
 
58
57
    def test_parse_empty(self):
59
 
        ignored = ignores.parse_ignore_file(BytesIO(b''))
 
58
        ignored = ignores.parse_ignore_file(StringIO(''))
60
59
        self.assertEqual(set([]), ignored)
61
60
        
62
61
    def test_parse_non_utf8(self):
63
62
        """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'
 
63
        ignored = ignores.parse_ignore_file(StringIO(
 
64
                'utf8filename_a\n'
 
65
                'invalid utf8\x80\n'
 
66
                'utf8filename_b\n'
68
67
                ))
69
 
        self.assertEqual({
70
 
                        u'utf8filename_a',
71
 
                        u'utf8filename_b',
72
 
                       }, ignored)
 
68
        self.assertEqual(set([
 
69
                        'utf8filename_a',
 
70
                        'utf8filename_b',
 
71
                       ]), ignored)
73
72
 
74
73
 
75
74
class TestUserIgnores(TestCaseInTempDir):
82
81
        self.assertEqual(set(ignores.USER_DEFAULTS), user_ignores)
83
82
 
84
83
        self.assertPathExists(ignore_path)
85
 
        with open(ignore_path, 'rb') as f:
 
84
        f = open(ignore_path, 'rb')
 
85
        try:
86
86
            entries = ignores.parse_ignore_file(f)
 
87
        finally:
 
88
            f.close()
87
89
        self.assertEqual(set(ignores.USER_DEFAULTS), entries)
88
90
 
89
91
    def test_use_existing(self):
90
 
        patterns = [u'*.o', u'*.py[co]', u'\xe5*']
 
92
        patterns = ['*.o', '*.py[co]', u'\xe5*']
91
93
        ignores._set_user_ignores(patterns)
92
94
 
93
95
        user_ignores = ignores.get_user_ignores()
139
141
        added = ignores.add_unique_user_ignores(
140
142
            ['xxx', './bar', 'xxx', 'dir1/', 'dir2/', 'dir3\\'])
141
143
        self.assertEqual(['xxx', 'dir2'], added)
142
 
        self.assertEqual({'foo', './bar', u'b\xe5z',
143
 
                              'xxx', 'dir1', 'dir2', 'dir3'},
 
144
        self.assertEqual(set(['foo', './bar', u'b\xe5z',
 
145
                              'xxx', 'dir1', 'dir2', 'dir3']),
144
146
                         ignores.get_user_ignores())
145
147
 
146
148
 
158
160
        self.assertEqual(set(), ignores.get_runtime_ignores())
159
161
 
160
162
        ignores.add_runtime_ignores(['foo'])
161
 
        self.assertEqual({'foo'}, ignores.get_runtime_ignores())
 
163
        self.assertEqual(set(['foo']), ignores.get_runtime_ignores())
162
164
 
163
165
    def test_add_duplicate(self):
164
166
        """Adding the same ignore twice shouldn't add a new entry."""
165
167
        ignores.add_runtime_ignores(['foo', 'bar'])
166
 
        self.assertEqual({'foo', 'bar'}, ignores.get_runtime_ignores())
 
168
        self.assertEqual(set(['foo', 'bar']), ignores.get_runtime_ignores())
167
169
 
168
170
        ignores.add_runtime_ignores(['bar'])
169
 
        self.assertEqual({'foo', 'bar'}, ignores.get_runtime_ignores())
 
171
        self.assertEqual(set(['foo', 'bar']), ignores.get_runtime_ignores())
170
172
 
171
173
 
172
174
class TestTreeIgnores(TestCaseWithTransport):
173
 
 
 
175
    
174
176
    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))
 
177
        contents = open(".bzrignore", 'rU').read().strip().split('\n')
 
178
        self.assertEquals(sorted(patterns), sorted(contents))
178
179
 
179
180
    def test_new_file(self):
180
181
        tree = self.make_branch_and_tree(".")
181
 
        ignores.tree_ignores_add_patterns(tree, [u"myentry"])
 
182
        ignores.tree_ignores_add_patterns(tree, ["myentry"])
182
183
        self.assertTrue(tree.has_filename(".bzrignore"))
183
184
        self.assertPatternsEquals(["myentry"])
184
185
 
185
186
    def test_add_to_existing(self):
186
187
        tree = self.make_branch_and_tree(".")
187
 
        self.build_tree_contents([('.bzrignore', b"myentry1\n")])
 
188
        self.build_tree_contents([('.bzrignore', "myentry1\n")])
188
189
        tree.add([".bzrignore"])
189
 
        ignores.tree_ignores_add_patterns(tree, [u"myentry2", u"foo"])
 
190
        ignores.tree_ignores_add_patterns(tree, ["myentry2", "foo"])
190
191
        self.assertPatternsEquals(["myentry1", "myentry2", "foo"])
191
192
 
192
193
    def test_adds_ending_newline(self):
193
194
        tree = self.make_branch_and_tree(".")
194
 
        self.build_tree_contents([('.bzrignore', b"myentry1")])
 
195
        self.build_tree_contents([('.bzrignore', "myentry1")])
195
196
        tree.add([".bzrignore"])
196
 
        ignores.tree_ignores_add_patterns(tree, [u"myentry2"])
 
197
        ignores.tree_ignores_add_patterns(tree, ["myentry2"])
197
198
        self.assertPatternsEquals(["myentry1", "myentry2"])
198
 
        with open(".bzrignore") as f:
199
 
            text = f.read()
200
 
        self.assertTrue(text.endswith(('\r\n', '\n', '\r')))
 
199
        text = open(".bzrignore", 'r').read()
 
200
        self.assertTrue(text.endswith('\r\n') or
 
201
                        text.endswith('\n') or
 
202
                        text.endswith('\r'))
201
203
 
202
204
    def test_does_not_add_dupe(self):
203
205
        tree = self.make_branch_and_tree(".")
204
 
        self.build_tree_contents([('.bzrignore', b"myentry\n")])
 
206
        self.build_tree_contents([('.bzrignore', "myentry\n")])
205
207
        tree.add([".bzrignore"])
206
 
        ignores.tree_ignores_add_patterns(tree, [u"myentry"])
 
208
        ignores.tree_ignores_add_patterns(tree, ["myentry"])
207
209
        self.assertPatternsEquals(["myentry"])
208
210
 
209
211
    def test_non_ascii(self):
212
214
                                   u"myentry\u1234\n".encode('utf-8'))])
213
215
        tree.add([".bzrignore"])
214
216
        ignores.tree_ignores_add_patterns(tree, [u"myentry\u5678"])
215
 
        self.assertPatternsEquals([u"myentry\u1234", u"myentry\u5678"])
 
217
        self.assertPatternsEquals([u"myentry\u1234".encode('utf-8'),
 
218
                                   u"myentry\u5678".encode('utf-8')])
216
219
 
217
220
    def test_crlf(self):
218
221
        tree = self.make_branch_and_tree(".")
219
 
        self.build_tree_contents([('.bzrignore', b"myentry1\r\n")])
 
222
        self.build_tree_contents([('.bzrignore', "myentry1\r\n")])
220
223
        tree.add([".bzrignore"])
221
224
        ignores.tree_ignores_add_patterns(tree, ["myentry2", "foo"])
222
 
        with open('.bzrignore') as f:
223
 
            self.assertEqual(f.read(), 'myentry1\r\nmyentry2\r\nfoo\r\n')
 
225
        self.assertEquals(open('.bzrignore', 'rb').read(), 'myentry1\r\nmyentry2\r\nfoo\r\n')
224
226
        self.assertPatternsEquals(["myentry1", "myentry2", "foo"])