Skip to content

Commit

Permalink
Allow facter test log to be written to a file
Browse files Browse the repository at this point in the history
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
  • Loading branch information
joshcooper committed May 22, 2024
1 parent 2d26bf6 commit 7390fe8
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ def colorize(str, color)
require 'webmock/rspec'
WebMock.disable_net_connect!

# Configure test logger
logdest = StringIO.new
logdest = File.new(ENV['FACTER_TEST_LOG'], 'w') if ENV['FACTER_TEST_LOG']
logger = Logger.new(logdest)

# Configure RSpec
RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
Expand Down Expand Up @@ -77,10 +82,13 @@ def colorize(str, color)
end

config.before(:all) do
Facter::Log.class_variable_set(:@@logger, Logger.new(StringIO.new)) # rubocop:disable Style/ClassVars
Facter::Log.class_variable_set(:@@logger, logger) # rubocop:disable Style/ClassVars
end

config.before do
config.before do |test|
m = test.metadata
logger.info("*** BEGIN TEST #{m[:file_path]}:#{m[:line_number]}")

LegacyFacter.clear
Facter.clear_messages
end
Expand Down

0 comments on commit 7390fe8

Please sign in to comment.