1
from cStringIO import StringIO
4
from paste.httpexceptions import HTTPServerError
6
from bzrlib import errors
8
from loggerhead.apps.branch import BranchWSGIApp
9
from loggerhead.controllers.annotate_ui import AnnotateUI
10
from loggerhead.controllers.inventory_ui import InventoryUI
11
from loggerhead.controllers.revision_ui import RevisionUI
12
from loggerhead.tests.test_simple import BasicTests
13
from loggerhead import util
16
class TestInventoryUI(BasicTests):
18
def make_bzrbranch_and_inventory_ui_for_tree_shape(self, shape):
19
tree = self.make_branch_and_tree('.')
20
self.build_tree(shape)
23
tree.branch.lock_read()
24
self.addCleanup(tree.branch.unlock)
25
branch_app = BranchWSGIApp(tree.branch, '')
26
branch_app.log.setLevel(logging.CRITICAL)
27
# These are usually set in BranchWSGIApp.app(), which is set from env
28
# settings set by BranchesFromTransportRoot, so we fake it.
29
branch_app._static_url_base = '/'
30
branch_app._url_base = '/'
31
return tree.branch, InventoryUI(branch_app, branch_app.get_history)
33
def consume_app(self, app, extra_environ=None):
34
env = {'SCRIPT_NAME': '/files', 'PATH_INFO': ''}
35
if extra_environ is not None:
36
env.update(extra_environ)
39
def start_response(status, headers, exc_info=None):
40
start.append((status, headers, exc_info))
42
extra_content = list(app(env, start_response))
43
body.writelines(extra_content)
44
return start[0], body.getvalue()
46
def test_get_filelist(self):
47
bzrbranch, inv_ui = self.make_bzrbranch_and_inventory_ui_for_tree_shape(
49
inv = bzrbranch.repository.get_inventory(bzrbranch.last_revision())
50
self.assertEqual(1, len(inv_ui.get_filelist(inv, '', 'filename', 'head')))
53
bzrbranch, inv_ui = self.make_bzrbranch_and_inventory_ui_for_tree_shape(
55
start, content = self.consume_app(inv_ui)
56
self.assertEqual(('200 OK', [('Content-Type', 'text/html')], None),
58
self.assertContainsRe(content, 'filename')
60
def test_no_content_for_HEAD(self):
61
bzrbranch, inv_ui = self.make_bzrbranch_and_inventory_ui_for_tree_shape(
63
start, content = self.consume_app(inv_ui,
64
extra_environ={'REQUEST_METHOD': 'HEAD'})
65
self.assertEqual(('200 OK', [('Content-Type', 'text/html')], None),
67
self.assertEqual('', content)
70
class TestRevisionUI(BasicTests):
72
def make_bzrbranch_and_revision_ui_for_tree_shapes(self, shape1, shape2):
73
tree = self.make_branch_and_tree('.')
74
self.build_tree_contents(shape1)
77
self.build_tree_contents(shape2)
80
tree.branch.lock_read()
81
self.addCleanup(tree.branch.unlock)
82
branch_app = BranchWSGIApp(tree.branch)
83
branch_app._environ = {
88
branch_app._url_base = ''
89
branch_app.friendly_name = ''
90
return tree.branch, RevisionUI(branch_app, branch_app.get_history)
92
def test_get_values(self):
93
branch, rev_ui = self.make_bzrbranch_and_revision_ui_for_tree_shapes(
97
self.assertIsInstance(
98
rev_ui.get_values('', {}, []),
102
class TestAnnotateUI(BasicTests):
104
def make_annotate_ui_for_file_history(self, file_id, rev_ids_texts):
105
tree = self.make_branch_and_tree('.')
106
self.build_tree_contents([('filename', '')])
107
tree.add(['filename'], [file_id])
108
for rev_id, text in rev_ids_texts:
109
self.build_tree_contents([('filename', text)])
110
tree.commit(rev_id=rev_id, message='.')
111
tree.branch.lock_read()
112
self.addCleanup(tree.branch.unlock)
113
branch_app = BranchWSGIApp(tree.branch, friendly_name='test_name')
114
return AnnotateUI(branch_app, branch_app.get_history)
116
def test_annotate_file(self):
117
history = [('rev1', 'old\nold\n'), ('rev2', 'new\nold\n')]
118
ann_ui = self.make_annotate_ui_for_file_history('file_id', history)
119
# A lot of this state is set up by __call__, but we'll do it directly
121
ann_ui.args = ['rev2']
122
annotate_info = ann_ui.get_values('filename',
123
kwargs={'file_id': 'file_id'}, headers={})
124
annotated = list(annotate_info['annotated'])
125
self.assertEqual(2, len(annotated))
126
self.assertEqual('2', annotated[0].change.revno)
127
self.assertEqual('1', annotated[1].change.revno)