annotate test/unit/repository_test.rb @ 1519:afce8026aaeb redmine-2.4-integration

Merge from branch "live"
author Chris Cannam
date Tue, 09 Sep 2014 09:34:53 +0100
parents e248c7af89ec
children dffacf8a6908
rev   line source
Chris@441 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 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 Setting.commit_ref_keywords = 'refs , references, IssueID'
Chris@1464 186 Setting.commit_update_keywords = [
Chris@1464 187 {'keywords' => 'fixes , closes', 'status_id' => IssueStatus.where(:is_closed => true).first.id, 'done_ratio' => '90'}
Chris@1464 188 ]
Chris@0 189 Setting.default_language = 'en'
Chris@0 190 ActionMailer::Base.deliveries.clear
Chris@441 191
Chris@0 192 # make sure issue 1 is not already closed
Chris@0 193 fixed_issue = Issue.find(1)
Chris@0 194 assert !fixed_issue.status.is_closed?
Chris@0 195 old_status = fixed_issue.status
Chris@441 196
Chris@1115 197 with_settings :notified_events => %w(issue_added issue_updated) do
Chris@1115 198 Repository.scan_changesets_for_issue_ids
Chris@1115 199 end
Chris@0 200 assert_equal [101, 102], Issue.find(3).changeset_ids
Chris@441 201
Chris@0 202 # fixed issues
Chris@0 203 fixed_issue.reload
Chris@0 204 assert fixed_issue.status.is_closed?
Chris@0 205 assert_equal 90, fixed_issue.done_ratio
Chris@0 206 assert_equal [101], fixed_issue.changeset_ids
Chris@441 207
Chris@0 208 # issue change
Chris@1464 209 journal = fixed_issue.journals.reorder('created_on desc').first
Chris@0 210 assert_equal User.find_by_login('dlopper'), journal.user
Chris@0 211 assert_equal 'Applied in changeset r2.', journal.notes
Chris@441 212
Chris@0 213 # 2 email notifications
Chris@0 214 assert_equal 2, ActionMailer::Base.deliveries.size
Chris@0 215 mail = ActionMailer::Base.deliveries.first
Chris@1115 216 assert_not_nil mail
Chris@441 217 assert mail.subject.starts_with?(
Chris@441 218 "[#{fixed_issue.project.name} - #{fixed_issue.tracker.name} ##{fixed_issue.id}]")
Chris@1115 219 assert_mail_body_match(
Chris@1115 220 "Status changed from #{old_status} to #{fixed_issue.status}", mail)
Chris@441 221
Chris@0 222 # ignoring commits referencing an issue of another project
Chris@0 223 assert_equal [], Issue.find(4).changesets
Chris@0 224 end
Chris@441 225
Chris@0 226 def test_for_changeset_comments_strip
Chris@441 227 repository = Repository::Mercurial.create(
Chris@441 228 :project => Project.find( 4 ),
Chris@441 229 :url => '/foo/bar/baz' )
Chris@0 230 comment = <<-COMMENT
Chris@0 231 This is a loooooooooooooooooooooooooooong comment
Chris@0 232
Chris@0 233
Chris@0 234 COMMENT
Chris@0 235 changeset = Changeset.new(
Chris@441 236 :comments => comment, :commit_date => Time.now,
Chris@441 237 :revision => 0, :scmid => 'f39b7922fb3c',
Chris@441 238 :committer => 'foo <foo@example.com>',
Chris@441 239 :committed_on => Time.now, :repository => repository )
Chris@0 240 assert( changeset.save )
Chris@0 241 assert_not_equal( comment, changeset.comments )
Chris@441 242 assert_equal( 'This is a loooooooooooooooooooooooooooong comment',
Chris@441 243 changeset.comments )
Chris@0 244 end
Chris@245 245
Chris@909 246 def test_for_urls_strip_cvs
Chris@245 247 repository = Repository::Cvs.create(
Chris@245 248 :project => Project.find(4),
Chris@245 249 :url => ' :pserver:login:password@host:/path/to/the/repository',
Chris@245 250 :root_url => 'foo ',
Chris@245 251 :log_encoding => 'UTF-8')
Chris@0 252 assert repository.save
Chris@0 253 repository.reload
Chris@441 254 assert_equal ':pserver:login:password@host:/path/to/the/repository',
Chris@441 255 repository.url
Chris@0 256 assert_equal 'foo', repository.root_url
Chris@0 257 end
Chris@245 258
Chris@909 259 def test_for_urls_strip_subversion
Chris@909 260 repository = Repository::Subversion.create(
Chris@909 261 :project => Project.find(4),
Chris@909 262 :url => ' file:///dummy ')
Chris@909 263 assert repository.save
Chris@909 264 repository.reload
Chris@909 265 assert_equal 'file:///dummy', repository.url
Chris@909 266 end
Chris@909 267
Chris@909 268 def test_for_urls_strip_git
Chris@909 269 repository = Repository::Git.create(
Chris@909 270 :project => Project.find(4),
Chris@909 271 :url => ' c:\dummy ')
Chris@909 272 assert repository.save
Chris@909 273 repository.reload
Chris@909 274 assert_equal 'c:\dummy', repository.url
Chris@909 275 end
Chris@909 276
Chris@0 277 def test_manual_user_mapping
Chris@1464 278 assert_no_difference "Changeset.where('user_id <> 2').count" do
Chris@441 279 c = Changeset.create!(
Chris@441 280 :repository => @repository,
Chris@441 281 :committer => 'foo',
Chris@441 282 :committed_on => Time.now,
Chris@441 283 :revision => 100,
Chris@441 284 :comments => 'Committed by foo.'
Chris@441 285 )
Chris@0 286 assert_nil c.user
Chris@0 287 @repository.committer_ids = {'foo' => '2'}
Chris@0 288 assert_equal User.find(2), c.reload.user
Chris@0 289 # committer is now mapped
Chris@441 290 c = Changeset.create!(
Chris@441 291 :repository => @repository,
Chris@441 292 :committer => 'foo',
Chris@441 293 :committed_on => Time.now,
Chris@441 294 :revision => 101,
Chris@441 295 :comments => 'Another commit by foo.'
Chris@441 296 )
Chris@0 297 assert_equal User.find(2), c.user
Chris@0 298 end
Chris@0 299 end
Chris@441 300
Chris@0 301 def test_auto_user_mapping_by_username
Chris@441 302 c = Changeset.create!(
Chris@441 303 :repository => @repository,
Chris@441 304 :committer => 'jsmith',
Chris@441 305 :committed_on => Time.now,
Chris@441 306 :revision => 100,
Chris@441 307 :comments => 'Committed by john.'
Chris@441 308 )
Chris@0 309 assert_equal User.find(2), c.user
Chris@0 310 end
Chris@441 311
Chris@0 312 def test_auto_user_mapping_by_email
Chris@441 313 c = Changeset.create!(
Chris@441 314 :repository => @repository,
Chris@441 315 :committer => 'john <jsmith@somenet.foo>',
Chris@441 316 :committed_on => Time.now,
Chris@441 317 :revision => 100,
Chris@441 318 :comments => 'Committed by john.'
Chris@441 319 )
Chris@0 320 assert_equal User.find(2), c.user
Chris@0 321 end
Chris@441 322
Chris@441 323 def test_filesystem_avaialbe
Chris@441 324 klass = Repository::Filesystem
Chris@441 325 assert klass.scm_adapter_class
Chris@441 326 assert_equal true, klass.scm_available
Chris@441 327 end
Chris@441 328
Chris@1494 329 def test_extra_info_should_not_return_non_hash_value
Chris@1494 330 repo = Repository.new
Chris@1494 331 repo.extra_info = "foo"
Chris@1494 332 assert_nil repo.extra_info
Chris@1494 333 end
Chris@1494 334
Chris@441 335 def test_merge_extra_info
Chris@441 336 repo = Repository::Subversion.new(:project => Project.find(3))
Chris@441 337 assert !repo.save
Chris@441 338 repo.url = "svn://localhost"
Chris@441 339 assert repo.save
Chris@441 340 repo.reload
Chris@441 341 project = Project.find(3)
Chris@441 342 assert_equal repo, project.repository
Chris@441 343 assert_nil repo.extra_info
Chris@441 344 h1 = {"test_1" => {"test_11" => "test_value_11"}}
Chris@441 345 repo.merge_extra_info(h1)
Chris@441 346 assert_equal h1, repo.extra_info
Chris@441 347 h2 = {"test_2" => {
Chris@441 348 "test_21" => "test_value_21",
Chris@441 349 "test_22" => "test_value_22",
Chris@441 350 }}
Chris@441 351 repo.merge_extra_info(h2)
Chris@441 352 assert_equal (h = {"test_11" => "test_value_11"}),
Chris@441 353 repo.extra_info["test_1"]
Chris@441 354 assert_equal "test_value_21",
Chris@441 355 repo.extra_info["test_2"]["test_21"]
Chris@441 356 h3 = {"test_2" => {
Chris@441 357 "test_23" => "test_value_23",
Chris@441 358 "test_24" => "test_value_24",
Chris@441 359 }}
Chris@441 360 repo.merge_extra_info(h3)
Chris@441 361 assert_equal (h = {"test_11" => "test_value_11"}),
Chris@441 362 repo.extra_info["test_1"]
Chris@441 363 assert_nil repo.extra_info["test_2"]["test_21"]
Chris@441 364 assert_equal "test_value_23",
Chris@441 365 repo.extra_info["test_2"]["test_23"]
Chris@441 366 end
Chris@1115 367
Chris@1115 368 def test_sort_should_not_raise_an_error_with_nil_identifiers
Chris@1115 369 r1 = Repository.new
Chris@1115 370 r2 = Repository.new
Chris@1115 371
Chris@1115 372 assert_nothing_raised do
Chris@1115 373 [r1, r2].sort
Chris@1115 374 end
Chris@1115 375 end
Chris@0 376 end