win_shortcut: Fail when command is not absolute path (#26533)

This PR ensures the user gets a proper error when the `src` is not an absolute path.

And some cosmetic cleanup, and improve integration tests.
This commit is contained in:
Dag Wieers 2017-07-18 22:32:06 +02:00 committed by Matt Davis
commit 1e8713a50a
5 changed files with 345 additions and 181 deletions

View file

@ -28,13 +28,13 @@ $check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "b
$src = Get-AnsibleParam -obj $params -name "src"
$dest = Get-AnsibleParam -obj $params -name "dest" -type "path" -failifempty $true
$state = Get-AnsibleParam -obj $params -name "state" -type "string" -default "present" -validateset "present","absent"
$state = Get-AnsibleParam -obj $params -name "state" -type "string" -default "present" -validateset "absent","present"
$orig_args = Get-AnsibleParam -obj $params -name "args" -type "string"
$directory = Get-AnsibleParam -obj $params -name "directory" -type "path"
$hotkey = Get-AnsibleParam -obj $params -name "hotkey" -type "string"
$icon = Get-AnsibleParam -obj $params -name "icon" -type "path"
$orig_description = Get-AnsibleParam -obj $params -name "description" -type "string"
$windowstyle = Get-AnsibleParam -obj $params -name "windowstyle" -type "string" -validateset "normal","maximized","minimized"
$windowstyle = Get-AnsibleParam -obj $params -name "windowstyle" -type "string" -validateset "maximized","minimized","normal"
# Expand environment variables on non-path types
$args = Expand-Environment($orig_args)
@ -63,7 +63,7 @@ If ($state -eq "absent") {
Remove-Item -Path $dest -WhatIf:$check_mode
} Catch {
# Report removal failure
Fail-Json $result "Failed to remove shortcut $dest. (" + $_.Exception.Message + ")"
Fail-Json -obj $result -message "Failed to remove shortcut '$dest'. ($($_.Exception.Message))"
}
# Report removal success
$result.changed = $true
@ -82,6 +82,9 @@ If ($state -eq "absent") {
If (Get-Command -Name $src -Type Application -ErrorAction SilentlyContinue) {
$src = (Get-Command -Name $src -Type Application).Definition
}
If (-not (Split-Path -Path $src -IsAbsolute)) {
Fail-Json -obj $result -message "Source '$src' is not found in PATH and not an absolute path."
}
}
If ($src -ne $null -and $ShortCut.TargetPath -ne $src) {
@ -138,7 +141,7 @@ If ($state -eq "absent") {
Try {
$ShortCut.Save()
} Catch {
Fail-Json $result "Failed to create shortcut $dest. (" + $_.Exception.Message + ")"
Fail-Json -obj $result -message "Failed to create shortcut '$dest'. ($($_.Exception.Message))"
}
}
}