Project

General

Profile

Feature #1410 » buffered_io.patch

Pierre Paysant-Le Roux, 2008-12-08 12:05

View differences:

app/controllers/repositories_controller.rb Sun Dec 07 16:21:40 2008 +0100 → app/controllers/repositories_controller.rb Mon Dec 08 12:04:28 2008 +0100
94 94
  def revisions
95 95
    @changeset_count = @repository.changesets.count
96 96
    @changeset_pages = Paginator.new self, @changeset_count,
97
								      per_page_option,
98
								      params['page']								
97
    per_page_option,
98
    params['page']								
99 99
    @changesets = @repository.changesets.find(:all,
100
						:limit  =>  @changeset_pages.items_per_page,
101
						:offset =>  @changeset_pages.current.offset,
102
            :include => :user)
103

  
100
                                              :limit  =>  @changeset_pages.items_per_page,
101
                                              :offset =>  @changeset_pages.current.offset,
102
                                              :include => :user)
103
    
104 104
    respond_to do |format|
105 105
      format.html { render :layout => false if request.xhr? }
106 106
      format.atom { render_feed(@changesets, :title => "#{@project.name}: #{l(:label_revision_plural)}") }
......
114 114
    # If the entry is a dir, show the browser
115 115
    browse and return if @entry.is_dir?
116 116
    
117
    @content = @repository.cat(@path, @rev)
118
    show_error_not_found and return unless @content
119
    if 'raw' == params[:format] || @content.is_binary_data?
117
    content = @repository.cat(@path, @rev)
118
    show_error_not_found and return unless content
119
    if 'raw' == params[:format] || content.is_binary_data?
120 120
      # Force the download if it's a binary file
121
      send_data @content, :filename => @path.split('/').last
121
      headers.update(
122
                     #'Content-Type'              => options[:type].to_s.strip,
123
                     'Content-Disposition'       => "attachment; filename=\"#{@path.split('/').last}\"",
124
                     'Content-Transfer-Encoding' => 'binary'
125
                     )
126
      headers['Content-Length'] = @entry.size unless @entry.size.nil?
127
      headers['Cache-Control'] = 'private' if headers['Cache-Control'] == 'no-cache'
128
      
129
      render( :text => Proc.new { |response, output|
130
                begin  
131
                  logger.info "Streaming entry #{@path}" unless logger.nil?
132
                  len = 4096
133
                  while buf = content.read(len)
134
                    output.write(buf)
135
                  end
136
                ensure
137
                  content.close
138
                end
139
              })
122 140
    else
123 141
      # Prevent empty lines when displaying a file with Windows style eol
124
      @content.gsub!("\r\n", "\n")
125
   end
142
      @content = content.read.gsub("\r\n", "\n")
143
      content.close
144
    end
126 145
  end
127 146
  
128 147
  def annotate
......
133 152
  def revision
134 153
    @changeset = @repository.changesets.find_by_revision(@rev)
135 154
    raise ChangesetNotFound unless @changeset
136

  
155
    
137 156
    respond_to do |format|
138 157
      format.html
139 158
      format.js {render :layout => false}
......
149 168
      filename = "changeset_r#{@rev}"
150 169
      filename << "_r#{@rev_to}" if @rev_to
151 170
      send_data @diff.join, :filename => "#{filename}.diff",
152
                            :type => 'text/x-patch',
153
                            :disposition => 'attachment'
171
      :type => 'text/x-patch',
172
      :disposition => 'attachment'
154 173
    else
155 174
      @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
156 175
      @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
......
188 207
    end
189 208
  end
190 209
  
191
private
210
  private
192 211
  def find_project
193 212
    @project = Project.find(params[:id])
194 213
  rescue ActiveRecord::RecordNotFound
......
211 230
  rescue InvalidRevisionParam
212 231
    show_error_not_found
213 232
  end
214

  
233
  
215 234
  def show_error_not_found
216 235
    render_error l(:error_scm_not_found)
217 236
  end
......
228 247
    commits_by_day = repository.changesets.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
229 248
    commits_by_month = [0] * 12
230 249
    commits_by_day.each {|c| commits_by_month[c.first.to_date.months_ago] += c.last }
231

  
250
    
232 251
    changes_by_day = repository.changes.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
233 252
    changes_by_month = [0] * 12
