Skip to content

Commit 3ab309d

Browse files
authored
Merge pull request #13 from hhoopes/hlh/rails-7.1-splatting
Disallow Formatter from passing along empty arrays further downstream
2 parents c995180 + c1ac1d0 commit 3ab309d

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

lib/fluent_logger_rails/tagged_hash_formatter.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,10 @@ def format_severity(severity)
4545
def tagged(*tags)
4646
tags = tags.flatten.compact if tags.is_a?(Array)
4747

48-
add_tags(*tags) if tags
49-
48+
add_tags(*tags) if tags.present?
5049
yield(self)
5150
ensure
52-
remove_tags(*tags) if tags
51+
remove_tags(*tags) if tags.present?
5352
end
5453

5554
def add_tags(*tags)

spec/integration/fluent_logger_rails/tagged_hash_formatter_spec.rb

+11-9
Original file line numberDiff line numberDiff line change
@@ -38,37 +38,39 @@
3838

3939
context 'nil tag' do
4040
it 'does not attempt to process the tags' do
41-
expect(formatter).not_to(receive(:remove_tags).with(anything))
41+
expect_any_instance_of(described_class).not_to receive(:remove_tags)
4242

43-
formatter.tagged(tags) { expect(formatter.current_tags).to(eq({ tags: [] })) }
43+
formatter.tagged(tags) { expect(formatter.current_tags).to eq({}) }
4444
end
4545
end
4646

4747
context 'with a nested array of nil tags' do
4848
let(:tags) { [nil] }
4949

5050
it 'does not attempt to process the tags' do
51-
expect(formatter).not_to(receive(:remove_tags).with(anything))
51+
expect_any_instance_of(described_class).not_to receive(:remove_tags)
5252

53-
formatter.tagged([tags]) { expect(formatter.current_tags).to(eq({ tags: [] })) }
53+
formatter.tagged([tags]) { expect(formatter.current_tags).to eq({}) }
5454
end
5555
end
5656

5757
context 'with a string tag' do
5858
let(:tags) { 'tag' }
5959

60-
it 'adds the tag' do
61-
formatter.tagged(tags) { expect(formatter.current_tags).to(eq({ tags: [tags] })) }
60+
it 'adds and removes the tag' do
61+
expect_any_instance_of(described_class).to receive(:remove_tags).with(tags)
62+
63+
formatter.tagged(tags) { expect(formatter.current_tags).to eq({ tags: [tags] }) }
6264
end
6365
end
6466

6567
context 'with a hash' do
6668
let(:tags) { { port: 80, host: '127.0.0.1' } }
6769

6870
it 'adds the tags' do
69-
formatter.tagged(**tags) do
70-
expect(formatter.current_tags).to(eq(tags))
71-
end
71+
expect_any_instance_of(described_class).to receive(:remove_tags).with(tags)
72+
73+
formatter.tagged(**tags) { expect(formatter.current_tags).to eq(tags) }
7274
end
7375
end
7476
end

0 commit comments

Comments
 (0)