Skip to content

Commit ed2039a

Browse files
committed
Allow facter test log to be written to a file
If the FACTER_TEST_LOG environment variable is set, then use it as the log destination when running facter tests. This is borrowed from puppet's PUPPET_TEST_LOG. The test file path and line number are written so you can tell which test generated the log output: I, [2024-05-20T16:26:04.852790 #3073378] INFO -- : *** BEGIN TEST ./spec/custom_facts/core/execution_spec.rb:63 I, [2024-05-20T16:26:04.853239 #3073378] INFO -- : *** BEGIN TEST ./spec/custom_facts/core/execution_spec.rb:71 W, [2024-05-20T16:26:04.853473 #3073378] WARN -- : Facter::Core::Execution::Posix - Unexpected key passed to Facter::Core::Execution.execute option: time_limit - valid keys: on_fail,expand,logger,timeout I, [2024-05-20T16:26:04.853680 #3073378] INFO -- : *** BEGIN TEST ./spec/custom_facts/core/execution_spec.rb:71
1 parent 7cced2e commit ed2039a

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

spec/spec_helper.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def colorize(str, color)
4848
require 'webmock/rspec'
4949
WebMock.disable_net_connect!
5050

51+
# Configure test logger
52+
logdest = ENV['FACTER_TEST_LOG'] ? File.new(ENV['FACTER_TEST_LOG'], 'w') : StringIO.new
53+
logger = Logger.new(logdest)
54+
5155
# Configure RSpec
5256
RSpec.configure do |config|
5357
# Enable flags like --only-failures and --next-failure
@@ -77,10 +81,13 @@ def colorize(str, color)
7781
end
7882

7983
config.before(:all) do
80-
Facter::Log.class_variable_set(:@@logger, Logger.new(StringIO.new)) # rubocop:disable Style/ClassVars
84+
Facter::Log.class_variable_set(:@@logger, logger) # rubocop:disable Style/ClassVars
8185
end
8286

83-
config.before do
87+
config.before do |test|
88+
m = test.metadata
89+
logger.info("*** BEGIN TEST #{m[:file_path]}:#{m[:line_number]}")
90+
8491
LegacyFacter.clear
8592
Facter.clear_messages
8693
end

0 commit comments

Comments
 (0)