~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Copyright (C) 2008 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Tests for finding, parsing and searching rule-based preferences."""

import os
import sys

from bzrlib import (
    config,
    errors,
    rules,
    tests,
    )
from bzrlib.util.configobj import configobj


class TestIniBasedRulesSearcher(tests.TestCase):

    def make_searcher(self, text):
        """Make a _RulesSearcher from a string"""
        if text is None:
            lines = None
        else:
            lines = text.splitlines()
        return rules._IniBasedRulesSearcher(lines)

    def test_unknown_namespace(self):
        self.assertRaises(errors.UnknownRules, rules._IniBasedRulesSearcher,
            ["[junk]", "foo=bar"])

    def test_get_items_file_missing(self):
        rs = self.make_searcher(None)
        self.assertEquals((), rs.get_items('a.txt'))
        self.assertEquals((), rs.get_selected_items('a.txt', ['foo']))

    def test_get_items_file_empty(self):
        rs = self.make_searcher("")
        self.assertEquals((), rs.get_items('a.txt'))
        self.assertEquals((), rs.get_selected_items('a.txt', ['foo']))

    def test_get_items_from_extension_match(self):
        rs = self.make_searcher("[name *.txt]\nfoo=bar\na=True\n")
        self.assertEquals((), rs.get_items('a.py'))
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
            rs.get_items('a.txt'))
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
            rs.get_items('dir/a.txt'))
        self.assertEquals((('foo', 'bar'),),
            rs.get_selected_items('a.txt', ['foo']))

    def test_get_items_from_multiple_glob_match(self):
        rs = self.make_searcher(
            "[name *.txt *.py 'x x' \"y y\"]\nfoo=bar\na=True\n")
        self.assertEquals((), rs.get_items('NEWS'))
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
            rs.get_items('a.py'))
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
            rs.get_items('a.txt'))
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
            rs.get_items('x x'))
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
            rs.get_items('y y'))

    def test_get_items_pathname_match(self):
        rs = self.make_searcher("[name ./a.txt]\nfoo=baz\n")
        self.assertEquals((('foo', 'baz'),),
            rs.get_items('a.txt'))
        self.assertEquals((), rs.get_items('dir/a.txt'))

    def test_get_items_match_first(self):
        rs = self.make_searcher(
            "[name ./a.txt]\nfoo=baz\n"
            "[name *.txt]\nfoo=bar\na=True\n")
        self.assertEquals((('foo', 'baz'),),
            rs.get_items('a.txt'))
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
            rs.get_items('dir/a.txt'))


class TestStackedRulesSearcher(tests.TestCase):

    def make_searcher(self, text1=None, text2=None):
        """Make a _StackedRulesSearcher with 0, 1 or 2 items"""
        searchers = []
        if text1 is not None:
            searchers.append(rules._IniBasedRulesSearcher(
                text1.splitlines()))
        if text2 is not None:
            searchers.append(rules._IniBasedRulesSearcher(
                text2.splitlines()))
        return rules._StackedRulesSearcher(searchers)

    def test_stack_searching(self):
        rs = self.make_searcher(
            "[name ./a.txt]\nfoo=baz\n",
            "[name *.txt]\nfoo=bar\na=True\n")
        self.assertEquals((('foo', 'baz'),),
            rs.get_items('a.txt'))
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
            rs.get_items('dir/a.txt'))


class TestRulesPath(tests.TestCase):

    def setUp(self):
        super(TestRulesPath, self).setUp()
        os.environ['HOME'] = '/home/bogus'
        if sys.platform == 'win32':
            os.environ['BZR_HOME'] = \
                r'C:\Documents and Settings\bogus\Application Data'
            self.bzr_home = \
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0'
        else:
            self.bzr_home = '/home/bogus/.bazaar'

    def test_rules_filename(self):
        self.assertEqual(rules.rules_filename(),
                         self.bzr_home + '/rules')