Skip to content

Commit

Permalink
test: Cover Rails.logger
Browse files Browse the repository at this point in the history
Signed-off-by: Ferenc Géczi <[email protected]>
  • Loading branch information
Ferenc- committed Jan 17, 2024
1 parent 6d817c2 commit b0065dc
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
38 changes: 38 additions & 0 deletions test/frameworks/rails_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# (c) Copyright IBM Corp. 2024

require 'test_helper'
require 'rack/test'
require 'rails'

class RailsTest < Minitest::Test
include Rack::Test::Methods
begin
ENV['INSTANA_TEST'] = nil
APP = Rack::Builder.parse_file('test/support/apps/rails_generic/config.ru')
ensure
ENV['INSTANA_TEST'] = 'true'
end

railties_version = Gem::Specification.find_by_name('railties').version
if railties_version < Gem::Version.new('7.1.0')
APP = APP.first
end

def app
APP
end

def test_log
clear_all!

get '/base/log_warning'
assert last_response.ok?

spans = ::Instana.processor.queued_spans
assert spans[0][:data][:log]
log_span = spans[0][:data][:log]

assert_equal "Warn", log_span[:level]
assert_equal "This is a test warning", log_span[:message]
end
end
30 changes: 30 additions & 0 deletions test/support/apps/rails_generic/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# (c) Copyright IBM Corp. 2024

require 'rails'
require 'action_controller/railtie'

class TestControllerApplication < Rails::Application
config.eager_load = 'test'
config.consider_all_requests_local = false
config.secret_key_base = 'test_key'
config.secret_token = 'test_token'

if Rails::VERSION::MAJOR > 5
config.hosts.clear
end

routes.append do
get '/base/log_warning' => 'test_base#log_warning'
end
end

class TestBaseController < ActionController::Base
def log_warning
Rails.logger.warn "This is a test warning"
render plain: 'Test warning logged'
end
end

TestControllerApplication.initialize!

run TestControllerApplication

0 comments on commit b0065dc

Please sign in to comment.