summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2020-10-19 14:04:22 -0700
committerGitHub <noreply@github.com>2020-10-19 14:04:22 -0700
commit9760f061aae612b59bdf16556e97cb2b43deb9dd (patch)
treecb1f72fddc804a0bc1bd962b6f8d2e4f757cfc41
parentebc3773f77c0e14da725f5a467aedaf5080491c7 (diff)
parentb297c74f198774a746306784ecc2870e7f75b685 (diff)
downloadchef-9760f061aae612b59bdf16556e97cb2b43deb9dd.tar.gz
Merge pull request #10531 from chef/cleanup-platform_mock
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--spec/support/mock/constant.rb52
-rw-r--r--spec/support/mock/platform.rb40
2 files changed, 24 insertions, 68 deletions
diff --git a/spec/support/mock/constant.rb b/spec/support/mock/constant.rb
deleted file mode 100644
index 7acd02e9d7..0000000000
--- a/spec/support/mock/constant.rb
+++ /dev/null
@@ -1,52 +0,0 @@
-# Allows easy mocking of global and class constants
-
-# Inspired by:
-# http://missingbit.blogspot.com/2011/07/stubbing-constants-in-rspec_20.html
-# http://digitaldumptruck.jotabout.com/?p=551
-
-def mock_constants(constants)
- saved_constants = {}
- constants.each do |constant, val|
- source_object, const_name = parse_constant(constant)
- saved_constants[constant] = source_object.const_get(const_name)
- with_warnings(nil) { source_object.const_set(const_name, val) }
- end
-
- begin
- yield
- ensure
- constants.each_key do |constant|
- source_object, const_name = parse_constant(constant)
- with_warnings(nil) { source_object.const_set(const_name, saved_constants[constant]) }
- end
- end
-end
-
-def parse_constant(constant)
- source, _, constant_name = constant.to_s.rpartition("::")
- [constantize(source), constant_name]
-end
-
-# Taken from ActiveSupport
-
-# File activesupport/lib/active_support/core_ext/kernel/reporting.rb, line 3
-#
-# Sets $VERBOSE for the duration of the block and back to its original value afterwards.
-def with_warnings(flag)
- old_verbose, $VERBOSE = $VERBOSE, flag
- yield
-ensure
- $VERBOSE = old_verbose
-end
-
-# File activesupport/lib/active_support/inflector/methods.rb, line 209
-def constantize(camel_cased_word)
- names = camel_cased_word.split("::")
- names.shift if names.empty? || names.first.empty?
-
- constant = Object
- names.each do |name|
- constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
- end
- constant
-end
diff --git a/spec/support/mock/platform.rb b/spec/support/mock/platform.rb
index 883d953371..e2d2149fad 100644
--- a/spec/support/mock/platform.rb
+++ b/spec/support/mock/platform.rb
@@ -5,21 +5,29 @@
# 'i386-mingw32' (windows) or 'x86_64-darwin11.2.0' (unix). Useful for
# testing code that mixes in platform specific modules like +Chef::Mixin::Securable+
# or +Chef::FileAccessControl+
-def platform_mock(platform = :unix)
- allow(ChefUtils).to receive(:windows?).and_return(platform == :windows ? true : false)
- ENV["SYSTEMDRIVE"] = (platform == :windows ? "C:" : nil)
+RSpec.configure do |c|
+ c.include(Module.new do
+ def platform_mock(platform = :unix)
+ case platform
+ when :windows
+ Chef::Config.set_defaults_for_windows
+ allow(ChefUtils).to receive(:windows?).and_return(true)
+ stub_const("ENV", ENV.to_hash.merge("SYSTEMDRIVE" => "C:"))
+ stub_const("RUBY_PLATFORM", "i386-mingw32")
+ stub_const("File::PATH_SEPARATOR", ";")
+ stub_const("File::ALT_SEPARATOR", "\\")
+ when :unix
+ Chef::Config.set_defaults_for_nix
+ allow(ChefUtils).to receive(:windows?).and_return(false)
+ stub_const("ENV", ENV.to_hash.merge("SYSTEMDRIVE" => nil))
+ stub_const("RUBY_PLATFORM", "x86_64-darwin11.2.0")
+ stub_const("File::PATH_SEPARATOR", ":")
+ stub_const("File::ALT_SEPARATOR", nil)
+ else
+ raise "#{__method__}: unrecognized platform '#{platform}', expected one of ':unix' or ':windows'"
+ end
- if platform == :windows
- Chef::Config.set_defaults_for_windows
- else
- Chef::Config.set_defaults_for_nix
- end
-
- if block_given?
- mock_constants({ "RUBY_PLATFORM" => (platform == :windows ? "i386-mingw32" : "x86_64-darwin11.2.0"),
- "File::PATH_SEPARATOR" => (platform == :windows ? ";" : ":"),
- "File::ALT_SEPARATOR" => (platform == :windows ? "\\" : nil) }) do
- yield
- end
- end
+ yield
+ end
+ end)
end