Коммит 7c1abc2c создал по автору Lin Jen-Shin's avatar Lin Jen-Shin
Просмотр файлов

Use a buffer for Ansi2html so it's not affecting Stream

Fixes https://sentry.gitlap.com/gitlab/gitlabcom/issues/27545/
владелец ec9f6180
......@@ -14,14 +14,7 @@ class Stream
def initialize
@stream = yield
if @stream
@stream.binmode
# Ci::Ansi2html::Converter would read from @stream directly,
# using @stream.each_line to be specific. It's safe to set
# the encoding here because IO#seek(bytes) and IO#read(bytes)
# are not characters based, so encoding doesn't matter to them.
@stream.set_encoding(Encoding.default_external)
end
@stream.binmode if @stream
end
def valid?
......@@ -63,13 +56,14 @@ def raw(last_lines: nil)
end
def html_with_state(state = nil)
::Ci::Ansi2html.convert(stream, state)
buffer = create_stream_for_ansi2html(stream) if stream
::Ci::Ansi2html.convert(buffer, state)
end
def html(last_lines: nil)
text = raw(last_lines: last_lines)
stream = StringIO.new(text)
::Ci::Ansi2html.convert(stream).html
buffer = create_stream_for_ansi2html(text)
::Ci::Ansi2html.convert(buffer).html
end
def extract_coverage(regex)
......@@ -122,6 +116,24 @@ def read_last_lines(last_lines)
chunks.join.lines.last(last_lines).join
end
def create_stream_for_ansi2html(io_or_string)
buffer = StringIO.new
case io_or_string
when IO, StringIO
IO.copy_stream(io_or_string, buffer)
when String
buffer.string = io_or_string
else
raise TypeError,
"should be an IO or String, but got: #{io_or_string.class}"
end
buffer.set_encoding(Encoding.default_external)
buffer.rewind
buffer
end
end
end
end
......
......@@ -71,9 +71,13 @@
end
describe '#append' do
let(:tempfile) { Tempfile.new }
let(:stream) do
described_class.new do
StringIO.new("12345678")
tempfile.write("12345678")
tempfile.rewind
tempfile
end
end
......@@ -84,6 +88,17 @@
expect(stream.size).to eq(6)
expect(stream.raw).to eq("123489")
end
it 'appends in binary mode' do
'😺'.force_encoding('ASCII-8BIT').each_char.with_index do |byte, offset|
stream.append(byte, offset)
end
stream.seek(0)
expect(stream.size).to eq(4)
expect(stream.raw).to eq('😺')
end
end
describe '#set' do
......
Поддерживает Markdown
0% или .
You are about to add 0 people to the discussion. Proceed with caution.
Сначала завершите редактирование этого сообщения!
Пожалуйста, зарегистрируйтесь или чтобы прокомментировать