~bzr-pqm/bzr/bzr.dev

3398.1.4 by Ian Clatworthy
add properties_filename() test
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
3398.1.4 by Ian Clatworthy
add properties_filename() test
16
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
17
"""Tests for finding, parsing and searching rule-based preferences."""
3398.1.4 by Ian Clatworthy
add properties_filename() test
18
19
import os
20
import sys
21
22
from bzrlib import (
23
    config,
3398.1.30 by Ian Clatworthy
test unknown rules detection
24
    errors,
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
25
    rules,
3398.1.4 by Ian Clatworthy
add properties_filename() test
26
    tests,
27
    )
28
from bzrlib.util.configobj import configobj
29
30
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
31
class TestIniBasedRulesSearcher(tests.TestCase):
32
3398.1.33 by Ian Clatworthy
use a string, not lists, for test data
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)
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
40
3398.1.30 by Ian Clatworthy
test unknown rules detection
41
    def test_unknown_namespace(self):
42
        self.assertRaises(errors.UnknownRules, rules._IniBasedRulesSearcher,
43
            ["[junk]", "foo=bar"])
44
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
45
    def test_get_items_file_missing(self):
46
        rs = self.make_searcher(None)
3564.1.1 by Ian Clatworthy
RuleSearchers need to return () instead of []
47
        self.assertEquals((), rs.get_items('a.txt'))
48
        self.assertEquals((), rs.get_selected_items('a.txt', ['foo']))
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
49
50
    def test_get_items_file_empty(self):
3398.1.33 by Ian Clatworthy
use a string, not lists, for test data
51
        rs = self.make_searcher("")
3564.1.1 by Ian Clatworthy
RuleSearchers need to return () instead of []
52
        self.assertEquals((), rs.get_items('a.txt'))
53
        self.assertEquals((), rs.get_selected_items('a.txt', ['foo']))
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
54
55
    def test_get_items_from_extension_match(self):
3398.1.33 by Ian Clatworthy
use a string, not lists, for test data
56
        rs = self.make_searcher("[name *.txt]\nfoo=bar\na=True\n")
3564.1.1 by Ian Clatworthy
RuleSearchers need to return () instead of []
57
        self.assertEquals((), rs.get_items('a.py'))
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
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'),),
3398.1.34 by Ian Clatworthy
changed API design as requested by jam during review
63
            rs.get_selected_items('a.txt', ['foo']))
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
64
3943.3.1 by Marius Kruger
add test and support for multi-glob rules
65
    def test_get_items_from_multiple_glob_match(self):
3943.3.2 by Marius Kruger
actually support quoting the patterns as the docs suggest
66
        rs = self.make_searcher(
67
            "[name *.txt *.py 'x x' \"y y\"]\nfoo=bar\na=True\n")
3943.3.1 by Marius Kruger
add test and support for multi-glob rules
68
        self.assertEquals((), rs.get_items('NEWS'))
69
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
70
            rs.get_items('a.py'))
71
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
72
            rs.get_items('a.txt'))
3943.3.2 by Marius Kruger
actually support quoting the patterns as the docs suggest
73
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
74
            rs.get_items('x x'))
75
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
76
            rs.get_items('y y'))
3943.3.1 by Marius Kruger
add test and support for multi-glob rules
77
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
78
    def test_get_items_pathname_match(self):
3398.1.33 by Ian Clatworthy
use a string, not lists, for test data
79
        rs = self.make_searcher("[name ./a.txt]\nfoo=baz\n")
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
80
        self.assertEquals((('foo', 'baz'),),
81
            rs.get_items('a.txt'))
3564.1.1 by Ian Clatworthy
RuleSearchers need to return () instead of []
82
        self.assertEquals((), rs.get_items('dir/a.txt'))
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
83
84
    def test_get_items_match_first(self):
3398.1.33 by Ian Clatworthy
use a string, not lists, for test data
85
        rs = self.make_searcher(
86
            "[name ./a.txt]\nfoo=baz\n"
87
            "[name *.txt]\nfoo=bar\na=True\n")
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
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'))
92
93
3398.1.18 by Ian Clatworthy
add tests for _StackedRulesSearcher
94
class TestStackedRulesSearcher(tests.TestCase):
95
3398.1.33 by Ian Clatworthy
use a string, not lists, for test data
96
    def make_searcher(self, text1=None, text2=None):
3398.1.18 by Ian Clatworthy
add tests for _StackedRulesSearcher
97
        """Make a _StackedRulesSearcher with 0, 1 or 2 items"""
98
        searchers = []
3398.1.33 by Ian Clatworthy
use a string, not lists, for test data
99
        if text1 is not None:
100
            searchers.append(rules._IniBasedRulesSearcher(
101
                text1.splitlines()))
102
        if text2 is not None:
103
            searchers.append(rules._IniBasedRulesSearcher(
104
                text2.splitlines()))
3398.1.18 by Ian Clatworthy
add tests for _StackedRulesSearcher
105
        return rules._StackedRulesSearcher(searchers)
106
107
    def test_stack_searching(self):
108
        rs = self.make_searcher(
3398.1.33 by Ian Clatworthy
use a string, not lists, for test data
109
            "[name ./a.txt]\nfoo=baz\n",
110
            "[name *.txt]\nfoo=bar\na=True\n")
3398.1.18 by Ian Clatworthy
add tests for _StackedRulesSearcher
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'))
115
116
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
117
class TestRulesPath(tests.TestCase):
3398.1.4 by Ian Clatworthy
add properties_filename() test
118
119
    def setUp(self):
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
120
        super(TestRulesPath, self).setUp()
3398.1.4 by Ian Clatworthy
add properties_filename() test
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'
125
            self.bzr_home = \
126
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0'
127
        else:
128
            self.bzr_home = '/home/bogus/.bazaar'
129
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
130
    def test_rules_filename(self):
131
        self.assertEqual(rules.rules_filename(),
3398.1.23 by Ian Clatworthy
update doc to reflect file naming per poolie's review
132
                         self.bzr_home + '/rules')