diff options
author | Ho-Sheng Hsiao <hosheng.hsiao@gmail.com> | 2012-04-04 17:34:21 -0400 |
---|---|---|
committer | Ho-Sheng Hsiao <hosheng.hsiao@gmail.com> | 2012-04-24 12:47:17 -0400 |
commit | faacb3b7d05040d5bd36159928bcee7a35020477 (patch) | |
tree | 61c3807ae994ccc7d0fdc28fb9473e996f74386c /chef | |
parent | 1172bb6b4459d0cf0a0dc8d44ac8454a58aa4ede (diff) | |
download | chef-faacb3b7d05040d5bd36159928bcee7a35020477.tar.gz |
[CHEF-3020][RSPEC] Reduced spec_helpers to essentials
If you need to add something shared globally, add it into a file in
spec/support. Since everything except for spec/support/platform gets
automatically loaded, there is no need to clutter up spec_helpers
Diffstat (limited to 'chef')
-rw-r--r-- | chef/spec/spec_helper.rb | 50 | ||||
-rw-r--r-- | chef/spec/support/chef_helpers.rb | 32 | ||||
-rw-r--r-- | chef/spec/support/platform_helpers.rb | 19 | ||||
-rw-r--r-- | chef/spec/support/shared/functional/knife.rb | 37 | ||||
-rw-r--r-- | chef/spec/unit/application/knife_spec.rb | 2 |
5 files changed, 96 insertions, 44 deletions
diff --git a/chef/spec/spec_helper.rb b/chef/spec/spec_helper.rb index f09c782bd7..bdef3eade0 100644 --- a/chef/spec/spec_helper.rb +++ b/chef/spec/spec_helper.rb @@ -14,7 +14,9 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# + +# If you need to add anything in here, don't. +# Add it to one of the files in spec/support # Abuse ruby's constant lookup to avoid undefined constant errors module Shef @@ -41,6 +43,9 @@ require 'chef/util/file_edit' Dir[File.join(File.dirname(__FILE__), 'lib', '**', '*.rb')].sort.each { |lib| require lib } +# Explicitly require spec helpers that need to load first +require 'spec/support/platform_helpers' + # Autoloads support files # Excludes support/platforms by default # Do not change the gsub. @@ -49,49 +54,6 @@ Dir["spec/support/**/*.rb"]. map { |f| f.gsub(%r{.rb$}, '') }. each { |f| require f } -CHEF_SPEC_DATA = File.expand_path(File.dirname(__FILE__) + "/data/") -CHEF_SPEC_BACKUP_PATH = File.join(Dir.tmpdir, 'test-backup-path') - -Chef::Config[:log_level] = :fatal -Chef::Config[:cache_type] = "Memory" -Chef::Config[:cache_options] = { } -Chef::Config[:persistent_queue] = false -Chef::Config[:file_backup_path] = CHEF_SPEC_BACKUP_PATH - -Chef::Log.level(Chef::Config.log_level) -Chef::Config.solo(false) - -Chef::Log.logger = Logger.new(StringIO.new) - -def windows? - if RUBY_PLATFORM =~ /mswin|mingw|windows/ - true - else - false - end -end - -DEV_NULL = windows? ? 'NUL' : '/dev/null' - -def redefine_argv(value) - Object.send(:remove_const, :ARGV) - Object.send(:const_set, :ARGV, value) -end - -def with_argv(*argv) - original_argv = ARGV - redefine_argv(argv.flatten) - begin - yield - ensure - redefine_argv(original_argv) - end -end - -def sha256_checksum(path) - Digest::SHA256.hexdigest(File.read(path)) -end - RSpec.configure do |config| config.include(Matchers) end diff --git a/chef/spec/support/chef_helpers.rb b/chef/spec/support/chef_helpers.rb new file mode 100644 index 0000000000..47d90a0953 --- /dev/null +++ b/chef/spec/support/chef_helpers.rb @@ -0,0 +1,32 @@ +# Copyright:: Copyright (c) 2008 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +CHEF_SPEC_DATA = File.expand_path(File.dirname(__FILE__) + "/../data/") +CHEF_SPEC_BACKUP_PATH = File.join(Dir.tmpdir, 'test-backup-path') + +Chef::Config[:log_level] = :fatal +Chef::Config[:cache_type] = "Memory" +Chef::Config[:cache_options] = { } +Chef::Config[:persistent_queue] = false +Chef::Config[:file_backup_path] = CHEF_SPEC_BACKUP_PATH + +Chef::Log.level(Chef::Config.log_level) +Chef::Config.solo(false) + +Chef::Log.logger = Logger.new(StringIO.new) + +def sha256_checksum(path) + Digest::SHA256.hexdigest(File.read(path)) +end diff --git a/chef/spec/support/platform_helpers.rb b/chef/spec/support/platform_helpers.rb new file mode 100644 index 0000000000..59bd663882 --- /dev/null +++ b/chef/spec/support/platform_helpers.rb @@ -0,0 +1,19 @@ +def ruby_19? + !!(RUBY_VERSION =~ /^1.9/) +end + +def ruby_18? + !!(RUBY_VERSION =~ /^1.8/) +end + +def windows? + !!(RUBY_PLATFORM =~ /mswin|mingw|windows/) +end + +# def jruby? + +def unix? + !windows? +end + +DEV_NULL = windows? ? 'NUL' : '/dev/null' diff --git a/chef/spec/support/shared/functional/knife.rb b/chef/spec/support/shared/functional/knife.rb new file mode 100644 index 0000000000..e96de7c27a --- /dev/null +++ b/chef/spec/support/shared/functional/knife.rb @@ -0,0 +1,37 @@ +# +# Author:: Adam Jacob (<adam@opscode.com>) +# Author:: AJ Christensen (<aj@junglist.gen.nz>) +# Author:: Ho-Sheng Hsiao (<hosh@opscode.com>) +# Copyright:: Copyright (c) 2008 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +module SpecHelpers + module Knife + def redefine_argv(value) + Object.send(:remove_const, :ARGV) + Object.send(:const_set, :ARGV, value) + end + + def with_argv(*argv) + original_argv = ARGV + redefine_argv(argv.flatten) + begin + yield + ensure + redefine_argv(original_argv) + end + end + end +end diff --git a/chef/spec/unit/application/knife_spec.rb b/chef/spec/unit/application/knife_spec.rb index 84ee44f746..e45a130cae 100644 --- a/chef/spec/unit/application/knife_spec.rb +++ b/chef/spec/unit/application/knife_spec.rb @@ -20,6 +20,8 @@ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_hel require "#{CHEF_SPEC_DATA}/knife_subcommand/test_yourself" describe Chef::Application::Knife do + include SpecHelpers::Knife + before(:all) do class NoopKnifeCommand < Chef::Knife def run |