1
# Copyright (C) 2008 Canonical Ltd
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.
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.
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
17
"""Views stored within a working tree.
19
The views are actually in the WorkingTree.views namespace, but these are
20
1:1 with WorkingTree implementations so can be tested from here.
24
from bzrlib import errors
25
from bzrlib.tests import TestSkipped
26
from bzrlib.workingtree import WorkingTree
28
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
31
class TestTreeViews(TestCaseWithWorkingTree):
34
# formats that don't support views can skip the rest of these
36
fmt = self.workingtree_format
37
f = getattr(fmt, 'supports_views')
39
raise TestSkipped("format %s doesn't declare whether it "
40
"supports views, assuming not" % fmt)
42
raise TestSkipped("format %s doesn't support views" % fmt)
43
TestCaseWithWorkingTree.setUp(self)
45
def test_views_initially_empty(self):
46
wt = self.make_branch_and_tree('wt')
47
current, views = wt.views.get_view_info()
48
self.assertEqual(None, current)
49
self.assertEqual({}, views)
51
def test_set_and_get_view_info(self):
52
wt = self.make_branch_and_tree('wt')
53
view_current = 'view-name'
55
view_current: ['dir-1'],
56
'other-name': ['dir-2']}
57
wt.views.set_view_info(view_current, view_dict)
58
current, views = wt.views.get_view_info()
59
self.assertEquals(view_current, current)
60
self.assertEquals(view_dict, views)
61
# then reopen the tree and see they're still there
62
wt = WorkingTree.open('wt')
63
current, views = wt.views.get_view_info()
64
self.assertEquals(view_current, current)
65
self.assertEquals(view_dict, views)
66
# test setting a current view which does not exist
67
self.assertRaises(errors.NoSuchView,
68
wt.views.set_view_info, 'yet-another', view_dict)
69
current, views = wt.views.get_view_info()
70
self.assertEquals(view_current, current)
71
self.assertEquals(view_dict, views)
72
# test clearing the current view
73
wt.views.set_view_info(None, view_dict)
74
current, views = wt.views.get_view_info()
75
self.assertEquals(None, current)
76
self.assertEquals(view_dict, views)
78
def test_lookup_view(self):
79
wt = self.make_branch_and_tree('wt')
80
view_current = 'view-name'
82
view_current: ['dir-1'],
83
'other-name': ['dir-2']}
84
wt.views.set_view_info(view_current, view_dict)
85
# test lookup of the default view
86
result = wt.views.lookup_view()
87
self.assertEqual(result, ['dir-1'])
88
# test lookup of a named view
89
result = wt.views.lookup_view('other-name')
90
self.assertEqual(result, ['dir-2'])
92
def test_set_view(self):
93
wt = self.make_branch_and_tree('wt')
94
# test that set_view sets the current view by default
95
wt.views.set_view('view-1', ['dir-1'])
96
current, views = wt.views.get_view_info()
97
self.assertEquals('view-1', current)
98
self.assertEquals({'view-1': ['dir-1']}, views)
99
# test adding a view and not making it the current one
100
wt.views.set_view('view-2', ['dir-2'], make_current=False)
101
current, views = wt.views.get_view_info()
102
self.assertEquals('view-1', current)
103
self.assertEquals({'view-1': ['dir-1'], 'view-2': ['dir-2']}, views)
105
def test_unicode_view(self):
106
wt = self.make_branch_and_tree('wt')
107
view_name = u'\u3070'
108
view_files = ['foo', 'bar/']
109
view_dict = {view_name: view_files}
110
wt.views.set_view_info(view_name, view_dict)
111
current, views = wt.views.get_view_info()
112
self.assertEquals(view_name, current)
113
self.assertEquals(view_dict, views)
115
def test_no_such_view(self):
116
wt = self.make_branch_and_tree('wt')
118
wt.views.lookup_view('opaque')
119
except errors.NoSuchView, e:
120
self.assertEquals(e.view_name, 'opaque')
121
self.assertEquals(str(e), 'No such view: opaque.')
123
self.fail("didn't get expected exception")
125
def test_delete_view(self):
126
wt = self.make_branch_and_tree('wt')
127
view_name = u'\N{GREEK SMALL LETTER ALPHA}'
128
view_files = ['alphas/']
129
wt.views.set_view(view_name, view_files)
130
# now try to delete it
131
wt.views.delete_view(view_name)
132
# now you can't look it up
133
self.assertRaises(errors.NoSuchView,
134
wt.views.lookup_view, view_name)
135
# and it's not in the dictionary
136
self.assertEquals(wt.views.get_view_info()[1], {})
137
# and you can't remove it a second time
138
self.assertRaises(errors.NoSuchView,
139
wt.views.delete_view, view_name)
140
# or remove a view that never existed
141
self.assertRaises(errors.NoSuchView,
142
wt.views.delete_view, view_name + '2')
145
class TestUnsupportedViews(TestCaseWithWorkingTree):
146
"""Formats that don't support views should give reasonable errors."""
149
fmt = self.workingtree_format
150
supported = getattr(fmt, 'supports_views')
151
if supported is None:
152
warn("Format %s doesn't declare whether it supports views or not"
154
raise TestSkipped('No view support at all')
156
raise TestSkipped("Format %s declares that views are supported"
158
# it's covered by TestTreeViews
159
TestCaseWithWorkingTree.setUp(self)
161
def test_view_methods_raise(self):
162
wt = self.make_branch_and_tree('wt')
163
self.assertRaises(errors.ViewsNotSupported,
164
wt.views.set_view_info, 'bar', {'bar': ['bars/']})
165
self.assertRaises(errors.ViewsNotSupported,
166
wt.views.get_view_info)
167
self.assertRaises(errors.ViewsNotSupported,
168
wt.views.lookup_view, 'foo')
169
self.assertRaises(errors.ViewsNotSupported,
170
wt.views.set_view, 'foo', 'bar')
171
self.assertRaises(errors.ViewsNotSupported,
172
wt.views.delete_view, 'foo')