234 253
    changes_by_day.each {|c| changes_by_month[c.first.to_date.months_ago] += c.last }
235
   
254
    
236 255
    fields = []
237 256
    month_names = l(:actionview_datehelper_select_month_names_abbr).split(',')
238 257
    12.times {|m| fields << month_names[((Date.today.month - 1 - m) % 12)]}
239
  
258
    
240 259
    graph = SVG::Graph::Bar.new(
241
      :height => 300,
242
      :width => 800,
243
      :fields => fields.reverse,
244
      :stack => :side,
245
      :scale_integers => true,
246
      :step_x_labels => 2,
247
      :show_data_values => false,
248
      :graph_title => l(:label_commits_per_month),
249
      :show_graph_title => true
250
    )
260
                                :height => 300,
261
                                :width => 800,
262
                                :fields => fields.reverse,
263
                                :stack => :side,
264
                                :scale_integers => true,
265
                                :step_x_labels => 2,
266
                                :show_data_values => false,
267
                                :graph_title => l(:label_commits_per_month),
268
                                :show_graph_title => true
269
                                )
251 270
    
252 271
    graph.add_data(
253
      :data => commits_by_month[0..11].reverse,
254
      :title => l(:label_revision_plural)
255
    )
256

  
272
                   :data => commits_by_month[0..11].reverse,
273
                   :title => l(:label_revision_plural)
274
                   )
275
    
257 276
    graph.add_data(
258
      :data => changes_by_month[0..11].reverse,
259
      :title => l(:label_change_plural)
260
    )
277
                   :data => changes_by_month[0..11].reverse,
278
                   :title => l(:label_change_plural)
279
                   )
261 280
    
262 281
    graph.burn
263 282
  end
264

  
283
  
265 284
  def graph_commits_per_author(repository)
266 285
    commits_by_author = repository.changesets.count(:all, :group => :committer)
267 286
    commits_by_author.sort! {|x, y| x.last <=> y.last}
268

  
287
    
269 288
    changes_by_author = repository.changes.count(:all, :group => :committer)
270 289
    h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
271 290
    
......
281 300
    fields = fields.collect {|c| c.gsub(%r{<.+@.+>}, '') }
282 301
    
283 302
    graph = SVG::Graph::BarHorizontal.new(
284
      :height => 400,
285
      :width => 800,
286
      :fields => fields,
287
      :stack => :side,
288
      :scale_integers => true,
289
      :show_data_values => false,
290
      :rotate_y_labels => false,
291
      :graph_title => l(:label_commits_per_author),
292
      :show_graph_title => true
293
    )
303
                                          :height => 400,
304
                                          :width => 800,
305
                                          :fields => fields,
306
                                          :stack => :side,
307
                                          :scale_integers => true,
308
                                          :show_data_values => false,
309
                                          :rotate_y_labels => false,
310
                                          :graph_title => l(:label_commits_per_author),
311
                                          :show_graph_title => true
312
                                          )
294 313
    
295 314
    graph.add_data(
296
      :data => commits_data,
297
      :title => l(:label_revision_plural)
298
    )
299

  
315
                   :data => commits_data,
316
                   :title => l(:label_revision_plural)
317
                   )
318
    
300 319
    graph.add_data(
301
      :data => changes_data,
302
      :title => l(:label_change_plural)
303
    )
304
       
320
                   :data => changes_data,
321
                   :title => l(:label_change_plural)
322
                   )
323
    
305 324
    graph.burn
306 325
  end
326
  
327
end
307 328

  
308
end
309
  
310 329
class Date
311 330
  def months_ago(date = Date.today)
312 331
    (date.year - self.year)*12 + (date.month - self.month)
313 332
  end
314

  
333
  
315 334
  def weeks_ago(date = Date.today)
316 335
    (date.year - self.year)*52 + (date.cweek - self.cweek)
317 336
  end
app/models/repository.rb Sun Dec 07 16:21:40 2008 +0100 → app/models/repository.rb Mon Dec 08 12:04:28 2008 +0100
67 67
    scm.properties(path, identifier)
68 68
  end
69 69
  
70
  def cat(path, identifier=nil)
71
    scm.cat(path, identifier)
70
  def cat(path, identifier=nil, &block)
71
    scm.cat(path, identifier, &block)
72 72
  end
73 73
  
74 74
  def diff(path, rev, rev_to)
