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