Skip to content

Commit

Permalink
DEV: Don’t replace Rails logger in specs
Browse files Browse the repository at this point in the history
Instead of replacing the Rails logger in specs, we can instead use
broadcast_to which has been introduced in Rails 7.
  • Loading branch information
Flink committed Nov 13, 2024
1 parent ff69ece commit 016a5aa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
16 changes: 8 additions & 8 deletions spec/lib/discourse_activity_pub/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
describe "#log" do
let!(:default_message) { "log message" }
let!(:prefixed_message) { "#{described_class::PREFIX} #{default_message}" }
let(:fake_logger) { FakeLogger.new }

before do
@orig_rails_logger = Rails.logger
@orig_ap_logger = DiscourseActivityPub::AP.logger
Rails.logger = @rails_logger = FakeLogger.new
Rails.logger.broadcast_to(fake_logger)
DiscourseActivityPub::AP.logger = @ap_logger = FakeLogger.new
freeze_time
end

after do
Rails.logger = @orig_rails_logger
Rails.logger.stop_broadcasting_to(fake_logger)
DiscourseActivityPub::AP.logger = @orig_ap_logger
end

Expand All @@ -27,7 +27,7 @@ def perform(type: :error, message: default_message, json: {})

it "does not log anything" do
expect(perform).to eq(nil)
expect(@rails_logger.errors.present?).to eq(false)
expect(fake_logger.errors.present?).to eq(false)
expect(@ap_logger.errors.present?).to eq(false)
end
end
Expand All @@ -41,7 +41,7 @@ def perform(type: :error, message: default_message, json: {})

it "logs a prefixed message in rails" do
perform
expect(@rails_logger.errors.first).to eq(prefixed_message)
expect(fake_logger.errors.first).to eq(prefixed_message)
end

it "does not log anything in activitypub" do
Expand All @@ -62,7 +62,7 @@ def perform(type: :error, message: default_message, json: {})

it "does not add anything to the rails log" do
perform(json: json)
expect(@rails_logger.errors.first).to eq(prefixed_message)
expect(fake_logger.errors.first).to eq(prefixed_message)
end

it "adds a YAML representation of the JSON to the activitypub log" do
Expand All @@ -83,14 +83,14 @@ def perform(type: :error, message: default_message, json: {})

it "does not add anything to the rails log" do
perform(json: json)
expect(@rails_logger.errors.first).to eq(prefixed_message)
expect(fake_logger.errors.first).to eq(prefixed_message)
end
end

context "when not in a development environment" do
it "adds a YAML representation of the JSON to the rails log" do
perform(json: json)
expect(@rails_logger.errors.first).to eq("#{prefixed_message}\n#{json.to_yaml}")
expect(fake_logger.errors.first).to eq("#{prefixed_message}\n#{json.to_yaml}")
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/plugin_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,13 @@ def expect_post(returns: true)
end

def setup_logging
@fake_logger = FakeLogger.new
SiteSetting.activity_pub_verbose_logging = true
@orig_logger = Rails.logger
Rails.logger = @fake_logger = FakeLogger.new
Rails.logger.broadcast_to(@fake_logger)
end

def teardown_logging
Rails.logger = @orig_logger
Rails.logger.stop_broadcasting_to(@fake_logger)
SiteSetting.activity_pub_verbose_logging = false
end

Expand Down

0 comments on commit 016a5aa

Please sign in to comment.