/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: Marius Kruger
  • Date: 2010-07-10 21:28:56 UTC
  • mto: (5384.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5385.
  • Revision ID: marius.kruger@enerweb.co.za-20100710212856-uq4ji3go0u5se7hx
* Update documentation
* add NEWS

Show diffs side-by-side

added added

removed removed

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