/dev/null Thu Jan 01 00:00:00 1970 +0000 → lib/buffered_io.rb Mon Dec 08 12:04:28 2008 +0100
1
# This program is free software; you can redistribute it and/or
2
# modify it under the terms of the GNU General Public License
3
# as published by the Free Software Foundation; either version 2
4
# of the License, or (at your option) any later version.
5
# 
6
# This program is distributed in the hope that it will be useful,
7
# but WITHOUT ANY WARRANTY; without even the implied warranty of
8
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9
# GNU General Public License for more details.
10
# 
11
# You should have received a copy of the GNU General Public License
12
# along with this program; if not, write to the Free Software
13
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
14
class BufferedIO
15

  
16
  attr_accessor :cache
17

  
18
  def initialize(io)
19
    @source = io
20
    @cache = false
21
    @synced = true
22
  end
23

  
24
  def cache=(b)
25
    if b and synced?
26
      @cache = true
27
    else
28
      @cache = false
29
    end
30
  end
31

  
32
  def internal_buffer
33
    @internal_buffer || @internal_buffer = StringIO.new
34
  end
35

  
36
  def is_binary_data?
37
    if synced?
38
      position = internal_buffer.pos
39
      internal_buffer.rewind
40
      s = read_and_cache(4096)
41
      internal_buffer.pos = position
42
      s.is_binary_data?
43
    else
44
      raise Exception.new("Cannot do this test on an uncached buffer")      
45
    end
46
  end
47

  
48
  def synced?
49
    @synced
50
  end
51

  
52
  def read(x=nil)
53
    if cache
54
      read_and_cache(x)
55
    else
56
      read_and_dont_cache(x)
57
    end
58
  end 
59

  
60
  def pos=(x)
61
    if x > internal_buffer.size
62
      if sinced? and cache
63
        _append @source.read(x - internal_buffer.pos)
64
        internal_buffer.pos = x
65
      else
66
        @source.pos = x
67
      end
68
    else
69
      internal_buffer.pos = x
70
    end
71
  end 
72

  
73
  ["pos", "rewind", "tell"].each do |m| 
74
    define_method(m) do
75
      if synced?
76
        internal_buffer.send(m)
77
      else
78
        @source.send(m)
79
      end
80
    end 
81
  end
82

  
83
  def close
84
    @source.close
85
  end
86
  
87
  private
88
  
89
  def _append(s) 
90
    unless s.nil?
91
      internal_buffer << s 
92
      internal_buffer.pos -= s.size
93
    end 
94
  end
95

  
96
  def read_source(x)
97
    @synced = false
98
    @source.read(x)
99
  end
100

  
101
  # Read the buffer by filling readed data in the cache.
102
  def read_and_cache(x=nil)
103
    if synced?
104
      to_read = x ? to_read = x + internal_buffer.pos - internal_buffer.size : nil 
105
      _append(@source.read(to_read)) if to_read.nil? or to_read > 0 
106
      internal_buffer.read(x)       
107
    else
108
      # Data is missing
109
      raise "Cannot cache a partially cached stream"
110
    end
111
  end
112

  
113
  # Read without filling the cache and ram by the way.
114
  # Not cached data will not be readable anymore if the
115
  # buffer cannot rewind.
116
  def read_and_dont_cache(x=nil)
117
    if synced?
118
      # Source and cache buffer are synced 
119
      if internal_buffer.pos == internal_buffer.size
120
        # There is nothing to read in the cache
121
        read_source(x)
122
      else # internal_buffer.pos < @source.pos
123
        if x.nil? or (internal_buffer.pos + x) > internal_buffer.size
124
          # There is a first part to read in the cache
125
          # and another in the source
126
          internal_buffer.read + read_source(x)
127
        else
128
          # All is in the cache
129
          internal_buffer.read(x)
130
        end
131
      end
132
    else
133
      @internal_buffer = nil
134
      read_source(x)
135
    end
136
  end
137
end
lib/redmine/scm/adapters/abstract_adapter.rb Sun Dec 07 16:21:40 2008 +0100 → lib/redmine/scm/adapters/abstract_adapter.rb Mon Dec 08 12:04:28 2008 +0100
113 113
          return nil
114 114
        end
115 115
        
116
        def cat(path, identifier=nil)
116
        def cat(path, identifier=nil, &block)
