Skip to content

Commit 9227c40

Browse files
committed
feat: ensure that all data written by MonitoredPipe is ASCII-8BIT encoded
1 parent 3270b75 commit 9227c40

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

lib/process_executer/monitored_pipe.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ def initialize(redirection_destination, chunk_size: 100_000)
128128
@condition_variable = ConditionVariable.new
129129
@chunk_size = chunk_size
130130
@pipe_reader, @pipe_writer = IO.pipe
131+
132+
# Set the encoding of the pipe reader to ASCII_8BIT. This is not strictly
133+
# necessary since read_nonblock always returns a String where encoding is
134+
# Encoding::ASCII_8BIT, but it is a good practice to explicitly set the
135+
# encoding.
136+
pipe_reader.set_encoding(Encoding::ASCII_8BIT)
137+
131138
@state = :open
132139
@thread = start_monitoring_thread
133140

@@ -400,6 +407,7 @@ def monitor
400407
# @return [void]
401408
# @api private
402409
def monitor_pipe
410+
# read_nonblock always returns a String where encoding is Encoding::ASCII_8BIT
403411
new_data = pipe_reader.read_nonblock(chunk_size)
404412
write_data(new_data)
405413
rescue IO::WaitReadable

spec/process_executer/monitored_pipe_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,4 +744,28 @@
744744
end
745745
end
746746
end
747+
748+
describe 'encoding of output' do
749+
let(:output_writer) do
750+
Class.new do
751+
def initialize
752+
@data = []
753+
end
754+
755+
attr_reader :data
756+
757+
def write(new_data)
758+
@data << new_data
759+
end
760+
end.new
761+
end
762+
763+
it 'should write data with BINARY encoding' do
764+
monitored_pipe.write('違いを生み出すサンプルテキスト'.encode(Encoding::UTF_8))
765+
monitored_pipe.write('이것은 파일이다'.encode(Encoding::UTF_8))
766+
monitored_pipe.close
767+
expect(output_writer.data.size).to eq(2)
768+
expect(output_writer.data).to all(satisfy { |d| d.encoding == Encoding::BINARY })
769+
end
770+
end
747771
end

0 commit comments

Comments
 (0)