@ -3,68 +3,68 @@
# Copyright: (c) 2017, Erwan Quelin (@equelin) <erwan.quelin@gmail.com>
# Copyright: (c) 2017, Erwan Quelin (@equelin) <erwan.quelin@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# Requires -Module Ansible.ModuleUtils.Legacy
#AnsibleRequires -CSharpUtil Ansible.Basic
$ErrorActionPreference = 'Stop'
$spec = @ {
options = @ {
$params = Parse-Args -arguments $args -supports_check_mode $true
output_file = @ { type = " str " }
$check_mode = Get-AnsibleParam -obj $params -name " _ansible_check_mode " -type " bool " -default $false
output_format = @ { type = " str " ; default = " NunitXML " }
$diff_mode = Get-AnsibleParam -obj $params -name " _ansible_diff " -type " bool " -default $false
path = @ { type = " str " ; required = $true }
tags = @ { type = " list " ; elements = " str " }
test_parameters = @ { type = " dict " }
version = @ { type = " str " ; aliases = @ ( , " minimum_version " ) }
}
supports_check_mode = $true
}
# Modules parameters
$module = [ Ansible.Basic.AnsibleModule ] :: Create ( $args , $spec )
$path = Get-AnsibleParam -obj $params -name " path " -type " str " -failifempty $true
$ output_file = $module . Params . output_fil e
$tags = Get-AnsibleParam -obj $params -name " tags " -type " list "
$ output_format = $module . Params . output_format
$output_file = Get-AnsibleParam -obj $params -name " output_file " -type " str "
$ path = $module . Params . path
$ output_format = Get-AnsibleParam -obj $params -name " output_format " -type " str " -default " NunitXML "
$ tags = $module . Params . tags
$test_parameters = Get-AnsibleParam -obj $params -name " test_parameters " -type " dict "
$test_parameters = $module . Params . test_parameters
$ minimum_version = Get-AnsibleParam -obj $params -name " minimum_version " -type " str " -failifempty $false
$ version = $module . Params . version
$result = @ {
Try {
changed = $false
$version = [ version ] $version
}
}
Catch {
if ( $diff_mode ) {
$module . FailJson ( " Value ' $version ' for parameter 'minimum_version' is not a valid version format " )
$result . diff = @ { }
}
}
# CODE
# Make sure path is a real path
# Test if parameter $version is valid
Try {
Try {
$minimum_version = [ version ] $minimum_version
$path = $path . TrimEnd ( " \ " )
$path = ( Get-item -LiteralPath $path ) . FullName
}
}
Catch {
Catch {
Fail-Json -obj $result -message " Value ' $minimum_version ' for parameter 'minimum_version' is not a valid version format "
$module . FailJson ( " Cannot find file or directory: ' $path ' as it does not exist " )
}
}
# Import Pester module if available
# Import Pester module if available
$ Module = 'Pester'
$ Pester = 'Pester'
If ( -not ( Get-Module -Name $ Module -ErrorAction SilentlyContinue ) ) {
If ( -not ( Get-Module -Name $ Pester -ErrorAction SilentlyContinue ) ) {
If ( Get-Module -Name $ Module -ListAvailable -ErrorAction SilentlyContinue ) {
If ( Get-Module -Name $ Pester -ListAvailable -ErrorAction SilentlyContinue ) {
Import-Module $ Module
Import-Module $ Pester
} else {
} else {
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. "
$module . FailJson ( " Cannot find module: $Pester . Check if pester is installed, and if it is not, install using win_psmodule or win_chocolatey. " )
}
}
}
}
# Add actual pester's module version in the ansible's result variable
# Add actual pester's module version in the ansible's result variable
$Pester_version = ( Get-Module -Name $ Module ) . Version . ToString ( )
$Pester_version = ( Get-Module -Name $ Pester ) . Version . ToString ( )
$ r esult. pester_version = $Pester_version
$ module. R esult. pester_version = $Pester_version
# Test if the Pester module is available with a version greater or equal than the one specified in the $version parameter
# Test if the Pester module is available with a version greater or equal than the one specified in the $version parameter
If ( ( -not ( Get-Module -Name $Module -ErrorAction SilentlyContinue | Where-Object { $_ . Version -ge $minimum_version } ) ) -and ( $minimum_version ) ) {
If ( ( -not ( Get-Module -Name $Pester -ErrorAction SilentlyContinue | Where-Object { $_ . Version -ge $version } ) ) -and ( $version ) ) {
Fail-Json -obj $result -message " $Module version is not greater or equal to $minimum_version "
$module . FailJson ( " $Pester version is not greater or equal to $version " )
}
# Testing if test file or directory exist
If ( -not ( Test-Path -LiteralPath $path ) ) {
Fail-Json -obj $result -message " Cannot find file or directory: ' $path ' as it does not exist "
}
}
#Prepare Invoke-Pester parameters depending of the Pester's version.
#Prepare Invoke-Pester parameters depending of the Pester's version.
#Invoke-Pester output deactivation behave differently depending on the Pester's version
#Invoke-Pester output deactivation behave differently depending on the Pester's version
If ( $ r esult. pester_version -ge " 4.0.0 " ) {
If ( $module . Result . pester_version -ge " 4.0.0 " ) {
$Parameters = @ {
$Parameters = @ {
" show " = " none "
" show " = " none "
" PassThru " = $True
" PassThru " = $True
@ -84,6 +84,7 @@ if($output_file){
$Parameters . OutputFile = $output_file
$Parameters . OutputFile = $output_file
$Parameters . OutputFormat = $output_format
$Parameters . OutputFormat = $output_format
}
}
# Run Pester tests
# Run Pester tests
If ( Test-Path -LiteralPath $path -PathType Leaf ) {
If ( Test-Path -LiteralPath $path -PathType Leaf ) {
$test_parameters_check_mode_msg = ''
$test_parameters_check_mode_msg = ''
@ -94,30 +95,22 @@ If (Test-Path -LiteralPath $path -PathType Leaf) {
else {
else {
$Parameters . Script = $Path
$Parameters . Script = $Path
}
}
if ( $check_mode ) {
$result . output = " Run pester test in the file: $path $test_parameters_check_mode_msg "
if ( $module . CheckMode ) {
$module . Result . output = " Run pester test in the file: $path $test_parameters_check_mode_msg "
} else {
} else {
try {
$module . Result . output = Invoke-Pester @Parameters
$result . output = Invoke-Pester @Parameters
} catch {
Fail-Json -obj $result -message $_ . Exception
}
}
}
} else {
} else {
# Run Pester tests against all the .ps1 file in the local folder
$Parameters . Script = $path
$files = Get-ChildItem -Path $path | Where-Object { $_ . extension -eq " .ps1 " }
if ( $ check_m ode) {
if ( $ module. CheckM ode) {
$ result. output = " Run pester test(s) who are in the folder : $path "
$ module. Result . output = " Run Pester test(s) : $path "
} else {
} else {
try {
$module . Result . output = Invoke-Pester @Parameters
$result . output = Invoke-Pester $files . FullName @Parameters
} catch {
Fail-Json -obj $result -message $_ . Exception
}
}
}
}
}
$ r esult. changed = $true
$module . Result . changed = $true
Exit-Json -obj $result
$module . ExitJson ( )