~bzr-pqm/bzr/bzr.dev

6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2008-2011, 2016 Canonical Ltd
3398.1.4 by Ian Clatworthy
add properties_filename() test
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 sys
20
21
from bzrlib import (
3398.1.30 by Ian Clatworthy
test unknown rules detection
22
    errors,
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
23
    rules,
3398.1.4 by Ian Clatworthy
add properties_filename() test
24
    tests,
25
    )
26
27
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
28
class TestIniBasedRulesSearcher(tests.TestCase):
29
3398.1.33 by Ian Clatworthy
use a string, not lists, for test data
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)
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
37
3398.1.30 by Ian Clatworthy
test unknown rules detection
38
    def test_unknown_namespace(self):
39
        self.assertRaises(errors.UnknownRules, rules._IniBasedRulesSearcher,
40
            ["[junk]", "foo=bar"])
41
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
42
    def test_get_items_file_missing(self):
43
        rs = self.make_searcher(None)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
44
        self.assertEqual((), rs.get_items('a.txt'))
45
        self.assertEqual((), rs.get_selected_items('a.txt', ['foo']))
46
        self.assertEqual(None, rs.get_single_value('a.txt', 'foo'))
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
47
48
    def test_get_items_file_empty(self):
3398.1.33 by Ian Clatworthy
use a string, not lists, for test data
49
        rs = self.make_searcher("")
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
50
        self.assertEqual((), rs.get_items('a.txt'))
51
        self.assertEqual((), rs.get_selected_items('a.txt', ['foo']))
52
        self.assertEqual(None, rs.get_single_value('a.txt', 'foo'))
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
53
54
    def test_get_items_from_extension_match(self):
3398.1.33 by Ian Clatworthy
use a string, not lists, for test data
55
        rs = self.make_searcher("[name *.txt]\nfoo=bar\na=True\n")
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
56
        self.assertEqual((), rs.get_items('a.py'))
57
        self.assertEqual((('foo', 'bar'), ('a', 'True')),
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
58
            rs.get_items('a.txt'))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
59
        self.assertEqual((('foo', 'bar'), ('a', 'True')),
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
60
            rs.get_items('dir/a.txt'))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
61
        self.assertEqual((('foo', 'bar'),),
3398.1.34 by Ian Clatworthy
changed API design as requested by jam during review
62
            rs.get_selected_items('a.txt', ['foo']))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