117 117
          return nil
118 118
        end
119 119
        
......
172 172
        def self.shellout(cmd, &block)
173 173
          logger.debug "Shelling out: #{cmd}" if logger && logger.debug?
174 174
          begin
175
            IO.popen(cmd, "r+") do |io|
175
            if block_given?
176
              IO.popen(cmd, "r+") do |io|
177
                io.close_write
178
                block.call(io)
179
              end
180
            else
181
              io = IO.popen(cmd, "r+")
176 182
              io.close_write
177
              block.call(io) if block_given?
183
              return io
178 184
            end
179 185
          rescue Errno::ENOENT => e
180 186
            msg = strip_credential(e.message)
lib/redmine/scm/adapters/bazaar_adapter.rb Sun Dec 07 16:21:40 2008 +0100 → lib/redmine/scm/adapters/bazaar_adapter.rb Mon Dec 08 12:04:28 2008 +0100
151 151
          diff
152 152
        end
153 153
        
154
        def cat(path, identifier=nil)
154
        def cat(path, identifier=nil, &block)
155 155
          cmd = "#{BZR_BIN} cat"
156 156
          cmd << " -r#{identifier.to_i}" if identifier && identifier.to_i > 0
157 157
          cmd << " #{target(path)}"
158 158
          cat = nil
159
          shellout(cmd) do |io|
159
          if block_given?
160
            shellout(cmd) do |io|
161
              io.binmode
162
              yield BufferedIO.new(io)
163
            end
164
          else
165
            io = shellout(cmd)
160 166
            io.binmode
161
            cat = io.read
167
            return BufferedIO.new(io)
162 168
          end
163
          return nil if $? && $?.exitstatus != 0
164
          cat
165 169
        end
166 170
        
167 171
        def annotate(path, identifier=nil)
lib/redmine/scm/adapters/cvs_adapter.rb Sun Dec 07 16:21:40 2008 +0100 → lib/redmine/scm/adapters/cvs_adapter.rb Mon Dec 08 12:04:28 2008 +0100
240 240
          diff
241 241
        end  
242 242
        
243
        def cat(path, identifier=nil)
243
        def cat(path, identifier=nil, &block)
244 244
          identifier = (identifier) ? identifier : "HEAD"
245 245
          logger.debug "<cvs> cat path:'#{path}',identifier #{identifier}"
246 246
          path_with_project="#{url}#{with_leading_slash(path)}"
......
248 248
          cmd << " -D \"#{time_to_cvstime(identifier)}\"" if identifier
249 249
          cmd << " -p #{shell_quote path_with_project}"
250 250
          cat = nil
251
          shellout(cmd) do |io|
252
            cat = io.read
251
          if block_given?
252
            shellout(cmd) do |io|
253
              io.binmode
254
              yield BufferedIO.new(io)
255
            end
256
          else
257
            io = shellout(cmd)
258
            io.binmode
259
            return BufferedIO.new(io)
253 260
          end
254
          return nil if $? && $?.exitstatus != 0
255
          cat
256 261
        end  
257 262

  
258 263
        def annotate(path, identifier=nil)
lib/redmine/scm/adapters/darcs_adapter.rb Sun Dec 07 16:21:40 2008 +0100 → lib/redmine/scm/adapters/darcs_adapter.rb Mon Dec 08 12:04:28 2008 +0100
134 134
          diff
135 135
        end
136 136
        
137
        def cat(path, identifier=nil)
137
        def cat(path, identifier=nil, &block)
138 138
          cmd = "#{DARCS_BIN} show content --repodir #{@url}"
139 139
          cmd << " --match \"hash #{identifier}\"" if identifier
140 140
          cmd << " #{shell_quote path}"
141 141
          cat = nil
142
          shellout(cmd) do |io|
142
          if block_given?
143
            shellout(cmd) do |io|
144
              io.binmode
145
              yield BufferedIO.new(io)
146
            end
147
          else
148
            io = shellout(cmd)
143 149
            io.binmode
144
            cat = io.read
150
            return BufferedIO.new(io)
145 151
          end
146
          return nil if $? && $?.exitstatus != 0
147
          cat
148 152
        end
149 153

  
150 154
        private
