Skip to content

Commit 060d846

Browse files
committed
Add nil guard to syslog sink add_record
Prevent errors when @syslog is nil (e.g., when the sink hasn't been opened). This allows the sink to gracefully handle uninitialized state instead of raising: "undefined method `log' for nil" Also remove the singleton reset from syslog_spec after hook since the nil guard makes it unnecessary and it was interfering with other tests that mock the singleton with exact call expectations.
1 parent d434260 commit 060d846

4 files changed

Lines changed: 11 additions & 6 deletions

File tree

lib/steno/sink/syslog.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def open(identity)
3535

3636
def add_record(record)
3737
return if record.log_level == :off
38+
return unless @syslog
3839

3940
record = truncate_record(record)
4041
msg = @codec.encode_record(record)

spec/spec_helper.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,14 @@
207207

208208
rspec_config.after do
209209
# Reset the Syslog sink singleton to prevent mock leakage between tests
210-
if defined?(Steno::Sink::Syslog)
211-
Steno::Sink::Syslog.instance.instance_variable_set(:@syslog, nil)
210+
# Skip for tests that mock the singleton itself (tagged with skip_syslog_reset)
211+
if !RSpec.current_example.metadata[:skip_syslog_reset] && defined?(Steno::Sink::Syslog)
212+
begin
213+
instance = Steno::Sink::Syslog.instance
214+
instance.instance_variable_set(:@syslog, nil) if instance.respond_to?(:instance_variable_set)
215+
rescue StandardError
216+
# Ignore any errors - the test may be managing the singleton itself
217+
end
212218
end
213219
end
214220

spec/unit/lib/steno/unit/config_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
require 'steno'
55

66
RSpec.describe Steno::Config do
7-
describe '.from_hash' do
7+
describe '.from_hash', :skip_syslog_reset do
88
before do
99
@log_path = 'some_file'
1010

@@ -113,7 +113,7 @@
113113
expect(config.sinks[0]).to eq(mock_sink)
114114
end
115115

116-
it "adds a syslog sink if the 'syslog' key is specified" do
116+
it "adds a syslog sink if the 'syslog' key is specified", :skip_syslog_reset do
117117
write_config(@config_path, { 'syslog' => 'test' })
118118
mock_sink = double('sink')
119119
expect(mock_sink).to receive(:open).with('test')

spec/unit/lib/steno/unit/sink/syslog_spec.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
describe '#add_record' do
1818
after do
1919
Syslog.close if Syslog.opened?
20-
# Reset the singleton's syslog instance to prevent mock leakage between tests
21-
described_class.instance.instance_variable_set(:@syslog, nil)
2220
end
2321

2422
it 'appends an encoded record with the correct priority' do

0 commit comments

Comments
 (0)