##// END OF EJS Templates
add unit test to have one default repository...
Toshi MARUYAMA -
r12217:691e218a75db
parent child
Show More
@@ -1,368 +1,393
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class RepositoryTest < ActiveSupport::TestCase
21 21 fixtures :projects,
22 22 :trackers,
23 23 :projects_trackers,
24 24 :enabled_modules,
25 25 :repositories,
26 26 :issues,
27 27 :issue_statuses,
28 28 :issue_categories,
29 29 :changesets,
30 30 :changes,
31 31 :users,
32 32 :members,
33 33 :member_roles,
34 34 :roles,
35 35 :enumerations
36 36
37 37 include Redmine::I18n
38 38
39 39 def setup
40 40 @repository = Project.find(1).repository
41 41 end
42 42
43 43 def test_blank_log_encoding_error_message
44 44 set_language_if_valid 'en'
45 45 repo = Repository::Bazaar.new(
46 46 :project => Project.find(3),
47 47 :url => "/test",
48 48 :log_encoding => ''
49 49 )
50 50 assert !repo.save
51 51 assert_include "Commit messages encoding can't be blank",
52 52 repo.errors.full_messages
53 53 end
54 54
55 55 def test_blank_log_encoding_error_message_fr
56 56 set_language_if_valid 'fr'
57 57 str = "Encodage des messages de commit doit \xc3\xaatre renseign\xc3\xa9(e)"
58 58 str.force_encoding('UTF-8') if str.respond_to?(:force_encoding)
59 59 repo = Repository::Bazaar.new(
60 60 :project => Project.find(3),
61 61 :url => "/test"
62 62 )
63 63 assert !repo.save
64 64 assert_include str, repo.errors.full_messages
65 65 end
66 66
67 67 def test_create
68 68 repository = Repository::Subversion.new(:project => Project.find(3))
69 69 assert !repository.save
70 70
71 71 repository.url = "svn://localhost"
72 72 assert repository.save
73 73 repository.reload
74 74
75 75 project = Project.find(3)
76 76 assert_equal repository, project.repository
77 77 end
78 78
79 79 def test_first_repository_should_be_set_as_default
80 80 repository1 = Repository::Subversion.new(
81 81 :project => Project.find(3),
82 82 :identifier => 'svn1',
83 83 :url => 'file:///svn1'
84 84 )
85 85 assert repository1.save
86 86 assert repository1.is_default?
87 87
88 88 repository2 = Repository::Subversion.new(
89 89 :project => Project.find(3),
90 90 :identifier => 'svn2',
91 91 :url => 'file:///svn2'
92 92 )
93 93 assert repository2.save
94 94 assert !repository2.is_default?
95 95
96 96 assert_equal repository1, Project.find(3).repository
97 97 assert_equal [repository1, repository2], Project.find(3).repositories.sort
98 98 end
99 99
100 def test_default_repository_should_be_one
101 assert_equal 0, Project.find(3).repositories.count
102 repository1 = Repository::Subversion.new(
103 :project => Project.find(3),
104 :identifier => 'svn1',
105 :url => 'file:///svn1'
106 )
107 assert repository1.save
108 assert repository1.is_default?
109
110 repository2 = Repository::Subversion.new(
111 :project => Project.find(3),
112 :identifier => 'svn2',
113 :url => 'file:///svn2',
114 :is_default => true
115 )
116 assert repository2.save
117 assert repository2.is_default?
118 repository1.reload
119 assert !repository1.is_default?
120
121 assert_equal repository2, Project.find(3).repository
122 assert_equal [repository2, repository1], Project.find(3).repositories.sort
123 end
124
100 125 def test_identifier_should_accept_letters_digits_dashes_and_underscores
101 126 r = Repository::Subversion.new(
102 127 :project_id => 3,
103 128 :identifier => 'svn-123_45',
104 129 :url => 'file:///svn'
105 130 )
106 131 assert r.save
107 132 end
108 133
109 134 def test_identifier_should_not_be_frozen_for_a_new_repository
110 135 assert_equal false, Repository.new.identifier_frozen?
111 136 end
112 137
113 138 def test_identifier_should_not_be_frozen_for_a_saved_repository_with_blank_identifier
114 139 Repository.update_all(["identifier = ''"], "id = 10")
115 140
116 141 assert_equal false, Repository.find(10).identifier_frozen?
117 142 end
118 143
119 144 def test_identifier_should_be_frozen_for_a_saved_repository_with_valid_identifier
120 145 Repository.update_all(["identifier = 'abc123'"], "id = 10")
121 146
122 147 assert_equal true, Repository.find(10).identifier_frozen?
123 148 end
124 149
125 150 def test_identifier_should_not_accept_change_if_frozen
126 151 r = Repository.new(:identifier => 'foo')
127 152 r.stubs(:identifier_frozen?).returns(true)
128 153
129 154 r.identifier = 'bar'
130 155 assert_equal 'foo', r.identifier
131 156 end
132 157
133 158 def test_identifier_should_accept_change_if_not_frozen
134 159 r = Repository.new(:identifier => 'foo')
135 160 r.stubs(:identifier_frozen?).returns(false)
136 161
137 162 r.identifier = 'bar'
138 163 assert_equal 'bar', r.identifier
139 164 end
140 165
141 166 def test_destroy
142 167 repository = Repository.find(10)
143 168 changesets = repository.changesets.count
144 169 changes = repository.filechanges.count
145 170
146 171 assert_difference 'Changeset.count', -changesets do
147 172 assert_difference 'Change.count', -changes do
148 173 Repository.find(10).destroy
149 174 end
150 175 end
151 176 end
152 177
153 178 def test_destroy_should_delete_parents_associations
154 179 changeset = Changeset.find(102)
155 180 changeset.parents = Changeset.find_all_by_id([100, 101])
156 181 assert_difference 'Changeset.connection.select_all("select * from changeset_parents").count', -2 do
157 182 Repository.find(10).destroy
158 183 end
159 184 end
160 185
161 186 def test_destroy_should_delete_issues_associations
162 187 changeset = Changeset.find(102)
163 188 changeset.issues = Issue.find_all_by_id([1, 2])
164 189 assert_difference 'Changeset.connection.select_all("select * from changesets_issues").count', -2 do
165 190 Repository.find(10).destroy
166 191 end
167 192 end
168 193
169 194 def test_should_not_create_with_disabled_scm
170 195 # disable Subversion
171 196 with_settings :enabled_scm => ['Darcs', 'Git'] do
172 197 repository = Repository::Subversion.new(
173 198 :project => Project.find(3), :url => "svn://localhost")
174 199 assert !repository.save
175 200 assert_include I18n.translate('activerecord.errors.messages.invalid'),
176 201 repository.errors[:type]
177 202 end
178 203 end
179 204
180 205 def test_scan_changesets_for_issue_ids
181 206 Setting.default_language = 'en'
182 207
183 208 Setting.commit_ref_keywords = 'refs , references, IssueID'
184 209 Setting.commit_update_keywords = [
185 210 {'keywords' => 'fixes , closes', 'status_id' => IssueStatus.where(:is_closed => true).first.id, 'done_ratio' => '90'}
186 211 ]
187 212 Setting.default_language = 'en'
188 213 ActionMailer::Base.deliveries.clear
189 214
190 215 # make sure issue 1 is not already closed
191 216 fixed_issue = Issue.find(1)
192 217 assert !fixed_issue.status.is_closed?
193 218 old_status = fixed_issue.status
194 219
195 220 with_settings :notified_events => %w(issue_added issue_updated) do
196 221 Repository.scan_changesets_for_issue_ids
197 222 end
198 223 assert_equal [101, 102], Issue.find(3).changeset_ids
199 224
200 225 # fixed issues
201 226 fixed_issue.reload
202 227 assert fixed_issue.status.is_closed?
203 228 assert_equal 90, fixed_issue.done_ratio
204 229 assert_equal [101], fixed_issue.changeset_ids
205 230
206 231 # issue change
207 232 journal = fixed_issue.journals.reorder('created_on desc').first
208 233 assert_equal User.find_by_login('dlopper'), journal.user
209 234 assert_equal 'Applied in changeset r2.', journal.notes
210 235
211 236 # 2 email notifications
212 237 assert_equal 2, ActionMailer::Base.deliveries.size
213 238 mail = ActionMailer::Base.deliveries.first
214 239 assert_not_nil mail
215 240 assert mail.subject.starts_with?(
216 241 "[#{fixed_issue.project.name} - #{fixed_issue.tracker.name} ##{fixed_issue.id}]")
217 242 assert_mail_body_match(
218 243 "Status changed from #{old_status} to #{fixed_issue.status}", mail)
219 244
220 245 # ignoring commits referencing an issue of another project
221 246 assert_equal [], Issue.find(4).changesets
222 247 end
223 248
224 249 def test_for_changeset_comments_strip
225 250 repository = Repository::Mercurial.create(
226 251 :project => Project.find( 4 ),
227 252 :url => '/foo/bar/baz' )
228 253 comment = <<-COMMENT
229 254 This is a loooooooooooooooooooooooooooong comment
230 255
231 256
232 257 COMMENT
233 258 changeset = Changeset.new(
234 259 :comments => comment, :commit_date => Time.now,
235 260 :revision => 0, :scmid => 'f39b7922fb3c',
236 261 :committer => 'foo <foo@example.com>',
237 262 :committed_on => Time.now, :repository => repository )
238 263 assert( changeset.save )
239 264 assert_not_equal( comment, changeset.comments )
240 265 assert_equal( 'This is a loooooooooooooooooooooooooooong comment',
241 266 changeset.comments )
242 267 end
243 268
244 269 def test_for_urls_strip_cvs
245 270 repository = Repository::Cvs.create(
246 271 :project => Project.find(4),
247 272 :url => ' :pserver:login:password@host:/path/to/the/repository',
248 273 :root_url => 'foo ',
249 274 :log_encoding => 'UTF-8')
250 275 assert repository.save
251 276 repository.reload
252 277 assert_equal ':pserver:login:password@host:/path/to/the/repository',
253 278 repository.url
254 279 assert_equal 'foo', repository.root_url
255 280 end
256 281
257 282 def test_for_urls_strip_subversion
258 283 repository = Repository::Subversion.create(
259 284 :project => Project.find(4),
260 285 :url => ' file:///dummy ')
261 286 assert repository.save
262 287 repository.reload
263 288 assert_equal 'file:///dummy', repository.url
264 289 end
265 290
266 291 def test_for_urls_strip_git
267 292 repository = Repository::Git.create(
268 293 :project => Project.find(4),
269 294 :url => ' c:\dummy ')
270 295 assert repository.save
271 296 repository.reload
272 297 assert_equal 'c:\dummy', repository.url
273 298 end
274 299
275 300 def test_manual_user_mapping
276 301 assert_no_difference "Changeset.where('user_id <> 2').count" do
277 302 c = Changeset.create!(
278 303 :repository => @repository,
279 304 :committer => 'foo',
280 305 :committed_on => Time.now,
281 306 :revision => 100,
282 307 :comments => 'Committed by foo.'
283 308 )
284 309 assert_nil c.user
285 310 @repository.committer_ids = {'foo' => '2'}
286 311 assert_equal User.find(2), c.reload.user
287 312 # committer is now mapped
288 313 c = Changeset.create!(
289 314 :repository => @repository,
290 315 :committer => 'foo',
291 316 :committed_on => Time.now,
292 317 :revision => 101,
293 318 :comments => 'Another commit by foo.'
294 319 )
295 320 assert_equal User.find(2), c.user
296 321 end
297 322 end
298 323
299 324 def test_auto_user_mapping_by_username
300 325 c = Changeset.create!(
301 326 :repository => @repository,
302 327 :committer => 'jsmith',
303 328 :committed_on => Time.now,
304 329 :revision => 100,
305 330 :comments => 'Committed by john.'
306 331 )
307 332 assert_equal User.find(2), c.user
308 333 end
309 334
310 335 def test_auto_user_mapping_by_email
311 336 c = Changeset.create!(
312 337 :repository => @repository,
313 338 :committer => 'john <jsmith@somenet.foo>',
314 339 :committed_on => Time.now,
315 340 :revision => 100,
316 341 :comments => 'Committed by john.'
317 342 )
318 343 assert_equal User.find(2), c.user
319 344 end
320 345
321 346 def test_filesystem_avaialbe
322 347 klass = Repository::Filesystem
323 348 assert klass.scm_adapter_class
324 349 assert_equal true, klass.scm_available
325 350 end
326 351
327 352 def test_merge_extra_info
328 353 repo = Repository::Subversion.new(:project => Project.find(3))
329 354 assert !repo.save
330 355 repo.url = "svn://localhost"
331 356 assert repo.save
332 357 repo.reload
333 358 project = Project.find(3)
334 359 assert_equal repo, project.repository
335 360 assert_nil repo.extra_info
336 361 h1 = {"test_1" => {"test_11" => "test_value_11"}}
337 362 repo.merge_extra_info(h1)
338 363 assert_equal h1, repo.extra_info
339 364 h2 = {"test_2" => {
340 365 "test_21" => "test_value_21",
341 366 "test_22" => "test_value_22",
342 367 }}
343 368 repo.merge_extra_info(h2)
344 369 assert_equal (h = {"test_11" => "test_value_11"}),
345 370 repo.extra_info["test_1"]
346 371 assert_equal "test_value_21",
347 372 repo.extra_info["test_2"]["test_21"]
348 373 h3 = {"test_2" => {
349 374 "test_23" => "test_value_23",
350 375 "test_24" => "test_value_24",
351 376 }}
352 377 repo.merge_extra_info(h3)
353 378 assert_equal (h = {"test_11" => "test_value_11"}),
354 379 repo.extra_info["test_1"]
355 380 assert_nil repo.extra_info["test_2"]["test_21"]
356 381 assert_equal "test_value_23",
357 382 repo.extra_info["test_2"]["test_23"]
358 383 end
359 384
360 385 def test_sort_should_not_raise_an_error_with_nil_identifiers
361 386 r1 = Repository.new
362 387 r2 = Repository.new
363 388
364 389 assert_nothing_raised do
365 390 [r1, r2].sort
366 391 end
367 392 end
368 393 end
General Comments 0
You need to be logged in to leave comments. Login now