Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

CHEF-3090: shellout loglevel compat #288

Merged
merged 4 commits into from almost 2 years ago

2 participants

Ho-Sheng Hsiao Bryan McLellan
Ho-Sheng Hsiao
Collaborator
hosh commented

Fixes CHEF-3090 (http://tickets.opscode.com/browse/CHEF-3090)

Chef 10.8 uses mixlib-shellout. However, certain option keys have now changed:

  • :command_log_level -> :log_level
  • :command_log_prepend -> :log_tag

This patch adds a compatibility layer. It will rewrite the legacy options and emit deprecation notices.

Bryan McLellan btm merged commit bfe2b06 into from
Bryan McLellan btm closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 4 unique commits by 2 authors.

May 07, 2012
Ho-Sheng Hsiao [CHEF-3090][SHELL_OUT] Mixin::ShellOut.shell_out! should filter legac…
…y run_command options
3bed217
May 09, 2012
Ho-Sheng Hsiao [CHEF-3090][SHELL_OUT] Mixin::ShellOut.shell_out! should convert lega…
…cy run_command options specified as strings
3d8958b
Ho-Sheng Hsiao [CHEF-3090][SHELL_OUT] Mixin::ShellOut.shell_out! should emit depreca…
…tion warnings when converting legacy run_command options
981cce0
May 10, 2012
Ho-Sheng Hsiao [CHEF-3090][RB187] Mixin::ShellOut spec should run under Ruby 1.8.7 bfe2b06
This page is out of date. Refresh to see the latest.
30  chef/lib/chef/mixin/shell_out.rb
@@ -23,7 +23,7 @@ module Mixin
23 23
     module ShellOut
24 24
 
25 25
       def shell_out(*command_args)
26  
-        cmd = Mixlib::ShellOut.new(*command_args)
  26
+        cmd = Mixlib::ShellOut.new(*run_command_compatible_options(command_args))
27 27
         if STDOUT.tty? && !Chef::Config[:daemon] && Chef::Log.debug?
28 28
           cmd.live_stream = STDOUT
29 29
         end
@@ -36,6 +36,34 @@ def shell_out!(*command_args)
36 36
         cmd.error!
37 37
         cmd
38 38
       end
  39
+
  40
+      DEPRECATED_OPTIONS =
  41
+        [ [:command_log_level,   :log_level],
  42
+          [:command_log_prepend, :log_tag] ]
  43
+
  44
+      # CHEF-3090: Deprecate command_log_level and command_log_prepend
  45
+      # Patterned after https://github.com/opscode/chef/commit/e1509990b559984b43e428d4d801c394e970f432
  46
+      def run_command_compatible_options(command_args)
  47
+        return command_args unless command_args.last.is_a?(Hash)
  48
+
  49
+        _command_args = command_args.dup
  50
+        _options = _command_args.last
  51
+
  52
+        DEPRECATED_OPTIONS.each do |old_option, new_option|
  53
+          # Edge case: someone specifies :command_log_level and 'command_log_level' in the option hash
  54
+          next unless value = _options.delete(old_option) || _options.delete(old_option.to_s)
  55
+          deprecate_option old_option, new_option
  56
+          _options[new_option] = value
  57
+        end
  58
+
  59
+        return _command_args
  60
+      end
  61
+
  62
+      private
  63
+
  64
+      def deprecate_option(old_option, new_option)
  65
+        Chef::Log.logger.warn "DEPRECATION: Chef::Mixin::ShellOut option :#{old_option} is deprecated. Use :#{new_option}"
  66
+      end
39 67
     end
40 68
   end
41 69
 end
109  chef/spec/unit/mixin/shell_out_spec.rb
... ...
@@ -0,0 +1,109 @@
  1
+#
  2
+# Author:: Ho-Sheng Hsiao (hosh@opscode.com)
  3
+# Code derived from spec/unit/mixin/command_spec.rb
  4
+#
  5
+# Original header:
  6
+# Author:: Hongli Lai (hongli@phusion.nl)
  7
+# Copyright:: Copyright (c) 2009 Phusion
  8
+# License:: Apache License, Version 2.0
  9
+#
  10
+# Licensed under the Apache License, Version 2.0 (the "License");
  11
+# you may not use this file except in compliance with the License.
  12
+# You may obtain a copy of the License at
  13
+#
  14
+#     http://www.apache.org/licenses/LICENSE-2.0
  15
+#
  16
+# Unless required by applicable law or agreed to in writing, software
  17
+# distributed under the License is distributed on an "AS IS" BASIS,
  18
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19
+# See the License for the specific language governing permissions and
  20
+# limitations under the License.
  21
+#
  22
+
  23
+require 'spec_helper'
  24
+
  25
+describe Chef::Mixin::ShellOut do
  26
+  include Chef::Mixin::ShellOut
  27
+
  28
+  describe '#run_command_compatible_options' do
  29
+    subject { run_command_compatible_options(command_args) }
  30
+    let(:command_args) { [ cmd, options ] }
  31
+    let(:cmd) { "echo '#{rand(1000)}'" }
  32
+
  33
+    let(:output) { StringIO.new }
  34
+    let(:capture_log_output) { Chef::Log.logger = Logger.new(output)  }
  35
+    let(:assume_deprecation_log_level) { Chef::Log.stub!(:level).and_return(:warn) }
  36
+
  37
+    context 'without options' do
  38
+      let(:command_args) { [ cmd ] }
  39
+
  40
+      it 'should not edit command args' do
  41
+        should eql(command_args)
  42
+      end
  43
+    end
  44
+
  45
+    context 'without deprecated options' do
  46
+      let(:options) { { :environment => environment } }
  47
+      let(:environment) { { 'LC_ALL' => 'C' } }
  48
+
  49
+      it 'should not edit command args' do
  50
+        should eql(command_args)
  51
+      end
  52
+    end
  53
+
  54
+    def self.should_emit_deprecation_warning_about(old_option, new_option)
  55
+      it 'should emit a deprecation warning' do
  56
+        assume_deprecation_log_level and capture_log_output
  57
+        subject
  58
+        output.string.should match /DEPRECATION:/
  59
+        output.string.should match Regexp.escape(old_option.to_s)
  60
+        output.string.should match Regexp.escape(new_option.to_s)
  61
+      end
  62
+    end
  63
+
  64
+    context 'with :command_log_level option' do
  65
+      let(:options) { { :command_log_level => command_log_level } }
  66
+      let(:command_log_level) { :warn }
  67
+
  68
+      it 'should convert :command_log_level to :log_level' do
  69
+        should eql [ cmd, { :log_level => command_log_level } ]
  70
+      end
  71
+
  72
+      should_emit_deprecation_warning_about :command_log_level, :log_level
  73
+    end
  74
+
  75
+    context 'with :command_log_prepend option' do
  76
+      let(:options) { { :command_log_prepend => command_log_prepend } }
  77
+      let(:command_log_prepend) { 'PROVIDER:' }
  78
+
  79
+      it 'should convert :command_log_prepend to :log_tag' do
  80
+        should eql [ cmd, { :log_tag => command_log_prepend } ]
  81
+      end
  82
+
  83
+      should_emit_deprecation_warning_about :command_log_prepend, :log_tag
  84
+    end
  85
+
  86
+    context "with 'command_log_level' option" do
  87
+      let(:options) { { 'command_log_level' => command_log_level } }
  88
+      let(:command_log_level) { :warn }
  89
+
  90
+      it "should convert 'command_log_level' to :log_level" do
  91
+        should eql [ cmd, { :log_level => command_log_level } ]
  92
+      end
  93
+
  94
+      should_emit_deprecation_warning_about :command_log_level, :log_level
  95
+    end
  96
+
  97
+    context "with 'command_log_prepend' option" do
  98
+      let(:options) { { 'command_log_prepend' => command_log_prepend } }
  99
+      let(:command_log_prepend) { 'PROVIDER:' }
  100
+
  101
+      it "should convert 'command_log_prepend' to :log_tag" do
  102
+        should eql [ cmd, { :log_tag => command_log_prepend } ]
  103
+      end
  104
+
  105
+      should_emit_deprecation_warning_about :command_log_prepend, :log_tag
  106
+    end
  107
+
  108
+  end
  109
+end
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.