|
7 | 7 |
|
8 | 8 | subject { described_class.new(options) } |
9 | 9 |
|
10 | | - let(:tmpfile) { Tempfile.new('logstash-spec-output-csv').path } |
11 | | - let(:output) { File.readlines(tmpfile) } |
12 | | - let(:csv_output) { CSV.read(tmpfile) } |
| 10 | + let(:tmpfile) { Tempfile.new('logstash-spec-output-csv') } |
| 11 | + let(:tmpfile_path) { tmpfile.path } |
| 12 | + let(:output) { File.readlines(tmpfile_path) } |
| 13 | + let(:csv_output) { CSV.read(tmpfile_path) } |
13 | 14 |
|
14 | 15 | before(:each) do |
15 | 16 | subject.register |
16 | 17 | subject.multi_receive(events) |
17 | 18 | end |
18 | 19 |
|
| 20 | + # Explicitly close and unlink temp file to ensure immediate cleanup (Tempfile will cleanup eventually) |
| 21 | + after(:each) do |
| 22 | + tmpfile.close |
| 23 | + tmpfile.unlink |
| 24 | + end |
| 25 | + |
19 | 26 | context "when configured with a single field" do |
20 | 27 | let(:events) { [ LogStash::Event.new("foo" => "bar") ] } |
21 | | - let(:options) { { "path" => tmpfile, "fields" => "foo" } } |
| 28 | + let(:options) { { "path" => tmpfile_path, "fields" => "foo" } } |
22 | 29 | it "writes a single field to a csv file" do |
23 | 30 | expect(output.count).to eq(1) |
24 | 31 | expect(output.first).to eq("bar\n") |
|
30 | 37 | [ LogStash::Event.new("foo" => "bar", "baz" => "quux"), |
31 | 38 | LogStash::Event.new("foo" => "bar", "baz" => "quux") ] |
32 | 39 | end |
33 | | - let(:options) { { "path" => tmpfile, "fields" => ["foo", "baz"] } } |
| 40 | + let(:options) { { "path" => tmpfile_path, "fields" => ["foo", "baz"] } } |
34 | 41 | it "writes a line per event " do |
35 | 42 | expect(output.count).to eq(2) |
36 | 43 | end |
|
44 | 51 | let(:events) do |
45 | 52 | [ LogStash::Event.new("foo" => "bar", "baz" => "quux") ] |
46 | 53 | end |
47 | | - let(:options) { { "path" => tmpfile, "fields" => ["foo", "not_there", "baz"] } } |
| 54 | + let(:options) { { "path" => tmpfile_path, "fields" => ["foo", "not_there", "baz"] } } |
48 | 55 |
|
49 | 56 | it "skips on the resulting line" do |
50 | 57 | expect(output.size).to eq(1) |
|
56 | 63 | let(:events) do |
57 | 64 | [ LogStash::Event.new("foo" => "one,two", "baz" => "quux") ] |
58 | 65 | end |
59 | | - let(:options) { { "path" => tmpfile, "fields" => ["foo", "baz"] } } |
| 66 | + let(:options) { { "path" => tmpfile_path, "fields" => ["foo", "baz"] } } |
60 | 67 | it "correctly escapes them" do |
61 | 68 | expect(output.size).to eq(1) |
62 | 69 | expect(output[0]).to eq("\"one,two\",quux\n") |
|
67 | 74 | let(:events) do |
68 | 75 | [ LogStash::Event.new("foo" => 'one\ntwo', "baz" => "quux") ] |
69 | 76 | end |
70 | | - let(:options) { { "path" => tmpfile, "fields" => ["foo", "baz"] } } |
| 77 | + let(:options) { { "path" => tmpfile_path, "fields" => ["foo", "baz"] } } |
71 | 78 | it "correctly escapes them" do |
72 | 79 | expect(csv_output.size).to eq(1) |
73 | 80 | expect(csv_output[0]).to eq(['one\ntwo', 'quux']) |
|
78 | 85 | let(:events) do |
79 | 86 | [ LogStash::Event.new("foo" => {"one" => "two"}, "baz" => "quux") ] |
80 | 87 | end |
81 | | - let(:options) { { "path" => tmpfile, "fields" => ["foo", "baz"] } } |
| 88 | + let(:options) { { "path" => tmpfile_path, "fields" => ["foo", "baz"] } } |
82 | 89 |
|
83 | 90 | it "are written as json" do |
84 | 91 | expect(csv_output.size).to eq(1) |
|
89 | 96 | let(:events) do |
90 | 97 | [ LogStash::Event.new("foo" => {"one" => "two"}, "baz" => "quux") ] |
91 | 98 | end |
92 | | - let(:options) { { "path" => tmpfile, "fields" => ["[foo][one]", "baz"] } } |
| 99 | + let(:options) { { "path" => tmpfile_path, "fields" => ["[foo][one]", "baz"] } } |
93 | 100 |
|
94 | 101 | it "are referenced using field references" do |
95 | 102 | expect(csv_output.size).to eq(1) |
|
102 | 109 | let(:events) do |
103 | 110 | [ LogStash::Event.new("foo" => {"one" => "two"}, "baz" => "quux") ] |
104 | 111 | end |
105 | | - let(:options) { { "path" => tmpfile, "fields" => ["[foo][missing]", "baz"] } } |
| 112 | + let(:options) { { "path" => tmpfile_path, "fields" => ["[foo][missing]", "baz"] } } |
106 | 113 |
|
107 | 114 | it "are blank" do |
108 | 115 | expect(output.size).to eq(1) |
|
114 | 121 | let(:events) do |
115 | 122 | [ LogStash::Event.new("foo" => "one", "baz" => "two") ] |
116 | 123 | end |
117 | | - let(:options) { { "path" => tmpfile, "fields" => ["foo", "baz"], "csv_options" => {"col_sep" => "|" } } } |
| 124 | + let(:options) { { "path" => tmpfile_path, "fields" => ["foo", "baz"], "csv_options" => {"col_sep" => "|" } } } |
118 | 125 |
|
119 | 126 | it "uses separator in output" do |
120 | 127 | expect(output.size).to eq(1) |
|
127 | 134 | [ LogStash::Event.new("foo" => "one", "baz" => "two"), |
128 | 135 | LogStash::Event.new("foo" => "one", "baz" => "two") ] |
129 | 136 | end |
130 | | - let(:options) { { "path" => tmpfile, "fields" => ["foo", "baz"], "csv_options" => {"col_sep" => "|", "row_sep" => "\t" } } } |
| 137 | + let(:options) { { "path" => tmpfile_path, "fields" => ["foo", "baz"], "csv_options" => {"col_sep" => "|", "row_sep" => "\t" } } } |
131 | 138 |
|
132 | 139 | it "uses separator in output" do |
133 | 140 | expect(output.size).to eq(1) |
|
149 | 156 | [ LogStash::Event.new(event_data) ] |
150 | 157 | end |
151 | 158 |
|
152 | | - let(:options) { { "path" => tmpfile, "fields" => ["f1", "f2", "f3", "f4", "f5"] } } |
| 159 | + let(:options) { { "path" => tmpfile_path, "fields" => ["f1", "f2", "f3", "f4", "f5"] } } |
153 | 160 | it "escapes them correctly" do |
154 | 161 | expect(csv_output.size).to eq(1) |
155 | 162 | expect(csv_output[0][0]).to eq("1+1") |
|
0 commit comments