/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): Martin
  • Date: 2017-06-05 01:55:02 UTC
  • mfrom: (6651.4.3 plugin_rewrite)
  • Revision ID: breezy.the.bot@gmail.com-20170605015502-tqiyvpz3qt00fge1
Rewrite of plugin module

Merged from https://code.launchpad.net/~gz/brz/plugin_rewrite/+merge/325033

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
20
 
 
21
 
from bzrlib import config, errors, ignores
22
 
from bzrlib.tests import TestCase, TestCaseInTempDir, TestCaseWithTransport
 
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
    )
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(
 
36
        ignored = ignores.parse_ignore_file(BytesIO(
29
37
                './rootdir\n'
30
38
                'randomfile*\n'
31
39
                'path/from/ro?t\n'
37
45
                '!RE:^\.z.*\n'
38
46
                '!!./.zcompdump\n'
39
47
                ))
40
 
        self.assertEqual(set(['./rootdir',
 
48
        self.assertEqual({'./rootdir',
41
49
                          'randomfile*',
42
50
                          'path/from/ro?t',
43
51
                          u'unicode\xb5',
45
53
                          ' xx ',
46
54
                          '!RE:^\.z.*',
47
55
                          '!!./.zcompdump',
48
 
                         ]), ignored)
 
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(
 
64
        ignored = ignores.parse_ignore_file(BytesIO(
57
65
                'utf8filename_a\n'
58
66
                'invalid utf8\x80\n'
59
67
                'utf8filename_b\n'
60
68
                ))
61
 
        self.assertEqual(set([
 
69
        self.assertEqual({
62
70
                        'utf8filename_a',
63
71
                        'utf8filename_b',
64
 
                       ]), ignored)
 
72
                       }, ignored)
65
73
 
66
74
 
67
75
class TestUserIgnores(TestCaseInTempDir):
69
77
    def test_create_if_missing(self):
70
78
        # $HOME should be set to '.'
71
79
        ignore_path = config.user_ignore_config_filename()
72
 
        self.failIfExists(ignore_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)
 
84
        self.assertPathExists(ignore_path)
77
85
        f = open(ignore_path, 'rb')
78
86
        try:
79
87
            entries = ignores.parse_ignore_file(f)
134
142
        added = ignores.add_unique_user_ignores(
135
143
            ['xxx', './bar', 'xxx', 'dir1/', 'dir2/', 'dir3\\'])
136
144
        self.assertEqual(['xxx', 'dir2'], added)
137
 
        self.assertEqual(set(['foo', './bar', u'b\xe5z',
138
 
                              'xxx', 'dir1', 'dir2', 'dir3']),
 
145
        self.assertEqual({'foo', './bar', u'b\xe5z',
 
146
                              'xxx', 'dir1', 'dir2', 'dir3'},
139
147
                         ignores.get_user_ignores())
140
148
 
141
149
 
142
150
class TestRuntimeIgnores(TestCase):
143
151
 
144
152
    def setUp(self):
145
 
        TestCase.setUp(self)
 
153
        super(TestRuntimeIgnores, self).setUp()
146
154
 
147
155
        # For the purposes of these tests, we must have no
148
156
        # runtime ignores
153
161
        self.assertEqual(set(), ignores.get_runtime_ignores())
154
162
 
155
163
        ignores.add_runtime_ignores(['foo'])
156
 
        self.assertEqual(set(['foo']), ignores.get_runtime_ignores())
 
164
        self.assertEqual({'foo'}, ignores.get_runtime_ignores())
157
165
 
158
166
    def test_add_duplicate(self):
159
167
        """Adding the same ignore twice shouldn't add a new entry."""
160
168
        ignores.add_runtime_ignores(['foo', 'bar'])
161
 
        self.assertEqual(set(['foo', 'bar']), ignores.get_runtime_ignores())
 
169
        self.assertEqual({'foo', 'bar'}, ignores.get_runtime_ignores())
162
170
 
163
171
        ignores.add_runtime_ignores(['bar'])
164
 
        self.assertEqual(set(['foo', 'bar']), ignores.get_runtime_ignores())
 
172
        self.assertEqual({'foo', 'bar'}, ignores.get_runtime_ignores())
165
173
 
166
174
 
167
175
class TestTreeIgnores(TestCaseWithTransport):
 
176
    
 
177
    def assertPatternsEquals(self, patterns):
 
178
        contents = open(".bzrignore", 'rU').read().strip().split('\n')
 
179
        self.assertEqual(sorted(patterns), sorted(contents))
168
180
 
169
181
    def test_new_file(self):
170
182
        tree = self.make_branch_and_tree(".")
171
183
        ignores.tree_ignores_add_patterns(tree, ["myentry"])
172
184
        self.assertTrue(tree.has_filename(".bzrignore"))
173
 
        self.assertEquals("myentry\n",
174
 
                          open(".bzrignore", 'r').read())
 
185
        self.assertPatternsEquals(["myentry"])
175
186
 
176
187
    def test_add_to_existing(self):
177
188
        tree = self.make_branch_and_tree(".")
178
189
        self.build_tree_contents([('.bzrignore', "myentry1\n")])
179
190
        tree.add([".bzrignore"])
180
191
        ignores.tree_ignores_add_patterns(tree, ["myentry2", "foo"])
181
 
        self.assertEquals("myentry1\nmyentry2\nfoo\n",
182
 
                          open(".bzrignore", 'r').read())
 
192
        self.assertPatternsEquals(["myentry1", "myentry2", "foo"])
183
193
 
184
194
    def test_adds_ending_newline(self):
185
195
        tree = self.make_branch_and_tree(".")
186
196
        self.build_tree_contents([('.bzrignore', "myentry1")])
187
197
        tree.add([".bzrignore"])
188
198
        ignores.tree_ignores_add_patterns(tree, ["myentry2"])
189
 
        self.assertEquals("myentry1\nmyentry2\n",
190
 
                          open(".bzrignore", 'r').read())
191
 
 
 
199
        self.assertPatternsEquals(["myentry1", "myentry2"])
 
200
        text = open(".bzrignore", 'r').read()
 
201
        self.assertTrue(text.endswith('\r\n') or
 
202
                        text.endswith('\n') or
 
203
                        text.endswith('\r'))
 
204
 
 
205
    def test_does_not_add_dupe(self):
 
206
        tree = self.make_branch_and_tree(".")
 
207
        self.build_tree_contents([('.bzrignore', "myentry\n")])
 
208
        tree.add([".bzrignore"])
 
209
        ignores.tree_ignores_add_patterns(tree, ["myentry"])
 
210
        self.assertPatternsEquals(["myentry"])
 
211
 
 
212
    def test_non_ascii(self):
 
213
        tree = self.make_branch_and_tree(".")
 
214
        self.build_tree_contents([('.bzrignore',
 
215
                                   u"myentry\u1234\n".encode('utf-8'))])
 
216
        tree.add([".bzrignore"])
 
217
        ignores.tree_ignores_add_patterns(tree, [u"myentry\u5678"])
 
218
        self.assertPatternsEquals([u"myentry\u1234".encode('utf-8'),
 
219
                                   u"myentry\u5678".encode('utf-8')])
 
220
 
 
221
    def test_crlf(self):
 
222
        tree = self.make_branch_and_tree(".")
 
223
        self.build_tree_contents([('.bzrignore', "myentry1\r\n")])
 
224
        tree.add([".bzrignore"])
 
225
        ignores.tree_ignores_add_patterns(tree, ["myentry2", "foo"])
 
226
        self.assertEqual(open('.bzrignore', 'rb').read(), 'myentry1\r\nmyentry2\r\nfoo\r\n')
 
227
        self.assertPatternsEquals(["myentry1", "myentry2", "foo"])