File tree Expand file tree Collapse file tree 2 files changed +38
-12
lines changed
Expand file tree Collapse file tree 2 files changed +38
-12
lines changed Original file line number Diff line number Diff line change @@ -129,24 +129,35 @@ def exists?
129129 # [String] contents of the file
130130 #
131131 def read ( *args )
132- if @content
133- if args . empty?
132+ if args . empty?
133+ if @content
134134 @content
135+ elsif is_path?
136+ File . open ( @file , "rb" ) { |file | file . read }
135137 else
136- length , outbuf = args
137- raise ArgumentError , "outbuf argument not supported since the content is already loaded" if outbuf
138- @content [ 0 , length ]
138+ @file . try ( :rewind )
139+ @content = @file . read
140+ @file . try ( :close ) unless @file . class . ancestors . include? ( ::StringIO ) || @file . try ( :closed? )
141+ @content
139142 end
140- elsif is_path?
141- File . open ( @file , "rb" ) { |file | file . read ( *args ) }
142143 else
143- @file . try ( :rewind )
144- @content = @file . read ( *args )
145- @file . try ( :close ) unless @file . class . ancestors . include? ( ::StringIO ) || @file . try ( :closed? )
146- @content
144+ if is_path?
145+ @file = File . open ( path , "rb" )
146+ elsif @file . is_a? ( CarrierWave ::Uploader ::Base )
147+ @file = StringIO . new ( @file . read )
148+ end
149+
150+ @file . read ( *args )
147151 end
148152 end
149153
154+ ##
155+ # Rewinds the underlying file.
156+ #
157+ def rewind
158+ @file . rewind if @file . respond_to? ( :rewind )
159+ end
160+
150161 ##
151162 # Moves the file to the given path
152163 #
Original file line number Diff line number Diff line change 407407 end
408408
409409 it "always reads from the file if arguments are given" do
410- sanitized_file . read
410+ sanitized_file . read ( 0 )
411+ expect ( sanitized_file . file ) . to receive ( :read ) . with ( 4 ) . and_call_original
412+ expect ( sanitized_file . read ( 4 ) ) . to eq ( "this" )
413+ end
414+
415+ it "properly emulates the behavior of IO#read" do
416+ expect ( sanitized_file . read ( 4 ) ) . to eq ( "this" )
417+ expect ( sanitized_file . read ( 10 ) ) . to eq ( " is stuff" )
418+ expect ( sanitized_file . read ( 1 ) ) . to be_nil
419+ end
420+ end
421+
422+ describe "#rewind" do
423+ it "rewinds the underlying file" do
424+ sanitized_file . read ( 5 )
425+ sanitized_file . rewind
411426 expect ( sanitized_file . read ( 4 ) ) . to eq ( "this" )
412427 end
413428 end
You can’t perform that action at this time.
0 commit comments