/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_rules.py

  • Committer: Vincent Ladeuil
  • Date: 2012-01-18 14:09:19 UTC
  • mto: This revision was merged to the branch mainline in revision 6468.
  • Revision ID: v.ladeuil+lp@free.fr-20120118140919-rlvdrhpc0nq1lbwi
Change set/remove to require a lock for the branch config files.

This means that tests (or any plugin for that matter) do not requires an
explicit lock on the branch anymore to change a single option. This also
means the optimisation becomes "opt-in" and as such won't be as
spectacular as it may be and/or harder to get right (nothing fails
anymore).

This reduces the diff by ~300 lines.

Code/tests that were updating more than one config option is still taking
a lock to at least avoid some IOs and demonstrate the benefits through
the decreased number of hpss calls.

The duplication between BranchStack and BranchOnlyStack will be removed
once the same sharing is in place for local config files, at which point
the Stack class itself may be able to host the changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008-2011 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Tests for finding, parsing and searching rule-based preferences."""
 
18
 
 
19
import sys
 
20
 
 
21
from bzrlib import (
 
22
    errors,
 
23
    rules,
 
24
    tests,
 
25
    )
 
26
 
 
27
 
 
28
class TestIniBasedRulesSearcher(tests.TestCase):
 
29
 
 
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)
 
37
 
 
38
    def test_unknown_namespace(self):
 
39
        self.assertRaises(errors.UnknownRules, rules._IniBasedRulesSearcher,
 
40
            ["[junk]", "foo=bar"])
 
41
 
 
42
    def test_get_items_file_missing(self):
 
43
        rs = self.make_searcher(None)
 
44
        self.assertEquals((), rs.get_items('a.txt'))
 
45
        self.assertEquals((), rs.get_selected_items('a.txt', ['foo']))
 
46
        self.assertEquals(None, rs.get_single_value('a.txt', 'foo'))
 
47
 
 
48
    def test_get_items_file_empty(self):
 
49
        rs = self.make_searcher("")
 
50
        self.assertEquals((), rs.get_items('a.txt'))
 
51
        self.assertEquals((), rs.get_selected_items('a.txt', ['foo']))
 
52
        self.assertEquals(None, rs.get_single_value('a.txt', 'foo'))
 
53
 
 
54
    def test_get_items_from_extension_match(self):
 
55
        rs = self.make_searcher("[name *.txt]\nfoo=bar\na=True\n")
 
56
        self.assertEquals((), rs.get_items('a.py'))
 
57
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
 
58
            rs.get_items('a.txt'))
 
59
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
 
60
            rs.get_items('dir/a.txt'))
 
61
        self.assertEquals((('foo', 'bar'),),
 
62
            rs.get_selected_items('a.txt', ['foo']))
 
63
        self.assertEquals('bar', rs.get_single_value('a.txt', 'foo'))
 
64
 
 
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')),
 
70
            rs.get_items('a.py'))
 
71
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
 
72
            rs.get_items('a.txt'))
 
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'))
 
77
        self.assertEquals('bar', rs.get_single_value('a.txt', 'foo'))
 
78
 
 
79
    def test_get_items_pathname_match(self):
 
80
        rs = self.make_searcher("[name ./a.txt]\nfoo=baz\n")
 
81
        self.assertEquals((('foo', 'baz'),),
 
82
            rs.get_items('a.txt'))
 
83
        self.assertEquals('baz', rs.get_single_value('a.txt', 'foo'))
 
84
        self.assertEquals((), rs.get_items('dir/a.txt'))
 
85
        self.assertEquals(None, rs.get_single_value('dir/a.txt', 'foo'))
 
86
 
 
87
    def test_get_items_match_first(self):
 
88
        rs = self.make_searcher(
 
89
            "[name ./a.txt]\nfoo=baz\n"
 
90
            "[name *.txt]\nfoo=bar\na=True\n")
 
91
        self.assertEquals((('foo', 'baz'),),
 
92
            rs.get_items('a.txt'))
 
93
        self.assertEquals('baz', rs.get_single_value('a.txt', 'foo'))
 
94
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
 
95
            rs.get_items('dir/a.txt'))
 
96
        self.assertEquals('bar', rs.get_single_value('dir/a.txt', 'foo'))
 
97
 
 
98
 
 
99
class TestStackedRulesSearcher(tests.TestCase):
 
100
 
 
101
    def make_searcher(self, text1=None, text2=None):
 
102
        """Make a _StackedRulesSearcher with 0, 1 or 2 items"""
 
103
        searchers = []
 
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()))
 
110
        return rules._StackedRulesSearcher(searchers)
 
111
 
 
112
    def test_stack_searching(self):
 
113
        rs = self.make_searcher(
 
114
            "[name ./a.txt]\nfoo=baz\n",
 
115
            "[name *.txt]\nfoo=bar\na=True\n")
 
116
        self.assertEquals((('foo', 'baz'),),
 
117
            rs.get_items('a.txt'))
 
118
        self.assertEquals('baz', rs.get_single_value('a.txt', 'foo'))
 
119
        self.assertEquals(None, rs.get_single_value('a.txt', 'a'))
 
120
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
 
121
            rs.get_items('dir/a.txt'))
 
122
        self.assertEquals('bar', rs.get_single_value('dir/a.txt', 'foo'))
 
123
        self.assertEquals('True', rs.get_single_value('dir/a.txt', 'a'))
 
124
 
 
125
 
 
126
class TestRulesPath(tests.TestCase):
 
127
 
 
128
    def setUp(self):
 
129
        super(TestRulesPath, self).setUp()
 
130
        self.overrideEnv('HOME', '/home/bogus')
 
131
        if sys.platform == 'win32':
 
132
            self.overrideEnv(
 
133
                'BZR_HOME', r'C:\Documents and Settings\bogus\Application Data')
 
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
 
 
139
    def test_rules_filename(self):
 
140
        self.assertEqual(rules.rules_filename(),
 
141
                         self.bzr_home + '/rules')