1 #!powershell 2 3 # Copyright: (c) 2017, Erwan Quelin (@equelin) <erwan.quelin@gmail.com> 4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) 5 6 #Requires -Module Ansible.ModuleUtils.Legacy 7 8 $ErrorActionPreference = 'Stop' 9 10 $params = Parse-Args -arguments $args -supports_check_mode $true 11 $check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false 12 $diff_mode = Get-AnsibleParam -obj $params -name "_ansible_diff" -type "bool" -default $false 13 14 # Modules parameters 15 16 $path = Get-AnsibleParam -obj $params -name "path" -type "str" -failifempty $true 17 $tags = Get-AnsibleParam -obj $params -name "tags" -type "list" 18 $test_parameters = Get-AnsibleParam -obj $params -name "test_parameters" -type "dict" 19 $minimum_version = Get-AnsibleParam -obj $params -name "minimum_version" -type "str" -failifempty $false 20 21 $result = @{ 22 changed = $false 23 } 24 25 if ($diff_mode) { 26 $result.diff = @{} 27 } 28 29 # CODE 30 # Test if parameter $version is valid 31 Try { 32 $minimum_version = [version]$minimum_version 33 } 34 Catch { 35 Fail-Json -obj $result -message "Value '$minimum_version' for parameter 'minimum_version' is not a valid version format" 36 } 37 38 # Import Pester module if available 39 $Module = 'Pester' 40 41 If (-not (Get-Module -Name $Module -ErrorAction SilentlyContinue)) { 42 If (Get-Module -Name $Module -ListAvailable -ErrorAction SilentlyContinue) { 43 Import-Module $Module 44 } else { 45 Fail-Json -obj $result -message "Cannot find module: $Module. Check if pester is installed, and if it is not, install using win_psmodule or win_chocolatey." 46 } 47 } 48 49 # Add actual pester's module version in the ansible's result variable 50 $Pester_version = (Get-Module -Name $Module).Version.ToString() 51 $result.pester_version = $Pester_version 52 53 # Test if the Pester module is available with a version greater or equal than the one specified in the $version parameter 54 If ((-not (Get-Module -Name $Module -ErrorAction SilentlyContinue | Where-Object {$_.Version -ge $minimum_version})) -and ($minimum_version)) { 55 Fail-Json -obj $result -message "$Module version is not greater or equal to $minimum_version" 56 } 57 58 # Testing if test file or directory exist 59 If (-not (Test-Path -LiteralPath $path)) { 60 Fail-Json -obj $result -message "Cannot find file or directory: '$path' as it does not exist" 61 } 62 63 #Prepare Invoke-Pester parameters depending of the Pester's version. 64 #Invoke-Pester output deactivation behave differently depending on the Pester's version 65 If ($result.pester_version -ge "4.0.0") { 66 $Parameters = @{ 67 "show" = "none" 68 "PassThru" = $True 69 } 70 } else { 71 $Parameters = @{ 72 "quiet" = $True 73 "PassThru" = $True 74 } 75 } 76 77 if($tags.count){ 78 $Parameters.Tag = $tags 79 } 80 # Run Pester tests 81 If (Test-Path -LiteralPath $path -PathType Leaf) { 82 $test_parameters_check_mode_msg = '' 83 if ($test_parameters.keys.count) { 84 $Parameters.Script = @{Path = $Path ; Parameters = $test_parameters } 85 $test_parameters_check_mode_msg = " with $($test_parameters.keys -join ',') parameters" 86 } 87 else { 88 $Parameters.Script = $Path 89 } 90 if ($check_mode) { 91 $result.output = "Run pester test in the file: $path$test_parameters_check_mode_msg" 92 } else { 93 try { 94 $result.output = Invoke-Pester @Parameters 95 } catch { 96 Fail-Json -obj $result -message $_.Exception 97 } 98 } 99 } else { 100 # Run Pester tests against all the .ps1 file in the local folder 101 $files = Get-ChildItem -Path $path | Where-Object {$_.extension -eq ".ps1"} 102 103 if ($check_mode) { 104 $result.output = "Run pester test(s) who are in the folder: $path" 105 } else { 106 try { 107 $result.output = Invoke-Pester $files.FullName @Parameters 108 } catch { 109 Fail-Json -obj $result -message $_.Exception 110 } 111 } 112 } 113 114 $result.changed = $true 115 116 Exit-Json -obj $result 117