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_pathname_match(self):
 
 
66
        rs = self.make_searcher("[name ./a.txt]\nfoo=baz\n")
 
 
67
        self.assertEquals((('foo', 'baz'),),
 
 
68
            rs.get_items('a.txt'))
 
 
69
        self.assertEquals((), rs.get_items('dir/a.txt'))
 
 
71
    def test_get_items_match_first(self):
 
 
72
        rs = self.make_searcher(
 
 
73
            "[name ./a.txt]\nfoo=baz\n"
 
 
74
            "[name *.txt]\nfoo=bar\na=True\n")
 
 
75
        self.assertEquals((('foo', 'baz'),),
 
 
76
            rs.get_items('a.txt'))
 
 
77
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
 
 
78
            rs.get_items('dir/a.txt'))
 
 
81
class TestStackedRulesSearcher(tests.TestCase):
 
 
83
    def make_searcher(self, text1=None, text2=None):
 
 
84
        """Make a _StackedRulesSearcher with 0, 1 or 2 items"""
 
 
87
            searchers.append(rules._IniBasedRulesSearcher(
 
 
90
            searchers.append(rules._IniBasedRulesSearcher(
 
 
92
        return rules._StackedRulesSearcher(searchers)
 
 
94
    def test_stack_searching(self):
 
 
95
        rs = self.make_searcher(
 
 
96
            "[name ./a.txt]\nfoo=baz\n",
 
 
97
            "[name *.txt]\nfoo=bar\na=True\n")
 
 
98
        self.assertEquals((('foo', 'baz'),),
 
 
99
            rs.get_items('a.txt'))
 
 
100
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
 
 
101
            rs.get_items('dir/a.txt'))
 
 
104
class TestRulesPath(tests.TestCase):
 
 
107
        super(TestRulesPath, self).setUp()
 
 
108
        os.environ['HOME'] = '/home/bogus'
 
 
109
        if sys.platform == 'win32':
 
 
110
            os.environ['BZR_HOME'] = \
 
 
111
                r'C:\Documents and Settings\bogus\Application Data'
 
 
113
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0'
 
 
115
            self.bzr_home = '/home/bogus/.bazaar'
 
 
117
    def test_rules_filename(self):
 
 
118
        self.assertEqual(rules.rules_filename(),
 
 
119
                         self.bzr_home + '/rules')