1
# Copyright (C) 2008 Canonical Ltd
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Tests for finding, parsing and searching rule-based preferences."""
28
from bzrlib.util.configobj import configobj
31
class TestIniBasedRulesSearcher(tests.TestCase):
33
def make_searcher(self, text):
34
"""Make a _RulesSearcher from a string"""
38
lines = text.splitlines()
39
return rules._IniBasedRulesSearcher(lines)
41
def test_unknown_namespace(self):
42
self.assertRaises(errors.UnknownRules, rules._IniBasedRulesSearcher,
43
["[junk]", "foo=bar"])
45
def test_get_items_file_missing(self):
46
rs = self.make_searcher(None)
47
self.assertEquals((), rs.get_items('a.txt'))
48
self.assertEquals((), rs.get_selected_items('a.txt', ['foo']))
50
def test_get_items_file_empty(self):
51
rs = self.make_searcher("")
52
self.assertEquals((), rs.get_items('a.txt'))
53
self.assertEquals((), rs.get_selected_items('a.txt', ['foo']))
55
def test_get_items_from_extension_match(self):
56
rs = self.make_searcher("[name *.txt]\nfoo=bar\na=True\n")
57
self.assertEquals((), rs.get_items('a.py'))
58
self.assertEquals((('foo', 'bar'), ('a', 'True')),
59
rs.get_items('a.txt'))
60
self.assertEquals((('foo', 'bar'), ('a', 'True')),
61
rs.get_items('dir/a.txt'))
62
self.assertEquals((('foo', 'bar'),),
63
rs.get_selected_items('a.txt', ['foo']))
65
def test_get_items_from_multiple_glob_match(self):
66
rs = self.make_searcher(
67
"[name *.txt *.py 'x x' \"y y\"]\nfoo=bar\na=True\n")
68
self.assertEquals((), rs.get_items('NEWS'))
69
self.assertEquals((('foo', 'bar'), ('a', 'True')),
71
self.assertEquals((('foo', 'bar'), ('a', 'True')),
72
rs.get_items('a.txt'))
73
self.assertEquals((('foo', 'bar'), ('a', 'True')),
75
self.assertEquals((('foo', 'bar'), ('a', 'True')),
78
def test_get_items_pathname_match(self):
79
rs = self.make_searcher("[name ./a.txt]\nfoo=baz\n")
80
self.assertEquals((('foo', 'baz'),),
81
rs.get_items('a.txt'))
82
self.assertEquals((), rs.get_items('dir/a.txt'))
84
def test_get_items_match_first(self):
85
rs = self.make_searcher(
86
"[name ./a.txt]\nfoo=baz\n"
87
"[name *.txt]\nfoo=bar\na=True\n")
88
self.assertEquals((('foo', 'baz'),),
89
rs.get_items('a.txt'))
90
self.assertEquals((('foo', 'bar'), ('a', 'True')),
91
rs.get_items('dir/a.txt'))
94
class TestStackedRulesSearcher(tests.TestCase):
96
def make_searcher(self, text1=None, text2=None):
97
"""Make a _StackedRulesSearcher with 0, 1 or 2 items"""
100
searchers.append(rules._IniBasedRulesSearcher(
102
if text2 is not None:
103
searchers.append(rules._IniBasedRulesSearcher(
105
return rules._StackedRulesSearcher(searchers)
107
def test_stack_searching(self):
108
rs = self.make_searcher(
109
"[name ./a.txt]\nfoo=baz\n",
110
"[name *.txt]\nfoo=bar\na=True\n")
111
self.assertEquals((('foo', 'baz'),),
112
rs.get_items('a.txt'))
113
self.assertEquals((('foo', 'bar'), ('a', 'True')),
114
rs.get_items('dir/a.txt'))
117
class TestRulesPath(tests.TestCase):
120
super(TestRulesPath, self).setUp()
121
os.environ['HOME'] = '/home/bogus'
122
if sys.platform == 'win32':
123
os.environ['BZR_HOME'] = \
124
r'C:\Documents and Settings\bogus\Application Data'
126
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0'
128
self.bzr_home = '/home/bogus/.bazaar'
130
def test_rules_filename(self):
131
self.assertEqual(rules.rules_filename(),
132
self.bzr_home + '/rules')