Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions spec/outputs/csv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@

subject { described_class.new(options) }

let(:tmpfile) { Tempfile.new('logstash-spec-output-csv').path }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yeah looks like the bug was that the a new Tempfile was created (side effect of actually making the file) but there was no reference to it because this was simply reference to the path. So it was free to be cleaned up. Keeping the reference to the Tempfile object should solve it.

let(:output) { File.readlines(tmpfile) }
let(:csv_output) { CSV.read(tmpfile) }
let(:tmpfile) { Tempfile.new('logstash-spec-output-csv') }
let(:tmpfile_path) { tmpfile.path }
let(:output) { File.readlines(tmpfile_path) }
let(:csv_output) { CSV.read(tmpfile_path) }

before(:each) do
subject.register
subject.multi_receive(events)
end

# Explicitly close and unlink temp file to ensure immediate cleanup (Tempfile will cleanup eventually)
after(:each) do
tmpfile.close
tmpfile.unlink
end

context "when configured with a single field" do
let(:events) { [ LogStash::Event.new("foo" => "bar") ] }
let(:options) { { "path" => tmpfile, "fields" => "foo" } }
let(:options) { { "path" => tmpfile_path, "fields" => "foo" } }
it "writes a single field to a csv file" do
expect(output.count).to eq(1)
expect(output.first).to eq("bar\n")
Expand All @@ -30,7 +37,7 @@
[ LogStash::Event.new("foo" => "bar", "baz" => "quux"),
LogStash::Event.new("foo" => "bar", "baz" => "quux") ]
end
let(:options) { { "path" => tmpfile, "fields" => ["foo", "baz"] } }
let(:options) { { "path" => tmpfile_path, "fields" => ["foo", "baz"] } }
it "writes a line per event " do
expect(output.count).to eq(2)
end
Expand All @@ -44,7 +51,7 @@
let(:events) do
[ LogStash::Event.new("foo" => "bar", "baz" => "quux") ]
end
let(:options) { { "path" => tmpfile, "fields" => ["foo", "not_there", "baz"] } }
let(:options) { { "path" => tmpfile_path, "fields" => ["foo", "not_there", "baz"] } }

it "skips on the resulting line" do
expect(output.size).to eq(1)
Expand All @@ -56,7 +63,7 @@
let(:events) do
[ LogStash::Event.new("foo" => "one,two", "baz" => "quux") ]
end
let(:options) { { "path" => tmpfile, "fields" => ["foo", "baz"] } }
let(:options) { { "path" => tmpfile_path, "fields" => ["foo", "baz"] } }
it "correctly escapes them" do
expect(output.size).to eq(1)
expect(output[0]).to eq("\"one,two\",quux\n")
Expand All @@ -67,7 +74,7 @@
let(:events) do
[ LogStash::Event.new("foo" => 'one\ntwo', "baz" => "quux") ]
end
let(:options) { { "path" => tmpfile, "fields" => ["foo", "baz"] } }
let(:options) { { "path" => tmpfile_path, "fields" => ["foo", "baz"] } }
it "correctly escapes them" do
expect(csv_output.size).to eq(1)
expect(csv_output[0]).to eq(['one\ntwo', 'quux'])
Expand All @@ -78,7 +85,7 @@
let(:events) do
[ LogStash::Event.new("foo" => {"one" => "two"}, "baz" => "quux") ]
end
let(:options) { { "path" => tmpfile, "fields" => ["foo", "baz"] } }
let(:options) { { "path" => tmpfile_path, "fields" => ["foo", "baz"] } }

it "are written as json" do
expect(csv_output.size).to eq(1)
Expand All @@ -89,7 +96,7 @@
let(:events) do
[ LogStash::Event.new("foo" => {"one" => "two"}, "baz" => "quux") ]
end
let(:options) { { "path" => tmpfile, "fields" => ["[foo][one]", "baz"] } }
let(:options) { { "path" => tmpfile_path, "fields" => ["[foo][one]", "baz"] } }

it "are referenced using field references" do
expect(csv_output.size).to eq(1)
Expand All @@ -102,7 +109,7 @@
let(:events) do
[ LogStash::Event.new("foo" => {"one" => "two"}, "baz" => "quux") ]
end
let(:options) { { "path" => tmpfile, "fields" => ["[foo][missing]", "baz"] } }
let(:options) { { "path" => tmpfile_path, "fields" => ["[foo][missing]", "baz"] } }

it "are blank" do
expect(output.size).to eq(1)
Expand All @@ -114,7 +121,7 @@
let(:events) do
[ LogStash::Event.new("foo" => "one", "baz" => "two") ]
end
let(:options) { { "path" => tmpfile, "fields" => ["foo", "baz"], "csv_options" => {"col_sep" => "|" } } }
let(:options) { { "path" => tmpfile_path, "fields" => ["foo", "baz"], "csv_options" => {"col_sep" => "|" } } }

it "uses separator in output" do
expect(output.size).to eq(1)
Expand All @@ -127,7 +134,7 @@
[ LogStash::Event.new("foo" => "one", "baz" => "two"),
LogStash::Event.new("foo" => "one", "baz" => "two") ]
end
let(:options) { { "path" => tmpfile, "fields" => ["foo", "baz"], "csv_options" => {"col_sep" => "|", "row_sep" => "\t" } } }
let(:options) { { "path" => tmpfile_path, "fields" => ["foo", "baz"], "csv_options" => {"col_sep" => "|", "row_sep" => "\t" } } }

it "uses separator in output" do
expect(output.size).to eq(1)
Expand All @@ -149,7 +156,7 @@
[ LogStash::Event.new(event_data) ]
end

let(:options) { { "path" => tmpfile, "fields" => ["f1", "f2", "f3", "f4", "f5"] } }
let(:options) { { "path" => tmpfile_path, "fields" => ["f1", "f2", "f3", "f4", "f5"] } }
it "escapes them correctly" do
expect(csv_output.size).to eq(1)
expect(csv_output[0][0]).to eq("1+1")
Expand Down