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
|
|
15 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
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 |
|
65 |
def test_get_items_pathname_match(self): |
|
3398.1.33
by Ian Clatworthy
use a string, not lists, for test data |
66 |
rs = self.make_searcher("[name ./a.txt]\nfoo=baz\n") |
3398.1.15
by Ian Clatworthy
search branch.rules and bazaar.rules for preferences |
67 |
self.assertEquals((('foo', 'baz'),), |
68 |
rs.get_items('a.txt')) |
|
3564.1.1
by Ian Clatworthy
RuleSearchers need to return () instead of [] |
69 |
self.assertEquals((), rs.get_items('dir/a.txt')) |
3398.1.15
by Ian Clatworthy
search branch.rules and bazaar.rules for preferences |
70 |
|
71 |
def test_get_items_match_first(self): |
|
3398.1.33
by Ian Clatworthy
use a string, not lists, for test data |
72 |
rs = self.make_searcher( |
73 |
"[name ./a.txt]\nfoo=baz\n" |
|
74 |
"[name *.txt]\nfoo=bar\na=True\n") |
|
3398.1.15
by Ian Clatworthy
search branch.rules and bazaar.rules for preferences |
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 |
||
3398.1.18
by Ian Clatworthy
add tests for _StackedRulesSearcher |
81 |
class TestStackedRulesSearcher(tests.TestCase): |
82 |
||
3398.1.33
by Ian Clatworthy
use a string, not lists, for test data |
83 |
def make_searcher(self, text1=None, text2=None): |
3398.1.18
by Ian Clatworthy
add tests for _StackedRulesSearcher |
84 |
"""Make a _StackedRulesSearcher with 0, 1 or 2 items"""
|
85 |
searchers = [] |
|
3398.1.33
by Ian Clatworthy
use a string, not lists, for test data |
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())) |
|
3398.1.18
by Ian Clatworthy
add tests for _StackedRulesSearcher |
92 |
return rules._StackedRulesSearcher(searchers) |
93 |
||
94 |
def test_stack_searching(self): |
|
95 |
rs = self.make_searcher( |
|
3398.1.33
by Ian Clatworthy
use a string, not lists, for test data |
96 |
"[name ./a.txt]\nfoo=baz\n", |
97 |
"[name *.txt]\nfoo=bar\na=True\n") |
|
3398.1.18
by Ian Clatworthy
add tests for _StackedRulesSearcher |
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 |
||
3398.1.15
by Ian Clatworthy
search branch.rules and bazaar.rules for preferences |
104 |
class TestRulesPath(tests.TestCase): |
3398.1.4
by Ian Clatworthy
add properties_filename() test |
105 |
|
106 |
def setUp(self): |
|
3398.1.15
by Ian Clatworthy
search branch.rules and bazaar.rules for preferences |
107 |
super(TestRulesPath, self).setUp() |
3398.1.4
by Ian Clatworthy
add properties_filename() test |
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 |
||
3398.1.15
by Ian Clatworthy
search branch.rules and bazaar.rules for preferences |
117 |
def test_rules_filename(self): |
118 |
self.assertEqual(rules.rules_filename(), |
|
3398.1.23
by Ian Clatworthy
update doc to reflect file naming per poolie's review |
119 |
self.bzr_home + '/rules') |