diff options
author | danielsdeleo <dan@getchef.com> | 2015-02-06 14:58:26 -0800 |
---|---|---|
committer | danielsdeleo <dan@getchef.com> | 2015-02-06 15:00:57 -0800 |
commit | 74db11c60c3d39f72e4d09c88aa642af60bbb49d (patch) | |
tree | 3090cd88a5c71b51f12a5c1b34dbd8069d5bc82c | |
parent | e89989bd04f428c0bc0cd5ce0c459c35ebd54afe (diff) | |
download | chef-74db11c60c3d39f72e4d09c88aa642af60bbb49d.tar.gz |
Add config to treat deprecation warnings as errors
-rw-r--r-- | lib/chef/config.rb | 15 | ||||
-rw-r--r-- | lib/chef/exceptions.rb | 2 | ||||
-rw-r--r-- | spec/spec_helper.rb | 6 | ||||
-rw-r--r-- | spec/unit/config_spec.rb | 39 |
4 files changed, 62 insertions, 0 deletions
diff --git a/lib/chef/config.rb b/lib/chef/config.rb index 3fa7309edb..1a4ec06d98 100644 --- a/lib/chef/config.rb +++ b/lib/chef/config.rb @@ -499,6 +499,21 @@ class Chef # Deprecated: default(:cache_options) { { :path => PathHelper.join(file_cache_path, "checksums") } } + # Whether errors should be raised for deprecation warnings. When set to + # `false` (the default setting), a warning is emitted but code using + # deprecated methods/features/etc. should work normally otherwise. When set + # to `true`, usage of deprecated methods/features will raise a + # `DeprecatedFeatureError`. This is used by Chef's tests to ensure that + # deprecated functionality is not used internally by Chef. End users + # should generally leave this at the default setting (especially in + # production), but it may be useful when testing cookbooks or other code if + # the user wishes to aggressively address deprecations. + default(:treat_deprecation_warnings_as_errors) do + # Using an environment variable allows this setting to be inherited in + # tests that spawn new processes. + ENV.key?("CHEF_TREAT_DEPRECATION_WARNINGS_AS_ERRORS") + end + # knife configuration data config_context :knife do default :ssh_port, nil diff --git a/lib/chef/exceptions.rb b/lib/chef/exceptions.rb index 0fa8696584..78d77e3778 100644 --- a/lib/chef/exceptions.rb +++ b/lib/chef/exceptions.rb @@ -212,6 +212,8 @@ class Chef class NoProviderAvailable < RuntimeError; end + class DeprecatedFeatureError < RuntimeError; end + class MissingRole < RuntimeError NULL = Object.new diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b87736efef..ad1f3f5d0d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -164,6 +164,12 @@ RSpec.configure do |config| config.before(:each) do Chef::Config.reset + + # By default, treat deprecation warnings as errors in tests. + Chef::Config.treat_deprecation_warnings_as_errors(true) + + # Set environment variable so the setting persists in child processes + ENV['CHEF_TREAT_DEPRECATION_WARNINGS_AS_ERRORS'] = "1" end config.before(:suite) do diff --git a/spec/unit/config_spec.rb b/spec/unit/config_spec.rb index ed2003e8bf..06178f7733 100644 --- a/spec/unit/config_spec.rb +++ b/spec/unit/config_spec.rb @@ -508,4 +508,43 @@ describe Chef::Config do end end end + + describe "Treating deprecation warnings as errors" do + + context "when using our default RSpec configuration" do + + it "defaults to treating deprecation warnings as errors" do + expect(Chef::Config[:treat_deprecation_warnings_as_errors]).to be(true) + end + + it "sets CHEF_TREAT_DEPRECATION_WARNINGS_AS_ERRORS environment variable" do + expect(ENV['CHEF_TREAT_DEPRECATION_WARNINGS_AS_ERRORS']).to eq("1") + end + + it "treats deprecation warnings as errors in child processes when testing" do + # Doing a full integration test where we launch a child process is slow + # and liable to break for weird reasons (bundler env stuff, etc.), so + # we're just checking that the presence of the environment variable + # causes treat_deprecation_warnings_as_errors to be set to true after a + # config reset. + Chef::Config.reset + expect(Chef::Config[:treat_deprecation_warnings_as_errors]).to be(true) + end + + end + + context "outside of our test environment" do + + before do + ENV.delete('CHEF_TREAT_DEPRECATION_WARNINGS_AS_ERRORS') + Chef::Config.reset + end + + it "defaults to NOT treating deprecation warnings as errors" do + expect(Chef::Config[:treat_deprecation_warnings_as_errors]).to be(false) + end + end + + + end end |