From 8cfc2a3d43391cb8043c5f14dcbe95f7febe66e7 Mon Sep 17 00:00:00 2001 From: uu59 Date: Tue, 29 Jul 2014 14:58:27 +0900 Subject: [PATCH] Add spec for lib/ --- spec/file_reverse_reader_spec.rb | 34 ------------- spec/lib/file_reverse_reader_spec.rb | 73 ++++++++++++++++++++++++++++ spec/lib/fluentd-ui_spec.rb | 16 ++++++ 3 files changed, 89 insertions(+), 34 deletions(-) delete mode 100644 spec/file_reverse_reader_spec.rb create mode 100644 spec/lib/file_reverse_reader_spec.rb diff --git a/spec/file_reverse_reader_spec.rb b/spec/file_reverse_reader_spec.rb deleted file mode 100644 index 7beda20..0000000 --- a/spec/file_reverse_reader_spec.rb +++ /dev/null @@ -1,34 +0,0 @@ -require "enumerator" -require 'spec_helper' - -describe FileReverseReader do - describe "#each_line" do - let(:instance) { FileReverseReader.new(io, step) } - let(:io) { File.open(logfile) } - - subject { instance.enum_for(:each_line) } - - context "read at once" do - let(:logfile) { File.expand_path("./spec/support/fixtures/error0.log", Rails.root) } - - context "small file (read at once)" do - let(:step) { File.size(logfile) } - - it { subject.count.should == File.open(logfile).each_line.count } - it "reverse order" do - subject.to_a.should == File.open(logfile).each_line.to_a.map(&:strip).reverse - end - end - - context "large file" do - let(:step) { 2 } - - it { subject.count.should == File.open(logfile).each_line.count } - it "reverse order" do - subject.to_a.should == File.open(logfile).each_line.to_a.map(&:strip).reverse - end - end - end - end -end - diff --git a/spec/lib/file_reverse_reader_spec.rb b/spec/lib/file_reverse_reader_spec.rb new file mode 100644 index 0000000..cc5a1f5 --- /dev/null +++ b/spec/lib/file_reverse_reader_spec.rb @@ -0,0 +1,73 @@ +require "enumerator" +require 'spec_helper' + +describe FileReverseReader do + let(:instance) { FileReverseReader.new(io) } + let(:io) { File.open(logfile) } + + describe "#each_line" do + let(:instance) { FileReverseReader.new(io, step) } + subject { instance.enum_for(:each_line) } + let(:logfile) { File.expand_path("./spec/support/fixtures/error0.log", Rails.root) } + + context "read at once" do + + context "small file (read at once)" do + let(:step) { File.size(logfile) } + + it { subject.count.should == File.open(logfile).each_line.count } + it "reverse order" do + subject.to_a.should == File.open(logfile).each_line.to_a.map(&:strip).reverse + end + end + + context "large file" do + let(:step) { 2 } + + it { subject.count.should == File.open(logfile).each_line.count } + it "reverse order" do + subject.to_a.should == File.open(logfile).each_line.to_a.map(&:strip).reverse + end + end + end + end + + describe "#binary_file?" do + let(:logfile) { File.expand_path("./tmp/log.log", Rails.root) } + before { File.open(logfile, "wb"){|f| f.write content} } + subject { instance.binary_file? } + + context "contain ascii only" do + let(:content) { "ABCDE" } + it { should be_false } + end + + context "contain non-ascii" do + let(:content) { "\x89NG" } + it { should be_true } + end + end + + 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) } + + 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 +end + diff --git a/spec/lib/fluentd-ui_spec.rb b/spec/lib/fluentd-ui_spec.rb index a2fa4c3..dfb5e14 100644 --- a/spec/lib/fluentd-ui_spec.rb +++ b/spec/lib/fluentd-ui_spec.rb @@ -16,4 +16,20 @@ describe FluentdUI do it { should be_truthy } end end + + describe ".fluentd_version" do + before { Fluentd.stub(:instance).and_return(target) } + subject { FluentdUI.fluentd_version } + + context "not setup yet" do + let(:target) { nil } + it { should be_nil } + end + + context "did setup" do + let(:target) { build(:fluentd) } + let(:version) { "1.1.1" } + it { should == target.agent.version } + end + end end