From cc86538cced16df3f21da0c0779dff352885105f Mon Sep 17 00:00:00 2001 From: Aleksandar Ivanov Date: Fri, 9 Feb 2024 17:03:31 +0200 Subject: [PATCH] Generate rubocop_todo and autocorrect the specs The main part of the project will be fixed in a separate PR. --- .rubocop.yml | 4 + .rubocop_todo.yml | 206 ++++++++++++++++++ spec/integration/rails/action_mailer_spec.rb | 2 + spec/integration/rails/railtie_spec.rb | 4 +- spec/integration/rails/request_spec.rb | 6 +- spec/integration/sidekiq5_spec.rb | 28 +-- spec/integration/sidekiq60_spec.rb | 6 +- spec/integration/sidekiq61_spec.rb | 10 +- spec/integration/sidekiq65_spec.rb | 2 +- spec/integration/sinatra_spec.rb | 12 +- spec/loga/sidekiq5/job_logger_spec.rb | 2 + spec/loga/sidekiq6/job_logger_spec.rb | 4 +- spec/loga/sidekiq7/job_logger_spec.rb | 4 +- spec/loga/sidekiq_spec.rb | 6 +- spec/spec_helper.rb | 9 +- spec/support/gethostname_shared.rb | 4 +- spec/support/helpers.rb | 2 + spec/support/request_spec.rb | 164 +++++++------- spec/support/timecop_shared.rb | 4 +- spec/unit/loga/configuration_spec.rb | 20 +- spec/unit/loga/event_spec.rb | 6 +- .../loga/formatters/gelf_formatter_spec.rb | 28 +-- .../loga/formatters/simple_formatter_spec.rb | 12 +- .../log_subscribers/action_mailer_spec.rb | 10 +- spec/unit/loga/parameter_filter_spec.rb | 2 + spec/unit/loga/rack/logger_spec.rb | 34 +-- spec/unit/loga/rack/request_spec.rb | 2 + .../loga/service_version_strategies_spec.rb | 2 + spec/unit/loga/utilities_spec.rb | 2 + spec/unit/loga_spec.rb | 4 +- 30 files changed, 432 insertions(+), 169 deletions(-) create mode 100644 .rubocop_todo.yml diff --git a/.rubocop.yml b/.rubocop.yml index 24d7ff2..cf02cab 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,4 +1,8 @@ +inherit_from: .rubocop_todo.yml + AllCops: + TargetRubyVersion: 2.7 + NewCops: enable Exclude: - '*.gemspec' - 'gemfiles/*' diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..97bfa29 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,206 @@ +# This configuration was generated by +# `rubocop --auto-gen-config --exclude-limit 10000000` +# on 2024-02-09 15:03:07 UTC using RuboCop version 1.60.2. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 5 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: TreatCommentsAsGroupSeparators, ConsiderPunctuation, Include. +# Include: **/*.gemfile, **/Gemfile, **/gems.rb +Bundler/OrderedGems: + Exclude: + - 'Gemfile' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Layout/EmptyLineAfterGuardClause: + Exclude: + - 'lib/loga/formatters/gelf_formatter.rb' + +# Offense count: 34 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowMultipleStyles, EnforcedHashRocketStyle, EnforcedColonStyle, EnforcedLastArgumentHashStyle. +# SupportedHashRocketStyles: key, separator, table +# SupportedColonStyles: key, separator, table +# SupportedLastArgumentHashStyles: always_inspect, always_ignore, ignore_implicit, ignore_explicit +Layout/HashAlignment: + Exclude: + - 'lib/loga/configuration.rb' + - 'lib/loga/formatters/gelf_formatter.rb' + - 'lib/loga/rack/logger.rb' + - 'lib/loga/railtie.rb' + - 'lib/loga/sidekiq6/job_logger.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowDoxygenCommentStyle, AllowGemfileRubyComment. +Layout/LeadingCommentSpace: + Exclude: + - 'lib/loga/railtie.rb' + +# Offense count: 2 +# Configuration parameters: AllowComments, AllowEmptyLambdas. +Lint/EmptyBlock: + Exclude: + - 'Appraisals' + - 'spec/integration/sinatra_spec.rb' + +# Offense count: 1 +# Configuration parameters: AllowedParentClasses. +Lint/MissingSuper: + Exclude: + - 'lib/loga/formatters/gelf_formatter.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Lint/RedundantCopDisableDirective: + Exclude: + - 'lib/loga/railtie.rb' + +# Offense count: 2 +# This cop supports safe autocorrection (--autocorrect). +Lint/SendWithMixinArgument: + Exclude: + - 'lib/loga/railtie.rb' + +# Offense count: 1 +# Configuration parameters: AllowedMethods, AllowedPatterns. +Metrics/CyclomaticComplexity: + Max: 8 + +# Offense count: 1 +# Configuration parameters: AllowedMethods, AllowedPatterns. +Metrics/PerceivedComplexity: + Max: 9 + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Migration/DepartmentName: + Exclude: + - 'lib/loga/railtie.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: PreferredName. +Naming/RescuedExceptionsVariableName: + Exclude: + - 'lib/loga/sidekiq5/job_logger.rb' + +# Offense count: 3 +# Configuration parameters: Prefixes, AllowedPatterns. +# Prefixes: when, with, without +RSpec/ContextWording: + Exclude: + - 'spec/support/gethostname_shared.rb' + - 'spec/support/timecop_shared.rb' + - 'spec/unit/loga/configuration_spec.rb' + +# Offense count: 41 +# Configuration parameters: AllowSubject. +RSpec/MultipleMemoizedHelpers: + Max: 12 + +# Offense count: 6 +# Configuration parameters: AllowedPatterns. +# AllowedPatterns: ^expect_, ^assert_ +RSpec/NoExpectationExample: + Exclude: + - 'spec/integration/sidekiq60_spec.rb' + - 'spec/integration/sidekiq61_spec.rb' + - 'spec/integration/sidekiq65_spec.rb' + - 'spec/integration/sidekiq7_spec.rb' + +# Offense count: 2 +# Configuration parameters: Include, CustomTransform, IgnoreMethods, IgnoreMetadata. +# Include: **/*_spec.rb +RSpec/SpecFilePathFormat: + Exclude: + - '**/spec/routing/**/*' + - 'spec/integration/rails/action_mailer_spec.rb' + - 'spec/integration/rails/railtie_spec.rb' + +# Offense count: 1 +RSpec/SubjectDeclaration: + Exclude: + - 'spec/unit/loga/event_spec.rb' + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: AllowedReceivers. +Style/CollectionCompact: + Exclude: + - 'lib/loga/configuration.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: Keywords, RequireColon. +# Keywords: TODO, FIXME, OPTIMIZE, HACK, REVIEW, NOTE +Style/CommentAnnotation: + Exclude: + - 'lib/loga/configuration.rb' + +# Offense count: 25 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: always, always_true, never +Style/FrozenStringLiteralComment: + Exclude: + - 'Appraisals' + - 'Gemfile' + - 'Guardfile' + - 'Rakefile' + - 'lib/loga.rb' + - 'lib/loga/configuration.rb' + - 'lib/loga/event.rb' + - 'lib/loga/ext/core/tempfile.rb' + - 'lib/loga/ext/rails/rack/debug_exceptions.rb' + - 'lib/loga/ext/rails/rack/logger.rb' + - 'lib/loga/ext/rails/rack/logger3.rb' + - 'lib/loga/formatters/gelf_formatter.rb' + - 'lib/loga/formatters/simple_formatter.rb' + - 'lib/loga/log_subscribers/action_mailer.rb' + - 'lib/loga/parameter_filter.rb' + - 'lib/loga/rack/logger.rb' + - 'lib/loga/rack/request.rb' + - 'lib/loga/rack/request_id.rb' + - 'lib/loga/railtie.rb' + - 'lib/loga/service_version_strategies.rb' + - 'lib/loga/sidekiq5/job_logger.rb' + - 'lib/loga/sidekiq6/job_logger.rb' + - 'lib/loga/tagged_logging.rb' + - 'lib/loga/utilities.rb' + - 'lib/loga/version.rb' + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +Style/GlobalStdStream: + Exclude: + - 'lib/loga/configuration.rb' + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: literals, strict +Style/MutableConstant: + Exclude: + - 'lib/loga/rack/request.rb' + +# Offense count: 2 +Style/OpenStructUse: + Exclude: + - 'spec/unit/loga/formatters/gelf_formatter_spec.rb' + +# Offense count: 3 +# This cop supports safe autocorrection (--autocorrect). +Style/RedundantFileExtensionInRequire: + Exclude: + - 'lib/loga/railtie.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Style/RedundantFreeze: + Exclude: + - 'lib/loga/sidekiq7/job_logger.rb' diff --git a/spec/integration/rails/action_mailer_spec.rb b/spec/integration/rails/action_mailer_spec.rb index 47f1ceb..d52fca2 100644 --- a/spec/integration/rails/action_mailer_spec.rb +++ b/spec/integration/rails/action_mailer_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' RSpec.describe Loga::LogSubscribers::ActionMailer, if: Rails.env.production? do diff --git a/spec/integration/rails/railtie_spec.rb b/spec/integration/rails/railtie_spec.rb index 3b15335..b281e6b 100644 --- a/spec/integration/rails/railtie_spec.rb +++ b/spec/integration/rails/railtie_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'ostruct' RSpec.describe Loga::Railtie do @@ -38,7 +40,7 @@ end it 'disables colorized logging' do - expect(app.config.colorize_logging).to eq(false) + expect(app.config.colorize_logging).to be(false) end end diff --git a/spec/integration/rails/request_spec.rb b/spec/integration/rails/request_spec.rb index 9e6f338..6b15931 100644 --- a/spec/integration/rails/request_spec.rb +++ b/spec/integration/rails/request_spec.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + require 'spec_helper' -RSpec.describe 'Structured logging with Rails', timecop: true, - if: Rails.env.production? do +RSpec.describe 'Structured logging with Rails', :timecop, + if: Rails.env.production? do let(:app) { Rails.application } let(:log_entries) do diff --git a/spec/integration/sidekiq5_spec.rb b/spec/integration/sidekiq5_spec.rb index 6bc175d..8555e86 100644 --- a/spec/integration/sidekiq5_spec.rb +++ b/spec/integration/sidekiq5_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' require 'timecop' require 'fakeredis' @@ -24,8 +26,8 @@ def perform(_name); end attr_reader :latest_error, :mutex, :cond def initialize - @mutex = ::Mutex.new - @cond = ::ConditionVariable.new + @mutex = Mutex.new + @cond = ConditionVariable.new end def processor_died(_inst, err) @@ -84,7 +86,7 @@ def read_json_log(line:) aggregate_failures do expect(last_element['class']).to eq 'MySidekiqWorker' expect(last_element['args']).to eq ['Bob'] - expect(last_element['retry']).to eq true + expect(last_element['retry']).to be true expect(last_element['queue']).to eq 'default' end end @@ -107,16 +109,16 @@ def read_json_log(line:) aggregate_failures do expect(json_line).to include( - '_queue'=> 'default', - '_retry'=> true, - '_params'=> ['Bob'], - '_class'=> 'MySidekiqWorker', - '_type'=> 'sidekiq', - '_service.name'=> 'hello_world_app', - '_service.version'=> '1.0', - '_tags'=> '', - 'level'=> 6, - 'version'=> '1.1', + '_queue' => 'default', + '_retry' => true, + '_params' => ['Bob'], + '_class' => 'MySidekiqWorker', + '_type' => 'sidekiq', + '_service.name' => 'hello_world_app', + '_service.version' => '1.0', + '_tags' => '', + 'level' => 6, + 'version' => '1.1', ) %w[_created_at _enqueued_at _jid _duration timestamp host].each do |key| diff --git a/spec/integration/sidekiq60_spec.rb b/spec/integration/sidekiq60_spec.rb index b532fe9..4dcf870 100644 --- a/spec/integration/sidekiq60_spec.rb +++ b/spec/integration/sidekiq60_spec.rb @@ -31,8 +31,8 @@ def perform(_name) attr_reader :cond def initialize - @mutex = ::Mutex.new - @cond = ::ConditionVariable.new + @mutex = Mutex.new + @cond = ConditionVariable.new end def processor_died(_inst, err) @@ -103,7 +103,7 @@ def dump_log aggregate_failures do expect(last_element['class']).to eq 'MySidekiqWorker' expect(last_element['args']).to eq ['Bob'] - expect(last_element['retry']).to eq true + expect(last_element['retry']).to be true expect(last_element['queue']).to eq 'default' end end diff --git a/spec/integration/sidekiq61_spec.rb b/spec/integration/sidekiq61_spec.rb index 033641a..038a61c 100644 --- a/spec/integration/sidekiq61_spec.rb +++ b/spec/integration/sidekiq61_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' require 'timecop' require 'fakeredis' @@ -27,8 +29,8 @@ def perform(_name) attr_reader :latest_error, :mutex, :cond def initialize - @mutex = ::Mutex.new - @cond = ::ConditionVariable.new + @mutex = Mutex.new + @cond = ConditionVariable.new end def processor_died(_inst, err) @@ -46,7 +48,7 @@ def options concurrency: 3, queues: ['default'], job_logger: Loga::Sidekiq6::JobLogger, - }.tap { |opts| opts[:fetch] = ::Sidekiq::BasicFetch.new(opts) } + }.tap { |opts| opts[:fetch] = Sidekiq::BasicFetch.new(opts) } end end end @@ -87,7 +89,7 @@ def read_json_log(line:) aggregate_failures do expect(last_element['class']).to eq 'MySidekiqWorker' expect(last_element['args']).to eq ['Bob'] - expect(last_element['retry']).to eq true + expect(last_element['retry']).to be true expect(last_element['queue']).to eq 'default' end end diff --git a/spec/integration/sidekiq65_spec.rb b/spec/integration/sidekiq65_spec.rb index a338eb5..849d243 100644 --- a/spec/integration/sidekiq65_spec.rb +++ b/spec/integration/sidekiq65_spec.rb @@ -75,7 +75,7 @@ def read_json_log(line:) aggregate_failures do expect(last_element['class']).to eq 'MySidekiqWorker' expect(last_element['args']).to eq ['Bob'] - expect(last_element['retry']).to eq true + expect(last_element['retry']).to be true expect(last_element['queue']).to eq 'default' end end diff --git a/spec/integration/sinatra_spec.rb b/spec/integration/sinatra_spec.rb index 21f5c0a..a83032e 100644 --- a/spec/integration/sinatra_spec.rb +++ b/spec/integration/sinatra_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' class MySinatraApp < Sinatra::Base @@ -21,7 +23,7 @@ class MySinatraApp < Sinatra::Base end end -RSpec.describe 'Structured logging with Sinatra', :with_hostname, :timecop do +RSpec.describe 'Structured logging with Sinatra', :timecop, :with_hostname do let(:io) { StringIO.new } let(:format) {} let(:last_log_entry) do @@ -69,12 +71,12 @@ class MySinatraApp < Sinatra::Base let(:data) do { 'method' => 'GET', - 'path' => '/ok', + 'path' => '/ok', 'params' => { 'username'=>'yoshi' }, 'request_id' => '700a6a01', 'request_ip' => '127.0.0.1', 'user_agent' => nil, - 'duration' => 0, + 'duration' => 0, 'status' => 200, } end @@ -97,7 +99,7 @@ class MySinatraApp < Sinatra::Base let(:data) do super().merge( 'status' => 302, - 'path' => '/new', + 'path' => '/new', 'params' => {}, ) end @@ -112,7 +114,7 @@ class MySinatraApp < Sinatra::Base let(:data) do super().merge( 'status' => 500, - 'path' => '/error', + 'path' => '/error', 'params' => {}, ) end diff --git a/spec/loga/sidekiq5/job_logger_spec.rb b/spec/loga/sidekiq5/job_logger_spec.rb index a8694a6..d156eaa 100644 --- a/spec/loga/sidekiq5/job_logger_spec.rb +++ b/spec/loga/sidekiq5/job_logger_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' require 'loga/sidekiq5/job_logger' diff --git a/spec/loga/sidekiq6/job_logger_spec.rb b/spec/loga/sidekiq6/job_logger_spec.rb index c8a771d..9624c46 100644 --- a/spec/loga/sidekiq6/job_logger_spec.rb +++ b/spec/loga/sidekiq6/job_logger_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' require 'loga/sidekiq6/job_logger' @@ -24,7 +26,7 @@ # https://github.com/mperham/sidekiq/blob/v6.1.2/lib/sidekiq/job_logger.rb it 'inherits from ::Sidekiq::JobLogger' do - expect(subject).to be_a(::Sidekiq::JobLogger) + expect(subject).to be_a(Sidekiq::JobLogger) end describe '#call' do diff --git a/spec/loga/sidekiq7/job_logger_spec.rb b/spec/loga/sidekiq7/job_logger_spec.rb index 2fdc3eb..bc25401 100644 --- a/spec/loga/sidekiq7/job_logger_spec.rb +++ b/spec/loga/sidekiq7/job_logger_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' require 'loga/sidekiq7/job_logger' @@ -26,7 +28,7 @@ # https://github.com/mperham/sidekiq/blob/v6.1.2/lib/sidekiq/job_logger.rb it 'inherits from ::Sidekiq::JobLogger' do - expect(subject).to be_a(::Sidekiq::JobLogger) + expect(subject).to be_a(Sidekiq::JobLogger) end describe '#call' do diff --git a/spec/loga/sidekiq_spec.rb b/spec/loga/sidekiq_spec.rb index 83ec5a1..d58a64e 100644 --- a/spec/loga/sidekiq_spec.rb +++ b/spec/loga/sidekiq_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' RSpec.describe Loga::Sidekiq do @@ -32,9 +34,9 @@ case m['version'] when '51' - expect(::Sidekiq.options[:job_logger]).to eq(Loga::Sidekiq5::JobLogger) + expect(Sidekiq.options[:job_logger]).to eq(Loga::Sidekiq5::JobLogger) when '6' - expect(::Sidekiq.options[:job_logger]).to eq(Loga::Sidekiq6::JobLogger) + expect(Sidekiq.options[:job_logger]).to eq(Loga::Sidekiq6::JobLogger) end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2bed20b..9ac1a01 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'byebug' require 'pry' require 'support/gethostname_shared' @@ -8,16 +10,17 @@ require 'simplecov' SimpleCov.start do - command_name "ruby-#{RUBY_VERSION}-#{File.basename(ENV['BUNDLE_GEMFILE'], '.gemfile')}" + command_name "ruby-#{RUBY_VERSION}-#{File.basename(ENV.fetch('BUNDLE_GEMFILE', nil), + '.gemfile')}" # Exclude specs from showing up in the code coverage report. add_filter 'spec/' end -case ENV['BUNDLE_GEMFILE'] +case ENV.fetch('BUNDLE_GEMFILE', nil) when /rails/ rspec_pattern = 'integration/rails/**/*_spec.rb' - /(?rails\d{2})\.gemfile/ =~ ENV['BUNDLE_GEMFILE'] + /(?rails\d{2})\.gemfile/ =~ ENV.fetch('BUNDLE_GEMFILE', nil) require 'rails' require 'action_mailer' require File.expand_path("../fixtures/#{appraisal}.rb", __FILE__) diff --git a/spec/support/gethostname_shared.rb b/spec/support/gethostname_shared.rb index 28590e0..b0eabbd 100644 --- a/spec/support/gethostname_shared.rb +++ b/spec/support/gethostname_shared.rb @@ -1,4 +1,6 @@ -shared_context 'gethostname', with_hostname: true do +# frozen_string_literal: true + +shared_context 'gethostname', :with_hostname do let(:hostname) { 'bird.example.com' } before do diff --git a/spec/support/helpers.rb b/spec/support/helpers.rb index bc2ac9d..14321c3 100644 --- a/spec/support/helpers.rb +++ b/spec/support/helpers.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'bigdecimal' module Helpers diff --git a/spec/support/request_spec.rb b/spec/support/request_spec.rb index 739d319..6f0eb03 100644 --- a/spec/support/request_spec.rb +++ b/spec/support/request_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.shared_examples 'request logger' do describe 'get request' do it 'logs the request' do @@ -6,23 +8,23 @@ 'HTTP_USER_AGENT' => 'Chrome', 'HTTP_X_REQUEST_ID' => '471a34dc' expect(last_log_entry).to include( - 'version' => '1.1', - 'host' => 'bird.example.com', - 'short_message' => 'GET /ok?username=yoshi 200 in 0ms', - 'timestamp' => 1_450_150_205.123, - 'level' => 6, - '_type' => 'request', - '_service.name' => 'hello_world_app', - '_service.version' => '1.0', - '_request.method' => 'GET', - '_request.path' => '/ok', - '_request.params' => { 'username' => 'yoshi' }, + 'version' => '1.1', + 'host' => 'bird.example.com', + 'short_message' => 'GET /ok?username=yoshi 200 in 0ms', + 'timestamp' => 1_450_150_205.123, + 'level' => 6, + '_type' => 'request', + '_service.name' => 'hello_world_app', + '_service.version' => '1.0', + '_request.method' => 'GET', + '_request.path' => '/ok', + '_request.params' => { 'username' => 'yoshi' }, '_request.request_ip' => '127.0.0.1', '_request.user_agent' => 'Chrome', - '_request.status' => 200, + '_request.status' => 200, '_request.request_id' => '471a34dc', - '_request.duration' => 0, - '_tags' => '471a34dc TEST_TAG', + '_request.duration' => 0, + '_tags' => '471a34dc TEST_TAG', ) end end @@ -36,23 +38,23 @@ 'HTTP_USER_AGENT' => 'Chrome', 'HTTP_X_REQUEST_ID' => '471a34dc' expect(last_log_entry).to include( - 'version' => '1.1', - 'host' => 'bird.example.com', - 'short_message' => 'POST /users?username=yoshi 200 in 0ms', - 'timestamp' => 1_450_150_205.123, - 'level' => 6, - '_type' => 'request', - '_service.name' => 'hello_world_app', - '_service.version' => '1.0', - '_request.method' => 'POST', - '_request.path' => '/users', - '_request.params' => { 'username' => 'yoshi', 'email' => 'hello@world.com' }, + 'version' => '1.1', + 'host' => 'bird.example.com', + 'short_message' => 'POST /users?username=yoshi 200 in 0ms', + 'timestamp' => 1_450_150_205.123, + 'level' => 6, + '_type' => 'request', + '_service.name' => 'hello_world_app', + '_service.version' => '1.0', + '_request.method' => 'POST', + '_request.path' => '/users', + '_request.params' => { 'username' => 'yoshi', 'email' => 'hello@world.com' }, '_request.request_ip' => '127.0.0.1', '_request.user_agent' => 'Chrome', - '_request.status' => 200, + '_request.status' => 200, '_request.request_id' => '471a34dc', - '_request.duration' => 0, - '_tags' => '471a34dc TEST_TAG', + '_request.duration' => 0, + '_tags' => '471a34dc TEST_TAG', ) end @@ -68,23 +70,23 @@ get '/new', {}, 'HTTP_USER_AGENT' => 'Chrome', 'HTTP_X_REQUEST_ID' => '471a34dc' expect(last_log_entry).to include( - 'version' => '1.1', - 'host' => 'bird.example.com', - 'short_message' => 'GET /new 302 in 0ms', - 'timestamp' => 1_450_150_205.123, - 'level' => 6, - '_type' => 'request', - '_service.name' => 'hello_world_app', - '_service.version' => '1.0', - '_request.method' => 'GET', - '_request.path' => '/new', - '_request.params' => {}, + 'version' => '1.1', + 'host' => 'bird.example.com', + 'short_message' => 'GET /new 302 in 0ms', + 'timestamp' => 1_450_150_205.123, + 'level' => 6, + '_type' => 'request', + '_service.name' => 'hello_world_app', + '_service.version' => '1.0', + '_request.method' => 'GET', + '_request.path' => '/new', + '_request.params' => {}, '_request.request_ip' => '127.0.0.1', '_request.user_agent' => 'Chrome', - '_request.status' => 302, + '_request.status' => 302, '_request.request_id' => '471a34dc', - '_request.duration' => 0, - '_tags' => '471a34dc TEST_TAG', + '_request.duration' => 0, + '_tags' => '471a34dc TEST_TAG', ) end end @@ -96,26 +98,26 @@ 'HTTP_USER_AGENT' => 'Chrome', 'HTTP_X_REQUEST_ID' => '471a34dc' expect(last_log_entry).to include( - 'version' => '1.1', - 'host' => 'bird.example.com', - 'short_message' => 'GET /error?username=yoshi 500 in 0ms', - 'timestamp' => 1_450_150_205.123, - 'level' => 3, - '_type' => 'request', - '_service.name' => 'hello_world_app', - '_service.version' => '1.0', - '_request.method' => 'GET', - '_request.path' => '/error', - '_request.params' => { 'username' => 'yoshi' }, - '_request.request_ip' => '127.0.0.1', - '_request.user_agent' => 'Chrome', - '_request.status' => 500, - '_request.request_id' => '471a34dc', - '_request.duration' => 0, - '_exception.klass' => 'NoMethodError', - '_exception.message' => "undefined method `name' for nil:NilClass", + 'version' => '1.1', + 'host' => 'bird.example.com', + 'short_message' => 'GET /error?username=yoshi 500 in 0ms', + 'timestamp' => 1_450_150_205.123, + 'level' => 3, + '_type' => 'request', + '_service.name' => 'hello_world_app', + '_service.version' => '1.0', + '_request.method' => 'GET', + '_request.path' => '/error', + '_request.params' => { 'username' => 'yoshi' }, + '_request.request_ip' => '127.0.0.1', + '_request.user_agent' => 'Chrome', + '_request.status' => 500, + '_request.request_id' => '471a34dc', + '_request.duration' => 0, + '_exception.klass' => 'NoMethodError', + '_exception.message' => "undefined method `name' for nil:NilClass", '_exception.backtrace' => be_a(String), - '_tags' => '471a34dc TEST_TAG', + '_tags' => '471a34dc TEST_TAG', ) end end @@ -125,23 +127,23 @@ get '/not_found', {}, 'HTTP_X_REQUEST_ID' => '471a34dc' expect(last_log_entry).to include( - 'version' => '1.1', - 'host' => 'bird.example.com', - 'short_message' => 'GET /not_found 404 in 0ms', - 'timestamp' => 1_450_150_205.123, - 'level' => 6, - '_type' => 'request', - '_service.name' => 'hello_world_app', - '_service.version' => '1.0', - '_request.method' => 'GET', - '_request.path' => '/not_found', - '_request.params' => {}, - '_request.request_ip' => '127.0.0.1', - '_request.user_agent' => nil, - '_request.status' => 404, - '_request.request_id' => '471a34dc', - '_request.duration' => 0, - '_tags' => '471a34dc TEST_TAG', + 'version' => '1.1', + 'host' => 'bird.example.com', + 'short_message' => 'GET /not_found 404 in 0ms', + 'timestamp' => 1_450_150_205.123, + 'level' => 6, + '_type' => 'request', + '_service.name' => 'hello_world_app', + '_service.version' => '1.0', + '_request.method' => 'GET', + '_request.path' => '/not_found', + '_request.params' => {}, + '_request.request_ip' => '127.0.0.1', + '_request.user_agent' => nil, + '_request.status' => 404, + '_request.request_id' => '471a34dc', + '_request.duration' => 0, + '_tags' => '471a34dc TEST_TAG', ) end end @@ -188,9 +190,9 @@ bob_file: Rack::Test::UploadedFile.new('spec/fixtures/random_bin') expect(last_log_entry).to include( - 'short_message' => 'POST /users?username=yoshi 200 in 0ms', - 'level' => 6, - '_request.params' => { + 'short_message' => 'POST /users?username=yoshi 200 in 0ms', + 'level' => 6, + '_request.params' => { 'username' => 'yoshi', 'bob_file' => include( 'filename' => 'random_bin', diff --git a/spec/support/timecop_shared.rb b/spec/support/timecop_shared.rb index d27955e..494697f 100644 --- a/spec/support/timecop_shared.rb +++ b/spec/support/timecop_shared.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + require 'timecop' -shared_context 'timecop', timecop: true do +shared_context 'timecop', :timecop do # Allows fixed timestamps before(:all) { Timecop.freeze(time_anchor) } diff --git a/spec/unit/loga/configuration_spec.rb b/spec/unit/loga/configuration_spec.rb index 5e0b432..0e59bb7 100644 --- a/spec/unit/loga/configuration_spec.rb +++ b/spec/unit/loga/configuration_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Loga::Configuration do @@ -20,8 +22,8 @@ allow(Loga::ServiceVersionStrategies).to receive(:call).and_return('unknown.sha') end - context 'defaults', with_hostname: true do - specify { expect(subject.device).to eq(STDOUT) } + context 'defaults', :with_hostname do + specify { expect(subject.device).to eq($stdout) } specify { expect(subject.filter_exceptions).to eq(framework_exceptions) } specify { expect(subject.filter_parameters).to eq([]) } specify { expect(subject.format).to eq(:simple) } @@ -29,7 +31,7 @@ specify { expect(subject.level).to eq(:info) } specify { expect(subject.service_name).to eq('hello_world_app') } specify { expect(subject.service_version).to eq('unknown.sha') } - specify { expect(subject.sync).to eq(true) } + specify { expect(subject.sync).to be(true) } specify { expect(subject.tags).to eq([]) } end @@ -174,11 +176,11 @@ let(:logdev) { subject.logger.instance_variable_get(:@logdev) } { - debug: 0, - info: 1, - warn: 2, - error: 3, - fatal: 4, + debug: 0, + info: 1, + warn: 2, + error: 3, + fatal: 4, unknown: 5, }.each do |sym, level| context "when log level is #{sym}" do @@ -194,7 +196,7 @@ let(:options) { super().merge(sync: false) } it 'uses warn log level' do - expect(logdev.dev.sync).to eq(false) + expect(logdev.dev.sync).to be(false) end end end diff --git a/spec/unit/loga/event_spec.rb b/spec/unit/loga/event_spec.rb index 2efb923..1555d72 100644 --- a/spec/unit/loga/event_spec.rb +++ b/spec/unit/loga/event_spec.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + require 'spec_helper' -RSpec.describe Loga::Event, timecop: true do +RSpec.describe Loga::Event, :timecop do describe 'initialize' do context 'when no message is passed' do it 'sets message to an empty string' do @@ -9,7 +11,7 @@ end context 'when message is passed' do - let(:message) { "stuff \xC2".force_encoding 'ASCII-8BIT' } + let(:message) { "stuff \xC2".dup.force_encoding 'ASCII-8BIT' } let(:subject) { described_class.new message: message } it 'sanitizes the input to be UTF-8 convertable' do diff --git a/spec/unit/loga/formatters/gelf_formatter_spec.rb b/spec/unit/loga/formatters/gelf_formatter_spec.rb index 94955d0..90c2b80 100644 --- a/spec/unit/loga/formatters/gelf_formatter_spec.rb +++ b/spec/unit/loga/formatters/gelf_formatter_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Loga::Formatters::GELFFormatter do @@ -8,25 +10,25 @@ let(:host) { 'www.example.com' } let(:params) do { - service_name: service_name, + service_name: service_name, service_version: service_version, - host: host, + host: host, } end shared_examples 'valid GELF message' do it 'includes the required fields' do - expect(json).to include('version' => '1.1', - 'host' => host, + expect(json).to include('version' => '1.1', + 'host' => host, 'short_message' => be_a(String), - 'timestamp' => be_a(Float), - 'level' => 6) + 'timestamp' => be_a(Float), + 'level' => 6) end it 'includes Loga additional fields' do - expect(json).to include('_service.name' => service_name, + expect(json).to include('_service.name' => service_name, '_service.version' => service_version, - '_tags' => '') + '_tags' => '') end it 'outputs the timestamp in seconds since UNIX epoch' do @@ -233,11 +235,11 @@ class << self end { - 'DEBUG' => 7, - 'INFO' => 6, - 'WARN' => 4, - 'ERROR' => 3, - 'FATAL' => 2, + 'DEBUG' => 7, + 'INFO' => 6, + 'WARN' => 4, + 'ERROR' => 3, + 'FATAL' => 2, 'UNKNOWN' => 1, }.each do |ruby_severity, syslog_level| context "with severity #{ruby_severity}" do diff --git a/spec/unit/loga/formatters/simple_formatter_spec.rb b/spec/unit/loga/formatters/simple_formatter_spec.rb index 4a6d969..627cbec 100644 --- a/spec/unit/loga/formatters/simple_formatter_spec.rb +++ b/spec/unit/loga/formatters/simple_formatter_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' require 'loga/formatters/simple_formatter' @@ -112,11 +114,11 @@ end { - 'DEBUG' => 'D', - 'INFO' => 'I', - 'WARN' => 'W', - 'ERROR' => 'E', - 'FATAL' => 'F', + 'DEBUG' => 'D', + 'INFO' => 'I', + 'WARN' => 'W', + 'ERROR' => 'E', + 'FATAL' => 'F', 'UNKNOWN' => 'U', }.each do |ruby_severity, formatted_severity| context "with severity #{ruby_severity}" do diff --git a/spec/unit/loga/log_subscribers/action_mailer_spec.rb b/spec/unit/loga/log_subscribers/action_mailer_spec.rb index 3b71425..d270a1e 100644 --- a/spec/unit/loga/log_subscribers/action_mailer_spec.rb +++ b/spec/unit/loga/log_subscribers/action_mailer_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' require 'active_support' require 'loga/log_subscribers/action_mailer' @@ -9,7 +11,7 @@ let(:event) do instance_double( - 'ActiveSupport::Notifications::Event', + ActiveSupport::Notifications::Event, payload: payload, duration: 0.0001, time: Time.now, @@ -21,7 +23,7 @@ let(:payload) do { mailer: 'FakeMailer', - to: ['user@example.com'], + to: ['user@example.com'], } end let(:config) { instance_double Loga::Configuration, hide_pii: hide_pii } @@ -82,8 +84,8 @@ context 'when an email is sent' do let(:payload) do { - mailer: 'FakeMailer', - from: 'loremipsum@example.com', + mailer: 'FakeMailer', + from: 'loremipsum@example.com', subject: 'Lorem ipsum', } end diff --git a/spec/unit/loga/parameter_filter_spec.rb b/spec/unit/loga/parameter_filter_spec.rb index 75968e3..273542b 100644 --- a/spec/unit/loga/parameter_filter_spec.rb +++ b/spec/unit/loga/parameter_filter_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' RSpec.describe Loga::ParameterFilter do diff --git a/spec/unit/loga/rack/logger_spec.rb b/spec/unit/loga/rack/logger_spec.rb index f1ff6f2..b193026 100644 --- a/spec/unit/loga/rack/logger_spec.rb +++ b/spec/unit/loga/rack/logger_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' require 'rack/test' @@ -40,22 +42,22 @@ middleware.call(env, started_at) expect(Loga::Event).to have_received(:new).with( - data: { + data: { request: { - 'status' => response_status, - 'method' => 'GET', - 'path' => '/about_us', - 'params' => { 'limit' => '1' }, + 'status' => response_status, + 'method' => 'GET', + 'path' => '/about_us', + 'params' => { 'limit' => '1' }, 'request_id' => nil, 'request_ip' => nil, 'user_agent' => nil, - 'duration' => 500, + 'duration' => 500, }, }, exception: logged_exception, - message: %r{^GET \/about_us\?limit=1 #{response_status} in \d+ms$}, + message: %r{^GET /about_us\?limit=1 #{response_status} in \d+ms$}, timestamp: started_at, - type: 'request', + type: 'request', ) end @@ -82,22 +84,22 @@ expect { middleware.call(env, started_at) }.to raise_error(exception_class) expect(Loga::Event).to have_received(:new).with( - data: { + data: { request: { - 'status' => response_status, - 'method' => 'GET', - 'path' => '/about_us', - 'params' => { 'limit' => '1' }, + 'status' => response_status, + 'method' => 'GET', + 'path' => '/about_us', + 'params' => { 'limit' => '1' }, 'request_id' => nil, 'request_ip' => nil, 'user_agent' => nil, - 'duration' => 500, + 'duration' => 500, }, }, exception: logged_exception, - message: %r{^GET \/about_us\?limit=1 #{response_status} in \d+ms$}, + message: %r{^GET /about_us\?limit=1 #{response_status} in \d+ms$}, timestamp: started_at, - type: 'request', + type: 'request', ) end end diff --git a/spec/unit/loga/rack/request_spec.rb b/spec/unit/loga/rack/request_spec.rb index f2413df..7d405ed 100644 --- a/spec/unit/loga/rack/request_spec.rb +++ b/spec/unit/loga/rack/request_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Loga::Rack::Request do diff --git a/spec/unit/loga/service_version_strategies_spec.rb b/spec/unit/loga/service_version_strategies_spec.rb index 7b49a0f..73fe172 100644 --- a/spec/unit/loga/service_version_strategies_spec.rb +++ b/spec/unit/loga/service_version_strategies_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' RSpec.describe Loga::ServiceVersionStrategies do diff --git a/spec/unit/loga/utilities_spec.rb b/spec/unit/loga/utilities_spec.rb index 4d9162e..34fc186 100644 --- a/spec/unit/loga/utilities_spec.rb +++ b/spec/unit/loga/utilities_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Loga::Utilities do diff --git a/spec/unit/loga_spec.rb b/spec/unit/loga_spec.rb index 1a45f30..3632c7e 100644 --- a/spec/unit/loga_spec.rb +++ b/spec/unit/loga_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Loga do @@ -64,7 +66,7 @@ context 'when Loga is configured' do before { subject.configure(options) } - specify { expect(subject.logger).to be_kind_of(Logger) } + specify { expect(subject.logger).to be_a(Logger) } end end