~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_rules.py

  • Committer: John Arbash Meinel
  • Date: 2005-11-30 15:43:57 UTC
  • mto: (1185.50.1 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1518.
  • Revision ID: john@arbash-meinel.com-20051130154357-614206b3a7b83cd0
Refactored bzrlib/ui.py into a module with the possibility for multiple ui forms.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008-2011 Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
"""Tests for finding, parsing and searching rule-based preferences."""
18
 
 
19
 
import sys
20
 
 
21
 
from bzrlib import (
22
 
    errors,
23
 
    rules,
24
 
    tests,
25
 
    )
26
 
 
27
 
 
28
 
class TestIniBasedRulesSearcher(tests.TestCase):
29
 
 
30
 
    def make_searcher(self, text):
31
 
        """Make a _RulesSearcher from a string"""
32
 
        if text is None:
33
 
            lines = None
34
 
        else:
35
 
            lines = text.splitlines()
36
 
        return rules._IniBasedRulesSearcher(lines)
37
 
 
38
 
    def test_unknown_namespace(self):
39
 
        self.assertRaises(errors.UnknownRules, rules._IniBasedRulesSearcher,
40
 
            ["[junk]", "foo=bar"])
41
 
 
42
 
    def test_get_items_file_missing(self):
43
 
        rs = self.make_searcher(None)
44
 
        self.assertEquals((), rs.get_items('a.txt'))
45
 
        self.assertEquals((), rs.get_selected_items('a.txt', ['foo']))
46
 
 
47
 
    def test_get_items_file_empty(self):
48
 
        rs = self.make_searcher("")
49
 
        self.assertEquals((), rs.get_items('a.txt'))
50
 
        self.assertEquals((), rs.get_selected_items('a.txt', ['foo']))
51
 
 
52
 
    def test_get_items_from_extension_match(self):
53
 
        rs = self.make_searcher("[name *.txt]\nfoo=bar\na=True\n")
54
 
        self.assertEquals((), rs.get_items('a.py'))
55
 
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
56
 
            rs.get_items('a.txt'))
57
 
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
58
 
            rs.get_items('dir/a.txt'))
59
 
        self.assertEquals((('foo', 'bar'),),
60
 
            rs.get_selected_items('a.txt', ['foo']))
61
 
 
62
 
    def test_get_items_from_multiple_glob_match(self):
63
 
        rs = self.make_searcher(
64
 
            "[name *.txt *.py 'x x' \"y y\"]\nfoo=bar\na=True\n")
65
 
        self.assertEquals((), rs.get_items('NEWS'))
66
 
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
67
 
            rs.get_items('a.py'))
68
 
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
69
 
            rs.get_items('a.txt'))
70
 
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
71
 
            rs.get_items('x x'))
72
 
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
73
 
            rs.get_items('y y'))
74
 
 
75
 
    def test_get_items_pathname_match(self):
76
 
        rs = self.make_searcher("[name ./a.txt]\nfoo=baz\n")
77
 
        self.assertEquals((('foo', 'baz'),),
78
 
            rs.get_items('a.txt'))
79
 
        self.assertEquals((), rs.get_items('dir/a.txt'))
80
 
 
81
 
    def test_get_items_match_first(self):
82
 
        rs = self.make_searcher(
83
 
            "[name ./a.txt]\nfoo=baz\n"
84
 
            "[name *.txt]\nfoo=bar\na=True\n")
85
 
        self.assertEquals((('foo', 'baz'),),
86
 
            rs.get_items('a.txt'))
87
 
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
88
 
            rs.get_items('dir/a.txt'))
89
 
 
90
 
 
91
 
class TestStackedRulesSearcher(tests.TestCase):
92
 
 
93
 
    def make_searcher(self, text1=None, text2=None):
94
 
        """Make a _StackedRulesSearcher with 0, 1 or 2 items"""
95
 
        searchers = []
96
 
        if text1 is not None:
97
 
            searchers.append(rules._IniBasedRulesSearcher(
98
 
                text1.splitlines()))
99
 
        if text2 is not None:
100
 
            searchers.append(rules._IniBasedRulesSearcher(
101
 
                text2.splitlines()))
102
 
        return rules._StackedRulesSearcher(searchers)
103
 
 
104
 
    def test_stack_searching(self):
105
 
        rs = self.make_searcher(
106
 
            "[name ./a.txt]\nfoo=baz\n",
107
 
            "[name *.txt]\nfoo=bar\na=True\n")
108
 
        self.assertEquals((('foo', 'baz'),),
109
 
            rs.get_items('a.txt'))
110
 
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
111
 
            rs.get_items('dir/a.txt'))
112
 
 
113
 
 
114
 
class TestRulesPath(tests.TestCase):
115
 
 
116
 
    def setUp(self):
117
 
        super(TestRulesPath, self).setUp()
118
 
        self.overrideEnv('HOME', '/home/bogus')
119
 
        if sys.platform == 'win32':
120
 
            self.overrideEnv(
121
 
                'BZR_HOME', r'C:\Documents and Settings\bogus\Application Data')
122
 
            self.bzr_home = \
123
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0'
124
 
        else:
125
 
            self.bzr_home = '/home/bogus/.bazaar'
126
 
 
127
 
    def test_rules_filename(self):
128
 
        self.assertEqual(rules.rules_filename(),
129
 
                         self.bzr_home + '/rules')