lib/redmine/scm/adapters/filesystem_adapter.rb Sun Dec 07 16:21:40 2008 +0100 → lib/redmine/scm/adapters/filesystem_adapter.rb Mon Dec 08 12:04:28 2008 +0100
71 71
          entries.sort_by_name
72 72
        end
73 73
        
74
        def cat(path, identifier=nil)
75
          File.new(target(path), "rb").read
74
        def cat(path, identifier=nil, &block)
75
          if block_given?
76
            File.open(target(path), "rb") do |io|
77
              io.binmode
78
              yield BufferedIO.new(io)
79
            end
80
          else
81
            io = File.new(target(path), "rb")
82
            io.binmode
83
            return BufferedIO.new(io)
84
          end
76 85
        end
77 86

  
78 87
        private
lib/redmine/scm/adapters/git_adapter.rb Sun Dec 07 16:21:40 2008 +0100 → lib/redmine/scm/adapters/git_adapter.rb Mon Dec 08 12:04:28 2008 +0100
249 249
          blame
250 250
        end
251 251
        
252
        def cat(path, identifier=nil)
252
        def cat(path, identifier=nil, &block)
253 253
          if identifier.nil?
254 254
            identifier = 'HEAD'
255 255
          end
256 256
          cmd = "#{GIT_BIN} --git-dir #{target('')} show #{shell_quote(identifier + ':' + path)}"
257 257
          cat = nil
258
          shellout(cmd) do |io|
258
          if block_given?
259
            shellout(cmd) do |io|
260
              io.binmode
261
              yield BufferedIO.new(io)
262
            end
263
          else
264
            io = shellout(cmd)
259 265
            io.binmode
260
            cat = io.read
266
            return BufferedIO.new(io)
261 267
          end
262
          return nil if $? && $?.exitstatus != 0
263
          cat
264 268
        end
265 269
      end
266 270
    end
lib/redmine/scm/adapters/mercurial_adapter.rb Sun Dec 07 16:21:40 2008 +0100 → lib/redmine/scm/adapters/mercurial_adapter.rb Mon Dec 08 12:04:28 2008 +0100
92 92
                entries << Entry.new({:name => e.first,
93 93
                                       :path => (path.nil? or path.empty? ? e.first : "#{with_trailling_slash(path)}#{e.first}"),
94 94
                                       :kind => (e.size > 1 ? 'dir' : 'file'),
95
                                       
95 96
                                       :lastrev => Revision.new
96 97
                                     }) unless entries.detect{|entry| entry.name == e.first}
97 98
              end
......
169 170
          diff
170 171
        end
171 172
        
172
        def cat(path, identifier=nil)
173
        def cat(path, identifier=nil, &block)
173 174
          cmd = "#{HG_BIN} -R #{target('')} cat"
174 175
          cmd << " -r " + (identifier ? identifier.to_s : "tip")
175 176
          cmd << " #{target(path)}"
176 177
          cat = nil
177
          shellout(cmd) do |io|
178
          if block_given?
179
            shellout(cmd) do |io|
180
              io.binmode
181
              yield BufferedIO.new(io)
182
            end
183
          else
184
            io = shellout(cmd)
178 185
            io.binmode
179
            cat = io.read
186
            return BufferedIO.new(io)
180 187
          end
181
          return nil if $? && $?.exitstatus != 0
182
          cat
183 188
        end
184 189
        
185 190
        def annotate(path, identifier=nil)
lib/redmine/scm/adapters/subversion_adapter.rb Sun Dec 07 16:21:40 2008 +0100 → lib/redmine/scm/adapters/subversion_adapter.rb Mon Dec 08 12:04:28 2008 +0100
190 190
          diff
191 191
        end
192 192
        
193
        def cat(path, identifier=nil)
193
        def cat(path, identifier=nil, &block)
194 194
          identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
195 195
          cmd = "#{SVN_BIN} cat #{target(URI.escape(path))}@#{identifier}"
196 196
          cmd << credentials_string
197 197
          cat = nil
198
          shellout(cmd) do |io|
198
          if block_given?
199
            shellout(cmd) do |io|
200
              io.binmode
201
              yield BufferedIO.new(io)
202
            end
203
          else
204
            io = shellout(cmd)
199 205
            io.binmode
200
            cat = io.read
206
            return BufferedIO.new(io)
201 207
          end
202
          return nil if $? && $?.exitstatus != 0
203
          cat
208
          ensure
