Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Ensure Bisect::Channel is able to send data successfully when UTF-8 encoding is set #2852

Merged
merged 1 commit into from
Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/rspec/core/bisect/utilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,22 @@ def publish(event, *args)
end

# Wraps a pipe to support sending objects between a child and
# parent process.
# parent process. Where supported, encoding is explicitly
# set to ensure binary data is able to pass from child to
# parent.
# @private
class Channel
if String.method_defined?(:encoding)
MARSHAL_DUMP_ENCODING = Marshal.dump("").encoding
end

def initialize
@read_io, @write_io = IO.pipe

if defined?(MARSHAL_DUMP_ENCODING) && IO.method_defined?(:set_encoding)
# Ensure the pipe can send any content produced by Marshal.dump
@write_io.set_encoding MARSHAL_DUMP_ENCODING
end
end

def send(message)
Expand Down
36 changes: 36 additions & 0 deletions spec/rspec/core/bisect/utilities_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,41 @@ def foo(notification); end

expect(channel.receive).to eq :value_from_child
end

describe "in a UTF-8 encoding context (where possible)" do
if defined?(Encoding)
around(:each) do |example|
old_external = old_internal = nil

ignoring_warnings do
old_external, Encoding.default_external = Encoding.default_external, Encoding::UTF_8
old_internal, Encoding.default_internal = Encoding.default_internal, Encoding::UTF_8
end

example.run

ignoring_warnings do
Encoding.default_external = old_external
Encoding.default_internal = old_internal
end
end
end

it "successfully sends binary data within a process" do
channel = Bisect::Channel.new
expect { channel.send("\xF8") }.not_to raise_error
end

it "successfully sends binary data from a child process to its parent process" do
channel = Bisect::Channel.new

in_sub_process do
channel.send("\xF8")
end

expect(channel.receive).to eq("\xF8")
end
end

end
end