Skip to content

Commit

Permalink
Preview for utf-8 file. fix #121
Browse files Browse the repository at this point in the history
  • Loading branch information
uu59 committed Dec 17, 2014
1 parent 9d34399 commit b2eb590
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
3 changes: 2 additions & 1 deletion lib/file_reverse_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ def tail(limit = 10)

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

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
Expand Down
44 changes: 31 additions & 13 deletions spec/lib/file_reverse_reader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,41 @@

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 }
end
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 "50" do
let(:count) { 50 }
it { subject.to_a.size.should == count }
context "over log lines" do
let(:count) { log_lines + 100 }
it { subject.to_a.size.should == log_lines }
end
end

context "over log lines" do
let(:count) { log_lines + 100 }
it { subject.to_a.size.should == log_lines }
describe "non-ascii encoding" do
subject { instance.tail }

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
Expand Down

0 comments on commit b2eb590

Please sign in to comment.