diff options
Diffstat (limited to 'spec/ruby/core/io/write_spec.rb')
| -rw-r--r-- | spec/ruby/core/io/write_spec.rb | 87 |
1 files changed, 54 insertions, 33 deletions
diff --git a/spec/ruby/core/io/write_spec.rb b/spec/ruby/core/io/write_spec.rb index 6e89c7cd9e..1a745ba012 100644 --- a/spec/ruby/core/io/write_spec.rb +++ b/spec/ruby/core/io/write_spec.rb @@ -21,7 +21,7 @@ describe "IO#write on a file" do end it "does not check if the file is writable if writing zero bytes" do - -> { @readonly_file.write("") }.should_not raise_error + -> { @readonly_file.write("") }.should_not.raise end before :each do @@ -102,11 +102,18 @@ describe "IO#write on a file" do File.binread(@filename).should == "h\u0000\u0000\u0000i\u0000\u0000\u0000" end + it "ignores the 'bom|' prefix" do + File.open(@filename, "w", encoding: 'bom|utf-8') do |file| + file.write("hi") + end + File.binread(@filename).should == "hi" + end + it "raises a invalid byte sequence error if invalid bytes are being written" do # pack "\xFEhi" to avoid utf-8 conflict xFEhi = ([254].pack('C*') + 'hi').force_encoding('utf-8') File.open(@filename, "w", encoding: Encoding::US_ASCII) do |file| - -> { file.write(xFEhi) }.should raise_error(Encoding::InvalidByteSequenceError) + -> { file.write(xFEhi) }.should.raise(Encoding::InvalidByteSequenceError) end end @@ -150,7 +157,7 @@ describe "IO.write" do it "requires mode to be specified in :open_args" do -> { IO.write(@filename, 'hi', open_args: [{encoding: Encoding::UTF_32LE, binmode: true}]) - }.should raise_error(IOError, "not opened for writing") + }.should.raise(IOError, "not opened for writing") IO.write(@filename, 'hi', open_args: ["w", {encoding: Encoding::UTF_32LE, binmode: true}]).should == 8 IO.write(@filename, 'hi', open_args: [{encoding: Encoding::UTF_32LE, binmode: true, mode: "w"}]).should == 8 @@ -159,7 +166,7 @@ describe "IO.write" do it "requires mode to be specified in :open_args even if flags option passed" do -> { IO.write(@filename, 'hi', open_args: [{encoding: Encoding::UTF_32LE, binmode: true, flags: File::CREAT}]) - }.should raise_error(IOError, "not opened for writing") + }.should.raise(IOError, "not opened for writing") IO.write(@filename, 'hi', open_args: ["w", {encoding: Encoding::UTF_32LE, binmode: true, flags: File::CREAT}]).should == 8 IO.write(@filename, 'hi', open_args: [{encoding: Encoding::UTF_32LE, binmode: true, flags: File::CREAT, mode: "w"}]).should == 8 @@ -172,11 +179,11 @@ describe "IO.write" do it "raises ArgumentError if encoding is specified in mode parameter and is given as :encoding option" do -> { IO.write(@filename, 'hi', mode: "w:UTF-16LE:UTF-16BE", encoding: Encoding::UTF_32LE) - }.should raise_error(ArgumentError, "encoding specified twice") + }.should.raise(ArgumentError, "encoding specified twice") -> { IO.write(@filename, 'hi', mode: "w:UTF-16BE", encoding: Encoding::UTF_32LE) - }.should raise_error(ArgumentError, "encoding specified twice") + }.should.raise(ArgumentError, "encoding specified twice") end it "writes the file with the permissions in the :perm parameter" do @@ -203,23 +210,39 @@ describe "IO.write" do rm_r @fifo end - it "writes correctly" do - thr = Thread.new do - IO.read(@fifo) - end - begin - string = "hi" - IO.write(@fifo, string).should == string.length - ensure - thr.join + # rb_cloexec_open() is currently missing a retry on EINTR. + # @ioquatix is looking into fixing it. Quarantined until it's done. + quarantine! do + it "writes correctly" do + thr = Thread.new do + IO.read(@fifo) + end + begin + string = "hi" + IO.write(@fifo, string).should == string.length + ensure + thr.join + end end end end + + ruby_version_is ""..."4.0" do + # https://bugs.ruby-lang.org/issues/19630 + it "warns about deprecation given a path with a pipe" do + -> { + -> { + IO.write("|cat", "xxx") + }.should output_to_fd("xxx") + }.should complain(/IO process creation with a leading '\|'/) + end + end end end describe "IO#write" do it_behaves_like :io_write, :write + it_behaves_like :io_write_transcode, :write it "accepts multiple arguments" do IO.pipe do |r, w| @@ -258,25 +281,23 @@ platform_is :windows do end end -ruby_version_is "3.0" do - describe "IO#write on STDOUT" do - # https://bugs.ruby-lang.org/issues/14413 - platform_is_not :windows do - it "raises SignalException SIGPIPE if the stream is closed instead of Errno::EPIPE like other IOs" do - stderr_file = tmp("stderr") - begin - IO.popen([*ruby_exe, "-e", "loop { puts :ok }"], "r", err: stderr_file) do |io| - io.gets.should == "ok\n" - io.close - end - status = $? - status.should_not.success? - status.should.signaled? - Signal.signame(status.termsig).should == 'PIPE' - File.read(stderr_file).should.empty? - ensure - rm_r stderr_file +describe "IO#write on STDOUT" do + # https://bugs.ruby-lang.org/issues/14413 + platform_is_not :windows do + it "raises SignalException SIGPIPE if the stream is closed instead of Errno::EPIPE like other IOs" do + stderr_file = tmp("stderr") + begin + IO.popen([*ruby_exe, "-e", "loop { puts :ok }"], "r", err: stderr_file) do |io| + io.gets.should == "ok\n" + io.close end + status = $? + status.should_not.success? + status.should.signaled? + Signal.signame(status.termsig).should == 'PIPE' + File.read(stderr_file).should.empty? + ensure + rm_r stderr_file end end end |
