summaryrefslogtreecommitdiff
path: root/spec/ruby/core/io/copy_stream_spec.rb
blob: 344746fac3aaa34c3e5088c6b1ca0c6adc0e4aa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

describe :io_copy_stream_to_file, shared: true do
  it "copies the entire IO contents to the file" do
    IO.copy_stream(@object.from, @to_name)
    File.read(@to_name).should == @content
    IO.copy_stream(@from_bigfile, @to_name)
    File.read(@to_name).should == @content_bigfile
  end

  it "returns the number of bytes copied" do
    IO.copy_stream(@object.from, @to_name).should == @size
    IO.copy_stream(@from_bigfile, @to_name).should == @size_bigfile
  end

  it "copies only length bytes when specified" do
    IO.copy_stream(@object.from, @to_name, 8).should == 8
    File.read(@to_name).should == "Line one"
  end

  it "calls #to_path to convert on object to a file name" do
    obj = mock("io_copy_stream_to")
    obj.should_receive(:to_path).and_return(@to_name)

    IO.copy_stream(@object.from, obj)
    File.read(@to_name).should == @content
  end

  it "raises a TypeError if #to_path does not return a String" do
    obj = mock("io_copy_stream_to")
    obj.should_receive(:to_path).and_return(1)

    lambda { IO.copy_stream(@object.from, obj) }.should raise_error(TypeError)
  end
end

describe :io_copy_stream_to_file_with_offset, shared: true do
  platform_is_not :windows do
    it "copies only length bytes from the offset" do
      IO.copy_stream(@object.from, @to_name, 8, 4).should == 8
      File.read(@to_name).should == " one\n\nLi"
    end
  end
end

describe :io_copy_stream_to_io, shared: true do
  it "copies the entire IO contents to the IO" do
    IO.copy_stream(@object.from, @to_io)
    File.read(@to_name).should == @content
    IO.copy_stream(@from_bigfile, @to_io)
    File.read(@to_name).should == (@content + @content_bigfile)
  end

  it "returns the number of bytes copied" do
    IO.copy_stream(@object.from, @to_io).should == @size
    IO.copy_stream(@from_bigfile, @to_io).should == @size_bigfile
  end

  it "starts writing at the destination IO's current position" do
    @to_io.write("prelude ")
    IO.copy_stream(@object.from, @to_io)
    File.read(@to_name).should == ("prelude " + @content)
  end

  it "leaves the destination IO position at the last write" do
    IO.copy_stream(@object.from, @to_io)
    @to_io.pos.should == @size
  end

  it "raises an IOError if the destination IO is not open for writing" do
    @to_io.close
    @to_io = new_io @to_name, "r"
    lambda { IO.copy_stream @object.from, @to_io }.should raise_error(IOError)
  end

  it "does not close the destination IO" do
    IO.copy_stream(@object.from, @to_io)
    @to_io.closed?.should be_false
  end

  it "copies only length bytes when specified" do
    IO.copy_stream(@object.from, @to_io, 8).should == 8
    File.read(@to_name).should == "Line one"
  end
end

describe :io_copy_stream_to_io_with_offset, shared: true do
  platform_is_not :windows do
    it "copies only length bytes from the offset" do
      IO.copy_stream(@object.from, @to_io, 8, 4).should == 8
      File.read(@to_name).should == " one\n\nLi"
    end
  end
end

