Debian Codegraph @

test/integration/targets/powershell_exec_wrapper/library/test_fail.ps1

raw
1 #!powershell
2
3 #Requires -Module Ansible.ModuleUtils.Legacy
4
5 $params = Parse-Args $args -supports_check_mode $true
6
7 $data = Get-AnsibleParam -obj $params -name "data" -type "str" -default "normal"
8 $result = @{
9 changed = $false
10 }
11
12 $executable = if ($PSVersionTable.PSVersion -lt '6.0') {
13 'powershell.exe'
14 }
15 else {
16 "pwsh$(if ($IsWindows) { ".exe" })"
17 }
18 $executablePath = Join-Path -Path $PSHome -ChildPath $executable
19
20 <#
21 This module tests various error events in PowerShell to verify our hidden trap
22 catches them all and outputs a pretty error message with a traceback to help
23 users debug the actual issue
24
25 normal - normal execution, no errors
26 fail - Calls Fail-Json like normal
27 throw - throws an exception
28 error - Write-Error with ErrorActionPreferenceStop
29 cmdlet_error - Calls a Cmdlet with an invalid error
30 dotnet_exception - Calls a .NET function that will throw an error
31 function_throw - Throws an exception in a function
32 proc_exit_fine - calls an executable with a non-zero exit code with Exit-Json
33 proc_exit_fail - calls an executable with a non-zero exit code with Fail-Json
34 #>
35
36 Function Test-ThrowException {
37 throw "exception in function"
38 }
39
40 if ($data -eq "normal") {
41 Exit-Json -obj $result
42 }
43 elseif ($data -eq "fail") {
44 Fail-Json -obj $result -message "fail message"
45 }
46 elseif ($data -eq "throw") {
47 throw [ArgumentException]"module is thrown"
48 }
49 elseif ($data -eq "error") {
50 Write-Error -Message $data
51 }
52 elseif ($data -eq "cmdlet_error") {
53 Get-Item -Path "fake:\path"
54 }
55 elseif ($data -eq "dotnet_exception") {
56 [System.IO.Path]::GetFullPath($null)
57 }
58 elseif ($data -eq "function_throw") {
59 Test-ThrowException
60 }
61 elseif ($data -eq "proc_exit_fine") {
62 # verifies that if no error was actually fired and we have an output, we
63 # don't use the RC to validate if the module failed
64
65 & $executablePath -Command 'exit 2'
66 Exit-Json -obj $result
67 }
68 elseif ($data -eq "proc_exit_fail") {
69 & $executablePath -Command 'exit 2'
70 Fail-Json -obj $result -message "proc_exit_fail"
71 }
72
73 # verify no exception were silently caught during our tests
74 Fail-Json -obj $result -message "end of module"
75