Refactored WinRM test command filter
This commit is contained in:
parent
f44c795eed
commit
4a2a147926
|
@ -12,24 +12,55 @@ module VagrantPlugins
|
||||||
# test -x /tmp/some.exe
|
# test -x /tmp/some.exe
|
||||||
|
|
||||||
cmd_parts = command.strip.split(/\s+/)
|
cmd_parts = command.strip.split(/\s+/)
|
||||||
if cmd_parts[1] == '-d'
|
flag = cmd_parts[1]
|
||||||
# ensure it exists and is a directory
|
path = cmd_parts[2]
|
||||||
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'
|
if flag == '-d'
|
||||||
# ensure it exists and is a file
|
check_for_directory(path)
|
||||||
return "if ((Test-Path \"#{cmd_parts[2]}\") -and (!(get-item \"#{cmd_parts[2]}\").PSIsContainer)) { exit 0 } exit 1"
|
elsif flag == '-f' || flag == '-x'
|
||||||
|
check_for_file(path)
|
||||||
|
else
|
||||||
|
check_exists(path)
|
||||||
end
|
end
|
||||||
|
|
||||||
# otherwise, just check for existence
|
|
||||||
return "if (Test-Path \"#{cmd_parts[2]}\") { exit 0 } exit 1"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# if (Test-Path 'c:\windows' && (get-item 'c:\windows').PSIsContainer) { Write-Host 0 } Write-Host 1
|
|
||||||
|
|
||||||
def accept?(command)
|
def accept?(command)
|
||||||
command.start_with?("test ")
|
command.start_with?("test ")
|
||||||
end
|
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -25,23 +25,31 @@ describe VagrantPlugins::CommunicatorWinRM::CommandFilter, :unit => true do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'filters out test -d commands' do
|
it 'filters out test -d commands' do
|
||||||
expect(subject.filter('test -d /tmp/dir')).to eq(
|
expect(subject.filter('test -d /tmp/dir')).to include(
|
||||||
"if ((Test-Path '/tmp/dir') -and (get-item '/tmp/dir').PSIsContainer) { exit 0 } exit 1")
|
"$p = \"/tmp/dir\"")
|
||||||
|
expect(subject.filter('test -d /tmp/dir')).to include(
|
||||||
|
"if ((Test-Path $p) -and (get-item $p).PSIsContainer) {")
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'filters out test -f commands' do
|
it 'filters out test -f commands' do
|
||||||
expect(subject.filter('test -f /tmp/file.txt')).to eq(
|
expect(subject.filter('test -f /tmp/file.txt')).to include(
|
||||||
"if ((Test-Path '/tmp/file.txt') -and (!(get-item '/tmp/file.txt').PSIsContainer)) { exit 0 } exit 1")
|
"$p = \"/tmp/file.txt\"")
|
||||||
|
expect(subject.filter('test -f /tmp/file.txt')).to include(
|
||||||
|
"if ((Test-Path $p) -and (!(get-item $p).PSIsContainer)) {")
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'filters out test -x commands' do
|
it 'filters out test -x commands' do
|
||||||
expect(subject.filter('test -x /tmp/file.txt')).to eq(
|
expect(subject.filter('test -x /tmp/file.txt')).to include(
|
||||||
"if ((Test-Path '/tmp/file.txt') -and (!(get-item '/tmp/file.txt').PSIsContainer)) { exit 0 } exit 1")
|
"$p = \"/tmp/file.txt\"")
|
||||||
|
expect(subject.filter('test -x /tmp/file.txt')).to include(
|
||||||
|
"if ((Test-Path $p) -and (!(get-item $p).PSIsContainer)) {")
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'filters out other test commands' do
|
it 'filters out other test commands' do
|
||||||
expect(subject.filter('test -L /tmp/file.txt')).to eq(
|
expect(subject.filter('test -L /tmp/file.txt')).to include(
|
||||||
"if (Test-Path '/tmp/file.txt') { exit 0 } exit 1")
|
"$p = \"/tmp/file.txt\"")
|
||||||
|
expect(subject.filter('test -L /tmp/file.txt')).to include(
|
||||||
|
"if (Test-Path $p) {")
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'filters out rm -Rf commands' do
|
it 'filters out rm -Rf commands' do
|
||||||
|
|
Loading…
Reference in New Issue