1
from cStringIO import StringIO
5
from paste.httpexceptions import HTTPServerError
7
from bzrlib import errors
10
1
from loggerhead.apps.branch import BranchWSGIApp
11
2
from loggerhead.controllers.annotate_ui import AnnotateUI
12
3
from loggerhead.controllers.inventory_ui import InventoryUI
13
4
from loggerhead.controllers.revision_ui import RevisionUI
14
from loggerhead.tests.test_simple import BasicTests, consume_app
5
from loggerhead.tests.test_simple import BasicTests
15
6
from loggerhead import util
18
9
class TestInventoryUI(BasicTests):
20
def make_bzrbranch_for_tree_shape(self, shape):
11
def make_bzrbranch_and_inventory_ui_for_tree_shape(self, shape):
21
12
tree = self.make_branch_and_tree('.')
22
13
self.build_tree(shape)
25
self.addCleanup(tree.branch.lock_read().unlock)
28
def make_bzrbranch_and_inventory_ui_for_tree_shape(self, shape):
29
branch = self.make_bzrbranch_for_tree_shape(shape)
30
branch_app = self.make_branch_app(branch)
31
return branch, InventoryUI(branch_app, branch_app.get_history)
16
tree.branch.lock_read()
17
self.addCleanup(tree.branch.unlock)
18
branch_app = BranchWSGIApp(tree.branch)
19
return tree.branch, InventoryUI(branch_app, branch_app.get_history)
33
21
def test_get_filelist(self):
34
22
bzrbranch, inv_ui = self.make_bzrbranch_and_inventory_ui_for_tree_shape(
36
24
inv = bzrbranch.repository.get_inventory(bzrbranch.last_revision())
37
self.assertEqual(1, len(inv_ui.get_filelist(inv, '', 'filename', 'head')))
40
bzrbranch, inv_ui = self.make_bzrbranch_and_inventory_ui_for_tree_shape(
42
start, content = consume_app(inv_ui,
43
{'SCRIPT_NAME': '/files', 'PATH_INFO': ''})
44
self.assertEqual(('200 OK', [('Content-Type', 'text/html')], None),
46
self.assertContainsRe(content, 'filename')
48
def test_no_content_for_HEAD(self):
49
bzrbranch, inv_ui = self.make_bzrbranch_and_inventory_ui_for_tree_shape(
51
start, content = consume_app(inv_ui,
52
{'SCRIPT_NAME': '/files', 'PATH_INFO': '',
53
'REQUEST_METHOD': 'HEAD'})
54
self.assertEqual(('200 OK', [('Content-Type', 'text/html')], None),
56
self.assertEqual('', content)
58
def test_get_values_smoke(self):
59
branch = self.make_bzrbranch_for_tree_shape(['a-file'])
60
branch_app = self.make_branch_app(branch)
61
env = {'SCRIPT_NAME': '', 'PATH_INFO': '/files'}
62
inv_ui = branch_app.lookup_app(env)
63
inv_ui.parse_args(env)
64
values = inv_ui.get_values('', {}, {})
65
self.assertEqual('a-file', values['filelist'][0].filename)
67
def test_json_render_smoke(self):
68
branch = self.make_bzrbranch_for_tree_shape(['a-file'])
69
branch_app = self.make_branch_app(branch)
70
env = {'SCRIPT_NAME': '', 'PATH_INFO': '/+json/files'}
71
inv_ui = branch_app.lookup_app(env)
72
self.assertOkJsonResponse(inv_ui, env)
25
self.assertEqual(1, len(inv_ui.get_filelist(inv, '', 'filename')))
75
28
class TestRevisionUI(BasicTests):
77
def make_branch_app_for_revision_ui(self, shape1, shape2):
30
def make_bzrbranch_and_revision_ui_for_tree_shapes(self, shape1, shape2):
78
31
tree = self.make_branch_and_tree('.')
79
32
self.build_tree_contents(shape1)
81
tree.commit('msg 1', rev_id='rev-1')
82
35
self.build_tree_contents(shape2)
84
tree.commit('msg 2', rev_id='rev-2')
86
self.addCleanup(branch.lock_read().unlock)
87
return self.make_branch_app(branch)
38
tree.branch.lock_read()
39
self.addCleanup(tree.branch.unlock)
40
branch_app = BranchWSGIApp(tree.branch)
41
branch_app._environ = {
46
branch_app._url_base = ''
47
branch_app.friendly_name = ''
48
return tree.branch, RevisionUI(branch_app, branch_app.get_history)
89
50
def test_get_values(self):
90
branch_app = self.make_branch_app_for_revision_ui([], [])
91
env = {'SCRIPT_NAME': '', 'PATH_INFO': '/revision/2'}
92
rev_ui = branch_app.lookup_app(env)
93
rev_ui.parse_args(env)
94
self.assertIsInstance(rev_ui.get_values('', {}, []), dict)
96
def test_get_values_smoke(self):
97
branch_app = self.make_branch_app_for_revision_ui(
98
[('file', 'content\n'), ('other-file', 'other\n')],
99
[('file', 'new content\n')])
100
env = {'SCRIPT_NAME': '/',
101
'PATH_INFO': '/revision/head:'}
102
revision_ui = branch_app.lookup_app(env)
103
revision_ui.parse_args(env)
104
values = revision_ui.get_values('', {}, {})
106
self.assertEqual(values['revid'], 'rev-2')
107
self.assertEqual(values['change'].comment, 'msg 2')
108
self.assertEqual(values['file_changes'].modified[0].filename, 'file')
109
self.assertEqual(values['merged_in'], None)
111
def test_json_render_smoke(self):
112
branch_app = self.make_branch_app_for_revision_ui(
113
[('file', 'content\n'), ('other-file', 'other\n')],
114
[('file', 'new content\n')])
115
env = {'SCRIPT_NAME': '', 'PATH_INFO': '/+json/revision/head:'}
116
revision_ui = branch_app.lookup_app(env)
117
self.assertOkJsonResponse(revision_ui, env)
51
branch, rev_ui = self.make_bzrbranch_and_revision_ui_for_tree_shapes(
55
self.assertIsInstance(
56
rev_ui.get_values('', {}, []),
121
60
class TestAnnotateUI(BasicTests):
123
62
def make_annotate_ui_for_file_history(self, file_id, rev_ids_texts):
124
63
tree = self.make_branch_and_tree('.')
125
self.build_tree_contents([('filename', '')])
64
open('filename', 'w').write('')
126
65
tree.add(['filename'], [file_id])
127
66
for rev_id, text in rev_ids_texts:
128
self.build_tree_contents([('filename', text)])
67
open('filename', 'w').write(text)
129
68
tree.commit(rev_id=rev_id, message='.')
130
69
tree.branch.lock_read()
131
70
self.addCleanup(tree.branch.unlock)
132
branch_app = BranchWSGIApp(tree.branch, friendly_name='test_name')
71
branch_app = BranchWSGIApp(tree.branch)
133
72
return AnnotateUI(branch_app, branch_app.get_history)
135
74
def test_annotate_file(self):
136
75
history = [('rev1', 'old\nold\n'), ('rev2', 'new\nold\n')]
137
76
ann_ui = self.make_annotate_ui_for_file_history('file_id', history)
138
# A lot of this state is set up by __call__, but we'll do it directly
140
ann_ui.args = ['rev2']
141
annotate_info = ann_ui.get_values('filename',
142
kwargs={'file_id': 'file_id'}, headers={})
143
annotated = annotate_info['annotated']
77
annotated = list(ann_ui.annotate_file('file_id', 'rev2'))
144
78
self.assertEqual(2, len(annotated))
145
self.assertEqual('2', annotated[1].change.revno)
146
self.assertEqual('1', annotated[2].change.revno)
149
class TestFileDiffUI(BasicTests):
151
def make_branch_app_for_filediff_ui(self):
152
builder = self.make_branch_builder('branch')
153
builder.start_series()
154
builder.build_snapshot('rev-1-id', None, [
155
('add', ('', 'root-id', 'directory', '')),
156
('add', ('filename', 'f-id', 'file', 'content\n'))],
157
message="First commit.")
158
builder.build_snapshot('rev-2-id', None, [
159
('modify', ('f-id', 'new content\n'))])
160
builder.finish_series()
161
branch = builder.get_branch()
162
self.addCleanup(branch.lock_read().unlock)
163
return self.make_branch_app(branch)
165
def test_get_values_smoke(self):
166
branch_app = self.make_branch_app_for_filediff_ui()
167
env = {'SCRIPT_NAME': '/',
168
'PATH_INFO': '/+filediff/rev-2-id/rev-1-id/f-id'}
169
filediff_ui = branch_app.lookup_app(env)
170
filediff_ui.parse_args(env)
171
values = filediff_ui.get_values('', {}, {})
172
chunks = values['chunks']
173
self.assertEqual('insert', chunks[0].diff[1].type)
174
self.assertEqual('new content', chunks[0].diff[1].line)
176
def test_json_render_smoke(self):
177
branch_app = self.make_branch_app_for_filediff_ui()
178
env = {'SCRIPT_NAME': '/',
179
'PATH_INFO': '/+json/+filediff/rev-2-id/rev-1-id/f-id'}
180
filediff_ui = branch_app.lookup_app(env)
181
self.assertOkJsonResponse(filediff_ui, env)
184
class TestRevLogUI(BasicTests):
186
def make_branch_app_for_revlog_ui(self):
187
builder = self.make_branch_builder('branch')
188
builder.start_series()
189
builder.build_snapshot('rev-id', None, [
190
('add', ('', 'root-id', 'directory', '')),
191
('add', ('filename', 'f-id', 'file', 'content\n'))],
192
message="First commit.")
193
builder.finish_series()
194
branch = builder.get_branch()
195
self.addCleanup(branch.lock_read().unlock)
196
return self.make_branch_app(branch)
198
def test_get_values_smoke(self):
199
branch_app = self.make_branch_app_for_revlog_ui()
200
env = {'SCRIPT_NAME': '/',
201
'PATH_INFO': '/+revlog/rev-id'}
202
revlog_ui = branch_app.lookup_app(env)
203
revlog_ui.parse_args(env)
204
values = revlog_ui.get_values('', {}, {})
205
self.assertEqual(values['file_changes'].added[1].filename, 'filename')
206
self.assertEqual(values['entry'].comment, "First commit.")
208
def test_json_render_smoke(self):
209
branch_app = self.make_branch_app_for_revlog_ui()
210
env = {'SCRIPT_NAME': '', 'PATH_INFO': '/+json/+revlog/rev-id'}
211
revlog_ui = branch_app.lookup_app(env)
212
self.assertOkJsonResponse(revlog_ui, env)
214
class TestDownloadTarballUI(BasicTests):
217
super(TestDownloadTarballUI, self).setUp()
220
def test_download_tarball(self):
221
app = self.setUpLoggerhead()
222
res = app.get('/tarball')
223
f = open('tarball', 'w')
226
self.failIf(not tarfile.is_tarfile('tarball'))
b'\\ No newline at end of file'
79
self.assertEqual('2', annotated[0].change.revno)
80
self.assertEqual('1', annotated[1].change.revno)