annotate test/unit/repository_test.rb @ 1480:75fd8eace091 issue_556

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