annotate test/unit/changeset_test.rb @ 1174:928c0d0f624e bug_365

Close obsolete branch bug_365
author Chris Cannam
date Fri, 03 Feb 2012 14:03:51 +0000
parents cbce1fd3b1b7
children cbb26bc654de
rev   line source
Chris@0 1 # encoding: utf-8
Chris@0 2 #
Chris@0 3 # Redmine - project management software
Chris@0 4 # Copyright (C) 2006-2010 Jean-Philippe Lang
Chris@0 5 #
Chris@0 6 # This program is free software; you can redistribute it and/or
Chris@0 7 # modify it under the terms of the GNU General Public License
Chris@0 8 # as published by the Free Software Foundation; either version 2
Chris@0 9 # of the License, or (at your option) any later version.
Chris@0 10 #
Chris@0 11 # This program is distributed in the hope that it will be useful,
Chris@0 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 14 # GNU General Public License for more details.
Chris@0 15 #
Chris@0 16 # You should have received a copy of the GNU General Public License
Chris@0 17 # along with this program; if not, write to the Free Software
Chris@0 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 19
Chris@119 20 require File.expand_path('../../test_helper', __FILE__)
Chris@0 21
Chris@0 22 class ChangesetTest < ActiveSupport::TestCase
Chris@441 23 fixtures :projects, :repositories,
Chris@441 24 :issues, :issue_statuses, :issue_categories,
Chris@441 25 :changesets, :changes,
Chris@441 26 :enumerations,
Chris@441 27 :custom_fields, :custom_values,
Chris@441 28 :users, :members, :member_roles, :trackers,
Chris@441 29 :enabled_modules, :roles
Chris@0 30
Chris@0 31 def setup
Chris@0 32 end
Chris@441 33
Chris@0 34 def test_ref_keywords_any
Chris@0 35 ActionMailer::Base.deliveries.clear
Chris@441 36 Setting.commit_fix_status_id = IssueStatus.find(
Chris@441 37 :first, :conditions => ["is_closed = ?", true]).id
Chris@0 38 Setting.commit_fix_done_ratio = '90'
Chris@0 39 Setting.commit_ref_keywords = '*'
Chris@0 40 Setting.commit_fix_keywords = 'fixes , closes'
Chris@441 41
Chris@441 42 c = Changeset.new(:repository => Project.find(1).repository,
Chris@0 43 :committed_on => Time.now,
Chris@441 44 :comments => 'New commit (#2). Fixes #1')
Chris@0 45 c.scan_comment_for_issue_ids
Chris@441 46
Chris@0 47 assert_equal [1, 2], c.issue_ids.sort
Chris@0 48 fixed = Issue.find(1)
Chris@0 49 assert fixed.closed?
Chris@0 50 assert_equal 90, fixed.done_ratio
Chris@0 51 assert_equal 1, ActionMailer::Base.deliveries.size
Chris@0 52 end
Chris@441 53
Chris@119 54 def test_ref_keywords
Chris@119 55 Setting.commit_ref_keywords = 'refs'
Chris@119 56 Setting.commit_fix_keywords = ''
Chris@441 57
Chris@441 58 c = Changeset.new(:repository => Project.find(1).repository,
Chris@119 59 :committed_on => Time.now,
Chris@441 60 :comments => 'Ignores #2. Refs #1')
Chris@119 61 c.scan_comment_for_issue_ids
Chris@119 62
Chris@119 63 assert_equal [1], c.issue_ids.sort
Chris@119 64 end
Chris@441 65
Chris@119 66 def test_ref_keywords_any_only
Chris@119 67 Setting.commit_ref_keywords = '*'
Chris@119 68 Setting.commit_fix_keywords = ''
Chris@119 69
Chris@441 70 c = Changeset.new(:repository => Project.find(1).repository,
Chris@119 71 :committed_on => Time.now,
Chris@441 72 :comments => 'Ignores #2. Refs #1')
Chris@119 73 c.scan_comment_for_issue_ids
Chris@441 74
Chris@119 75 assert_equal [1, 2], c.issue_ids.sort
Chris@119 76 end
Chris@441 77
Chris@119 78 def test_ref_keywords_any_with_timelog
Chris@119 79 Setting.commit_ref_keywords = '*'
Chris@119 80 Setting.commit_logtime_enabled = '1'
Chris@119 81
Chris@245 82 {
Chris@245 83 '2' => 2.0,
Chris@245 84 '2h' => 2.0,
Chris@245 85 '2hours' => 2.0,
Chris@245 86 '15m' => 0.25,
Chris@245 87 '15min' => 0.25,
Chris@245 88 '3h15' => 3.25,
Chris@245 89 '3h15m' => 3.25,
Chris@245 90 '3h15min' => 3.25,
Chris@245 91 '3:15' => 3.25,
Chris@245 92 '3.25' => 3.25,
Chris@245 93 '3.25h' => 3.25,
Chris@245 94 '3,25' => 3.25,
Chris@245 95 '3,25h' => 3.25,
Chris@245 96 }.each do |syntax, expected_hours|
Chris@441 97 c = Changeset.new(:repository => Project.find(1).repository,
Chris@245 98 :committed_on => 24.hours.ago,
Chris@441 99 :comments => "Worked on this issue #1 @#{syntax}",
Chris@441 100 :revision => '520',
Chris@441 101 :user => User.find(2))
Chris@245 102 assert_difference 'TimeEntry.count' do
Chris@245 103 c.scan_comment_for_issue_ids
Chris@245 104 end
Chris@245 105 assert_equal [1], c.issue_ids.sort
Chris@441 106
Chris@245 107 time = TimeEntry.first(:order => 'id desc')
Chris@245 108 assert_equal 1, time.issue_id
Chris@245 109 assert_equal 1, time.project_id
Chris@245 110 assert_equal 2, time.user_id
Chris@441 111 assert_equal expected_hours, time.hours,
Chris@441 112 "@#{syntax} should be logged as #{expected_hours} hours but was #{time.hours}"
Chris@245 113 assert_equal Date.yesterday, time.spent_on
Chris@245 114 assert time.activity.is_default?
Chris@441 115 assert time.comments.include?('r520'),
Chris@441 116 "r520 was expected in time_entry comments: #{time.comments}"
Chris@119 117 end
Chris@119 118 end
Chris@441 119
Chris@119 120 def test_ref_keywords_closing_with_timelog
Chris@441 121 Setting.commit_fix_status_id = IssueStatus.find(
Chris@441 122 :first, :conditions => ["is_closed = ?", true]).id
Chris@119 123 Setting.commit_ref_keywords = '*'
Chris@119 124 Setting.commit_fix_keywords = 'fixes , closes'
Chris@119 125 Setting.commit_logtime_enabled = '1'
Chris@119 126
Chris@441 127 c = Changeset.new(:repository => Project.find(1).repository,
Chris@119 128 :committed_on => Time.now,
Chris@441 129 :comments => 'This is a comment. Fixes #1 @4.5, #2 @1',
Chris@441 130 :user => User.find(2))
Chris@119 131 assert_difference 'TimeEntry.count', 2 do
Chris@119 132 c.scan_comment_for_issue_ids
Chris@119 133 end
Chris@441 134
Chris@119 135 assert_equal [1, 2], c.issue_ids.sort
Chris@119 136 assert Issue.find(1).closed?
Chris@119 137 assert Issue.find(2).closed?
Chris@119 138
Chris@119 139 times = TimeEntry.all(:order => 'id desc', :limit => 2)
Chris@119 140 assert_equal [1, 2], times.collect(&:issue_id).sort
Chris@119 141 end
Chris@441 142
Chris@0 143 def test_ref_keywords_any_line_start
Chris@0 144 Setting.commit_ref_keywords = '*'
Chris@0 145
Chris@441 146 c = Changeset.new(:repository => Project.find(1).repository,
Chris@0 147 :committed_on => Time.now,
Chris@441 148 :comments => '#1 is the reason of this commit')
Chris@0 149 c.scan_comment_for_issue_ids
Chris@0 150
Chris@0 151 assert_equal [1], c.issue_ids.sort
Chris@0 152 end
Chris@0 153
Chris@0 154 def test_ref_keywords_allow_brackets_around_a_issue_number
Chris@0 155 Setting.commit_ref_keywords = '*'
Chris@0 156
Chris@441 157 c = Changeset.new(:repository => Project.find(1).repository,
Chris@0 158 :committed_on => Time.now,
Chris@441 159 :comments => '[#1] Worked on this issue')
Chris@0 160 c.scan_comment_for_issue_ids
Chris@0 161
Chris@0 162 assert_equal [1], c.issue_ids.sort
Chris@0 163 end
Chris@0 164
Chris@0 165 def test_ref_keywords_allow_brackets_around_multiple_issue_numbers
Chris@0 166 Setting.commit_ref_keywords = '*'
Chris@0 167
Chris@441 168 c = Changeset.new(:repository => Project.find(1).repository,
Chris@0 169 :committed_on => Time.now,
Chris@441 170 :comments => '[#1 #2, #3] Worked on these')
Chris@0 171 c.scan_comment_for_issue_ids
Chris@0 172
Chris@0 173 assert_equal [1,2,3], c.issue_ids.sort
Chris@0 174 end
Chris@441 175
Chris@0 176 def test_commit_referencing_a_subproject_issue
Chris@441 177 c = Changeset.new(:repository => Project.find(1).repository,
Chris@0 178 :committed_on => Time.now,
Chris@441 179 :comments => 'refs #5, a subproject issue')
Chris@0 180 c.scan_comment_for_issue_ids
Chris@441 181
Chris@0 182 assert_equal [5], c.issue_ids.sort
Chris@0 183 assert c.issues.first.project != c.project
Chris@0 184 end
Chris@0 185
Chris@0 186 def test_commit_referencing_a_parent_project_issue
Chris@0 187 # repository of child project
Chris@441 188 r = Repository::Subversion.create!(
Chris@441 189 :project => Project.find(3),
Chris@441 190 :url => 'svn://localhost/test')
Chris@441 191
Chris@441 192 c = Changeset.new(:repository => r,
Chris@0 193 :committed_on => Time.now,
Chris@441 194 :comments => 'refs #2, an issue of a parent project')
Chris@0 195 c.scan_comment_for_issue_ids
Chris@441 196
Chris@0 197 assert_equal [2], c.issue_ids.sort
Chris@0 198 assert c.issues.first.project != c.project
Chris@0 199 end
Chris@245 200
Chris@119 201 def test_text_tag_revision
Chris@119 202 c = Changeset.new(:revision => '520')
Chris@119 203 assert_equal 'r520', c.text_tag
Chris@119 204 end
Chris@245 205
Chris@119 206 def test_text_tag_hash
Chris@441 207 c = Changeset.new(
Chris@441 208 :scmid => '7234cb2750b63f47bff735edc50a1c0a433c2518',
Chris@441 209 :revision => '7234cb2750b63f47bff735edc50a1c0a433c2518')
Chris@119 210 assert_equal 'commit:7234cb2750b63f47bff735edc50a1c0a433c2518', c.text_tag
Chris@119 211 end
Chris@119 212
Chris@119 213 def test_text_tag_hash_all_number
Chris@119 214 c = Changeset.new(:scmid => '0123456789', :revision => '0123456789')
Chris@119 215 assert_equal 'commit:0123456789', c.text_tag
Chris@119 216 end
Chris@0 217
Chris@0 218 def test_previous
Chris@0 219 changeset = Changeset.find_by_revision('3')
Chris@0 220 assert_equal Changeset.find_by_revision('2'), changeset.previous
Chris@0 221 end
Chris@0 222
Chris@0 223 def test_previous_nil
Chris@0 224 changeset = Changeset.find_by_revision('1')
Chris@0 225 assert_nil changeset.previous
Chris@0 226 end
Chris@0 227
Chris@0 228 def test_next
Chris@0 229 changeset = Changeset.find_by_revision('2')
Chris@0 230 assert_equal Changeset.find_by_revision('3'), changeset.next
Chris@0 231 end
Chris@0 232
Chris@0 233 def test_next_nil
Chris@0 234 changeset = Changeset.find_by_revision('10')
Chris@0 235 assert_nil changeset.next
Chris@0 236 end
Chris@245 237
Chris@0 238 def test_comments_should_be_converted_to_utf8
Chris@441 239 proj = Project.find(3)
Chris@441 240 # str = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
Chris@441 241 str = "Texte encod\xe9 en ISO-8859-1."
Chris@441 242 str.force_encoding("ASCII-8BIT") if str.respond_to?(:force_encoding)
Chris@441 243 r = Repository::Bazaar.create!(
Chris@441 244 :project => proj,
Chris@441 245 :url => '/tmp/test/bazaar',
Chris@245 246 :log_encoding => 'ISO-8859-1' )
Chris@441 247 assert r
Chris@441 248 c = Changeset.new(:repository => r,
Chris@441 249 :committed_on => Time.now,
Chris@441 250 :revision => '123',
Chris@441 251 :scmid => '12345',
Chris@441 252 :comments => str)
Chris@441 253 assert( c.save )
Chris@441 254 str_utf8 = "Texte encod\xc3\xa9 en ISO-8859-1."
Chris@441 255 str_utf8.force_encoding("UTF-8") if str_utf8.respond_to?(:force_encoding)
Chris@441 256 assert_equal str_utf8, c.comments
Chris@0 257 end
Chris@245 258
Chris@441 259 def test_invalid_utf8_sequences_in_comments_should_be_replaced_latin1
Chris@441 260 proj = Project.find(3)
Chris@441 261 # str = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
Chris@441 262 str1 = "Texte encod\xe9 en ISO-8859-1."
Chris@441 263 str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test"
Chris@441 264 str1.force_encoding("UTF-8") if str1.respond_to?(:force_encoding)
Chris@441 265 str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding)
Chris@441 266 r = Repository::Bazaar.create!(
Chris@441 267 :project => proj,
Chris@441 268 :url => '/tmp/test/bazaar',
Chris@245 269 :log_encoding => 'UTF-8' )
Chris@441 270 assert r
Chris@441 271 c = Changeset.new(:repository => r,
Chris@441 272 :committed_on => Time.now,
Chris@441 273 :revision => '123',
Chris@441 274 :scmid => '12345',
Chris@441 275 :comments => str1,
Chris@441 276 :committer => str2)
Chris@441 277 assert( c.save )
Chris@441 278 assert_equal "Texte encod? en ISO-8859-1.", c.comments
Chris@441 279 assert_equal "?a?b?c?d?e test", c.committer
Chris@441 280 end
Chris@441 281
Chris@441 282 def test_invalid_utf8_sequences_in_comments_should_be_replaced_ja_jis
Chris@441 283 proj = Project.find(3)
Chris@441 284 str = "test\xb5\xfetest\xb5\xfe"
Chris@441 285 if str.respond_to?(:force_encoding)
Chris@441 286 str.force_encoding('ASCII-8BIT')
Chris@441 287 end
Chris@441 288 r = Repository::Bazaar.create!(
Chris@441 289 :project => proj,
Chris@441 290 :url => '/tmp/test/bazaar',
Chris@441 291 :log_encoding => 'ISO-2022-JP' )
Chris@441 292 assert r
Chris@441 293 c = Changeset.new(:repository => r,
Chris@441 294 :committed_on => Time.now,
Chris@441 295 :revision => '123',
Chris@441 296 :scmid => '12345',
Chris@441 297 :comments => str)
Chris@441 298 assert( c.save )
Chris@441 299 assert_equal "test??test??", c.comments
Chris@245 300 end
Chris@245 301
Chris@245 302 def test_comments_should_be_converted_all_latin1_to_utf8
Chris@441 303 s1 = "\xC2\x80"
Chris@441 304 s2 = "\xc3\x82\xc2\x80"
Chris@441 305 s4 = s2.dup
Chris@441 306 if s1.respond_to?(:force_encoding)
Chris@441 307 s3 = s1.dup
Chris@441 308 s1.force_encoding('ASCII-8BIT')
Chris@441 309 s2.force_encoding('ASCII-8BIT')
Chris@441 310 s3.force_encoding('ISO-8859-1')
Chris@441 311 s4.force_encoding('UTF-8')
Chris@441 312 assert_equal s3.encode('UTF-8'), s4
Chris@441 313 end
Chris@441 314 proj = Project.find(3)
Chris@441 315 r = Repository::Bazaar.create!(
Chris@441 316 :project => proj,
Chris@441 317 :url => '/tmp/test/bazaar',
Chris@245 318 :log_encoding => 'ISO-8859-1' )
Chris@441 319 assert r
Chris@441 320 c = Changeset.new(:repository => r,
Chris@441 321 :committed_on => Time.now,
Chris@441 322 :revision => '123',
Chris@441 323 :scmid => '12345',
Chris@441 324 :comments => s1)
Chris@441 325 assert( c.save )
Chris@441 326 assert_equal s4, c.comments
Chris@441 327 end
Chris@441 328
Chris@441 329 def test_invalid_utf8_sequences_in_paths_should_be_replaced
Chris@441 330 proj = Project.find(3)
Chris@441 331 str1 = "Texte encod\xe9 en ISO-8859-1"
Chris@441 332 str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test"
Chris@441 333 str1.force_encoding("UTF-8") if str1.respond_to?(:force_encoding)
Chris@441 334 str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding)
Chris@441 335 r = Repository::Bazaar.create!(
Chris@441 336 :project => proj,
Chris@441 337 :url => '/tmp/test/bazaar',
Chris@441 338 :log_encoding => 'UTF-8' )
Chris@441 339 assert r
Chris@441 340 cs = Changeset.new(
Chris@441 341 :repository => r,
Chris@441 342 :committed_on => Time.now,
Chris@441 343 :revision => '123',
Chris@441 344 :scmid => '12345',
Chris@441 345 :comments => "test")
Chris@441 346 assert(cs.save)
Chris@441 347 ch = Change.new(
Chris@441 348 :changeset => cs,
Chris@441 349 :action => "A",
Chris@441 350 :path => str1,
Chris@441 351 :from_path => str2,
Chris@441 352 :from_revision => "345")
Chris@441 353 assert(ch.save)
Chris@441 354 assert_equal "Texte encod? en ISO-8859-1", ch.path
Chris@441 355 assert_equal "?a?b?c?d?e test", ch.from_path
Chris@441 356 end
Chris@441 357
Chris@441 358 def test_comments_nil
Chris@441 359 proj = Project.find(3)
Chris@441 360 r = Repository::Bazaar.create!(
Chris@441 361 :project => proj,
Chris@441 362 :url => '/tmp/test/bazaar',
Chris@441 363 :log_encoding => 'ISO-8859-1' )
Chris@441 364 assert r
Chris@441 365 c = Changeset.new(:repository => r,
Chris@441 366 :committed_on => Time.now,
Chris@441 367 :revision => '123',
Chris@441 368 :scmid => '12345',
Chris@441 369 :comments => nil,
Chris@441 370 :committer => nil)
Chris@441 371 assert( c.save )
Chris@441 372 assert_equal "", c.comments
Chris@441 373 assert_equal nil, c.committer
Chris@441 374 if c.comments.respond_to?(:force_encoding)
Chris@441 375 assert_equal "UTF-8", c.comments.encoding.to_s
Chris@441 376 end
Chris@441 377 end
Chris@441 378
Chris@441 379 def test_comments_empty
Chris@441 380 proj = Project.find(3)
Chris@441 381 r = Repository::Bazaar.create!(
Chris@441 382 :project => proj,
Chris@441 383 :url => '/tmp/test/bazaar',
Chris@441 384 :log_encoding => 'ISO-8859-1' )
Chris@441 385 assert r
Chris@441 386 c = Changeset.new(:repository => r,
Chris@441 387 :committed_on => Time.now,
Chris@441 388 :revision => '123',
Chris@441 389 :scmid => '12345',
Chris@441 390 :comments => "",
Chris@441 391 :committer => "")
Chris@441 392 assert( c.save )
Chris@441 393 assert_equal "", c.comments
Chris@441 394 assert_equal "", c.committer
Chris@441 395 if c.comments.respond_to?(:force_encoding)
Chris@441 396 assert_equal "UTF-8", c.comments.encoding.to_s
Chris@441 397 assert_equal "UTF-8", c.committer.encoding.to_s
Chris@441 398 end
Chris@0 399 end
Chris@119 400
Chris@119 401 def test_identifier
Chris@119 402 c = Changeset.find_by_revision('1')
Chris@119 403 assert_equal c.revision, c.identifier
Chris@119 404 end
Chris@0 405 end