-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTest-Module.ps1
More file actions
98 lines (97 loc) · 4.79 KB
/
Test-Module.ps1
File metadata and controls
98 lines (97 loc) · 4.79 KB
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
#Requires -PSEdition Core
#Requires -Modules @{ ModuleName="Pester"; ModuleVersion="4.10.1" }
Param(
[Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = 'API key used for pester tests.')][ValidateNotNullOrEmpty()][System.String]$JCApiKey
, [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = 'OrgId used for pester tests.')][ValidateNotNullOrEmpty()][System.String]$JCOrgId
, [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = 'MTP API key used for select pester tests.')][ValidateNotNullOrEmpty()][System.String]$JCApiKeyMTP
, [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = 'MTP ProviderID used for select pester tests.')][ValidateNotNullOrEmpty()][System.String]$JCProviderId
, [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = 'ExcludeTagList for pester tests.')][ValidateNotNullOrEmpty()][System.String]$ExcludeTagList
, [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = 'IncludeTagList for pester tests.')][ValidateNotNullOrEmpty()][System.String]$IncludeTagList
, [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = 'Path to test-module.ps1')]
[ValidateNotNullOrEmpty()]
[System.IO.FileInfo]
[ValidateScript( {
If (-Not ($_ | Test-Path) )
{
Throw "File or folder does not exist: '$_'"
}
If (-Not ($_ | Test-Path -PathType Leaf) )
{
Throw "The Path argument must be a file. Folder paths are not allowed: $_"
}
If ($_ -notmatch "(test-module\.ps1)")
{
Throw "The file specified in the path argument must be test-module.ps1: $_"
}
Return $true
})]$testModulePath
)
# Create environmental variable so that they can be used by the pester tests
$ErrorActionPreference = 'Stop'
$env:JCApiKeyMTP = $JCApiKeyMTP
$env:JCProviderId = $JCProviderId
$env:JCApiKey = $JCApiKey
$env:JCOrgId = $JCOrgId
# Set Exclude Tags List to pass to tests
$env:ExcludeTagList = $ExcludeTagList
$env:IncludeTagList = $IncludeTagList
If (-not [System.String]::IsNullOrEmpty($env:JCApiKey) -and -not [System.String]::IsNullOrEmpty($env:JCOrgId) -and -not [System.String]::IsNullOrEmpty($env:JCApiKeyMTP) -and -not [System.String]::IsNullOrEmpty($env:JCProviderId))
{
Write-Host ('[VALIDATION] JCApiKey AND JCOrgId have been populated.') -BackgroundColor:('Black') -ForegroundColor:('Magenta')
# ./test-module.ps1 -Isolated # Not sure when to use this yet
# ./test-module.ps1 -Record # Run to create playback files
# ./test-module.ps1 -Playback # Run once playback files have been created
# ./test-module.ps1 -Live # Run to query against real API
# Now test Module
if ($testModulePath -Match "JumpCloud.SDK.V2") {
$sdkRoot = [System.IO.Path]::GetDirectoryName( (Resolve-Path $($testModulePath)))
$modules = Get-Module
foreach ($module in $modules)
{
If ($module.Name -eq "JumpCloud.SDK.V1")
{
Write-Host "Found $($module.name)"
Remove-Module $module.Name
}
}
Write-Host $sdkRoot
$NewPath = $sdkRoot.Replace('JumpCloud.SDK.V2', 'JumpCloud.SDK.V1')
$psd1Path = $NewPath + '/JumpCloud.SDK.V1.psd1'
Write-Host "new path: $NewPath"
Write-Host "pds1 path: $psd1Path"
Test-Path $psd1Path
Write-Host "PSD1 File:"
get-content $psd1Path -TotalCount 6 # just print out the header to see the generation date
try
{
Import-Module $psd1Path -Force -PassThru
}
catch
{
Import-Module $psd1Path -Force -PassThru
}
}
Invoke-Expression -Command:("$testModulePath -Live")
# Throw error if there were any failed tests
$PesterTestResultPath = (Get-ChildItem -Path:([System.IO.Path]::GetDirectoryName( (Resolve-Path $($testModulePath)) ) + "/test/results"))
If (Test-Path -Path:($PesterTestResultPath))
{
[xml]$PesterResults = Get-Content -Path:($PesterTestResultPath)
If ([int]$PesterResults.'testsuites'.failures -gt 0)
{
Write-Error ("Test Failures: $($PesterResults.'testsuites'.failures)")
}
If ([int]$PesterResults.'testsuites'.errors -gt 0)
{
Write-Error ("Test Errors: $($PesterResults.'testsuites'.errors)")
}
}
Else
{
Write-Error ("Unable to find file path: $PesterTestResultPath")
}
}
Else
{
Write-Error ("Unable to test because JCApiKey and JCOrgId have not been set.")
}