##// END OF EJS Templates
scm: mercurial: add scm command tests at unit model test (#4273)....
Toshi MARUYAMA -
r5243:b30ece064b8b
parent child
Show More
@@ -1,283 +1,289
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class RepositoryMercurialTest < ActiveSupport::TestCase
20 class RepositoryMercurialTest < ActiveSupport::TestCase
21 fixtures :projects
21 fixtures :projects
22
22
23 # No '..' in the repository path
23 # No '..' in the repository path
24 REPOSITORY_PATH = RAILS_ROOT.gsub(%r{config\/\.\.}, '') + '/tmp/test/mercurial_repository'
24 REPOSITORY_PATH = RAILS_ROOT.gsub(%r{config\/\.\.}, '') + '/tmp/test/mercurial_repository'
25
25
26 CHAR_1_HEX = "\xc3\x9c"
26 CHAR_1_HEX = "\xc3\x9c"
27
27
28 def setup
28 def setup
29 klass = Repository::Mercurial
30 assert_equal "Mercurial", klass.scm_name
31 assert klass.scm_adapter_class
32 assert_not_equal "", klass.scm_command
33 assert_equal true, klass.scm_available
34
29 @project = Project.find(3)
35 @project = Project.find(3)
30 @repository = Repository::Mercurial.create(
36 @repository = Repository::Mercurial.create(
31 :project => @project,
37 :project => @project,
32 :url => REPOSITORY_PATH,
38 :url => REPOSITORY_PATH,
33 :path_encoding => 'ISO-8859-1'
39 :path_encoding => 'ISO-8859-1'
34 )
40 )
35 assert @repository
41 assert @repository
36 @char_1 = CHAR_1_HEX.dup
42 @char_1 = CHAR_1_HEX.dup
37 @tag_char_1 = "tag-#{CHAR_1_HEX}-00"
43 @tag_char_1 = "tag-#{CHAR_1_HEX}-00"
38 @branch_char_0 = "branch-#{CHAR_1_HEX}-00"
44 @branch_char_0 = "branch-#{CHAR_1_HEX}-00"
39 @branch_char_1 = "branch-#{CHAR_1_HEX}-01"
45 @branch_char_1 = "branch-#{CHAR_1_HEX}-01"
40 if @char_1.respond_to?(:force_encoding)
46 if @char_1.respond_to?(:force_encoding)
41 @char_1.force_encoding('UTF-8')
47 @char_1.force_encoding('UTF-8')
42 @tag_char_1.force_encoding('UTF-8')
48 @tag_char_1.force_encoding('UTF-8')
43 @branch_char_0.force_encoding('UTF-8')
49 @branch_char_0.force_encoding('UTF-8')
44 @branch_char_1.force_encoding('UTF-8')
50 @branch_char_1.force_encoding('UTF-8')
45 end
51 end
46 end
52 end
47
53
48 if File.directory?(REPOSITORY_PATH)
54 if File.directory?(REPOSITORY_PATH)
49 def test_fetch_changesets_from_scratch
55 def test_fetch_changesets_from_scratch
50 @repository.fetch_changesets
56 @repository.fetch_changesets
51 @repository.reload
57 @repository.reload
52 assert_equal 29, @repository.changesets.count
58 assert_equal 29, @repository.changesets.count
53 assert_equal 37, @repository.changes.count
59 assert_equal 37, @repository.changes.count
54 assert_equal "Initial import.\nThe repository contains 3 files.",
60 assert_equal "Initial import.\nThe repository contains 3 files.",
55 @repository.changesets.find_by_revision('0').comments
61 @repository.changesets.find_by_revision('0').comments
56 end
62 end
57
63
58 def test_fetch_changesets_incremental
64 def test_fetch_changesets_incremental
59 @repository.fetch_changesets
65 @repository.fetch_changesets
60 # Remove changesets with revision > 2
66 # Remove changesets with revision > 2
61 @repository.changesets.find(:all).each {|c| c.destroy if c.revision.to_i > 2}
67 @repository.changesets.find(:all).each {|c| c.destroy if c.revision.to_i > 2}
62 @repository.reload
68 @repository.reload
63 assert_equal 3, @repository.changesets.count
69 assert_equal 3, @repository.changesets.count
64
70
65 @repository.fetch_changesets
71 @repository.fetch_changesets
66 assert_equal 29, @repository.changesets.count
72 assert_equal 29, @repository.changesets.count
67 end
73 end
68
74
69 def test_isodatesec
75 def test_isodatesec
70 # Template keyword 'isodatesec' supported in Mercurial 1.0 and higher
76 # Template keyword 'isodatesec' supported in Mercurial 1.0 and higher
71 if @repository.scm.class.client_version_above?([1, 0])
77 if @repository.scm.class.client_version_above?([1, 0])
72 @repository.fetch_changesets
78 @repository.fetch_changesets
73 @repository.reload
79 @repository.reload
74 rev0_committed_on = Time.gm(2007, 12, 14, 9, 22, 52)
80 rev0_committed_on = Time.gm(2007, 12, 14, 9, 22, 52)
75 assert_equal @repository.changesets.find_by_revision('0').committed_on, rev0_committed_on
81 assert_equal @repository.changesets.find_by_revision('0').committed_on, rev0_committed_on
76 end
82 end
77 end
83 end
78
84
79 def test_changeset_order_by_revision
85 def test_changeset_order_by_revision
80 @repository.fetch_changesets
86 @repository.fetch_changesets
81 @repository.reload
87 @repository.reload
82
88
83 c0 = @repository.latest_changeset
89 c0 = @repository.latest_changeset
84 c1 = @repository.changesets.find_by_revision('0')
90 c1 = @repository.changesets.find_by_revision('0')
85 # sorted by revision (id), not by date
91 # sorted by revision (id), not by date
86 assert c0.revision.to_i > c1.revision.to_i
92 assert c0.revision.to_i > c1.revision.to_i
87 assert c0.committed_on < c1.committed_on
93 assert c0.committed_on < c1.committed_on
88 end
94 end
89
95
90 def test_latest_changesets
96 def test_latest_changesets
91 @repository.fetch_changesets
97 @repository.fetch_changesets
92 @repository.reload
98 @repository.reload
93
99
94 # with_limit
100 # with_limit
95 changesets = @repository.latest_changesets('', nil, 2)
101 changesets = @repository.latest_changesets('', nil, 2)
96 assert_equal %w|28 27|, changesets.collect(&:revision)
102 assert_equal %w|28 27|, changesets.collect(&:revision)
97
103
98 # with_filepath
104 # with_filepath
99 changesets = @repository.latest_changesets(
105 changesets = @repository.latest_changesets(
100 '/sql_escape/percent%dir/percent%file1.txt', nil)
106 '/sql_escape/percent%dir/percent%file1.txt', nil)
101 assert_equal %w|11 10 9|, changesets.collect(&:revision)
107 assert_equal %w|11 10 9|, changesets.collect(&:revision)
102
108
103 changesets = @repository.latest_changesets(
109 changesets = @repository.latest_changesets(
104 '/sql_escape/underscore_dir/understrike_file.txt', nil)
110 '/sql_escape/underscore_dir/understrike_file.txt', nil)
105 assert_equal %w|12 9|, changesets.collect(&:revision)
111 assert_equal %w|12 9|, changesets.collect(&:revision)
106
112
107 changesets = @repository.latest_changesets('README', nil)
113 changesets = @repository.latest_changesets('README', nil)
108 assert_equal %w|28 17 8 6 1 0|, changesets.collect(&:revision)
114 assert_equal %w|28 17 8 6 1 0|, changesets.collect(&:revision)
109
115
110 changesets = @repository.latest_changesets('README','8')
116 changesets = @repository.latest_changesets('README','8')
111 assert_equal %w|8 6 1 0|, changesets.collect(&:revision)
117 assert_equal %w|8 6 1 0|, changesets.collect(&:revision)
112
118
113 changesets = @repository.latest_changesets('README','8', 2)
119 changesets = @repository.latest_changesets('README','8', 2)
114 assert_equal %w|8 6|, changesets.collect(&:revision)
120 assert_equal %w|8 6|, changesets.collect(&:revision)
115
121
116 # with_dirpath
122 # with_dirpath
117 changesets = @repository.latest_changesets('images', nil)
123 changesets = @repository.latest_changesets('images', nil)
118 assert_equal %w|1 0|, changesets.collect(&:revision)
124 assert_equal %w|1 0|, changesets.collect(&:revision)
119
125
120 path = 'sql_escape/percent%dir'
126 path = 'sql_escape/percent%dir'
121 changesets = @repository.latest_changesets(path, nil)
127 changesets = @repository.latest_changesets(path, nil)
122 assert_equal %w|13 11 10 9|, changesets.collect(&:revision)
128 assert_equal %w|13 11 10 9|, changesets.collect(&:revision)
123
129
124 changesets = @repository.latest_changesets(path, '11')
130 changesets = @repository.latest_changesets(path, '11')
125 assert_equal %w|11 10 9|, changesets.collect(&:revision)
131 assert_equal %w|11 10 9|, changesets.collect(&:revision)
126
132
127 changesets = @repository.latest_changesets(path, '11', 2)
133 changesets = @repository.latest_changesets(path, '11', 2)
128 assert_equal %w|11 10|, changesets.collect(&:revision)
134 assert_equal %w|11 10|, changesets.collect(&:revision)
129
135
130 path = 'sql_escape/underscore_dir'
136 path = 'sql_escape/underscore_dir'
131 changesets = @repository.latest_changesets(path, nil)
137 changesets = @repository.latest_changesets(path, nil)
132 assert_equal %w|13 12 9|, changesets.collect(&:revision)
138 assert_equal %w|13 12 9|, changesets.collect(&:revision)
133
139
134 changesets = @repository.latest_changesets(path, '12')
140 changesets = @repository.latest_changesets(path, '12')
135 assert_equal %w|12 9|, changesets.collect(&:revision)
141 assert_equal %w|12 9|, changesets.collect(&:revision)
136
142
137 changesets = @repository.latest_changesets(path, '12', 1)
143 changesets = @repository.latest_changesets(path, '12', 1)
138 assert_equal %w|12|, changesets.collect(&:revision)
144 assert_equal %w|12|, changesets.collect(&:revision)
139
145
140 # tag
146 # tag
141 changesets = @repository.latest_changesets('', 'tag_test.00')
147 changesets = @repository.latest_changesets('', 'tag_test.00')
142 assert_equal %w|5 4 3 2 1 0|, changesets.collect(&:revision)
148 assert_equal %w|5 4 3 2 1 0|, changesets.collect(&:revision)
143
149
144 changesets = @repository.latest_changesets('', 'tag_test.00', 2)
150 changesets = @repository.latest_changesets('', 'tag_test.00', 2)
145 assert_equal %w|5 4|, changesets.collect(&:revision)
151 assert_equal %w|5 4|, changesets.collect(&:revision)
146
152
147 changesets = @repository.latest_changesets('sources', 'tag_test.00')
153 changesets = @repository.latest_changesets('sources', 'tag_test.00')
148 assert_equal %w|4 3 2 1 0|, changesets.collect(&:revision)
154 assert_equal %w|4 3 2 1 0|, changesets.collect(&:revision)
149
155
150 changesets = @repository.latest_changesets('sources', 'tag_test.00', 2)
156 changesets = @repository.latest_changesets('sources', 'tag_test.00', 2)
151 assert_equal %w|4 3|, changesets.collect(&:revision)
157 assert_equal %w|4 3|, changesets.collect(&:revision)
152
158
153 # named branch
159 # named branch
154 changesets = @repository.latest_changesets('', @branch_char_1)
160 changesets = @repository.latest_changesets('', @branch_char_1)
155 assert_equal %w|27 26|, changesets.collect(&:revision)
161 assert_equal %w|27 26|, changesets.collect(&:revision)
156
162
157 changesets = @repository.latest_changesets("latin-1-dir/test-#{@char_1}-subdir", @branch_char_1)
163 changesets = @repository.latest_changesets("latin-1-dir/test-#{@char_1}-subdir", @branch_char_1)
158 assert_equal %w|27|, changesets.collect(&:revision)
164 assert_equal %w|27|, changesets.collect(&:revision)
159 end
165 end
160
166
161 def test_copied_files
167 def test_copied_files
162 @repository.fetch_changesets
168 @repository.fetch_changesets
163 @repository.reload
169 @repository.reload
164
170
165 cs1 = @repository.changesets.find_by_revision('13')
171 cs1 = @repository.changesets.find_by_revision('13')
166 assert_not_nil cs1
172 assert_not_nil cs1
167 c1 = cs1.changes.sort_by(&:path)
173 c1 = cs1.changes.sort_by(&:path)
168 assert_equal 2, c1.size
174 assert_equal 2, c1.size
169
175
170 assert_equal 'A', c1[0].action
176 assert_equal 'A', c1[0].action
171 assert_equal '/sql_escape/percent%dir/percentfile1.txt', c1[0].path
177 assert_equal '/sql_escape/percent%dir/percentfile1.txt', c1[0].path
172 assert_equal '/sql_escape/percent%dir/percent%file1.txt', c1[0].from_path
178 assert_equal '/sql_escape/percent%dir/percent%file1.txt', c1[0].from_path
173
179
174 assert_equal 'A', c1[1].action
180 assert_equal 'A', c1[1].action
175 assert_equal '/sql_escape/underscore_dir/understrike-file.txt', c1[1].path
181 assert_equal '/sql_escape/underscore_dir/understrike-file.txt', c1[1].path
176 assert_equal '/sql_escape/underscore_dir/understrike_file.txt', c1[1].from_path
182 assert_equal '/sql_escape/underscore_dir/understrike_file.txt', c1[1].from_path
177
183
178 cs2 = @repository.changesets.find_by_revision('15')
184 cs2 = @repository.changesets.find_by_revision('15')
179 c2 = cs2.changes
185 c2 = cs2.changes
180 assert_equal 1, c2.size
186 assert_equal 1, c2.size
181
187
182 assert_equal 'A', c2[0].action
188 assert_equal 'A', c2[0].action
183 assert_equal '/README (1)[2]&,%.-3_4', c2[0].path
189 assert_equal '/README (1)[2]&,%.-3_4', c2[0].path
184 assert_equal '/README', c2[0].from_path
190 assert_equal '/README', c2[0].from_path
185
191
186 cs3 = @repository.changesets.find_by_revision('19')
192 cs3 = @repository.changesets.find_by_revision('19')
187 c3 = cs3.changes
193 c3 = cs3.changes
188 assert_equal 1, c3.size
194 assert_equal 1, c3.size
189 assert_equal 'A', c3[0].action
195 assert_equal 'A', c3[0].action
190 assert_equal "/latin-1-dir/test-#{@char_1}-1.txt", c3[0].path
196 assert_equal "/latin-1-dir/test-#{@char_1}-1.txt", c3[0].path
191 assert_equal "/latin-1-dir/test-#{@char_1}.txt", c3[0].from_path
197 assert_equal "/latin-1-dir/test-#{@char_1}.txt", c3[0].from_path
192 end
198 end
193
199
194 def test_find_changeset_by_name
200 def test_find_changeset_by_name
195 @repository.fetch_changesets
201 @repository.fetch_changesets
196 @repository.reload
202 @repository.reload
197 %w|2 400bb8672109 400|.each do |r|
203 %w|2 400bb8672109 400|.each do |r|
198 assert_equal '2', @repository.find_changeset_by_name(r).revision
204 assert_equal '2', @repository.find_changeset_by_name(r).revision
199 end
205 end
200 end
206 end
201
207
202 def test_find_changeset_by_invalid_name
208 def test_find_changeset_by_invalid_name
203 @repository.fetch_changesets
209 @repository.fetch_changesets
204 @repository.reload
210 @repository.reload
205 assert_nil @repository.find_changeset_by_name('100000')
211 assert_nil @repository.find_changeset_by_name('100000')
206 end
212 end
207
213
208 def test_identifier
214 def test_identifier
209 @repository.fetch_changesets
215 @repository.fetch_changesets
210 @repository.reload
216 @repository.reload
211 c = @repository.changesets.find_by_revision('2')
217 c = @repository.changesets.find_by_revision('2')
212 assert_equal c.scmid, c.identifier
218 assert_equal c.scmid, c.identifier
213 end
219 end
214
220
215 def test_format_identifier
221 def test_format_identifier
216 @repository.fetch_changesets
222 @repository.fetch_changesets
217 @repository.reload
223 @repository.reload
218 c = @repository.changesets.find_by_revision('2')
224 c = @repository.changesets.find_by_revision('2')
219 assert_equal '2:400bb8672109', c.format_identifier
225 assert_equal '2:400bb8672109', c.format_identifier
220 end
226 end
221
227
222 def test_find_changeset_by_empty_name
228 def test_find_changeset_by_empty_name
223 @repository.fetch_changesets
229 @repository.fetch_changesets
224 @repository.reload
230 @repository.reload
225 ['', ' ', nil].each do |r|
231 ['', ' ', nil].each do |r|
226 assert_nil @repository.find_changeset_by_name(r)
232 assert_nil @repository.find_changeset_by_name(r)
227 end
233 end
228 end
234 end
229
235
230 def test_activities
236 def test_activities
231 c = Changeset.new(:repository => @repository,
237 c = Changeset.new(:repository => @repository,
232 :committed_on => Time.now,
238 :committed_on => Time.now,
233 :revision => '123',
239 :revision => '123',
234 :scmid => 'abc400bb8672',
240 :scmid => 'abc400bb8672',
235 :comments => 'test')
241 :comments => 'test')
236 assert c.event_title.include?('123:abc400bb8672:')
242 assert c.event_title.include?('123:abc400bb8672:')
237 assert_equal 'abc400bb8672', c.event_url[:rev]
243 assert_equal 'abc400bb8672', c.event_url[:rev]
238 end
244 end
239
245
240 def test_previous
246 def test_previous
241 @repository.fetch_changesets
247 @repository.fetch_changesets
242 @repository.reload
248 @repository.reload
243 %w|28 3ae45e2d177d 3ae45|.each do |r1|
249 %w|28 3ae45e2d177d 3ae45|.each do |r1|
244 changeset = @repository.find_changeset_by_name(r1)
250 changeset = @repository.find_changeset_by_name(r1)
245 %w|27 7bbf4c738e71 7bbf|.each do |r2|
251 %w|27 7bbf4c738e71 7bbf|.each do |r2|
246 assert_equal @repository.find_changeset_by_name(r2), changeset.previous
252 assert_equal @repository.find_changeset_by_name(r2), changeset.previous
247 end
253 end
248 end
254 end
249 end
255 end
250
256
251 def test_previous_nil
257 def test_previous_nil
252 @repository.fetch_changesets
258 @repository.fetch_changesets
253 @repository.reload
259 @repository.reload
254 %w|0 0885933ad4f6 0885|.each do |r1|
260 %w|0 0885933ad4f6 0885|.each do |r1|
255 changeset = @repository.find_changeset_by_name(r1)
261 changeset = @repository.find_changeset_by_name(r1)
256 assert_nil changeset.previous
262 assert_nil changeset.previous
257 end
263 end
258 end
264 end
259
265
260 def test_next
266 def test_next
261 @repository.fetch_changesets
267 @repository.fetch_changesets
262 @repository.reload
268 @repository.reload
263 %w|27 7bbf4c738e71 7bbf|.each do |r2|
269 %w|27 7bbf4c738e71 7bbf|.each do |r2|
264 changeset = @repository.find_changeset_by_name(r2)
270 changeset = @repository.find_changeset_by_name(r2)
265 %w|28 3ae45e2d177d 3ae45|.each do |r1|
271 %w|28 3ae45e2d177d 3ae45|.each do |r1|
266 assert_equal @repository.find_changeset_by_name(r1), changeset.next
272 assert_equal @repository.find_changeset_by_name(r1), changeset.next
267 end
273 end
268 end
274 end
269 end
275 end
270
276
271 def test_next_nil
277 def test_next_nil
272 @repository.fetch_changesets
278 @repository.fetch_changesets
273 @repository.reload
279 @repository.reload
274 %w|28 3ae45e2d177d 3ae45|.each do |r1|
280 %w|28 3ae45e2d177d 3ae45|.each do |r1|
275 changeset = @repository.find_changeset_by_name(r1)
281 changeset = @repository.find_changeset_by_name(r1)
276 assert_nil changeset.next
282 assert_nil changeset.next
277 end
283 end
278 end
284 end
279 else
285 else
280 puts "Mercurial test repository NOT FOUND. Skipping unit tests !!!"
286 puts "Mercurial test repository NOT FOUND. Skipping unit tests !!!"
281 def test_fake; assert true end
287 def test_fake; assert true end
282 end
288 end
283 end
289 end
General Comments 0
You need to be logged in to leave comments. Login now