-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.ps1
More file actions
144 lines (121 loc) · 5.07 KB
/
test.ps1
File metadata and controls
144 lines (121 loc) · 5.07 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<#
.SYNOPSIS
Run tests
.DESCRIPTION
Run the unit test of the actual module
.NOTES
Using TestingHelper this script will search for a Test module and run the tests
This script will be referenced from launch.json to run the tests on VSCode
.LINK
https://raw.githubusercontent.com/rulasg/StagingModule/main/test.ps1
.EXAMPLE
> ./test.ps1
#>
[CmdletBinding()]
param (
[Parameter()][switch]$ShowTestErrors
)
function Set-TestName{
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '', Scope='Function')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Scope='Function')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '', Scope='Function')]
[Alias("st")]
param (
[Parameter(Position=0,ValueFromPipeline)][string]$TestName
)
process{
$global:TestName = $TestName
}
}
function Get-TestName{
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '', Scope='Function')]
[Alias("gt")]
param (
)
process{
$global:TestName
}
}
function Clear-TestName{
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '', Scope='Function')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Scope='Function')]
[Alias("ct")]
param (
)
$global:TestName = $null
}
function Import-RequiredModule{
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Scope='Function')]
param (
[Parameter(ParameterSetName = "HT", ValueFromPipeline)][hashtable]$RequiredModule,
[Parameter(ParameterSetName = "RM",Position = 0)][string]$ModuleName,
[Parameter(ParameterSetName = "RM")][string]$ModuleVersion,
[Parameter(ParameterSetName = "HT")]
[Parameter(ParameterSetName = "RM")]
[switch]$AllowPrerelease,
[Parameter(ParameterSetName = "HT")]
[Parameter(ParameterSetName = "RM")]
[switch]$PassThru
)
process{
# Powershell module manifest does not allow versions with prerelease tags on them.
# Powershell modle manifest does not allow to add a arbitrary field to specify prerelease versions.
# Valid value (ModuleName, ModuleVersion, RequiredVersion, GUID)
# There is no way to specify a prerelease required module.
if($RequiredModule){
$ModuleName = $RequiredModule.ModuleName
$ModuleVersion = [string]::IsNullOrWhiteSpace($RequiredModule.RequiredVersion) ? $RequiredModule.ModuleVersion : $RequiredModule.RequiredVersion
}
"Importing module Name[{0}] Version[{1}] AllowPrerelease[{2}]" -f $ModuleName, $ModuleVersion, $AllowPrerelease | Write-Host -ForegroundColor DarkGray
# Following semVer we can manually specidy a taged version to specify that is prerelease
# Extract the semVer from it and set AllowPrerelease to true
if ($ModuleVersion) {
$V = $ModuleVersion.Split('-')
$semVer = $V[0]
$AllowPrerelease = ($AllowPrerelease -or ($null -ne $V[1]))
}
$module = Import-Module $ModuleName -PassThru -ErrorAction SilentlyContinue -MinimumVersion:$semVer -MaximumVersion:$semVer
if ($null -eq $module) {
"Installing module Name[{0}] Version[{1}] AllowPrerelease[{2}]" -f $ModuleName, $ModuleVersion, $AllowPrerelease | Write-Host -ForegroundColor DarkGray
$installed = Install-Module -Name $ModuleName -Force -AllowPrerelease:$AllowPrerelease -passThru -RequiredVersion:$ModuleVersion
$module = $installed | ForEach-Object {Import-Module -Name $_.Name -RequiredVersion ($_.Version.Split('-')[0]) -Force -PassThru}
}
"Imported module Name[{0}] Version[{1}] PreRelease[{2}]" -f $module.Name, $module.Version, $module.privatedata.psdata.prerelease | Write-Host -ForegroundColor DarkGray
if ($PassThru) {
$module
}
}
}
<#
. SYNOPSIS
Extracts the required modules from the module manifest
#>
function Get-RequiredModule{
[CmdletBinding()]
[OutputType([Object[]])]
param()
# Required Modules
$localPath = $PSScriptRoot
$manifest = $localPath | Join-Path -child "*.psd1" | Get-Item | Import-PowerShellDataFile
$requiredModule = $null -eq $manifest.RequiredModules ? @() : $manifest.RequiredModules
# Convert to hashtable
$ret = @()
$requiredModule | ForEach-Object{
$ret += $_ -is [string] ? @{ ModuleName = $_ } : $_
}
return $ret
}
# Install and load TestingHelper
# Import-RequiredModule -Name TestingHelper -AllowPrerelease
Import-RequiredModule "TestingHelper" -AllowPrerelease -ModuleVersion "3.0.10-preview"
# Install and Load Module dependencies
Get-RequiredModule | Import-RequiredModule -AllowPrerelease
if($TestName){
Invoke-TestingHelper -TestName $TestName -ShowTestErrors:$ShowTestErrors
} else {
Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors
}