##// END OF EJS Templates
scm: fix repository helper unit test fails in Ruby 1.9 and non UTF-8 locale....
Toshi MARUYAMA -
r5038:4923ff76faea
parent child
Show More
@@ -1,260 +1,259
1 1 # redMine - project management software
2 2 # Copyright (C) 2006 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require 'iconv'
19 19
20 20 module RepositoriesHelper
21 21 def format_revision(revision)
22 22 if revision.respond_to? :format_identifier
23 23 revision.format_identifier
24 24 else
25 25 revision.to_s
26 26 end
27 27 end
28 28
29 29 def truncate_at_line_break(text, length = 255)
30 30 if text
31 31 text.gsub(%r{^(.{#{length}}[^\n]*)\n.+$}m, '\\1...')
32 32 end
33 33 end
34 34
35 35 def render_properties(properties)
36 36 unless properties.nil? || properties.empty?
37 37 content = ''
38 38 properties.keys.sort.each do |property|
39 39 content << content_tag('li', "<b>#{h property}</b>: <span>#{h properties[property]}</span>")
40 40 end
41 41 content_tag('ul', content, :class => 'properties')
42 42 end
43 43 end
44 44
45 45 def render_changeset_changes
46 46 changes = @changeset.changes.find(:all, :limit => 1000, :order => 'path').collect do |change|
47 47 case change.action
48 48 when 'A'
49 49 # Detects moved/copied files
50 50 if !change.from_path.blank?
51 51 change.action = @changeset.changes.detect {|c| c.action == 'D' && c.path == change.from_path} ? 'R' : 'C'
52 52 end
53 53 change
54 54 when 'D'
55 55 @changeset.changes.detect {|c| c.from_path == change.path} ? nil : change
56 56 else
57 57 change
58 58 end
59 59 end.compact
60 60
61 61 tree = { }
62 62 changes.each do |change|
63 63 p = tree
64 64 dirs = change.path.to_s.split('/').select {|d| !d.blank?}
65 65 path = ''
66 66 dirs.each do |dir|
67 67 path += '/' + dir
68 68 p[:s] ||= {}
69 69 p = p[:s]
70 70 p[path] ||= {}
71 71 p = p[path]
72 72 end
73 73 p[:c] = change
74 74 end
75 75
76 76 render_changes_tree(tree[:s])
77 77 end
78 78
79 79 def render_changes_tree(tree)
80 80 return '' if tree.nil?
81 81
82 82 output = ''
83 83 output << '<ul>'
84 84 tree.keys.sort.each do |file|
85 85 style = 'change'
86 86 text = File.basename(h(file))
87 87 if s = tree[file][:s]
88 88 style << ' folder'
89 89 path_param = to_path_param(@repository.relative_path(file))
90 90 text = link_to(text, :controller => 'repositories',
91 91 :action => 'show',
92 92 :id => @project,
93 93 :path => path_param,
94 94 :rev => @changeset.identifier)
95 95 output << "<li class='#{style}'>#{text}</li>"
96 96 output << render_changes_tree(s)
97 97 elsif c = tree[file][:c]
98 98 style << " change-#{c.action}"
99 99 path_param = to_path_param(@repository.relative_path(c.path))
100 100 text = link_to(text, :controller => 'repositories',
101 101 :action => 'entry',
102 102 :id => @project,
103 103 :path => path_param,
104 104 :rev => @changeset.identifier) unless c.action == 'D'
105 105 text << " - #{c.revision}" unless c.revision.blank?
106 106 text << ' (' + link_to('diff', :controller => 'repositories',
107 107 :action => 'diff',
108 108 :id => @project,
109 109 :path => path_param,
110 110 :rev => @changeset.identifier) + ') ' if c.action == 'M'
111 111 text << ' ' + content_tag('span', c.from_path, :class => 'copied-from') unless c.from_path.blank?
112 112 output << "<li class='#{style}'>#{text}</li>"
113 113 end
114 114 end
115 115 output << '</ul>'
116 116 output
117 117 end
118 118
119 119 def to_utf8(str)
120 return str if str.blank?
120 return str if str.nil?
121 if str.respond_to?(:force_encoding)
122 str.force_encoding('ASCII-8BIT')
123 end
124 return str if str.empty?
125 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
121 126 if str.respond_to?(:force_encoding)
122 127 str.force_encoding('UTF-8')
123 else
124 # TODO:
125 # Japanese Shift_JIS(CP932) is not compatible with ASCII.
126 # UTF-7 and Japanese ISO-2022-JP are 7bits clean.
127 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
128 128 end
129
130 129 @encodings ||= Setting.repositories_encodings.split(',').collect(&:strip)
131 130 @encodings.each do |encoding|
132 131 begin
133 132 return Iconv.conv('UTF-8', encoding, str)
134 133 rescue Iconv::Failure
135 134 # do nothing here and try the next encoding
136 135 end
137 136 end
138 137 str = replace_invalid_utf8(str)
139 138 end
140 139
141 140 def replace_invalid_utf8(str)
142 141 if str.respond_to?(:force_encoding)
143 142 str.force_encoding('UTF-8')
144 143 if ! str.valid_encoding?
145 144 str = str.encode("US-ASCII", :invalid => :replace,
146 145 :undef => :replace, :replace => '?').encode("UTF-8")
147 146 end
148 147 end
149 148 str
150 149 end
151 150
152 151 def repository_field_tags(form, repository)
153 152 method = repository.class.name.demodulize.underscore + "_field_tags"
154 153 if repository.is_a?(Repository) &&
155 154 respond_to?(method) && method != 'repository_field_tags'
156 155 send(method, form, repository)
157 156 end
158 157 end
159 158
160 159 def scm_select_tag(repository)
161 160 scm_options = [["--- #{l(:actionview_instancetag_blank_option)} ---", '']]
162 161 Redmine::Scm::Base.all.each do |scm|
163 162 if Setting.enabled_scm.include?(scm) ||
164 163 (repository && repository.class.name.demodulize == scm)
165 164 scm_options << ["Repository::#{scm}".constantize.scm_name, scm]
166 165 end
167 166 end
168 167 select_tag('repository_scm',
169 168 options_for_select(scm_options, repository.class.name.demodulize),
170 169 :disabled => (repository && !repository.new_record?),
171 170 :onchange => remote_function(
172 171 :url => {
173 172 :controller => 'repositories',
174 173 :action => 'edit',
175 174 :id => @project
176 175 },
177 176 :method => :get,
178 177 :with => "Form.serialize(this.form)")
179 178 )
180 179 end
181 180
182 181 def with_leading_slash(path)
183 182 path.to_s.starts_with?('/') ? path : "/#{path}"
184 183 end
185 184
186 185 def without_leading_slash(path)
187 186 path.gsub(%r{^/+}, '')
188 187 end
189 188
190 189 def subversion_field_tags(form, repository)
191 190 content_tag('p', form.text_field(:url, :size => 60, :required => true,
192 191 :disabled => (repository && !repository.root_url.blank?)) +
193 192 '<br />(file:///, http://, https://, svn://, svn+[tunnelscheme]://)') +
194 193 content_tag('p', form.text_field(:login, :size => 30)) +
195 194 content_tag('p', form.password_field(
196 195 :password, :size => 30, :name => 'ignore',
197 196 :value => ((repository.new_record? || repository.password.blank?) ? '' : ('x'*15)),
198 197 :onfocus => "this.value=''; this.name='repository[password]';",
199 198 :onchange => "this.name='repository[password]';"))
200 199 end
201 200
202 201 def darcs_field_tags(form, repository)
203 202 content_tag('p', form.text_field(:url, :label => 'Root directory',
204 203 :size => 60, :required => true,
205 204 :disabled => (repository && !repository.new_record?))) +
206 205 content_tag('p', form.select(:log_encoding, [nil] + Setting::ENCODINGS,
207 206 :label => 'Commit messages encoding', :required => true))
208 207 end
209 208
210 209 def mercurial_field_tags(form, repository)
211 210 content_tag('p', form.text_field(:url, :label => 'Root directory',
212 211 :size => 60, :required => true,
213 212 :disabled => (repository && !repository.root_url.blank?)) +
214 213 '<br />local repository (e.g. /hgrepo, c:\hgrepo)' ) +
215 214 content_tag('p', form.select(
216 215 :path_encoding, [nil] + Setting::ENCODINGS,
217 216 :label => 'Path encoding') +
218 217 '<br />Default: UTF-8')
219 218 end
220 219
221 220 def git_field_tags(form, repository)
222 221 content_tag('p', form.text_field(:url, :label => 'Path to repository',
223 222 :size => 60, :required => true,
224 223 :disabled => (repository && !repository.root_url.blank?)) +
225 224 '<br />a bare and local repository (e.g. /gitrepo, c:\gitrepo)') +
226 225 content_tag('p', form.select(
227 226 :path_encoding, [nil] + Setting::ENCODINGS,
228 227 :label => 'Path encoding') +
229 228 '<br />Default: UTF-8')
230 229 end
231 230
232 231 def cvs_field_tags(form, repository)
233 232 content_tag('p', form.text_field(:root_url,
234 233 :label => 'CVSROOT', :size => 60, :required => true,
235 234 :disabled => !repository.new_record?)) +
236 235 content_tag('p', form.text_field(:url, :label => 'Module',
237 236 :size => 30, :required => true,
238 237 :disabled => !repository.new_record?)) +
239 238 content_tag('p', form.select(:log_encoding, [nil] + Setting::ENCODINGS,
240 239 :label => 'Commit messages encoding', :required => true))
241 240 end
242 241
243 242 def bazaar_field_tags(form, repository)
244 243 content_tag('p', form.text_field(:url, :label => 'Root directory',
245 244 :size => 60, :required => true,
246 245 :disabled => (repository && !repository.new_record?))) +
247 246 content_tag('p', form.select(:log_encoding, [nil] + Setting::ENCODINGS,
248 247 :label => 'Commit messages encoding', :required => true))
249 248 end
250 249
251 250 def filesystem_field_tags(form, repository)
252 251 content_tag('p', form.text_field(:url, :label => 'Root directory',
253 252 :size => 60, :required => true,
254 253 :disabled => (repository && !repository.root_url.blank?))) +
255 254 content_tag('p', form.select(
256 255 :path_encoding, [nil] + Setting::ENCODINGS,
257 256 :label => 'Path encoding') +
258 257 '<br />Default: UTF-8')
259 258 end
260 259 end
@@ -1,68 +1,73
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../../test_helper', __FILE__)
19 19
20 20 class RepositoryHelperTest < HelperTestCase
21 21 include RepositoriesHelper
22 22
23 23 def test_from_latin1_to_utf8
24 24 with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
25 25 s1 = "Texte encod\xc3\xa9"
26 26 s2 = "Texte encod\xe9"
27 s3 = s2
27 s3 = s2.dup
28 28 if s1.respond_to?(:force_encoding)
29 29 s1.force_encoding("UTF-8")
30 30 s2.force_encoding("ASCII-8BIT")
31 31 s3.force_encoding("UTF-8")
32 32 end
33 33 assert_equal s1, to_utf8(s2)
34 34 assert_equal s1, to_utf8(s3)
35 35 end
36 36 end
37 37
38 38 def test_from_euc_jp_to_utf8
39 39 with_settings :repositories_encodings => 'UTF-8,EUC-JP' do
40 40 s1 = "\xe3\x83\xac\xe3\x83\x83\xe3\x83\x89\xe3\x83\x9e\xe3\x82\xa4\xe3\x83\xb3"
41 41 s2 = "\xa5\xec\xa5\xc3\xa5\xc9\xa5\xde\xa5\xa4\xa5\xf3"
42 s3 = s2
42 s3 = s2.dup
43 43 if s1.respond_to?(:force_encoding)
44 44 s1.force_encoding("UTF-8")
45 45 s2.force_encoding("ASCII-8BIT")
46 46 s3.force_encoding("UTF-8")
47 47 end
48 48 assert_equal s1, to_utf8(s2)
49 49 assert_equal s1, to_utf8(s3)
50 50 end
51 51 end
52 52
53 53 def test_to_utf8_should_be_converted_all_latin1_to_utf8
54 54 with_settings :repositories_encodings => 'ISO-8859-1' do
55 55 s1 = "\xc3\x82\xc2\x80"
56 56 s2 = "\xC2\x80"
57 s3 = s2
57 s3 = s2.dup
58 58 if s1.respond_to?(:force_encoding)
59 59 s1.force_encoding("UTF-8")
60 60 s2.force_encoding("ASCII-8BIT")
61 61 s3.force_encoding("UTF-8")
62 62 end
63 63 assert_equal s1, to_utf8(s2)
64 64 assert_equal s1, to_utf8(s3)
65 65 end
66 66 end
67
68 def test_to_utf8_blank_string
69 assert_equal "", to_utf8("")
70 assert_equal nil, to_utf8(nil)
71 end
67 72 end
68 73
General Comments 0
You need to be logged in to leave comments. Login now