From 4a2a147926c901201488af9e7e60ced41adc0127 Mon Sep 17 00:00:00 2001 From: Shawn Neal Date: Wed, 23 Apr 2014 21:15:05 -0700 Subject: [PATCH] Refactored WinRM test command filter --- .../winrm/command_filters/test.rb | 53 +++++++++++++++---- .../winrm/command_filter_test.rb | 24 ++++++--- 2 files changed, 58 insertions(+), 19 deletions(-) diff --git a/plugins/communicators/winrm/command_filters/test.rb b/plugins/communicators/winrm/command_filters/test.rb index 76850be06..23fddc8bf 100644 --- a/plugins/communicators/winrm/command_filters/test.rb +++ b/plugins/communicators/winrm/command_filters/test.rb @@ -12,24 +12,55 @@ module VagrantPlugins # test -x /tmp/some.exe cmd_parts = command.strip.split(/\s+/) - if cmd_parts[1] == '-d' - # ensure it exists and is a directory - return "if ((Test-Path \"#{cmd_parts[2]}\") -and (get-item \"#{cmd_parts[2]}\").PSIsContainer) { exit 0 } exit 1" - elsif cmd_parts[1] == '-f' || cmd_parts[1] == '-x' - # ensure it exists and is a file - return "if ((Test-Path \"#{cmd_parts[2]}\") -and (!(get-item \"#{cmd_parts[2]}\").PSIsContainer)) { exit 0 } exit 1" + flag = cmd_parts[1] + path = cmd_parts[2] + + if flag == '-d' + check_for_directory(path) + elsif flag == '-f' || flag == '-x' + check_for_file(path) + else + check_exists(path) end - - # otherwise, just check for existence - return "if (Test-Path \"#{cmd_parts[2]}\") { exit 0 } exit 1" end - # if (Test-Path 'c:\windows' && (get-item 'c:\windows').PSIsContainer) { Write-Host 0 } Write-Host 1 - def accept?(command) command.start_with?("test ") end + + private + + def check_for_directory(path) + <<-EOH + $p = "#{path}" + if ((Test-Path $p) -and (get-item $p).PSIsContainer) { + exit 0 + } + exit 1 + EOH + end + + def check_for_file(path) + <<-EOH + $p = "#{path}" + if ((Test-Path $p) -and (!(get-item $p).PSIsContainer)) { + exit 0 + } + exit 1 + EOH + end + + def check_exists(path) + <<-EOH + $p = "#{path}" + if (Test-Path $p) { + exit 0 + } + exit 1 + EOH + end + end end diff --git a/test/unit/plugins/communicators/winrm/command_filter_test.rb b/test/unit/plugins/communicators/winrm/command_filter_test.rb index f226b8e9d..e56d1cd10 100644 --- a/test/unit/plugins/communicators/winrm/command_filter_test.rb +++ b/test/unit/plugins/communicators/winrm/command_filter_test.rb @@ -25,23 +25,31 @@ describe VagrantPlugins::CommunicatorWinRM::CommandFilter, :unit => true do end it 'filters out test -d commands' do - expect(subject.filter('test -d /tmp/dir')).to eq( - "if ((Test-Path '/tmp/dir') -and (get-item '/tmp/dir').PSIsContainer) { exit 0 } exit 1") + expect(subject.filter('test -d /tmp/dir')).to include( + "$p = \"/tmp/dir\"") + expect(subject.filter('test -d /tmp/dir')).to include( + "if ((Test-Path $p) -and (get-item $p).PSIsContainer) {") end it 'filters out test -f commands' do - expect(subject.filter('test -f /tmp/file.txt')).to eq( - "if ((Test-Path '/tmp/file.txt') -and (!(get-item '/tmp/file.txt').PSIsContainer)) { exit 0 } exit 1") + expect(subject.filter('test -f /tmp/file.txt')).to include( + "$p = \"/tmp/file.txt\"") + expect(subject.filter('test -f /tmp/file.txt')).to include( + "if ((Test-Path $p) -and (!(get-item $p).PSIsContainer)) {") end it 'filters out test -x commands' do - expect(subject.filter('test -x /tmp/file.txt')).to eq( - "if ((Test-Path '/tmp/file.txt') -and (!(get-item '/tmp/file.txt').PSIsContainer)) { exit 0 } exit 1") + expect(subject.filter('test -x /tmp/file.txt')).to include( + "$p = \"/tmp/file.txt\"") + expect(subject.filter('test -x /tmp/file.txt')).to include( + "if ((Test-Path $p) -and (!(get-item $p).PSIsContainer)) {") end it 'filters out other test commands' do - expect(subject.filter('test -L /tmp/file.txt')).to eq( - "if (Test-Path '/tmp/file.txt') { exit 0 } exit 1") + expect(subject.filter('test -L /tmp/file.txt')).to include( + "$p = \"/tmp/file.txt\"") + expect(subject.filter('test -L /tmp/file.txt')).to include( + "if (Test-Path $p) {") end it 'filters out rm -Rf commands' do