summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth Chisamore <schisamo@opscode.com>2012-01-23 21:36:52 -0800
committerSeth Chisamore <schisamo@opscode.com>2012-01-23 21:36:52 -0800
commit42ced1f15801c544cd6a07434a305bc4a55c5f67 (patch)
tree70c984dd9b2779760ff401f3e47e5c04334113d3
parent8856bd5cd0026ff38ad9498bd4fe640181ced7e4 (diff)
parenta7aa68c5c1cba9ba6bbffa3fc32b557d8a91ad9e (diff)
downloadchef-42ced1f15801c544cd6a07434a305bc4a55c5f67.tar.gz
Merge branch 'improve-mock-helpers'
-rw-r--r--chef/spec/spec_helper.rb49
-rw-r--r--chef/spec/support/mock/constant.rb52
-rw-r--r--chef/spec/support/mock/platform.rb17
3 files changed, 72 insertions, 46 deletions
diff --git a/chef/spec/spec_helper.rb b/chef/spec/spec_helper.rb
index 75d8df87a9..c833671f6d 100644
--- a/chef/spec/spec_helper.rb
+++ b/chef/spec/spec_helper.rb
@@ -80,56 +80,13 @@ def with_argv(*argv)
end
end
-# Mock global constants!
-
-# Sets $VERBOSE for the duration of the block and back to its original value afterwards.
-#
-# https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/kernel/reporting.rb#L3-30
-def with_warnings(flag)
- old_verbose, $VERBOSE = $VERBOSE, flag
- yield
-ensure
- $VERBOSE = old_verbose
-end
-
-# http://digitaldumptruck.jotabout.com/?p=551
-def with_constants(constants, &block)
- saved_constants = {}
- constants.each do |constant, val|
- saved_constants[ constant ] = Object.const_get( constant )
- with_warnings(nil) { Object.const_set( constant, val ) }
- end
- begin
- block.call
- ensure
- constants.each do |constant, val|
- with_warnings(nil) { Object.const_set( constant, saved_constants[ constant ] ) }
- end
- end
-end
-####################
-
-# 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?
- with_constants :RUBY_PLATFORM => (platform == :windows ? 'i386-mingw32' : 'x86_64-darwin11.2.0') do
- yield
- end
- end
-end
-
def sha256_checksum(path)
Digest::SHA256.hexdigest(File.read(path))
end
+# load mock helpers
+Dir[File.join(File.dirname(__FILE__), 'support', 'mock','**', '*.rb')].sort.each { |lib| require lib }
+
# load shared contexts & examples
Dir[File.join(File.dirname(__FILE__), 'support', 'shared','**', '*.rb')].sort.each { |lib| require lib }
diff --git a/chef/spec/support/mock/constant.rb b/chef/spec/support/mock/constant.rb
new file mode 100644
index 0000000000..c706ad29dd
--- /dev/null
+++ b/chef/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/chef/spec/support/mock/platform.rb b/chef/spec/support/mock/platform.rb
new file mode 100644
index 0000000000..0717fb67af
--- /dev/null
+++ b/chef/spec/support/mock/platform.rb
@@ -0,0 +1,17 @@
+# 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 ? ';' : ":")}) do
+ yield
+ end
+ end
+end