From cbc1baca127d048ef64f4d9febb0353ff0dab1b5 Mon Sep 17 00:00:00 2001 From: Robert Peacock Date: Wed, 27 Sep 2023 10:01:21 +0100 Subject: [PATCH 1/6] reorder asserts for consistency --- test/logger_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/logger_test.rb b/test/logger_test.rb index 80a73da..604c97a 100644 --- a/test/logger_test.rb +++ b/test/logger_test.rb @@ -335,8 +335,8 @@ actual_log = read_json(@buffer) assert_equal 'Artificially raised exception', actual_log[:message] - assert_equal 'divided by 0', actual_log[:error][:message] assert_equal 'ZeroDivisionError', actual_log[:error][:type] + assert_equal 'divided by 0', actual_log[:error][:message] assert_match 'test/logger_test.rb', actual_log[:error][:stack_trace].first end From 342a6e1c92723c49ddcb37eae3a76ed5a37c71c2 Mon Sep 17 00:00:00 2001 From: Robert Peacock Date: Wed, 27 Sep 2023 11:37:44 +0100 Subject: [PATCH 2/6] change signature of twiglet.error to enable single error arg --- lib/twiglet/logger.rb | 10 ++++++++-- test/logger_test.rb | 41 ++++++++++++++++++++++++++++++----------- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/lib/twiglet/logger.rb b/lib/twiglet/logger.rb index faae95a..61d7034 100644 --- a/lib/twiglet/logger.rb +++ b/lib/twiglet/logger.rb @@ -62,8 +62,14 @@ def warn(message_or_error = nil, &block) super(message, &block) end - def error(message = nil, error = nil, &block) - message = error_message(error, message) if error + def error(message_or_error = nil, error = nil, &block) + if error + message = error_message(error, message_or_error) + elsif message_or_error.is_a?(Exception) + message = error_message(message_or_error) + else + message = message_or_error + end super(message, &block) end diff --git a/test/logger_test.rb b/test/logger_test.rb index 604c97a..ed91ae3 100644 --- a/test/logger_test.rb +++ b/test/logger_test.rb @@ -353,35 +353,54 @@ end it 'should log an error with string message' do - e = StandardError.new('Unknown error') + e = StandardError.new('Some error') @logger.error('Artificially raised exception with string message', e) actual_log = read_json(@buffer) assert_equal 'Artificially raised exception with string message', actual_log[:message] assert_equal 'StandardError', actual_log[:error][:type] - assert_equal 'Unknown error', actual_log[:error][:message] + assert_equal 'Some error', actual_log[:error][:message] end - it 'should log error type properly even when active_support/json is required' do - require 'active_support/json' - e = StandardError.new('Unknown error') - @logger.error('Artificially raised exception with string message', e) + it 'should log an error if no message is given' do + e = StandardError.new('Some error') + @logger.error(e) actual_log = read_json(@buffer) + assert_equal 'Some error', actual_log[:message] assert_equal 'StandardError', actual_log[:error][:type] + assert_equal 'Some error', actual_log[:error][:message] end - - it 'can log just an error as "error", if no message is given' do - e = StandardError.new('some error') + + it 'should log an error if nil message is given' do + e = StandardError.new('Some error') @logger.error(nil, e) actual_log = read_json(@buffer) - assert_equal 'some error', actual_log[:message] + assert_equal 'Some error', actual_log[:message] + assert_equal 'StandardError', actual_log[:error][:type] + assert_equal 'Some error', actual_log[:error][:message] + end + + it 'should log a string if no error is given' do + @logger.error('Some error') + + actual_log = read_json(@buffer) + + assert_equal 'Some error', actual_log[:message] + end + + it 'should log error type properly even when active_support/json is required' do + require 'active_support/json' + e = StandardError.new('Unknown error') + @logger.error('Artificially raised exception with string message', e) + + actual_log = read_json(@buffer) + assert_equal 'StandardError', actual_log[:error][:type] - assert_equal 'some error', actual_log[:error][:message] end [:debug, :info, :warn].each do |level| From 0bfd40fe14f2bec317331e9cda83355b5a116ccd Mon Sep 17 00:00:00 2001 From: Robert Peacock Date: Wed, 27 Sep 2023 11:47:31 +0100 Subject: [PATCH 3/6] rubocop autofixes --- lib/twiglet/logger.rb | 14 +++++++------- test/logger_test.rb | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/twiglet/logger.rb b/lib/twiglet/logger.rb index 61d7034..399930c 100644 --- a/lib/twiglet/logger.rb +++ b/lib/twiglet/logger.rb @@ -63,13 +63,13 @@ def warn(message_or_error = nil, &block) end def error(message_or_error = nil, error = nil, &block) - if error - message = error_message(error, message_or_error) - elsif message_or_error.is_a?(Exception) - message = error_message(message_or_error) - else - message = message_or_error - end + message = if error + error_message(error, message_or_error) + elsif message_or_error.is_a?(Exception) + error_message(message_or_error) + else + message_or_error + end super(message, &block) end diff --git a/test/logger_test.rb b/test/logger_test.rb index ed91ae3..e4921db 100644 --- a/test/logger_test.rb +++ b/test/logger_test.rb @@ -373,7 +373,7 @@ assert_equal 'StandardError', actual_log[:error][:type] assert_equal 'Some error', actual_log[:error][:message] end - + it 'should log an error if nil message is given' do e = StandardError.new('Some error') @logger.error(nil, e) From 93abf609b4f280d5baac43a0dcc86b89b52cca69 Mon Sep 17 00:00:00 2001 From: Robert Peacock Date: Wed, 27 Sep 2023 11:56:51 +0100 Subject: [PATCH 4/6] add title to catalog-info --- catalog-info.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/catalog-info.yaml b/catalog-info.yaml index 5369821..58e99b7 100644 --- a/catalog-info.yaml +++ b/catalog-info.yaml @@ -2,6 +2,7 @@ apiVersion: backstage.io/v1alpha1 kind: Component metadata: name: twiglet + title: Twiglet description: simple logging spec: type: library From ac5eb5824b365cc6bc91b2ed467f03d2405483cf Mon Sep 17 00:00:00 2001 From: Robert Peacock Date: Wed, 27 Sep 2023 12:03:57 +0100 Subject: [PATCH 5/6] add annotations to catalog-info, name to match repo --- catalog-info.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/catalog-info.yaml b/catalog-info.yaml index 58e99b7..9357d90 100644 --- a/catalog-info.yaml +++ b/catalog-info.yaml @@ -1,9 +1,11 @@ apiVersion: backstage.io/v1alpha1 kind: Component metadata: - name: twiglet + name: twiglet-ruby title: Twiglet description: simple logging + annotations: + rubygems.org/name: "twiglet" spec: type: library lifecycle: production From 122c9ebada60a53ec735868d4829e3af1f97673d Mon Sep 17 00:00:00 2001 From: "dobby-app[bot]" <78910979+dobby-app[bot]@users.noreply.github.com> Date: Wed, 27 Sep 2023 11:04:59 +0000 Subject: [PATCH 6/6] bump patch version --- lib/twiglet/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/twiglet/version.rb b/lib/twiglet/version.rb index 8a546bf..86765e8 100644 --- a/lib/twiglet/version.rb +++ b/lib/twiglet/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Twiglet - VERSION = '3.9.1' + VERSION = '3.9.2' end