Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Ansible.ModuleUtils.FileUtil to respect ErrorAction if running in a try/catch #45451

Merged
merged 1 commit into from
Sep 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@ Function Get-AnsibleItem {
[Parameter(Mandatory=$true)][string]$Path
)
# Replacement for Get-Item
$file_attributes = [System.IO.File]::GetAttributes($Path)
try {
$file_attributes = [System.IO.File]::GetAttributes($Path)
} catch {
# if -ErrorAction SilentlyCotinue is set on the cmdlet and we failed to
# get the attributes, just return $null, otherwise throw the error
if ($ErrorActionPreference -ne "SilentlyContinue") {
throw $_
}
return $null
}
if ([Int32]$file_attributes -eq -1) {
throw New-Object -TypeName System.Management.Automation.ItemNotFoundException -ArgumentList "Cannot find path '$Path' because it does not exist."
} elseif ($file_attributes.HasFlag([System.IO.FileAttributes]::Directory)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,13 @@ Assert-Equals -actual $actual.FullName -expected C:\Windows
Assert-Equals -actual $actual.Attributes.HasFlag([System.IO.FileAttributes]::Directory) -expected $true
Assert-Equals -actual $actual.Exists -expected $true

# ensure Get-AnsibleItem doesn't fail in a try/catch and -ErrorAction SilentlyContinue - stop's a trap from trapping it
try {
$actual = Get-AnsibleItem -Path C:\fakepath -ErrorAction SilentlyContinue
} catch {
Fail-Json -obj $result -message "this should not fire"
}
Assert-Equals -actual $actual -expected $null

$result.data = "success"
Exit-Json -obj $result