From 59df4c55892462d2ceecf57655287dbd08ab6cad Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Mon, 1 Jul 2024 12:02:50 +0200 Subject: [PATCH] Log module loading warning to stderr When requesting output as JSON the warnings on stdout will prevent loading. It also fixes a typo in occurred. --- lib/hammer_cli/modules.rb | 2 +- test/unit/modules_test.rb | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/hammer_cli/modules.rb b/lib/hammer_cli/modules.rb index 5501100e..ef479d68 100644 --- a/lib/hammer_cli/modules.rb +++ b/lib/hammer_cli/modules.rb @@ -54,7 +54,7 @@ def self.load!(name) require_module(name) rescue Exception => e logger.error "Error while loading module #{name}." - puts _("Warning: An error occured while loading module %s.") % name + $stderr.puts _("Warning: An error occurred while loading module %s.") % name # with ModuleLoadingError we assume the error is already logged by the issuer logger.error e unless e.is_a?(HammerCLI::ModuleLoadingError) raise e diff --git a/test/unit/modules_test.rb b/test/unit/modules_test.rb index 7fd910b7..0e5f49f0 100644 --- a/test/unit/modules_test.rb +++ b/test/unit/modules_test.rb @@ -128,26 +128,25 @@ def self.version before :each do HammerCLI::Modules.stubs(:require_module).raises(RuntimeError) @error_msg = "ERROR Modules : Error while loading module hammer_cli_tom." - @warning_msg = "Warning: An error occured while loading module hammer_cli_tom." + @warning_msg = "Warning: An error occurred while loading module hammer_cli_tom." end it "must log an error if the load! fails" do - out, _ = capture_io do + assert_output("", "#{@warning_msg}\n") do assert_raises(RuntimeError) { HammerCLI::Modules.load!('hammer_cli_tom') } end - _(out).must_equal "#{@warning_msg}\n" _(@log_output.readline.strip).must_equal @error_msg end it "must log an error if the load fails" do - assert_output("#{@warning_msg}\n", "") do + assert_output("", "#{@warning_msg}\n") do HammerCLI::Modules.load("hammer_cli_tom") end _(@log_output.readline.strip).must_equal @error_msg end it "must return false when load fails" do - assert_output("#{@warning_msg}\n", "") do + assert_output("", "#{@warning_msg}\n") do _(HammerCLI::Modules.load("hammer_cli_tom")).must_equal false end end