Skip to content

Commit 818f8d6

Browse files
committed
Use temporary file not stringio so logs can be collected over forks
1 parent 88545cb commit 818f8d6

3 files changed

Lines changed: 34 additions & 14 deletions

File tree

lib/octocatalog-diff/util/parallel.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,16 @@ def self.run_tasks_parallel(result, task_array, logger)
102102

103103
task_array.each_with_index do |task, index|
104104
# simplecov doesn't see this because it's forked
105+
# Kernel.exit! avoids at_exit calls possibly set up by rspec tests
105106
# :nocov:
106107
this_pid = fork do
107-
logger.reopen if logger.respond_to?(:reopen)
108-
task_result = execute_task(task, logger)
109-
File.open(File.join(ipc_tempdir, "#{Process.pid}.yaml"), 'w') { |f| f.write Marshal.dump(task_result) }
110-
logger.close
111-
exit 0
108+
begin
109+
task_result = execute_task(task, logger)
110+
File.open(File.join(ipc_tempdir, "#{Process.pid}.yaml"), 'w') { |f| f.write Marshal.dump(task_result) }
111+
Kernel.exit! 0
112+
rescue
113+
Kernel.exit! 255
114+
end
112115
end
113116
# :nocov:
114117

spec/octocatalog-diff/integration/catalog_only_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
expect(logs).to match(/Compiling catalog for rspec-node.github.net/)
4343
expect(logs).to match(/Initialized OctocatalogDiff::Catalog::Noop for from-catalog/)
4444
expect(logs).to match(/Initialized OctocatalogDiff::Catalog::Computed for to-catalog/)
45+
expect(logs).to match(/Catalog for test-branch will be built with OctocatalogDiff::Catalog::Computed/)
46+
expect(logs).to match(/Success git checkout .+git-repo:test-branch/)
47+
expect(logs).to match(/Success build_catalog for test-branch/)
4548
end
4649

4750
it 'should produce a valid catalog' do

spec/octocatalog-diff/tests/spec_helper.rb

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
require 'logger'
33
require 'rspec'
44
require 'rspec/retry'
5-
require 'stringio'
65

76
# Enable SimpleCov coverage testing?
87
if ENV['COVERAGE']
@@ -33,6 +32,29 @@
3332

3433
module OctocatalogDiff
3534
class Spec
35+
# Set up a logger that is usuable across parent and child forks.
36+
class CustomLogger
37+
attr_accessor :logger
38+
39+
def initialize
40+
@log_tempdir = Dir.mktmpdir
41+
at_exit { FileUtils.remove_entry_secure @log_tempdir if File.directory?(@log_tempdir) }
42+
43+
@logger = Logger.new File.join(@log_tempdir, 'customlogger.out')
44+
@logger.level = Logger::DEBUG
45+
end
46+
47+
def string
48+
@content ||= File.read(File.join(@log_tempdir, 'customlogger.out'))
49+
end
50+
end
51+
52+
# Create a logger object so that its result can be inspected
53+
def self.setup_logger
54+
logger_obj = OctocatalogDiff::Spec::CustomLogger.new
55+
[logger_obj.logger, logger_obj]
56+
end
57+
3658
# Wrapper around puppet binstub
3759
PUPPET_BINARY = File.expand_path('../../../script/puppet', File.dirname(__FILE__)).freeze
3860
raise "Puppet binary (#{PUPPET_BINARY}) is missing" unless File.file?(PUPPET_BINARY)
@@ -205,14 +227,6 @@ def self.clean_up_tmpdir(*dir_in)
205227
end
206228
end
207229

208-
# Create a logger object so that its result can be inspected
209-
def self.setup_logger
210-
strio = StringIO.new
211-
logger = Logger.new strio
212-
logger.level = Logger::DEBUG
213-
[logger, strio]
214-
end
215-
216230
# To help test a diff against an answer without considering the file and line locations,
217231
# remove the file and line locations. This takes the JSON result from catalog-diff and returns
218232
# a cleaned-up array of diffs.

0 commit comments

Comments
 (0)