209
          io.close if (io and block_given?)
204 210
        end
205 211
        
206 212
        def annotate(path, identifier=nil)
test/functional/repositories_cvs_controller_test.rb Sun Dec 07 16:21:40 2008 +0100 → test/functional/repositories_cvs_controller_test.rb Mon Dec 08 12:04:28 2008 +0100
22 22
class RepositoriesController; def rescue_action(e) raise e end; end
23 23

  
24 24
class RepositoriesCvsControllerTest < Test::Unit::TestCase
25
  fixtures :projects, :users, :roles, :members, :enabled_modules,
26
           :issues, :issue_statuses, :changesets, :changes,
27
           :issue_categories, :enumerations, :custom_fields, :custom_values, :trackers
25 28

  
26 29
  # No '..' in the repository path
27 30
  REPOSITORY_PATH = RAILS_ROOT.gsub(%r{config\/\.\.}, '') + '/tmp/test/cvs_repository'
......
113 116
    def test_entry_download
114 117
      get :entry, :id => 1, :path => ['sources', 'watchers_controller.rb'], :format => 'raw'
115 118
      assert_response :success
119

  
120
      content = StringIO.new
121
      r = nil
122
      @response.body.call(r, content)
123
      content.rewind
124
      content = content.read.split("\n")
125
      assert_equal "end", content.last
116 126
    end
117 127

  
118 128
    def test_directory_entry
test/functional/repositories_mercurial_controller_test.rb Sun Dec 07 16:21:40 2008 +0100 → test/functional/repositories_mercurial_controller_test.rb Mon Dec 08 12:04:28 2008 +0100
97 97
      get :entry, :id => 3, :path => ['sources', 'watchers_controller.rb'], :format => 'raw'
98 98
      assert_response :success
99 99
      # File content
100
      assert @response.body.include?('WITHOUT ANY WARRANTY')
100
      content = StringIO.new
101
      r = nil
102
      @response.body.call(r, content)
103
      content.rewind
104
      content = content.read
105
      assert content.include?('WITHOUT ANY WARRANTY')
106
      assert_equal "end", content.split("\r\n").last
101 107
    end
102 108

  
103 109
    def test_directory_entry
test/functional/repositories_subversion_controller_test.rb Sun Dec 07 16:21:40 2008 +0100 → test/functional/repositories_subversion_controller_test.rb Mon Dec 08 12:04:28 2008 +0100
111 111
    def test_entry_download
112 112
      get :entry, :id => 1, :path => ['subversion_test', 'helloworld.c'], :format => 'raw'
113 113
      assert_response :success
114
      content = StringIO.new
115
      r = nil
116
      @response.body.call(r, content)
117
      content.rewind
118
      content = content.read.split("\r\n")
119
      assert_equal "#include <stdio.h>", content.first
120
      assert_equal "}", content.last
114 121
    end
115 122
    
116 123
    def test_directory_entry
/dev/null Thu Jan 01 00:00:00 1970 +0000 → test/unit/lib/buffered_io_test.rb Mon Dec 08 12:04:28 2008 +0100
1
# This program is free software; you can redistribute it and/or
2
# modify it under the terms of the GNU General Public License
3
# as published by the Free Software Foundation; either version 2
4
# of the License, or (at your option) any later version.
5
# 
6
# This program is distributed in the hope that it will be useful,
7
# but WITHOUT ANY WARRANTY; without even the implied warranty of
8
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9
# GNU General Public License for more details.
10
# 
11
# You should have received a copy of the GNU General Public License
12
# along with this program; if not, write to the Free Software
13
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
14

  
15
require File.dirname(__FILE__) + '/../../test_helper'
16

  
17
class BufferedIOTest < Test::Unit::TestCase
18

  
19
  CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
20
  def self.rand_string(length=8)
21
    s=''
22
    length.times{ s << CHARS[rand(CHARS.length)] }
23
    s
24
  end
25

  
26
  TEST_STRING=rand_string(1000)
27
  
28
  def test_stop_caching
29
    io = BufferedIO.new(StringIO.new(TEST_STRING))
30
    io.cache = true
31
    t = io.read(5)
32
    assert_equal 5, io.internal_buffer.size
33
    io.cache = false
34
    t += io.read
35
    assert_equal TEST_STRING, t
36
    assert_equal 5, io.internal_buffer.size
