From 86d17382531ad0035a79d922c30ff15f1dfc6e2d Mon Sep 17 00:00:00 2001 From: Mashhur Date: Wed, 29 Oct 2025 15:23:25 -0700 Subject: [PATCH 1/2] Fix the no file or dir CI issue that temfile might be loosing its ref. --- spec/outputs/csv_spec.rb | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/spec/outputs/csv_spec.rb b/spec/outputs/csv_spec.rb index d747639..dc07f69 100644 --- a/spec/outputs/csv_spec.rb +++ b/spec/outputs/csv_spec.rb @@ -7,18 +7,25 @@ subject { described_class.new(options) } - let(:tmpfile) { Tempfile.new('logstash-spec-output-csv').path } - 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 + # intentionally close and unlink to make sure the file will have always reference after tmpfile.path + 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") @@ -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 @@ -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) @@ -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") @@ -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']) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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") From 2cd95b90cc4ff65933ed7e1974252f70d8b56b84 Mon Sep 17 00:00:00 2001 From: Mashhur <99575341+mashhurs@users.noreply.github.com> Date: Wed, 29 Oct 2025 16:00:17 -0700 Subject: [PATCH 2/2] Update spec/outputs/csv_spec.rb Refine the comment for better understanding. Co-authored-by: Cas Donoghue --- spec/outputs/csv_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/outputs/csv_spec.rb b/spec/outputs/csv_spec.rb index dc07f69..890dae0 100644 --- a/spec/outputs/csv_spec.rb +++ b/spec/outputs/csv_spec.rb @@ -17,7 +17,7 @@ subject.multi_receive(events) end - # intentionally close and unlink to make sure the file will have always reference after tmpfile.path + # Explicitly close and unlink temp file to ensure immediate cleanup (Tempfile will cleanup eventually) after(:each) do tmpfile.close tmpfile.unlink