|
1 |
# Redmine - project management software
|
|
2 |
# Copyright (C) 2006-2012 Jean-Philippe Lang
|
|
3 |
#
|
|
4 |
# This program is free software; you can redistribute it and/or
|
|
5 |
# modify it under the terms of the GNU General Public License
|
|
6 |
# as published by the Free Software Foundation; either version 2
|
|
7 |
# of the License, or (at your option) any later version.
|
|
8 |
#
|
|
9 |
# This program is distributed in the hope that it will be useful,
|
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
# GNU General Public License for more details.
|
|
13 |
#
|
|
14 |
# You should have received a copy of the GNU General Public License
|
|
15 |
# along with this program; if not, write to the Free Software
|
|
16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
17 |
|
|
18 |
require File.expand_path('../../../../../../test_helper', __FILE__)
|
|
19 |
begin
|
|
20 |
require 'mocha'
|
|
21 |
|
|
22 |
class MercurialAdapterTest < ActiveSupport::TestCase
|
|
23 |
HELPERS_DIR = Redmine::Scm::Adapters::MercurialAdapter::HELPERS_DIR
|
|
24 |
TEMPLATE_NAME = Redmine::Scm::Adapters::MercurialAdapter::TEMPLATE_NAME
|
|
25 |
TEMPLATE_EXTENSION = Redmine::Scm::Adapters::MercurialAdapter::TEMPLATE_EXTENSION
|
|
26 |
|
|
27 |
REPOSITORY_PATH = Rails.root.join('tmp/test/mercurial_repository').to_s
|
|
28 |
CHAR_1_HEX = "\xc3\x9c"
|
|
29 |
|
|
30 |
if File.directory?(REPOSITORY_PATH)
|
|
31 |
def setup
|
|
32 |
adapter_class = Redmine::Scm::Adapters::MercurialAdapter
|
|
33 |
assert adapter_class
|
|
34 |
assert adapter_class.client_command
|
|
35 |
assert_equal true, adapter_class.client_available
|
|
36 |
assert_equal true, adapter_class.client_version_above?([0, 9, 5])
|
|
37 |
|
|
38 |
@adapter = Redmine::Scm::Adapters::MercurialAdapter.new(
|
|
39 |
REPOSITORY_PATH,
|
|
40 |
nil,
|
|
41 |
nil,
|
|
42 |
nil,
|
|
43 |
'ISO-8859-1')
|
|
44 |
@diff_c_support = true
|
|
45 |
@char_1 = CHAR_1_HEX.dup
|
|
46 |
@tag_char_1 = "tag-#{CHAR_1_HEX}-00"
|
|
47 |
@branch_char_0 = "branch-#{CHAR_1_HEX}-00"
|
|
48 |
@branch_char_1 = "branch-#{CHAR_1_HEX}-01"
|
|
49 |
if @tag_char_1.respond_to?(:force_encoding)
|
|
50 |
@char_1.force_encoding('UTF-8')
|
|
51 |
@tag_char_1.force_encoding('UTF-8')
|
|
52 |
@branch_char_0.force_encoding('UTF-8')
|
|
53 |
@branch_char_1.force_encoding('UTF-8')
|
|
54 |
end
|
|
55 |
end
|
|
56 |
|
|
57 |
def test_hgversion
|
|
58 |
to_test = { "Mercurial Distributed SCM (version 0.9.5)\n" => [0,9,5],
|
|
59 |
"Mercurial Distributed SCM (1.0)\n" => [1,0],
|
|
60 |
"Mercurial Distributed SCM (1e4ddc9ac9f7+20080325)\n" => nil,
|
|
61 |
"Mercurial Distributed SCM (1.0.1+20080525)\n" => [1,0,1],
|
|
62 |
"Mercurial Distributed SCM (1916e629a29d)\n" => nil,
|
|
63 |
"Mercurial SCM Distribuito (versione 0.9.5)\n" => [0,9,5],
|
|
64 |
"(1.6)\n(1.7)\n(1.8)" => [1,6],
|
|
65 |
"(1.7.1)\r\n(1.8.1)\r\n(1.9.1)" => [1,7,1]}
|
|
66 |
|
|
67 |
to_test.each do |s, v|
|
|
68 |
test_hgversion_for(s, v)
|
|
69 |
end
|
|
70 |
end
|
|
71 |
|
|
72 |
def test_template_path
|
|
73 |
to_test = {
|
|
74 |
[1,2] => "1.0",
|
|
75 |
[] => "1.0",
|
|
76 |
[1,2,1] => "1.0",
|
|
77 |
[1,7] => "1.0",
|
|
78 |
[1,7,1] => "1.0",
|
|
79 |
[2,0] => "1.0",
|
|
80 |
}
|
|
81 |
to_test.each do |v, template|
|
|
82 |
test_template_path_for(v, template)
|
|
83 |
end
|
|
84 |
end
|
|
85 |
|
|
86 |
def test_info
|
|
87 |
[REPOSITORY_PATH, REPOSITORY_PATH + "/",
|
|
88 |
REPOSITORY_PATH + "//"].each do |repo|
|
|
89 |
adp = Redmine::Scm::Adapters::MercurialAdapter.new(repo)
|
|
90 |
repo_path = adp.info.root_url.gsub(/\\/, "/")
|
|
91 |
assert_equal REPOSITORY_PATH, repo_path
|
|
92 |
assert_equal '31', adp.info.lastrev.revision
|
|
93 |
assert_equal '31eeee7395c8',adp.info.lastrev.scmid
|
|
94 |
end
|
|
95 |
end
|
|
96 |
|
|
97 |
def test_revisions
|
|
98 |
revisions = @adapter.revisions(nil, 2, 4)
|
|
99 |
assert_equal 3, revisions.size
|
|
100 |
assert_equal '2', revisions[0].revision
|
|
101 |
assert_equal '400bb8672109', revisions[0].scmid
|
|
102 |
assert_equal '4', revisions[2].revision
|
|
103 |
assert_equal 'def6d2f1254a', revisions[2].scmid
|
|
104 |
|
|
105 |
revisions = @adapter.revisions(nil, 2, 4, {:limit => 2})
|
|
106 |
assert_equal 2, revisions.size
|
|
107 |
assert_equal '2', revisions[0].revision
|
|
108 |
assert_equal '400bb8672109', revisions[0].scmid
|
|
109 |
end
|
|
110 |
|
|
111 |
def test_parents
|
|
112 |
revs1 = @adapter.revisions(nil, 0, 0)
|
|
113 |
assert_equal 1, revs1.size
|
|
114 |
assert_equal [], revs1[0].parents
|
|
115 |
revs2 = @adapter.revisions(nil, 1, 1)
|
|
116 |
assert_equal 1, revs2.size
|
|
117 |
assert_equal 1, revs2[0].parents.size
|
|
118 |
assert_equal "0885933ad4f6", revs2[0].parents[0]
|
|
119 |
revs3 = @adapter.revisions(nil, 30, 30)
|
|
120 |
assert_equal 1, revs3.size
|
|
121 |
assert_equal 2, revs3[0].parents.size
|
|
122 |
assert_equal "a94b0528f24f", revs3[0].parents[0]
|
|
123 |
assert_equal "3a330eb32958", revs3[0].parents[1]
|
|
124 |
end
|
|
125 |
|
|
126 |
def test_diff
|
|
127 |
if @adapter.class.client_version_above?([1, 2])
|
|
128 |
assert_nil @adapter.diff(nil, '100000')
|
|
129 |
end
|
|
130 |
assert_nil @adapter.diff(nil, '100000', '200000')
|
|
131 |
[2, '400bb8672109', '400', 400].each do |r1|
|
|
132 |
diff1 = @adapter.diff(nil, r1)
|
|
133 |
if @diff_c_support
|
|
134 |
assert_equal 28, diff1.size
|
|
135 |
buf = diff1[24].gsub(/\r\n|\r|\n/, "")
|
|
136 |
assert_equal "+ return true unless klass.respond_to?('watched_by')", buf
|
|
137 |
else
|
|
138 |
assert_equal 0, diff1.size
|
|
139 |
end
|
|
140 |
[4, 'def6d2f1254a'].each do |r2|
|
|
141 |
diff2 = @adapter.diff(nil, r1, r2)
|
|
142 |
assert_equal 49, diff2.size
|
|
143 |
buf = diff2[41].gsub(/\r\n|\r|\n/, "")
|
|
144 |
assert_equal "+class WelcomeController < ApplicationController", buf
|
|
145 |
diff3 = @adapter.diff('sources/watchers_controller.rb', r1, r2)
|
|
146 |
assert_equal 20, diff3.size
|
|
147 |
buf = diff3[12].gsub(/\r\n|\r|\n/, "")
|
|
148 |
assert_equal "+ @watched.remove_watcher(user)", buf
|
|
149 |
|
|
150 |
diff4 = @adapter.diff(nil, r2, r1)
|
|
151 |
assert_equal 49, diff4.size
|
|
152 |
buf = diff4[41].gsub(/\r\n|\r|\n/, "")
|
|
153 |
assert_equal "-class WelcomeController < ApplicationController", buf
|
|
154 |
diff5 = @adapter.diff('sources/watchers_controller.rb', r2, r1)
|
|
155 |
assert_equal 20, diff5.size
|
|
156 |
buf = diff5[9].gsub(/\r\n|\r|\n/, "")
|
|
157 |
assert_equal "- @watched.remove_watcher(user)", buf
|
|
158 |
end
|
|
159 |
end
|
|
160 |
end
|
|
161 |
|
|
162 |
def test_diff_made_by_revision
|
|
163 |
if @diff_c_support
|
|
164 |
[24, '24', '4cddb4e45f52'].each do |r1|
|
|
165 |
diff1 = @adapter.diff(nil, r1)
|
|
166 |
assert_equal 5, diff1.size
|
|
167 |
buf = diff1[4].gsub(/\r\n|\r|\n/, "")
|
|
168 |
assert_equal '+0885933ad4f68d77c2649cd11f8311276e7ef7ce tag-init-revision', buf
|
|
169 |
end
|
|
170 |
end
|
|
171 |
end
|
|
172 |
|
|
173 |
def test_cat
|
|
174 |
[2, '400bb8672109', '400', 400].each do |r|
|
|
175 |
buf = @adapter.cat('sources/welcome_controller.rb', r)
|
|
176 |
assert buf
|
|
177 |
lines = buf.split("\r\n")
|
|
178 |
assert_equal 25, lines.length
|
|
179 |
assert_equal 'class WelcomeController < ApplicationController', lines[17]
|
|
180 |
end
|
|
181 |
assert_nil @adapter.cat('sources/welcome_controller.rb')
|
|
182 |
end
|
|
183 |
|
|
184 |
def test_annotate
|
|
185 |
assert_equal [], @adapter.annotate("sources/welcome_controller.rb").lines
|
|
186 |
[2, '400bb8672109', '400', 400].each do |r|
|
|
187 |
ann = @adapter.annotate('sources/welcome_controller.rb', r)
|
|
188 |
assert ann
|
|
189 |
assert_equal '1', ann.revisions[17].revision
|
|
190 |
assert_equal '9d5b5b004199', ann.revisions[17].identifier
|
|
191 |
assert_equal 'jsmith', ann.revisions[0].author
|
|
192 |
assert_equal 25, ann.lines.length
|
|
193 |
assert_equal 'class WelcomeController < ApplicationController', ann.lines[17]
|
|
194 |
end
|
|
195 |
end
|
|
196 |
|
|
197 |
def test_entries
|
|
198 |
assert_nil @adapter.entries(nil, '100000')
|
|
199 |
|
|
200 |
assert_equal 1, @adapter.entries("sources", 3).size
|
|
201 |
assert_equal 1, @adapter.entries("sources", 'b3a615152df8').size
|
|
202 |
|
|
203 |
[2, '400bb8672109', '400', 400].each do |r|
|
|
204 |
entries1 = @adapter.entries(nil, r)
|
|
205 |
assert entries1
|
|
206 |
assert_equal 3, entries1.size
|
|
207 |
assert_equal 'sources', entries1[1].name
|
|
208 |
assert_equal 'sources', entries1[1].path
|
|
209 |
assert_equal 'dir', entries1[1].kind
|
|
210 |
readme = entries1[2]
|
|
211 |
assert_equal 'README', readme.name
|
|
212 |
assert_equal 'README', readme.path
|
|
213 |
assert_equal 'file', readme.kind
|
|
214 |
assert_equal 27, readme.size
|
|
215 |
assert_equal '1', readme.lastrev.revision
|
|
216 |
assert_equal '9d5b5b004199', readme.lastrev.identifier
|
|
217 |
# 2007-12-14 10:24:01 +0100
|
|
218 |
assert_equal Time.gm(2007, 12, 14, 9, 24, 1), readme.lastrev.time
|
|
219 |
|
|
220 |
entries2 = @adapter.entries('sources', r)
|
|
221 |
assert entries2
|
|
222 |
assert_equal 2, entries2.size
|
|
223 |
assert_equal 'watchers_controller.rb', entries2[0].name
|
|
224 |
assert_equal 'sources/watchers_controller.rb', entries2[0].path
|
|
225 |
assert_equal 'file', entries2[0].kind
|
|
226 |
assert_equal 'welcome_controller.rb', entries2[1].name
|
|
227 |
assert_equal 'sources/welcome_controller.rb', entries2[1].path
|
|
228 |
assert_equal 'file', entries2[1].kind
|
|
229 |
end
|
|
230 |
end
|
|
231 |
|
|
232 |
def test_entries_tag
|
|
233 |
entries1 = @adapter.entries(nil, 'tag_test.00')
|
|
234 |
assert entries1
|
|
235 |
assert_equal 3, entries1.size
|
|
236 |
assert_equal 'sources', entries1[1].name
|
|
237 |
assert_equal 'sources', entries1[1].path
|
|
238 |
assert_equal 'dir', entries1[1].kind
|
|
239 |
readme = entries1[2]
|
|
240 |
assert_equal 'README', readme.name
|
|
241 |
assert_equal 'README', readme.path
|
|
242 |
assert_equal 'file', readme.kind
|
|
243 |
assert_equal 21, readme.size
|
|
244 |
assert_equal '0', readme.lastrev.revision
|
|
245 |
assert_equal '0885933ad4f6', readme.lastrev.identifier
|
|
246 |
# 2007-12-14 10:22:52 +0100
|
|
247 |
assert_equal Time.gm(2007, 12, 14, 9, 22, 52), readme.lastrev.time
|
|
248 |
end
|
|
249 |
|
|
250 |
def test_entries_branch
|
|
251 |
entries1 = @adapter.entries(nil, 'test-branch-00')
|
|
252 |
assert entries1
|
|
253 |
assert_equal 5, entries1.size
|
|
254 |
assert_equal 'sql_escape', entries1[2].name
|
|
255 |
assert_equal 'sql_escape', entries1[2].path
|
|
256 |
assert_equal 'dir', entries1[2].kind
|
|
257 |
readme = entries1[4]
|
|
258 |
assert_equal 'README', readme.name
|
|
259 |
assert_equal 'README', readme.path
|
|
260 |
assert_equal 'file', readme.kind
|
|
261 |
assert_equal 365, readme.size
|
|
262 |
assert_equal '8', readme.lastrev.revision
|
|
263 |
assert_equal 'c51f5bb613cd', readme.lastrev.identifier
|
|
264 |
# 2001-02-01 00:00:00 -0900
|
|
265 |
assert_equal Time.gm(2001, 2, 1, 9, 0, 0), readme.lastrev.time
|
|
266 |
end
|
|
267 |
|
|
268 |
def test_locate_on_outdated_repository
|
|
269 |
assert_equal 1, @adapter.entries("images", 0).size
|
|
270 |
assert_equal 2, @adapter.entries("images").size
|
|
271 |
assert_equal 2, @adapter.entries("images", 2).size
|
|
272 |
end
|
|
273 |
|
|
274 |
def test_access_by_nodeid
|
|
275 |
path = 'sources/welcome_controller.rb'
|
|
276 |
assert_equal @adapter.cat(path, 2), @adapter.cat(path, '400bb8672109')
|
|
277 |
end
|
|
278 |
|
|
279 |
def test_access_by_fuzzy_nodeid
|
|
280 |
path = 'sources/welcome_controller.rb'
|
|
281 |
# falls back to nodeid
|
|
282 |
assert_equal @adapter.cat(path, 2), @adapter.cat(path, '400')
|
|
283 |
end
|
|
284 |
|
|
285 |
def test_tags
|
|
286 |
assert_equal [@tag_char_1, 'tag_test.00', 'tag-init-revision'], @adapter.tags
|
|
287 |
end
|
|
288 |
|
|
289 |
def test_tagmap
|
|
290 |
tm = {
|
|
291 |
@tag_char_1 => 'adf805632193',
|
|
292 |
'tag_test.00' => '6987191f453a',
|
|
293 |
'tag-init-revision' => '0885933ad4f6',
|
|
294 |
}
|
|
295 |
assert_equal tm, @adapter.tagmap
|
|
296 |
end
|
|
297 |
|
|
298 |
def test_branches
|
|
299 |
brs = []
|
|
300 |
@adapter.branches.each do |b|
|
|
301 |
brs << b
|
|
302 |
end
|
|
303 |
assert_equal 7, brs.length
|
|
304 |
assert_equal 'default', brs[0].to_s
|
|
305 |
assert_equal '31', brs[0].revision
|
|
306 |
assert_equal '31eeee7395c8', brs[0].scmid
|
|
307 |
assert_equal 'test-branch-01', brs[1].to_s
|
|
308 |
assert_equal '30', brs[1].revision
|
|
309 |
assert_equal 'ad4dc4f80284', brs[1].scmid
|
|
310 |
assert_equal @branch_char_1, brs[2].to_s
|
|
311 |
assert_equal '27', brs[2].revision
|
|
312 |
assert_equal '7bbf4c738e71', brs[2].scmid
|
|
313 |
assert_equal 'branch (1)[2]&,%.-3_4', brs[3].to_s
|
|
314 |
assert_equal '25', brs[3].revision
|
|
315 |
assert_equal 'afc61e85bde7', brs[3].scmid
|
|
316 |
assert_equal @branch_char_0, brs[4].to_s
|
|
317 |
assert_equal '23', brs[4].revision
|
|
318 |
assert_equal 'c8d3e4887474', brs[4].scmid
|
|
319 |
assert_equal 'test_branch.latin-1', brs[5].to_s
|
|
320 |
assert_equal '22', brs[5].revision
|
|
321 |
assert_equal 'c2ffe7da686a', brs[5].scmid
|
|
322 |
assert_equal 'test-branch-00', brs[6].to_s
|
|
323 |
assert_equal '13', brs[6].revision
|
|
324 |
assert_equal '3a330eb32958', brs[6].scmid
|
|
325 |
end
|
|
326 |
|
|
327 |
def test_branchmap
|
|
328 |
bm = {
|
|
329 |
'default' => '31eeee7395c8',
|
|
330 |
'test_branch.latin-1' => 'c2ffe7da686a',
|
|
331 |
'branch (1)[2]&,%.-3_4' => 'afc61e85bde7',
|
|
332 |
'test-branch-00' => '3a330eb32958',
|
|
333 |
"test-branch-01" => 'ad4dc4f80284',
|
|
334 |
@branch_char_0 => 'c8d3e4887474',
|
|
335 |
@branch_char_1 => '7bbf4c738e71',
|
|
336 |
}
|
|
337 |
assert_equal bm, @adapter.branchmap
|
|
338 |
end
|
|
339 |
|
|
340 |
def test_path_space
|
|
341 |
p = 'README (1)[2]&,%.-3_4'
|
|
342 |
[15, '933ca60293d7'].each do |r1|
|
|
343 |
assert @adapter.diff(p, r1)
|
|
344 |
assert @adapter.cat(p, r1)
|
|
345 |
assert_equal 1, @adapter.annotate(p, r1).lines.length
|
|
346 |
[25, 'afc61e85bde7'].each do |r2|
|
|
347 |
assert @adapter.diff(p, r1, r2)
|
|
348 |
end
|
|
349 |
end
|
|
350 |
end
|
|
351 |
|
|
352 |
def test_tag_non_ascii
|
|
353 |
p = "latin-1-dir/test-#{@char_1}-1.txt"
|
|
354 |
assert @adapter.cat(p, @tag_char_1)
|
|
355 |
assert_equal 1, @adapter.annotate(p, @tag_char_1).lines.length
|
|
356 |
end
|
|
357 |
|
|
358 |
def test_branch_non_ascii
|
|
359 |
p = "latin-1-dir/test-#{@char_1}-subdir/test-#{@char_1}-1.txt"
|
|
360 |
assert @adapter.cat(p, @branch_char_1)
|
|
361 |
assert_equal 1, @adapter.annotate(p, @branch_char_1).lines.length
|
|
362 |
end
|
|
363 |
|
|
364 |
def test_nodes_in_branch
|
|
365 |
[
|
|
366 |
'default',
|
|
367 |
@branch_char_1,
|
|
368 |
'branch (1)[2]&,%.-3_4',
|
|
369 |
@branch_char_0,
|
|
370 |
'test_branch.latin-1',
|
|
371 |
'test-branch-00',
|
|
372 |
].each do |bra|
|
|
373 |
nib0 = @adapter.nodes_in_branch(bra)
|
|
374 |
assert nib0
|
|
375 |
nib1 = @adapter.nodes_in_branch(bra, :limit => 1)
|
|
376 |
assert_equal 1, nib1.size
|
|
377 |
case bra
|
|
378 |
when 'branch (1)[2]&,%.-3_4'
|
|
379 |
if @adapter.class.client_version_above?([1, 6])
|
|
380 |
assert_equal 3, nib0.size
|
|
381 |
assert_equal nib0[0], 'afc61e85bde7'
|
|
382 |
nib2 = @adapter.nodes_in_branch(bra, :limit => 2)
|
|
383 |
assert_equal 2, nib2.size
|
|
384 |
assert_equal nib2[1], '933ca60293d7'
|
|
385 |
end
|
|
386 |
when @branch_char_1
|
|
387 |
if @adapter.class.client_version_above?([1, 6])
|
|
388 |
assert_equal 2, nib0.size
|
|
389 |
assert_equal nib0[1], '08ff3227303e'
|
|
390 |
nib2 = @adapter.nodes_in_branch(bra, :limit => 1)
|
|
391 |
assert_equal 1, nib2.size
|
|
392 |
assert_equal nib2[0], '7bbf4c738e71'
|
|
393 |
end
|
|
394 |
end
|
|
395 |
end
|
|
396 |
end
|
|
397 |
|
|
398 |
def test_path_encoding_default_utf8
|
|
399 |
adpt1 = Redmine::Scm::Adapters::MercurialAdapter.new(
|
|
400 |
REPOSITORY_PATH
|
|
401 |
)
|
|
402 |
assert_equal "UTF-8", adpt1.path_encoding
|
|
403 |
adpt2 = Redmine::Scm::Adapters::MercurialAdapter.new(
|
|
404 |
REPOSITORY_PATH,
|
|
405 |
nil,
|
|
406 |
nil,
|
|
407 |
nil,
|
|
408 |
""
|
|
409 |
)
|
|
410 |
assert_equal "UTF-8", adpt2.path_encoding
|
|
411 |
end
|
|
412 |
|
|
413 |
private
|
|
414 |
|
|
415 |
def test_hgversion_for(hgversion, version)
|
|
416 |
@adapter.class.expects(:hgversion_from_command_line).returns(hgversion)
|
|
417 |
assert_equal version, @adapter.class.hgversion
|
|
418 |
end
|
|
419 |
|
|
420 |
def test_template_path_for(version, template)
|
|
421 |
assert_equal "#{HELPERS_DIR}/#{TEMPLATE_NAME}-#{template}.#{TEMPLATE_EXTENSION}",
|
|
422 |
@adapter.class.template_path_for(version)
|
|
423 |
assert File.exist?(@adapter.class.template_path_for(version))
|
|
424 |
end
|
|
425 |
else
|
|
426 |
puts "Mercurial test repository NOT FOUND. Skipping unit tests !!!"
|
|
427 |
def test_fake; assert true end
|
|
428 |
end
|
|
429 |
end
|
|
430 |
rescue LoadError
|
|
431 |
class MercurialMochaFake < ActiveSupport::TestCase
|
|
432 |
def test_fake; assert(false, "Requires mocha to run those tests") end
|
|
433 |
end
|
|
434 |
end
|