37
  end
38

  
39
  def test_is_binary_data
40
    io = BufferedIO.new(StringIO.new(TEST_STRING))
41
    io2 = BufferedIO.new(StringIO.new(TEST_STRING))
42
    io2.cache = true
43
    [io, io2].each do |io|
44
      assert_equal false, io.is_binary_data?
45
      t = io.read
46
      assert_equal TEST_STRING, t
47
    end
48
  end
49

  
50
end
test/unit/repository_bazaar_test.rb Sun Dec 07 16:21:40 2008 +0100 → test/unit/repository_bazaar_test.rb Mon Dec 08 12:04:28 2008 +0100
70 70
    end
71 71
    
72 72
    def test_cat
73
      cat = @repository.scm.cat('directory/document.txt')
74
      assert cat =~ /Write the contents of a file as of a given revision to standard output/
73
      @repository.scm.cat('directory/document.txt') do |io| 
74
        cat = io.read
75
        assert cat =~ /Write the contents of a file as of a given revision to standard output/
76
      end
75 77
    end
76 78
    
77 79
    def test_annotate
test/unit/repository_cvs_test.rb Sun Dec 07 16:21:40 2008 +0100 → test/unit/repository_cvs_test.rb Mon Dec 08 12:04:28 2008 +0100
53 53
      @repository.fetch_changesets
54 54
      assert_equal 5, @repository.changesets.count
55 55
    end
56

  
57
    def test_cat
58
      content = @repository.scm.cat("sources/watchers_controller.rb").read.split("\n")
59
      assert_equal "end", content.last
60
    end
61

  
56 62
  else
57 63
    puts "CVS test repository NOT FOUND. Skipping unit tests !!!"
58 64
    def test_fake; assert true end
test/unit/repository_darcs_test.rb Sun Dec 07 16:21:40 2008 +0100 → test/unit/repository_darcs_test.rb Mon Dec 08 12:04:28 2008 +0100
51 51
    
52 52
    def test_cat
53 53
      @repository.fetch_changesets
54
      cat = @repository.cat("sources/welcome_controller.rb", 2)
54
      cat = @repository.cat("sources/welcome_controller.rb", 2).read
55 55
      assert_not_nil cat
56 56
      assert cat.include?('class WelcomeController < ApplicationController')
57 57
    end
test/unit/repository_filesystem_test.rb Sun Dec 07 16:21:40 2008 +0100 → test/unit/repository_filesystem_test.rb Mon Dec 08 12:04:28 2008 +0100
44 44
    end
45 45

  
46 46
    def test_cat
47
      assert_equal "TEST CAT\n", @repository.scm.cat("test")
47
      @repository.scm.cat("test") do |io|
48
        assert_equal "TEST CAT\n", io.read
49
      end
48 50
    end
49 51

  
50 52
  else
test/unit/repository_git_test.rb Sun Dec 07 16:21:40 2008 +0100 → test/unit/repository_git_test.rb Mon Dec 08 12:04:28 2008 +0100
62 62
      @repository.fetch_changesets
63 63
      assert_equal 6, @repository.changesets.count
64 64
    end
65

  
66
    def test_cat
67
       @repository.cat("README") do |io| 
68
        cat = io.read
69
        assert_not_nil cat
70
        lines = cat.split("\r\n")
71
        assert_equal 'Mercurial test repository', lines.first
72
        assert_equal 'It is written in Python.', lines.last
73
      end
74
    end
75

  
65 76
  else
66 77
    puts "Git test repository NOT FOUND. Skipping unit tests !!!"
67 78
    def test_fake; assert true end
test/unit/repository_subversion_test.rb Sun Dec 07 16:21:40 2008 +0100 → test/unit/repository_subversion_test.rb Mon Dec 08 12:04:28 2008 +0100
48 48
      @repository.fetch_changesets
49 49
      assert_equal 8, @repository.changesets.count
50 50
    end
51
    
52
    def test_cat
53
      content = @repository.cat("subversion_test/helloworld.c").read.split("\r\n")
54
      assert_equal "#include <stdio.h>", content.first
55
      assert_equal "}", content.last
56
    end
57

  
51 58
  else
52 59
    puts "Subversion test repository NOT FOUND. Skipping unit tests !!!"
53 60
    def test_fake; assert true end
(2-2/2)