diff --git a/lib/file_reverse_reader.rb b/lib/file_reverse_reader.rb index ba95cba..caf4b63 100644 --- a/lib/file_reverse_reader.rb +++ b/lib/file_reverse_reader.rb @@ -36,7 +36,7 @@ class FileReverseReader def binary_file? sample = io.read(1024) || "" - !sample.force_encoding('us-ascii').valid_encoding? + !sample.force_encoding('utf-8').valid_encoding? ensure io.rewind end @@ -44,6 +44,7 @@ class FileReverseReader private def split_each_line(buf, &block) + return unless buf.force_encoding('utf-8').valid_encoding? buf.split($/).reverse.each do |line| block.call(line) end diff --git a/spec/lib/file_reverse_reader_spec.rb b/spec/lib/file_reverse_reader_spec.rb index 9f6b481..169a4e8 100644 --- a/spec/lib/file_reverse_reader_spec.rb +++ b/spec/lib/file_reverse_reader_spec.rb @@ -50,23 +50,41 @@ describe FileReverseReader do describe "#tail" do let(:logfile) { File.expand_path("./tmp/log.log", Rails.root) } - let(:log_lines) { 100 } - before { File.open(logfile, "w"){|f| f.write("foo\n" * log_lines) } } - subject { instance.tail(count) } + before { File.open(logfile, "wb"){|f| f.write(content) } } - context "2" do - let(:count) { 2 } - it { subject.to_a.size.should == count } + describe "count" do + let(:log_lines) { 100 } + let(:content) { "foo\n" * log_lines } + subject { instance.tail(count) } + + context "2" do + let(:count) { 2 } + it { subject.to_a.size.should == count } + end + + context "50" do + let(:count) { 50 } + it { subject.to_a.size.should == count } + end + + context "over log lines" do + let(:count) { log_lines + 100 } + it { subject.to_a.size.should == log_lines } + end end - context "50" do - let(:count) { 50 } - it { subject.to_a.size.should == count } - end + describe "non-ascii encoding" do + subject { instance.tail } - context "over log lines" do - let(:count) { log_lines + 100 } - it { subject.to_a.size.should == log_lines } + context "compatible with utf-8" do + let(:content) { "utf8あいう\n" } + it { subject.to_a.should == [content.strip] } + end + + context "incompatible with utf-8" do + let(:content) { "eucあいう\n".encode('euc-jp') } + it { subject.to_a.should == [] } + end end end end