Skip to content

Commit

Permalink
Log module loading warning to stderr (#385)
Browse files Browse the repository at this point in the history
When requesting output as JSON the warnings on stdout will prevent
loading. It also fixes a typo in occurred.
  • Loading branch information
ekohl authored Jul 3, 2024
1 parent d72facc commit 3cb4152
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/hammer_cli/modules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions test/unit/modules_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3cb4152

Please sign in to comment.