Refactored WinRM test command filter

This commit is contained in:
Shawn Neal 2014-04-23 21:15:05 -07:00
parent f44c795eed
commit 4a2a147926
2 changed files with 58 additions and 19 deletions

View File

@ -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

View File

@ -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