describe "IO.copy_stream" do
  before :each do
    @from_name = fixture __FILE__, "copy_stream.txt"
    @to_name = tmp("io_copy_stream_io_name")

    @content = IO.read(@from_name)
    @size = @content.size

    @from_bigfile = tmp("io_copy_stream_bigfile")
    @content_bigfile = "A" * 17_000
    touch(@from_bigfile){|f| f.print @content_bigfile }
    @size_bigfile =  @content_bigfile.size
  end

  after :each do
    rm_r @to_name, @from_bigfile
  end

  describe "from an IO" do
    before :each do
      @from_io = new_io @from_name, "rb"
      IOSpecs::CopyStream.from = @from_io
    end

    after :each do
      @from_io.close
    end

    it "raises an IOError if the source IO is not open for reading" do
      @from_io.close
      @from_io = new_io @from_bigfile, "a"
      lambda { IO.copy_stream @from_io, @to_name }.should raise_error(IOError)
    end

    it "does not close the source IO" do
      IO.copy_stream(@from_io, @to_name)
      @from_io.closed?.should be_false
    end

    platform_is_not :windows do
      it "does not change the IO offset when an offset is specified" do
        @from_io.pos = 10
        IO.copy_stream(@from_io, @to_name, 8, 4)
        @from_io.pos.should == 10
      end
    end

    it "does change the IO offset when an offset is not specified" do
      @from_io.pos = 10
      IO.copy_stream(@from_io, @to_name)
      @from_io.pos.should == 42
    end

    describe "to a file name" do
      it_behaves_like :io_copy_stream_to_file, nil, IOSpecs::CopyStream
      it_behaves_like :io_copy_stream_to_file_with_offset, nil, IOSpecs::CopyStream
    end

    describe "to an IO" do
      before :each do
        @to_io = new_io @to_name, "wb"
      end

      after :each do
        @to_io.close
      end

      it_behaves_like :io_copy_stream_to_io, nil, IOSpecs::CopyStream
      it_behaves_like :io_copy_stream_to_io_with_offset, nil, IOSpecs::CopyStream
    end
  end

  describe "from a file name" do
    before :each do
      IOSpecs::CopyStream.from = @from_name
    end

    it "calls #to_path to convert on object to a file name" do
      obj = mock("io_copy_stream_from")
      obj.should_receive(:to_path).and_return(@from_name)

      IO.copy_stream(obj, @to_name)
      File.read(@to_name).should == @content
    end

    it "raises a TypeError if #to_path does not return a String" do
      obj = mock("io_copy_stream_from")
      obj.should_receive(:to_path).and_return(1)

      lambda { IO.copy_stream(obj, @to_name) }.should raise_error(TypeError)
    end

    describe "to a file name" do
      it_behaves_like :io_copy_stream_to_file, nil, IOSpecs::CopyStream
      it_behaves_like :io_copy_stream_to_file_with_offset, nil, IOSpecs::CopyStream
    end

    describe "to an IO" do
      before :each do
        @to_io = new_io @to_name, "wb"
      end

      after :each do
        @to_io.close
      end

      it_behaves_like :io_copy_stream_to_io, nil, IOSpecs::CopyStream
      it_behaves_like :io_copy_stream_to_io_with_offset, nil, IOSpecs::CopyStream
    end
  end

  describe "from a pipe IO" do
    before :each do
      @from_io = IOSpecs.pipe_fixture(@content)
      IOSpecs::CopyStream.from = @from_io
    end

    after :each do
      @from_io.close
    end

    it "does not close the source IO" do
      IO.copy_stream(@from_io, @to_name)
      @from_io.closed?.should be_false
    end

    platform_is_not :windows do
      it "raises an error when an offset is specified" do
        lambda { IO.copy_stream(@from_io, @to_name, 8, 4) }.should raise_error(Errno::ESPIPE)
      end
    end

    describe "to a file name" do
      it_behaves_like :io_copy_stream_to_file, nil, IOSpecs::CopyStream
    end

    describe "to an IO" do
      before :each do
        @to_io = new_io @to_name, "wb"
      end

      after :each do
        @to_io.close
      end

      it_behaves_like :io_copy_stream_to_io, nil, IOSpecs::CopyStream
    end
  end

  describe "with non-IO Objects" do
    before do
      @io = new_io @from_name, "rb"
    end

    after do
      @io.close unless @io.closed?
    end

    it "calls #readpartial on the source Object if defined" do
      from = IOSpecs::CopyStreamReadPartial.new @io

      IO.copy_stream(from, @to_name)
      File.read(@to_name).should == @content
    end

    it "calls #read on the source Object" do
      from = IOSpecs::CopyStreamRead.new @io

      IO.copy_stream(from, @to_name)
      File.read(@to_name).should == @content
    end

    it "calls #write on the destination Object" do
      to = mock("io_copy_stream_to_object")
      to.should_receive(:write).with(@content).and_return(@content.size)

      IO.copy_stream(@from_name, to)
    end

    it "does not call #pos on the source if no offset is given" do
      @io.should_not_receive(:pos)
      IO.copy_stream(@io, @to_name)
    end

  end
end