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

Commit 0211a2c

Browse files
committed
Merge pull request #2852 from mikejarema/main
Ensure Bisect::Channel is able to send data successfully when UTF-8 encoding is set
1 parent 99ab284 commit 0211a2c

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

lib/rspec/core/bisect/utilities.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,22 @@ def publish(event, *args)
2929
end
3030

3131
# Wraps a pipe to support sending objects between a child and
32-
# parent process.
32+
# parent process. Where supported, encoding is explicitly
33+
# set to ensure binary data is able to pass from child to
34+
# parent.
3335
# @private
3436
class Channel
37+
if String.method_defined?(:encoding)
38+
MARSHAL_DUMP_ENCODING = Marshal.dump("").encoding
39+
end
40+
3541
def initialize
3642
@read_io, @write_io = IO.pipe
43+
44+
if defined?(MARSHAL_DUMP_ENCODING) && IO.method_defined?(:set_encoding)
45+
# Ensure the pipe can send any content produced by Marshal.dump
46+
@write_io.set_encoding MARSHAL_DUMP_ENCODING
47+
end
3748
end
3849

3950
def send(message)

spec/rspec/core/bisect/utilities_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,41 @@ def foo(notification); end
3636

3737
expect(channel.receive).to eq :value_from_child
3838
end
39+
40+
describe "in a UTF-8 encoding context (where possible)" do
41+
if defined?(Encoding)
42+
around(:each) do |example|
43+
old_external = old_internal = nil
44+
45+
ignoring_warnings do
46+
old_external, Encoding.default_external = Encoding.default_external, Encoding::UTF_8
47+
old_internal, Encoding.default_internal = Encoding.default_internal, Encoding::UTF_8
48+
end
49+
50+
example.run
51+
52+
ignoring_warnings do
53+
Encoding.default_external = old_external
54+
Encoding.default_internal = old_internal
55+
end
56+
end
57+
end
58+
59+
it "successfully sends binary data within a process" do
60+
channel = Bisect::Channel.new
61+
expect { channel.send("\xF8") }.not_to raise_error
62+
end
63+
64+
it "successfully sends binary data from a child process to its parent process" do
65+
channel = Bisect::Channel.new
66+
67+
in_sub_process do
68+
channel.send("\xF8")
69+
end
70+
71+
expect(channel.receive).to eq("\xF8")
72+
end
73+
end
74+
3975
end
4076
end

0 commit comments

Comments
 (0)