summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJay Mundrawala <jdmundrawala@gmail.com>2014-08-20 21:40:51 -0700
committerJay Mundrawala <jdmundrawala@gmail.com>2014-09-19 12:47:33 -0700
commit7e5daa0ef8486275c82e694d1862d56b5a21f417 (patch)
tree1c4232d2b4e395c74127324066043aac358068b4
parent82f957acf2e17eeeb6415f804f082a5beb6b0d3d (diff)
downloadchef-7e5daa0ef8486275c82e694d1862d56b5a21f417.tar.gz
Added unit test for powershell cmdlet
-rw-r--r--lib/chef/util/powershell/cmdlet.rb4
-rw-r--r--spec/unit/util/powershell/cmdlet_spec.rb106
2 files changed, 108 insertions, 2 deletions
diff --git a/lib/chef/util/powershell/cmdlet.rb b/lib/chef/util/powershell/cmdlet.rb
index 0120f0aaf9..71bd876e40 100644
--- a/lib/chef/util/powershell/cmdlet.rb
+++ b/lib/chef/util/powershell/cmdlet.rb
@@ -90,7 +90,7 @@ class Chef::Util::Powershell
end
def escape_parameter_value(parameter_value)
- parameter_value.gsub(/\`|\'|\"/,'`\1')
+ parameter_value.gsub(/(`|'|"|#)/,'`\1')
end
def escape_string_parameter_value(parameter_value)
@@ -122,7 +122,7 @@ class Chef::Util::Powershell
raise ArgumentError, "Invalid argument type `#{switch_value.class}` specified for PowerShell switch `:#{switch_name.to_s}`. Arguments to PowerShell must be of type `String`, `Numeric`, `Float`, `FalseClass`, or `TrueClass`"
end
- switch_present ? ["-#{switch_name.to_s.downcase}", switch_argument].join(' ') : ''
+ switch_present ? ["-#{switch_name.to_s.downcase}", switch_argument].join(' ').strip : ''
end
command_switches.join(' ')
diff --git a/spec/unit/util/powershell/cmdlet_spec.rb b/spec/unit/util/powershell/cmdlet_spec.rb
new file mode 100644
index 0000000000..a964f607c8
--- /dev/null
+++ b/spec/unit/util/powershell/cmdlet_spec.rb
@@ -0,0 +1,106 @@
+#
+# Author:: Jay Mundrawala <jdm@getchef.com>
+# Copyright:: Copyright (c) 2014 Chef Software, 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.
+#
+
+require 'chef'
+require 'chef/util/powershell/cmdlet'
+
+describe Chef::Util::Powershell::Cmdlet do
+ before (:all) do
+ @node = Chef::Node.new
+ @cmdlet = Chef::Util::Powershell::Cmdlet.new(@node, 'Some-Commandlet')
+ end
+
+ describe '#validate_switch_name!' do
+ it 'should not raise an error if a name contains all upper case letters' do
+ @cmdlet.send(:validate_switch_name!, "HELLO")
+ end
+
+ it 'should not raise an error if the name contains all lower case letters' do
+ @cmdlet.send(:validate_switch_name!, "hello")
+ end
+
+ it 'should not raise an error if no special characters are used except _' do
+ @cmdlet.send(:validate_switch_name!, "hello_world")
+ end
+
+ %w{! @ # $ % ^ & * & * ( ) - = + \{ \} . ? < > \\ /}.each do |sym|
+ it "raises an Argument error if it configuration name contains #{sym}" do
+ expect {
+ @cmdlet.send(:validate_switch_name!, "Hello#{sym}")
+ }.to raise_error(ArgumentError)
+ end
+ end
+ end
+
+ describe '#escape_parameter_value' do
+ # Is this list really complete?
+ %w{` " # '}.each do |c|
+ it "escapse #{c}" do
+ @cmdlet.send(:escape_parameter_value, "stuff #{c}").should eql("stuff `#{c}")
+ end
+ end
+
+ it 'does not do anything to a string without special characters' do
+ @cmdlet.send(:escape_parameter_value, 'stuff').should eql('stuff')
+ end
+ end
+
+ describe '#escape_string_parameter_value' do
+ it "surrounds a string with ''" do
+ @cmdlet.send(:escape_string_parameter_value, 'stuff').should eql("'stuff'")
+ end
+ end
+
+ describe '#command_switches_string' do
+ it 'raises an ArgumentError if the key is not a symbol' do
+ expect {
+ @cmdlet.send(:command_switches_string, {'foo' => 'bar'})
+ }.to raise_error(ArgumentError)
+ end
+
+ it 'does not allow invalid switch names' do
+ expect {
+ @cmdlet.send(:command_switches_string, {:foo! => 'bar'})
+ }.to raise_error(ArgumentError)
+ end
+
+ it 'ignores switches with a false value' do
+ @cmdlet.send(:command_switches_string, {foo: false}).should eql('')
+ end
+
+ it 'should correctly handle a value type of string' do
+ @cmdlet.send(:command_switches_string, {foo: 'bar'}).should eql("-foo 'bar'")
+ end
+
+ it 'should correctly handle a value type of string even when it is 0 length' do
+ @cmdlet.send(:command_switches_string, {foo: ''}).should eql("-foo ''")
+ end
+
+ it 'should not quote integers' do
+ @cmdlet.send(:command_switches_string, {foo: 1}).should eql("-foo 1")
+ end
+
+ it 'should not quote floats' do
+ @cmdlet.send(:command_switches_string, {foo: 1.0}).should eql("-foo 1.0")
+ end
+
+ it 'has just the switch when the value is true' do
+ @cmdlet.send(:command_switches_string, {foo: true}).should eql("-foo")
+ end
+ end
+end