63
        self.assertEqual('bar', rs.get_single_value('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):
4913.5.14 by Gordon Tyler
Reverted changes to test_rules since the original should work now.
66
        rs = self.make_searcher(
67
            "[name *.txt *.py 'x x' \"y y\"]\nfoo=bar\na=True\n")
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
68
        self.assertEqual((), rs.get_items('NEWS'))
69
        self.assertEqual((('foo', 'bar'), ('a', 'True')),
3943.3.1 by Marius Kruger
add test and support for multi-glob rules
70
            rs.get_items('a.py'))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
71
        self.assertEqual((('foo', 'bar'), ('a', 'True')),
3943.3.1 by Marius Kruger
add test and support for multi-glob rules
72
            rs.get_items('a.txt'))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
73
        self.assertEqual((('foo', 'bar'), ('a', 'True')),
3943.3.2 by Marius Kruger
actually support quoting the patterns as the docs suggest
74
            rs.get_items('x x'))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
75
        self.assertEqual((('foo', 'bar'), ('a', 'True')),
3943.3.2 by Marius Kruger
actually support quoting the patterns as the docs suggest
76
            rs.get_items('y y'))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
77
        self.assertEqual('bar', rs.get_single_value('a.txt', 'foo'))
3943.3.1 by Marius Kruger
add test and support for multi-glob rules
78
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
79
    def test_get_items_pathname_match(self):
3398.1.33 by Ian Clatworthy
use a string, not lists, for test data
80
        rs = self.make_searcher("[name ./a.txt]\nfoo=baz\n")
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
81
        self.assertEqual((('foo', 'baz'),),
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
82
            rs.get_items('a.txt'))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
83
        self.assertEqual('baz', rs.get_single_value('a.txt', 'foo'))
84
        self.assertEqual((), rs.get_items('dir/a.txt'))
85
        self.assertEqual(None, rs.get_single_value('dir/a.txt', 'foo'))
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
86
87
    def test_get_items_match_first(self):
3398.1.33 by Ian Clatworthy
use a string, not lists, for test data
88
        rs = self.make_searcher(
89
            "[name ./a.txt]\nfoo=baz\n"
90
            "[name *.txt]\nfoo=bar\na=True\n")
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
91
        self.assertEqual((('foo', 'baz'),),
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
92
            rs.get_items('a.txt'))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
93
        self.assertEqual('baz', rs.get_single_value('a.txt', 'foo'))
94
        self.assertEqual((('foo', 'bar'), ('a', 'True')),
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
95
            rs.get_items('dir/a.txt'))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
96
        self.assertEqual('bar', rs.get_single_value('dir/a.txt', 'foo'))
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
97
98
3398.1.18 by Ian Clatworthy
add tests for _StackedRulesSearcher
99
class TestStackedRulesSearcher(tests.TestCase):
100
3398.1.33 by Ian Clatworthy
use a string, not lists, for test data
101
    def make_searcher(self, text1=None, text2=None):
3398.1.18 by Ian Clatworthy
add tests for _StackedRulesSearcher
102
        """Make a _StackedRulesSearcher with 0, 1 or 2 items"""
103
        searchers = []
3398.1.33 by Ian Clatworthy
use a string, not lists, for test data
104
        if text1 is not None:
105
            searchers.append(rules._IniBasedRulesSearcher(
106
                text1.splitlines()))
107
        if text2 is not None:
108
            searchers.append(rules._IniBasedRulesSearcher(
109
                text2.splitlines()))
3398.1.18 by Ian Clatworthy
add tests for _StackedRulesSearcher
110
        return rules._StackedRulesSearcher(searchers)
111
112
    def test_stack_searching(self):
113
        rs = self.make_searcher(
3398.1.33 by Ian Clatworthy
use a string, not lists, for test data
114
            "[name ./a.txt]\nfoo=baz\n",
115
            "[name *.txt]\nfoo=bar\na=True\n")
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
116
        self.assertEqual((('foo', 'baz'),),
3398.1.18 by Ian Clatworthy
add tests for _StackedRulesSearcher
117
            rs.get_items('a.txt'))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
118
        self.assertEqual('baz', rs.get_single_value('a.txt', 'foo'))
119
        self.assertEqual(None, rs.get_single_value('a.txt', 'a'))
120
        self.assertEqual((('foo', 'bar'), ('a', 'True')),
3398.1.18 by Ian Clatworthy
add tests for _StackedRulesSearcher
121
            rs.get_items('dir/a.txt'))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
122
        self.assertEqual('bar', rs.get_single_value('dir/a.txt', 'foo'))
123
        self.assertEqual('True', rs.get_single_value('dir/a.txt', 'a'))
3398.1.18 by Ian Clatworthy
add tests for _StackedRulesSearcher
124
125
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
126
class TestRulesPath(tests.TestCase):
3398.1.4 by Ian Clatworthy
add properties_filename() test
127
128
    def setUp(self):
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
129
        super(TestRulesPath, self).setUp()
5570.3.8 by Vincent Ladeuil
More use cases for overrideEnv.
130
        self.overrideEnv('HOME', '/home/bogus')
3398.1.4 by Ian Clatworthy
add properties_filename() test
131
        if sys.platform == 'win32':
5570.3.8 by Vincent Ladeuil
More use cases for overrideEnv.
132
            self.overrideEnv(
133
                'BZR_HOME', r'C:\Documents and Settings\bogus\Application Data')
3398.1.4 by Ian Clatworthy
add properties_filename() test
134
            self.bzr_home = \
135
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0'
136
        else:
137
            self.bzr_home = '/home/bogus/.bazaar'
138
3398.1.15 by Ian Clatworthy
search branch.rules and bazaar.rules for preferences
139
    def test_rules_filename(self):
140
        self.assertEqual(rules.rules_filename(),
3398.1.23 by Ian Clatworthy
update doc to reflect file naming per poolie's review
141
                         self.bzr_home + '/rules')