| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright: (c) 2017, Erwan Quelin (@equelin) <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
#AnsibleRequires -CSharpUtil Ansible.Basic |
| 7 |
|
| 8 |
$spec = @{ |
| 9 |
options = @{ |
| 10 |
output_file = @{ type = "str" } |
| 11 |
output_format = @{ type = "str"; default = "NunitXML" } |
| 12 |
path = @{ type = "str"; required = $true } |
| 13 |
tags = @{ type = "list"; elements = "str" } |
| 14 |
test_parameters = @{ type = "dict" } |
| 15 |
version = @{ type = "str"; aliases = @(, "minimum_version") } |
| 16 |
} |
| 17 |
supports_check_mode = $true |
| 18 |
} |
| 19 |
|
| 20 |
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec) |
| 21 |
|
| 22 |
$output_file = $module.Params.output_file |
| 23 |
$output_format = $module.Params.output_format |
| 24 |
$path = $module.Params.path |
| 25 |
$tags = $module.Params.tags |
| 26 |
$test_parameters = $module.Params.test_parameters |
| 27 |
$version = $module.Params.version |
| 28 |
|
| 29 |
Try { |
| 30 |
$version = [version]$version |
| 31 |
} |
| 32 |
Catch { |
| 33 |
$module.FailJson("Value '$version' for parameter 'minimum_version' is not a valid version format") |
| 34 |
} |
| 35 |
|
| 36 |
# Make sure path is a real path |
| 37 |
Try { |
| 38 |
$path = $path.TrimEnd("\") |
| 39 |
$path = (Get-item -LiteralPath $path).FullName |
| 40 |
} |
| 41 |
Catch { |
| 42 |
$module.FailJson("Cannot find file or directory: '$path' as it does not exist") |
| 43 |
} |
| 44 |
|
| 45 |
# Import Pester module if available |
| 46 |
$Pester = 'Pester' |
| 47 |
|
| 48 |
If (-not (Get-Module -Name $Pester -ErrorAction SilentlyContinue)) { |
| 49 |
If (Get-Module -Name $Pester -ListAvailable -ErrorAction SilentlyContinue) { |
| 50 |
Import-Module $Pester |
| 51 |
} |
| 52 |
else { |
| 53 |
$msg = -join @( |
| 54 |
"Cannot find module: $Pester. Check if pester is installed, and if it is not, " |
| 55 |
"install using win_psmodule or chocolatey.chocolatey.win_chocolatey." |
| 56 |
) |
| 57 |
$module.FailJson($msg) |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
# Add actual pester's module version in the ansible's result variable |
| 62 |
$Pester_version = (Get-Module -Name $Pester).Version.ToString() |
| 63 |
$module.Result.pester_version = $Pester_version |
| 64 |
|
| 65 |
# Test if the Pester module is available with a version greater or equal than the one specified in the $version parameter |
| 66 |
If ((-not (Get-Module -Name $Pester -ErrorAction SilentlyContinue | Where-Object { $_.Version -ge $version })) -and ($version)) { |
| 67 |
$module.FailJson("$Pester version is not greater or equal to $version") |
| 68 |
} |
| 69 |
|
| 70 |
#Prepare Invoke-Pester parameters depending of the Pester's version. |
| 71 |
#Invoke-Pester output deactivation behave differently depending on the Pester's version |
| 72 |
If ($module.Result.pester_version -ge "4.0.0") { |
| 73 |
$Parameters = @{ |
| 74 |
"show" = "none" |
| 75 |
"PassThru" = $True |
| 76 |
} |
| 77 |
} |
| 78 |
else { |
| 79 |
$Parameters = @{ |
| 80 |
"quiet" = $True |
| 81 |
"PassThru" = $True |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
if ($tags.count) { |
| 86 |
$Parameters.Tag = $tags |
| 87 |
} |
| 88 |
|
| 89 |
if ($output_file) { |
| 90 |
$Parameters.OutputFile = $output_file |
| 91 |
$Parameters.OutputFormat = $output_format |
| 92 |
} |
| 93 |
|
| 94 |
# Run Pester tests |
| 95 |
If (Test-Path -LiteralPath $path -PathType Leaf) { |
| 96 |
$test_parameters_check_mode_msg = '' |
| 97 |
if ($test_parameters.keys.count) { |
| 98 |
$Parameters.Script = @{Path = $Path ; Parameters = $test_parameters } |
| 99 |
$test_parameters_check_mode_msg = " with $($test_parameters.keys -join ',') parameters" |
| 100 |
} |
| 101 |
else { |
| 102 |
$Parameters.Script = $Path |
| 103 |
} |
| 104 |
|
| 105 |
if ($module.CheckMode) { |
| 106 |
$module.Result.output = "Run pester test in the file: $path$test_parameters_check_mode_msg" |
| 107 |
} |
| 108 |
else { |
| 109 |
$module.Result.output = Invoke-Pester @Parameters |
| 110 |
} |
| 111 |
} |
| 112 |
else { |
| 113 |
$Parameters.Script = $path |
| 114 |
|
| 115 |
if ($module.CheckMode) { |
| 116 |
$module.Result.output = "Run Pester test(s): $path" |
| 117 |
} |
| 118 |
else { |
| 119 |
$module.Result.output = Invoke-Pester @Parameters |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
$module.Result.changed = $true |
| 124 |
|
| 125 |
$module.ExitJson() |