diff options
author | Seth Chisamore <schisamo@opscode.com> | 2012-10-30 10:39:35 -0400 |
---|---|---|
committer | Seth Chisamore <schisamo@opscode.com> | 2012-10-30 10:39:35 -0400 |
commit | 24dc69a9a97e82a6e4207de68d6dcc664178249b (patch) | |
tree | 19bb289c9f88b4bbab066bc56b95d6d222fd5c35 /spec/support/mock | |
parent | 9348c1c9c80ee757354d624b7dc1b78ebc7605c4 (diff) | |
download | chef-24dc69a9a97e82a6e4207de68d6dcc664178249b.tar.gz |
[OC-3564] move core Chef to the repo root \o/ \m/
The opscode/chef repository now only contains the core Chef library code
used by chef-client, knife and chef-solo!
Diffstat (limited to 'spec/support/mock')
-rw-r--r-- | spec/support/mock/constant.rb | 52 | ||||
-rw-r--r-- | spec/support/mock/platform.rb | 18 |
2 files changed, 70 insertions, 0 deletions
diff --git a/spec/support/mock/constant.rb b/spec/support/mock/constant.rb new file mode 100644 index 0000000000..c706ad29dd --- /dev/null +++ b/spec/support/mock/constant.rb @@ -0,0 +1,52 @@ +# 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, &block) + 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 + block.call + ensure + constants.each do |constant, val| + 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 new file mode 100644 index 0000000000..78b704ea9b --- /dev/null +++ b/spec/support/mock/platform.rb @@ -0,0 +1,18 @@ +# makes Chef think it's running on a certain platform..useful for unit testing +# platform-specific functionality. +# +# If a block is given yields to the block with +RUBY_PLATFORM+ set to +# 'i386-mingw32' (windows) or 'x86_64-darwin11.2.0' (unix). Usueful for +# testing code that mixes in platform specific modules like +Chef::Mixin::Securable+ +# or +Chef::FileAccessControl+ +def platform_mock(platform = :unix, &block) + Chef::Platform.stub!(:windows?).and_return(platform == :windows ? true : false) + ENV['SYSTEMDRIVE'] = (platform == :windows ? 'C:' : nil) + 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 +end |