From eead88932542c1253edc3a948ca2fbf19b4629bf Mon Sep 17 00:00:00 2001 From: Ryan Davis Date: Wed, 15 May 2019 00:20:27 -0700 Subject: [PATCH] Fix test framework contention. RSpec is used internally. Minitest is used for our tests. They don't really like each other. This fixes that and gets our tests consistently running the correct number of tests (they were load-order dependent before and each platform had its own different number of tests it would run). Signed-off-by: Ryan Davis --- .../inspec-resource-lister/test/helper.rb | 1 + test/helper.rb | 60 ++++++++++++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/examples/plugins/inspec-resource-lister/test/helper.rb b/examples/plugins/inspec-resource-lister/test/helper.rb index 721d764c3..089707f93 100644 --- a/examples/plugins/inspec-resource-lister/test/helper.rb +++ b/examples/plugins/inspec-resource-lister/test/helper.rb @@ -23,3 +23,4 @@ require 'minitest/spec' require 'minitest/autorun' # You might want to put some debugging tools here. We run tests to find bugs, after all. +# require 'byebug' diff --git a/test/helper.rb b/test/helper.rb index 6b36b923c..24d216545 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -1,6 +1,13 @@ # author: Dominik Richter # author: Christoph Hartmann +## +# Do not add any code above this line. + +## +# Do not add any other code to this code block. Simplecov and +# coveralls only until the next code block: + require 'simplecov' require 'coveralls' @@ -16,7 +23,58 @@ SimpleCov.start do add_group 'Backends', 'lib/inspec/backend' end -require 'minitest/autorun' +## +# +# Do not add any other code from here until the end of this code +# block. +# +# Before ANYTHING else happens, this must happen: +# +# 1) require minitest/autorun +# 2) alias describe to mt_describe +# 3) require rspec +# 4) disable_monkey_patching from rspec +# 5) alias mt_describe back to describe using change_global_dsl. +# +# Explanation: eventually, our tests get around to inspec/runner_rspec +# (and a few others), and they load rspec. When rspec loads, it +# creates it's own global `describe` method, overwriting minitest's. +# When you tell RSpec to disable_monkey_patching, instead of using +# remove_method, they use undef_method, which blocks access to our +# Kernel.describe. We then need to go back in and reactivate it in +# order for our tests to finish declaring their tests and eventually +# actually running. +# +# Before this, the tests would get to the point of loading rspec, then +# all subsequently loaded spec-style tests would just disappear into +# the aether. Differences in test load order created differences in +# test count and vast differences in test time (which should have been +# a clue that something was up--windows is just NOT THAT FAST). +# +# The OTHER way to fix this is to ban spec style tests in our +# codebase. This is a more rational approach but requires more work. I +# need these tests up and all running and dependable. We can make them +# right later. + +require "minitest/autorun" + +module Kernel + alias mt_describe describe +end + +require "rspec" + +RSpec.configure do |config| + config.disable_monkey_patching! +end + +RSpec::Core::DSL.change_global_dsl do + alias describe mt_describe +end + +# End of rspec vs minitest fight +######################################################################## + require 'webmock/minitest' require 'mocha/setup' require 'fileutils'