diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e0714b..97d3518 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [0.2.3] - Unreleased +### Fixed + +- Incorrect configuration of the logger + ## [0.2.2] - 2024-09-05 ### Added diff --git a/Gemfile b/Gemfile index d181e80..067b030 100644 --- a/Gemfile +++ b/Gemfile @@ -9,4 +9,5 @@ gem "rake" gem "minitest" gem "simplecov" gem "simplecov-json" -gem "reissue" \ No newline at end of file +gem "reissue" +gem "ostruct" \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 5baf0a2..75d667d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -16,6 +16,7 @@ GEM reline (>= 0.4.2) json (2.7.2) minitest (5.24.1) + ostruct (0.6.0) psych (5.1.2) stringio rake (13.2.1) @@ -43,6 +44,7 @@ DEPENDENCIES chat_notifier! debug minitest + ostruct rake reissue simplecov diff --git a/lib/chat_notifier.rb b/lib/chat_notifier.rb index 5e181e7..4a17330 100644 --- a/lib/chat_notifier.rb +++ b/lib/chat_notifier.rb @@ -11,9 +11,9 @@ module ChatNotifier DebugExceptionLocation = Data.define(:location) DebugSummary = Data.define(:failed_examples) + require "logger" + @logger = Logger.new($stdout) class << self - require "logger" - @logger = Logger.new($stdout) attr_accessor :logger def app diff --git a/test/chat_notifier_test.rb b/test/chat_notifier_test.rb new file mode 100644 index 0000000..ac260d1 --- /dev/null +++ b/test/chat_notifier_test.rb @@ -0,0 +1,84 @@ +require "test_helper" +require "ostruct" +require "chat_notifier" + +module ChatNotifier + class Test < Minitest::Test + def setup + # Setup any necessary test data or mocks here + @env = {} # Replace with actual environment data if needed + @summary = "Test summary" + end + + def test_app_returns_rails_app_name + Object.const_set( + :Rails, + OpenStruct.new( + application: OpenStruct.new( + class: OpenStruct.new( + module_parent: "TestApp" + ) + ) + ) + ) + assert_equal "TestApp", ChatNotifier.app + end + + def test_app_returns_env_name + ENV["NOTIFY_APP_NAME"] = "TestApp" + assert_equal "TestApp", ChatNotifier.app + end + + def test_debug! + mock_repository = Minitest::Mock.new + mock_environment = Minitest::Mock.new + mock_chatter = Minitest::Mock.new(ChatNotifier::Chatter::Debug) + mock_messenger = Minitest::Mock.new + + Repository.stub(:for, mock_repository) do + TestEnvironment.stub(:for, mock_environment) do + Chatter.stub(:const_get, mock_chatter) do + mock_chatter.expect(:new, mock_chatter, settings: @env, repository: mock_repository, environment: mock_environment) + Messenger.stub(:for, mock_messenger) do + mock_chatter.expect(:post, nil, [mock_messenger]) + + ChatNotifier.debug!(@env, summary: @summary) + + mock_chatter.verify + end + end + end + end + end + + def test_call + original_app_name = ENV["NOTIFY_APP_NAME"] + ENV["NOTIFY_APP_NAME"] = "TestApp" + # Mock dependencies + mock_repository = Minitest::Mock.new + mock_environment = Minitest::Mock.new + mock_messenger = Minitest::Mock.new + mock_box = Minitest::Mock.new + + Repository.stub(:for, mock_repository) do + TestEnvironment.stub(:for, mock_environment) do + Chatter.stub(:handling, [mock_box]) do + Messenger.stub(:for, mock_messenger) do + mock_box.expect(:conditional_post, nil, [mock_messenger]) + + ChatNotifier.call(summary: @summary) + + mock_box.verify + end + end + end + end + ensure + if original_app_name.nil? + ENV.delete("NOTIFY_APP_NAME") + else + ENV["NOTIFY_APP_NAME"] = original_app_name + end + end + end +end