Commit e7d3fe44 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Only set the encoding before passing to Ansi2html

parent dac23fa2
...@@ -50,8 +50,6 @@ module Gitlab ...@@ -50,8 +50,6 @@ module Gitlab
end end
def read def read
return unless exist?
stream = Gitlab::Ci::Trace::Stream.new do stream = Gitlab::Ci::Trace::Stream.new do
if current_path if current_path
File.open(current_path, "rb") File.open(current_path, "rb")
......
...@@ -14,9 +14,7 @@ module Gitlab ...@@ -14,9 +14,7 @@ module Gitlab
def initialize def initialize
@stream = yield @stream = yield
@stream.binmode @stream.binmode if @stream
# Ci::Ansi2html::Converter would read from @stream directly
@stream.set_encoding(Encoding.default_external)
end end
def valid? def valid?
...@@ -58,12 +56,14 @@ module Gitlab ...@@ -58,12 +56,14 @@ module Gitlab
end end
def html_with_state(state = nil) def html_with_state(state = nil)
set_encoding_for_ansi2html
::Ci::Ansi2html.convert(stream, state) ::Ci::Ansi2html.convert(stream, state)
end end
def html(last_lines: nil) def html(last_lines: nil)
text = raw(last_lines: last_lines) text = raw(last_lines: last_lines)
stream = StringIO.new(text) stream = StringIO.new(text)
set_encoding_for_ansi2html(stream)
::Ci::Ansi2html.convert(stream).html ::Ci::Ansi2html.convert(stream).html
end end
...@@ -117,6 +117,11 @@ module Gitlab ...@@ -117,6 +117,11 @@ module Gitlab
chunks.join.lines.last(last_lines).join chunks.join.lines.last(last_lines).join
end end
def set_encoding_for_ansi2html(stream = @stream)
# Ci::Ansi2html::Converter would read from @stream directly
stream.set_encoding(Encoding.default_external)
end
end end
end end
end end
......
...@@ -34,12 +34,12 @@ describe Gitlab::Ci::Trace::Stream do ...@@ -34,12 +34,12 @@ describe Gitlab::Ci::Trace::Stream do
end end
context 'when the trace contains ANSI sequence and Unicode' do context 'when the trace contains ANSI sequence and Unicode' do
let(:io) do let(:stream) do
File.open(expand_fixture_path('trace/ansi-sequence-and-unicode')) described_class.new do
File.open(expand_fixture_path('trace/ansi-sequence-and-unicode'))
end
end end
let(:stream) { described_class.new { io } }
it 'forwards to the next linefeed, case 1' do it 'forwards to the next linefeed, case 1' do
stream.limit(7) stream.limit(7)
...@@ -60,11 +60,11 @@ describe Gitlab::Ci::Trace::Stream do ...@@ -60,11 +60,11 @@ describe Gitlab::Ci::Trace::Stream do
# See https://gitlab.com/gitlab-org/gitlab-ce/issues/30796 # See https://gitlab.com/gitlab-org/gitlab-ce/issues/30796
it 'reads in binary, output as Encoding.default_external' do it 'reads in binary, output as Encoding.default_external' do
stream.limit(29) stream.limit(52)
result = io.read # Ci::Ansi2html::Converter would read with each_line result = stream.html
expect(result).to eq("\e[01;32m許功蓋\e[0m\n") expect(result.lines.first).to eq("ヾ(´༎ຶД༎ຶ`)ノ<br><span class=\"term-fg-green\">許功蓋</span><br>")
expect(result.encoding).to eq(Encoding.default_external) expect(result.encoding).to eq(Encoding.default_external)
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment