~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_rules.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Tests for finding, parsing and searching rule-based preferences."""
 
18
 
 
19
import os
 
20
import sys
 
21
 
 
22
from bzrlib import (
 
23
    config,
 
24
    errors,
 
25
    rules,
 
26
    tests,
 
27
    )
 
28
from bzrlib.util.configobj import configobj
 
29
 
 
30
 
 
31
class TestIniBasedRulesSearcher(tests.TestCase):
 
32
 
 
33
    def make_searcher(self, text):
 
34
        """Make a _RulesSearcher from a string"""
 
35
        if text is None:
 
36
            lines = None
 
37
        else:
 
38
            lines = text.splitlines()
 
39
        return rules._IniBasedRulesSearcher(lines)
 
40
 
 
41
    def test_unknown_namespace(self):
 
42
        self.assertRaises(errors.UnknownRules, rules._IniBasedRulesSearcher,
 
43
            ["[junk]", "foo=bar"])
 
44
 
 
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']))
 
49
 
 
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']))
 
54
 
 
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']))
 
64
 
 
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'))
 
70
 
 
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'))
 
79
 
 
80
 
 
81
class TestStackedRulesSearcher(tests.TestCase):
 
82
 
 
83
    def make_searcher(self, text1=None, text2=None):
 
84
        """Make a _StackedRulesSearcher with 0, 1 or 2 items"""
 
85
        searchers = []
 
86
        if text1 is not None:
 
87
            searchers.append(rules._IniBasedRulesSearcher(
 
88
                text1.splitlines()))
 
89
        if text2 is not None:
 
90
            searchers.append(rules._IniBasedRulesSearcher(
 
91
                text2.splitlines()))
 
92
        return rules._StackedRulesSearcher(searchers)
 
93
 
 
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'))
 
102
 
 
103
 
 
104
class TestRulesPath(tests.TestCase):
 
105
 
 
106
    def setUp(self):
 
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'
 
112
            self.bzr_home = \
 
113
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0'
 
114
        else:
 
115
            self.bzr_home = '/home/bogus/.bazaar'
 
116
 
 
117
    def test_rules_filename(self):
 
118
        self.assertEqual(rules.rules_filename(),
 
119
                         self.bzr_home + '/rules')