From fe34eb7869bab6d2cd8601dbb4b9f5311ed411f3 Mon Sep 17 00:00:00 2001 From: Ryan Davis Date: Fri, 4 Oct 2019 17:31:50 -0700 Subject: [PATCH] Moved quick_resource and Fake helpers to test/helpers/resources.rb Signed-off-by: Ryan Davis --- test/helper.rb | 45 +----------------------------- test/helpers/resources.rb | 58 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 44 deletions(-) create mode 100644 test/helpers/resources.rb diff --git a/test/helper.rb b/test/helper.rb index 04c16732b..2885c46d5 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -70,6 +70,7 @@ require "mocha/setup" require "inspec/log" require "inspec/backend" require "helpers/mock_loader" +require "helpers/resources" TMP_CACHE = {} # rubocop: disable Style/MutableConstant @@ -150,50 +151,6 @@ class Minitest::Test def skip_windows! skip_until 2019, 10, 30, "These have never passed" if windows? end - - ## - # This creates a real resource with default config/backend. - # - # Use this whenever possible. Let's phase out the MockLoader pain. - - def quick_resource(name, *args, &block) - backend = Inspec::Backend.create(Inspec::Config.new) - backend.extend Fake::Backend - - klass = Inspec::Resource.registry[name] - - instance = klass.new(backend, name, *args) - instance.extend Fake::Resource - instance.mock_command(&block) if block - instance - end -end - -module Fake - Command = Struct.new(:stdout, :stderr, :exit_status) - - module Backend - def stdout_file(path) - result(path, nil, 0) - end - - def stderr_file(path) - result(nil, path, 0) - end - - def result(stdout_path, stderr_path, exit) - stdout = stdout_path ? File.read(stdout_path) : "" - stderr = stderr_path ? File.read(stderr_path) : "" - - ::Fake::Command.new(stdout, stderr, 0) - end - end - - module Resource - def mock_command(&block) - inspec.define_singleton_method :command, &block - end - end end class InspecTest < Minitest::Test diff --git a/test/helpers/resources.rb b/test/helpers/resources.rb new file mode 100644 index 000000000..4e27b2c7d --- /dev/null +++ b/test/helpers/resources.rb @@ -0,0 +1,58 @@ +class Minitest::Test + ## + # This creates a real resource with default config/backend. + # + # Use this whenever possible. Let's phase out the MockLoader pain. + + def quick_resource(name, platform = :linux, *args, &block) + backend = Inspec::Backend.create(Inspec::Config.new) + backend.extend Fake::Backend + + os = MockLoader::OPERATING_SYSTEMS[platform] + raise "Unknown platform: %p" % [platform] unless os + + # mock.mock_os(@platform) + platform = Train::Platforms.name(os[:name]) + platform.find_family_hierarchy # TODO: remove? UGH! adds platform= + platform.platform = os + # platform.add_platform_methods # TODO: remove? + # TODO: this should have a setter + # TODO: backend.backend is the WORST name + backend.backend.instance_variable_set :@platform, platform + # end mock.mock_os + + klass = Inspec::Resource.registry[name] + + instance = klass.new(backend, name, *args) + instance.extend Fake::Resource + instance.mock_command(&block) if block + instance + end +end + +module Fake + Command = Struct.new(:stdout, :stderr, :exit_status) + + module Backend + def stdout_file(path) + result(path, nil, 0) + end + + def stderr_file(path) + result(nil, path, 0) + end + + def result(stdout_path, stderr_path, exit) + stdout = stdout_path ? File.read(stdout_path) : "" + stderr = stderr_path ? File.read(stderr_path) : "" + + ::Fake::Command.new(stdout, stderr, 0) + end + end + + module Resource + def mock_command(&block) + inspec.define_singleton_method :command, &block + end + end +end