##// END OF EJS Templates
scm: code clean up test/unit/changeset_test.rb....
Toshi MARUYAMA -
r4864:ee0d7d9bf058
parent child
Show More
@@ -1,291 +1,290
1 # encoding: utf-8
1 # encoding: utf-8
2 #
2 #
3 # Redmine - project management software
3 # Redmine - project management software
4 # Copyright (C) 2006-2010 Jean-Philippe Lang
4 # Copyright (C) 2006-2010 Jean-Philippe Lang
5 #
5 #
6 # This program is free software; you can redistribute it and/or
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
9 # of the License, or (at your option) any later version.
10 #
10 #
11 # This program is distributed in the hope that it will be useful,
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
14 # GNU General Public License for more details.
15 #
15 #
16 # You should have received a copy of the GNU General Public License
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
19
20 require File.expand_path('../../test_helper', __FILE__)
20 require File.expand_path('../../test_helper', __FILE__)
21
21
22 class ChangesetTest < ActiveSupport::TestCase
22 class ChangesetTest < ActiveSupport::TestCase
23 fixtures :projects, :repositories, :issues, :issue_statuses, :changesets, :changes, :issue_categories, :enumerations, :custom_fields, :custom_values, :users, :members, :member_roles, :trackers
23 fixtures :projects, :repositories, :issues, :issue_statuses, :changesets, :changes, :issue_categories, :enumerations, :custom_fields, :custom_values, :users, :members, :member_roles, :trackers
24
24
25 def setup
25 def setup
26 end
26 end
27
27
28 def test_ref_keywords_any
28 def test_ref_keywords_any
29 ActionMailer::Base.deliveries.clear
29 ActionMailer::Base.deliveries.clear
30 Setting.commit_fix_status_id = IssueStatus.find(:first, :conditions => ["is_closed = ?", true]).id
30 Setting.commit_fix_status_id = IssueStatus.find(:first, :conditions => ["is_closed = ?", true]).id
31 Setting.commit_fix_done_ratio = '90'
31 Setting.commit_fix_done_ratio = '90'
32 Setting.commit_ref_keywords = '*'
32 Setting.commit_ref_keywords = '*'
33 Setting.commit_fix_keywords = 'fixes , closes'
33 Setting.commit_fix_keywords = 'fixes , closes'
34
34
35 c = Changeset.new(:repository => Project.find(1).repository,
35 c = Changeset.new(:repository => Project.find(1).repository,
36 :committed_on => Time.now,
36 :committed_on => Time.now,
37 :comments => 'New commit (#2). Fixes #1')
37 :comments => 'New commit (#2). Fixes #1')
38 c.scan_comment_for_issue_ids
38 c.scan_comment_for_issue_ids
39
39
40 assert_equal [1, 2], c.issue_ids.sort
40 assert_equal [1, 2], c.issue_ids.sort
41 fixed = Issue.find(1)
41 fixed = Issue.find(1)
42 assert fixed.closed?
42 assert fixed.closed?
43 assert_equal 90, fixed.done_ratio
43 assert_equal 90, fixed.done_ratio
44 assert_equal 1, ActionMailer::Base.deliveries.size
44 assert_equal 1, ActionMailer::Base.deliveries.size
45 end
45 end
46
46
47 def test_ref_keywords
47 def test_ref_keywords
48 Setting.commit_ref_keywords = 'refs'
48 Setting.commit_ref_keywords = 'refs'
49 Setting.commit_fix_keywords = ''
49 Setting.commit_fix_keywords = ''
50
50
51 c = Changeset.new(:repository => Project.find(1).repository,
51 c = Changeset.new(:repository => Project.find(1).repository,
52 :committed_on => Time.now,
52 :committed_on => Time.now,
53 :comments => 'Ignores #2. Refs #1')
53 :comments => 'Ignores #2. Refs #1')
54 c.scan_comment_for_issue_ids
54 c.scan_comment_for_issue_ids
55
55
56 assert_equal [1], c.issue_ids.sort
56 assert_equal [1], c.issue_ids.sort
57 end
57 end
58
58
59 def test_ref_keywords_any_only
59 def test_ref_keywords_any_only
60 Setting.commit_ref_keywords = '*'
60 Setting.commit_ref_keywords = '*'
61 Setting.commit_fix_keywords = ''
61 Setting.commit_fix_keywords = ''
62
62
63 c = Changeset.new(:repository => Project.find(1).repository,
63 c = Changeset.new(:repository => Project.find(1).repository,
64 :committed_on => Time.now,
64 :committed_on => Time.now,
65 :comments => 'Ignores #2. Refs #1')
65 :comments => 'Ignores #2. Refs #1')
66 c.scan_comment_for_issue_ids
66 c.scan_comment_for_issue_ids
67
67
68 assert_equal [1, 2], c.issue_ids.sort
68 assert_equal [1, 2], c.issue_ids.sort
69 end
69 end
70
70
71 def test_ref_keywords_any_with_timelog
71 def test_ref_keywords_any_with_timelog
72 Setting.commit_ref_keywords = '*'
72 Setting.commit_ref_keywords = '*'
73 Setting.commit_logtime_enabled = '1'
73 Setting.commit_logtime_enabled = '1'
74
74
75 {
75 {
76 '2' => 2.0,
76 '2' => 2.0,
77 '2h' => 2.0,
77 '2h' => 2.0,
78 '2hours' => 2.0,
78 '2hours' => 2.0,
79 '15m' => 0.25,
79 '15m' => 0.25,
80 '15min' => 0.25,
80 '15min' => 0.25,
81 '3h15' => 3.25,
81 '3h15' => 3.25,
82 '3h15m' => 3.25,
82 '3h15m' => 3.25,
83 '3h15min' => 3.25,
83 '3h15min' => 3.25,
84 '3:15' => 3.25,
84 '3:15' => 3.25,
85 '3.25' => 3.25,
85 '3.25' => 3.25,
86 '3.25h' => 3.25,
86 '3.25h' => 3.25,
87 '3,25' => 3.25,
87 '3,25' => 3.25,
88 '3,25h' => 3.25,
88 '3,25h' => 3.25,
89 }.each do |syntax, expected_hours|
89 }.each do |syntax, expected_hours|
90 c = Changeset.new(:repository => Project.find(1).repository,
90 c = Changeset.new(:repository => Project.find(1).repository,
91 :committed_on => 24.hours.ago,
91 :committed_on => 24.hours.ago,
92 :comments => "Worked on this issue #1 @#{syntax}",
92 :comments => "Worked on this issue #1 @#{syntax}",
93 :revision => '520',
93 :revision => '520',
94 :user => User.find(2))
94 :user => User.find(2))
95 assert_difference 'TimeEntry.count' do
95 assert_difference 'TimeEntry.count' do
96 c.scan_comment_for_issue_ids
96 c.scan_comment_for_issue_ids
97 end
97 end
98 assert_equal [1], c.issue_ids.sort
98 assert_equal [1], c.issue_ids.sort
99
99
100 time = TimeEntry.first(:order => 'id desc')
100 time = TimeEntry.first(:order => 'id desc')
101 assert_equal 1, time.issue_id
101 assert_equal 1, time.issue_id
102 assert_equal 1, time.project_id
102 assert_equal 1, time.project_id
103 assert_equal 2, time.user_id
103 assert_equal 2, time.user_id
104 assert_equal expected_hours, time.hours, "@#{syntax} should be logged as #{expected_hours} hours but was #{time.hours}"
104 assert_equal expected_hours, time.hours, "@#{syntax} should be logged as #{expected_hours} hours but was #{time.hours}"
105 assert_equal Date.yesterday, time.spent_on
105 assert_equal Date.yesterday, time.spent_on
106 assert time.activity.is_default?
106 assert time.activity.is_default?
107 assert time.comments.include?('r520'), "r520 was expected in time_entry comments: #{time.comments}"
107 assert time.comments.include?('r520'), "r520 was expected in time_entry comments: #{time.comments}"
108 end
108 end
109 end
109 end
110
110
111 def test_ref_keywords_closing_with_timelog
111 def test_ref_keywords_closing_with_timelog
112 Setting.commit_fix_status_id = IssueStatus.find(:first, :conditions => ["is_closed = ?", true]).id
112 Setting.commit_fix_status_id = IssueStatus.find(:first, :conditions => ["is_closed = ?", true]).id
113 Setting.commit_ref_keywords = '*'
113 Setting.commit_ref_keywords = '*'
114 Setting.commit_fix_keywords = 'fixes , closes'
114 Setting.commit_fix_keywords = 'fixes , closes'
115 Setting.commit_logtime_enabled = '1'
115 Setting.commit_logtime_enabled = '1'
116
116
117 c = Changeset.new(:repository => Project.find(1).repository,
117 c = Changeset.new(:repository => Project.find(1).repository,
118 :committed_on => Time.now,
118 :committed_on => Time.now,
119 :comments => 'This is a comment. Fixes #1 @4.5, #2 @1',
119 :comments => 'This is a comment. Fixes #1 @4.5, #2 @1',
120 :user => User.find(2))
120 :user => User.find(2))
121 assert_difference 'TimeEntry.count', 2 do
121 assert_difference 'TimeEntry.count', 2 do
122 c.scan_comment_for_issue_ids
122 c.scan_comment_for_issue_ids
123 end
123 end
124
124
125 assert_equal [1, 2], c.issue_ids.sort
125 assert_equal [1, 2], c.issue_ids.sort
126 assert Issue.find(1).closed?
126 assert Issue.find(1).closed?
127 assert Issue.find(2).closed?
127 assert Issue.find(2).closed?
128
128
129 times = TimeEntry.all(:order => 'id desc', :limit => 2)
129 times = TimeEntry.all(:order => 'id desc', :limit => 2)
130 assert_equal [1, 2], times.collect(&:issue_id).sort
130 assert_equal [1, 2], times.collect(&:issue_id).sort
131 end
131 end
132
132
133 def test_ref_keywords_any_line_start
133 def test_ref_keywords_any_line_start
134 Setting.commit_ref_keywords = '*'
134 Setting.commit_ref_keywords = '*'
135
135
136 c = Changeset.new(:repository => Project.find(1).repository,
136 c = Changeset.new(:repository => Project.find(1).repository,
137 :committed_on => Time.now,
137 :committed_on => Time.now,
138 :comments => '#1 is the reason of this commit')
138 :comments => '#1 is the reason of this commit')
139 c.scan_comment_for_issue_ids
139 c.scan_comment_for_issue_ids
140
140
141 assert_equal [1], c.issue_ids.sort
141 assert_equal [1], c.issue_ids.sort
142 end
142 end
143
143
144 def test_ref_keywords_allow_brackets_around_a_issue_number
144 def test_ref_keywords_allow_brackets_around_a_issue_number
145 Setting.commit_ref_keywords = '*'
145 Setting.commit_ref_keywords = '*'
146
146
147 c = Changeset.new(:repository => Project.find(1).repository,
147 c = Changeset.new(:repository => Project.find(1).repository,
148 :committed_on => Time.now,
148 :committed_on => Time.now,
149 :comments => '[#1] Worked on this issue')
149 :comments => '[#1] Worked on this issue')
150 c.scan_comment_for_issue_ids
150 c.scan_comment_for_issue_ids
151
151
152 assert_equal [1], c.issue_ids.sort
152 assert_equal [1], c.issue_ids.sort
153 end
153 end
154
154
155 def test_ref_keywords_allow_brackets_around_multiple_issue_numbers
155 def test_ref_keywords_allow_brackets_around_multiple_issue_numbers
156 Setting.commit_ref_keywords = '*'
156 Setting.commit_ref_keywords = '*'
157
157
158 c = Changeset.new(:repository => Project.find(1).repository,
158 c = Changeset.new(:repository => Project.find(1).repository,
159 :committed_on => Time.now,
159 :committed_on => Time.now,
160 :comments => '[#1 #2, #3] Worked on these')
160 :comments => '[#1 #2, #3] Worked on these')
161 c.scan_comment_for_issue_ids
161 c.scan_comment_for_issue_ids
162
162
163 assert_equal [1,2,3], c.issue_ids.sort
163 assert_equal [1,2,3], c.issue_ids.sort
164 end
164 end
165
165
166 def test_commit_referencing_a_subproject_issue
166 def test_commit_referencing_a_subproject_issue
167 c = Changeset.new(:repository => Project.find(1).repository,
167 c = Changeset.new(:repository => Project.find(1).repository,
168 :committed_on => Time.now,
168 :committed_on => Time.now,
169 :comments => 'refs #5, a subproject issue')
169 :comments => 'refs #5, a subproject issue')
170 c.scan_comment_for_issue_ids
170 c.scan_comment_for_issue_ids
171
171
172 assert_equal [5], c.issue_ids.sort
172 assert_equal [5], c.issue_ids.sort
173 assert c.issues.first.project != c.project
173 assert c.issues.first.project != c.project
174 end
174 end
175
175
176 def test_commit_referencing_a_parent_project_issue
176 def test_commit_referencing_a_parent_project_issue
177 # repository of child project
177 # repository of child project
178 r = Repository::Subversion.create!(:project => Project.find(3), :url => 'svn://localhost/test')
178 r = Repository::Subversion.create!(:project => Project.find(3), :url => 'svn://localhost/test')
179
179
180 c = Changeset.new(:repository => r,
180 c = Changeset.new(:repository => r,
181 :committed_on => Time.now,
181 :committed_on => Time.now,
182 :comments => 'refs #2, an issue of a parent project')
182 :comments => 'refs #2, an issue of a parent project')
183 c.scan_comment_for_issue_ids
183 c.scan_comment_for_issue_ids
184
184
185 assert_equal [2], c.issue_ids.sort
185 assert_equal [2], c.issue_ids.sort
186 assert c.issues.first.project != c.project
186 assert c.issues.first.project != c.project
187 end
187 end
188
188
189 def test_text_tag_revision
189 def test_text_tag_revision
190 c = Changeset.new(:revision => '520')
190 c = Changeset.new(:revision => '520')
191 assert_equal 'r520', c.text_tag
191 assert_equal 'r520', c.text_tag
192 end
192 end
193
193
194 def test_text_tag_hash
194 def test_text_tag_hash
195 c = Changeset.new(:scmid => '7234cb2750b63f47bff735edc50a1c0a433c2518', :revision => '7234cb2750b63f47bff735edc50a1c0a433c2518')
195 c = Changeset.new(:scmid => '7234cb2750b63f47bff735edc50a1c0a433c2518', :revision => '7234cb2750b63f47bff735edc50a1c0a433c2518')
196 assert_equal 'commit:7234cb2750b63f47bff735edc50a1c0a433c2518', c.text_tag
196 assert_equal 'commit:7234cb2750b63f47bff735edc50a1c0a433c2518', c.text_tag
197 end
197 end
198
198
199 def test_text_tag_hash_all_number
199 def test_text_tag_hash_all_number
200 c = Changeset.new(:scmid => '0123456789', :revision => '0123456789')
200 c = Changeset.new(:scmid => '0123456789', :revision => '0123456789')
201 assert_equal 'commit:0123456789', c.text_tag
201 assert_equal 'commit:0123456789', c.text_tag
202 end
202 end
203
203
204 def test_previous
204 def test_previous
205 changeset = Changeset.find_by_revision('3')
205 changeset = Changeset.find_by_revision('3')
206 assert_equal Changeset.find_by_revision('2'), changeset.previous
206 assert_equal Changeset.find_by_revision('2'), changeset.previous
207 end
207 end
208
208
209 def test_previous_nil
209 def test_previous_nil
210 changeset = Changeset.find_by_revision('1')
210 changeset = Changeset.find_by_revision('1')
211 assert_nil changeset.previous
211 assert_nil changeset.previous
212 end
212 end
213
213
214 def test_next
214 def test_next
215 changeset = Changeset.find_by_revision('2')
215 changeset = Changeset.find_by_revision('2')
216 assert_equal Changeset.find_by_revision('3'), changeset.next
216 assert_equal Changeset.find_by_revision('3'), changeset.next
217 end
217 end
218
218
219 def test_next_nil
219 def test_next_nil
220 changeset = Changeset.find_by_revision('10')
220 changeset = Changeset.find_by_revision('10')
221 assert_nil changeset.next
221 assert_nil changeset.next
222 end
222 end
223
223
224 def test_comments_should_be_converted_to_utf8
224 def test_comments_should_be_converted_to_utf8
225 proj = Project.find(3)
225 proj = Project.find(3)
226 str = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
226 str = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
227 r = Repository::Bazaar.create!(
227 r = Repository::Bazaar.create!(
228 :project => proj, :url => '/tmp/test/bazaar',
228 :project => proj, :url => '/tmp/test/bazaar',
229 :log_encoding => 'ISO-8859-1' )
229 :log_encoding => 'ISO-8859-1' )
230 assert r
230 assert r
231 c = Changeset.new(:repository => r,
231 c = Changeset.new(:repository => r,
232 :committed_on => Time.now,
232 :committed_on => Time.now,
233 :revision => '123',
233 :revision => '123',
234 :scmid => '12345',
234 :scmid => '12345',
235 :comments => str)
235 :comments => str)
236 assert( c.save )
236 assert( c.save )
237 assert_equal "Texte encodΓ© en ISO-8859-1.", c.comments
237 assert_equal "Texte encodΓ© en ISO-8859-1.", c.comments
238 end
238 end
239
239
240 def test_invalid_utf8_sequences_in_comments_should_be_stripped
240 def test_invalid_utf8_sequences_in_comments_should_be_stripped
241 proj = Project.find(3)
241 proj = Project.find(3)
242 str = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
242 str = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
243 r = Repository::Bazaar.create!(
243 r = Repository::Bazaar.create!(
244 :project => proj, :url => '/tmp/test/bazaar',
244 :project => proj, :url => '/tmp/test/bazaar',
245 :log_encoding => 'UTF-8' )
245 :log_encoding => 'UTF-8' )
246 assert r
246 assert r
247 c = Changeset.new(:repository => r,
247 c = Changeset.new(:repository => r,
248 :committed_on => Time.now,
248 :committed_on => Time.now,
249 :revision => '123',
249 :revision => '123',
250 :scmid => '12345',
250 :scmid => '12345',
251 :comments => str)
251 :comments => str)
252 assert( c.save )
252 assert( c.save )
253 if str.respond_to?(:force_encoding)
253 if str.respond_to?(:force_encoding)
254 assert_equal "Texte encod? en ISO-8859-1.", c.comments
254 assert_equal "Texte encod? en ISO-8859-1.", c.comments
255 else
255 else
256 assert_equal "Texte encod en ISO-8859-1.", c.comments
256 assert_equal "Texte encod en ISO-8859-1.", c.comments
257 end
257 end
258 end
258 end
259
259
260 def test_comments_should_be_converted_all_latin1_to_utf8
260 def test_comments_should_be_converted_all_latin1_to_utf8
261 s1 = "\xC2\x80"
261 s1 = "\xC2\x80"
262 s2 = "\xc3\x82\xc2\x80"
262 s2 = "\xc3\x82\xc2\x80"
263 if s1.respond_to?(:force_encoding)
263 if s1.respond_to?(:force_encoding)
264 s3 = s1
264 s3 = s1
265 s4 = s2
265 s4 = s2
266 s1.force_encoding('ASCII-8BIT')
266 s1.force_encoding('ASCII-8BIT')
267 s2.force_encoding('ASCII-8BIT')
267 s2.force_encoding('ASCII-8BIT')
268 s3.force_encoding('ISO-8859-1')
268 s3.force_encoding('ISO-8859-1')
269 s4.force_encoding('UTF-8')
269 s4.force_encoding('UTF-8')
270 assert_equal s3.encode('UTF-8'), s4
270 assert_equal s3.encode('UTF-8'), s4
271 end
271 end
272 proj = Project.find(3)
272 proj = Project.find(3)
273 r = Repository::Bazaar.create!(
273 r = Repository::Bazaar.create!(
274 :project => proj, :url => '/tmp/test/bazaar',
274 :project => proj, :url => '/tmp/test/bazaar',
275 :log_encoding => 'ISO-8859-1' )
275 :log_encoding => 'ISO-8859-1' )
276 assert r
276 assert r
277 assert r
278 c = Changeset.new(:repository => r,
277 c = Changeset.new(:repository => r,
279 :committed_on => Time.now,
278 :committed_on => Time.now,
280 :revision => '123',
279 :revision => '123',
281 :scmid => '12345',
280 :scmid => '12345',
282 :comments => s1)
281 :comments => s1)
283 assert( c.save )
282 assert( c.save )
284 assert_equal s2, c.comments
283 assert_equal s2, c.comments
285 end
284 end
286
285
287 def test_identifier
286 def test_identifier
288 c = Changeset.find_by_revision('1')
287 c = Changeset.find_by_revision('1')
289 assert_equal c.revision, c.identifier
288 assert_equal c.revision, c.identifier
290 end
289 end
291 end
290 end
General Comments 0
You need to be logged in to leave comments. Login now