forked from dsccommunity/SqlServerDsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assert-TestEnvironment.ps1
105 lines (86 loc) · 3.7 KB
/
Assert-TestEnvironment.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<#
.SYNOPSIS
Assert that the test environment is properly setup and loaded into the
PowerShell session.
.DESCRIPTION
Assert that the test environment is properly setup and loaded into the
PowerShell session.
.EXAMPLE
.\Assert-testEnvironment.ps1
Will assert that the current PowerShell session is ready to run tests.
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param
(
)
#region Verify prerequisites Pester
$pesterModuleName = 'Pester'
# This is the minimum version that can be used with the tests in this repo.
$pesterModuleMinimumVersion = '4.0.2'
<#
Pester v4.4.0 has a fix for '-Not -Throw' so it shows the actual error
message if an unexpected exception does occur. It will help when debugging
tests.
If no Pester module exist, then use this as the minimum version.
#>
$pesterModuleRecommendedMinimumVersion = '4.4.0'
$pesterModule = Get-Module $pesterModuleName -ListAvailable -Verbose:$false |
Where-Object -Property 'Version' -GE -Value $pesterModuleMinimumVersion |
Sort-Object -Property 'Version' -Descending |
Select-Object -First 1
$dependencyMissing = $false
if (-not $pesterModule)
{
<#
Not installing the module here because it's not known what scope the
user want (can) to install the module in.
#>
$message = 'Missing a compatible version of the {0} module. Minimum version of {0} module can be ''{2}'', but the recommended minimum version is ''{1}''.' -f $pesterModuleName, $pesterModuleRecommendedMinimumVersion, $pesterModuleMinimumVersion
Write-Warning -Message $message
$dependencyMissing = $true
}
else
{
Write-Verbose -Message ('A compatible {0} module is already installed (v{1}). If you want to use a newer version of {0} module, please install it manually.' -f $pesterModule.Name, $pesterModule.Version)
}
#endregion Verify prerequisites Pester
#region Verify prerequisites PSDepend
$psDependModuleName = 'PSDepend'
# This is the minimum version that can be used with the tests in this repo.
$psDependModuleMinimumVersion = '0.3.0'
$psDependModuleRecommendedMinimumVersion = 'latest'
$psDependModule = Get-Module $psDependModuleName -ListAvailable -Verbose:$false |
Where-Object -Property 'Version' -GE -Value $psDependModuleMinimumVersion |
Sort-Object -Property 'Version' -Descending |
Select-Object -First 1
if (-not $psDependModule)
{
<#
Not installing the module here because it's not known what scope the
user want (can) to install the module in.
#>
$message = 'Missing a compatible version of the {0} module. Minimum version of {0} module can be ''{2}'', but the recommended minimum version is ''{1}''. Please install {0} module manually, then run this script again.' -f $psDependModuleName, $psDependModuleRecommendedMinimumVersion, $psDependModuleMinimumVersion
Write-Warning -Message $message
$dependencyMissing = $true
}
else
{
Write-Verbose -Message ('A compatible {0} module is already installed (v{1}). If you want to use a newer version of {0} module, please install it manually.' -f $psDependModule.Name, $psDependModule.Version)
}
#endregion Verify prerequisites PSDepend
if ($dependencyMissing)
{
Write-Output -InputObject 'Please install the necessary dependencies manually, then run this script again.'
return
}
$dependenciesPath = Join-Path $PSScriptRoot -ChildPath 'Tests'
Write-Verbose -Message ('Running Invoke-PSDepend using dependencies found under the path ''{0}''.' -f $dependenciesPath)
if ($PSBoundParameters.ContainsKey('Confirm'))
{
$invokePSDependConfirmation = $ConfirmPreference
}
else
{
$invokePSDependConfirmation = $false
}
Invoke-PSDepend -Path $dependenciesPath -Confirm:$invokePSDependConfirmation