8000 Initial drop of PowerShellGet Pester tests from Windows Source Depot … · PowerShell/PowerShellGetv2@785d3e6 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jun 13, 2024. It is now read-only.

Commit 785d3e6

Browse files
authored
Initial drop of PowerShellGet Pester tests from Windows Source Depot (#31)
Initial drop of PowerShellGet Pester tests from Windows Source Depot
1 parent b968632 commit 785d3e6

File tree

83 files changed

+8050
-23
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+8050
-23
lines changed

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ powershell.version
4040
# generated man files
4141
/assets/powershell.1*
4242

43+
# Modified .cat and .psd1 files during test run
44+
/Tests/TestModules/Microsoft.PowerShell.Archive/1.0.1.5/Microsoft.PowerShell.Archive.cat
45+
/Tests/TestModules/Microsoft.PowerShell.Archive/1.0.1.5/Microsoft.PowerShell.Archive.psd1
46+
/Tests/TestModules/TestArchiveModule/1.0.1.1/TestArchiveModule.cat
47+
/Tests/TestModules/TestArchiveModule/1.0.1.1/TestArchiveModule.psd1
48+
/Tests/TestModules/TestArchiveModule/1.0.1.11/TestArchiveModule.cat
49+
/Tests/TestModules/TestArchiveModule/1.0.1.11/TestArchiveModule.psd1
50+
/Tests/TestModules/TestArchiveModule/1.0.1.2/TestArchiveModule.cat
51+
/Tests/TestModules/TestArchiveModule/1.0.1.2/TestArchiveModule.psd1
52+
/Tests/TestModules/TestArchiveModule/1.0.1.5/TestArchiveModule.cat
53+
/Tests/TestModules/TestArchiveModule/1.0.1.5/TestArchiveModule.psd1
54+
4355
# resgen output
4456
gen
4557

Tests/Asserts.psm1

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
2+
###########################################
3+
# #
4+
# Assert Functions #
5+
# #
6+
# Copyright (C) Microsoft Corporation, 2016 #
7+
# #
8+
###########################################
9+
10+
#
11+
# Assert <bool>
12+
#
13+
# AssertEquals <object1> <object2>
14+
#
15+
# AssertEqualsCaseInsensitive <object1> <object2>
16+
#
17+
# AssertNotEquals <object1> <object2>
18+
#
19+
# AssertNotEqualsCaseInsensitive <object1> <object2>
20+
#
21+
# AssertNotNull <object>
22+
#
23+
# AssertFullyQualifiedErrorIdEquals <scriptblock> <expectedFullyQualifiedErrorId>
24+
#
25+
26+
#
27+
# Converts a callstack into a simple string we can use in the error message of the form:
28+
# bar.ps1: Line 1 <- foo.ps1: Line 1 <- prompt
29+
#
30+
function CallStackToString
31+
{
32+
param($callstack)
33+
34+
$str = @($callStack | foreach { $_.Location }) -join ' <- ';
35+
return $str;
36+
}
37+
38+
#
39+
# Creates a new exception for when the Assert fails
40+
#
41+
function NewException
42+
{
43+
param
44+
(
45+
$message
46+
)
47+
48+
$callstack = get-pscallstack
49+
$callStackStr = CallStackToString ($callstack | select -skip 1);
50+
$errMessage = $message;
51+
$exception = new-object System.Management.Automation.RuntimeException $errMessage;
52+
$exception.Data['PSCallStack'] = $callstack;
53+
54+
return $exception;
55+
}
56+
57+
58+
# Usage: Assert <bool> <message>
59+
#
60+
function Assert
61+
{
62+
$errMessage = '';
63+
64+
if ($args.Length -ne 2)
65+
{
66+
$errMessage = "Assert takes two parameters."
67+
}
68+
69+
if (!$args[0])
70+
{
71+
$errMessage = $args[1]
72+
}
73+
74+
if ($errMessage)
75+
{
76+
throw (NewException $errMessage);
77+
}
78+
}
79+
80+
# Usage: AssertFullyQualifiedErrorIdEquals <scriptblock> <expectedFullyQualifiedErrorId>
81+
#
82+
function AssertFullyQualifiedErrorIdEquals([scriptblock]$scriptblock, [string]$expectedFullyQualifiedErrorId)
83+
{
84+
# Save old error action preference to restore it in the finally block.
85+
$oldErrorActionPreference = $ErrorActionPreference
86+
87+
try
88+
{
89+
$ErrorActionPreference = "Continue"
90+
$myError = $null
91+
$myError = . { $out = & $scriptblock } 2>&1
92+
}
93+
catch
94+
{
95+
$myError = $_
96+
if ($myError -eq $null)
97+
{
98+
throw (NewException "No error records were writen for the given script block: $scriptblock");
99+
return
100+
}
101+
102+
$message = "FullyQualifiedId does not match: Excepted '" + $expectedFullyQualifiedErrorId + "' and got '" + $myError.FullyQualifiedErrorId + "'"
103+
AssertEquals $myError.FullyQualifiedErrorId $expectedFullyQualifiedErrorId $message
104+
}
105+
finally
106+
{
107+
$ErrorActionPreference = $oldErrorActionPreference
108+
}
109+
}
110+
111+
# Usage: AssertEquals <object1> <object2> <message>
112+
#
113+
function AssertEquals
114+
{
115+
Assert ($args.Length -ge 2 -and $args.Length -le 3) "AssertEquals takes either two or three parameters."
116+
if ($args.Length -eq 2)
117+
{
118+
Assert ($args[0] -ceq $args[1]) ("'" + $args[0] + "' does not equal '" + $args[1] + "'")
119+
}
120+
else
121+
{
122+
Assert ($args[0] -ceq $args[1]) ($args[2] + ": " + "'" + $args[0] + "' does not equal '" + $args[1] + "'")
123+
}
124+
}
125+
126+
# Usage: AssertEqualsCaseInsensitive <object1> <object2> <message>
127+
#
128+
function AssertEqualsCaseInsensitive
129+
{
130+
Assert ($args.Length -ge 2 -and $args.Length -le 3) "AssertEqualsCaseInsensitive takes either two or three parameters."
131+
if ($args.Length -eq 2)
132+
{
133+
Assert ($args[0] -ieq $args[1]) ("'" + $args[0] + "' does not equal '" + $args[1] + "'")
134+
}
135+
else
136+
{
137+
Assert ($args[0] -ieq $args[1]) ($args[2] + ": " + "'" + $args[0] + "' does not equal '" + $args[1] + "'")
138+
}
139+
}
140+
141+
# Usage: AssertNotEquals <object1> <object2> <message>
142+
#
143+
function AssertNotEquals
144+
{
145+
Assert ($args.Length -ge 2 -and $args.Length -le 3) "AssertNotEquals takes either two or three parameters."
146+
if ($args.Length -eq 2)
147+
{
148+
Assert ($args[0] -cne $args[1]) ("'" + $args[0] + "' equals '" + $args[1] + "'")
149+
}
150+
else
151+
{
152+
Assert ($args[0] -cne $args[1]) $args[2]
153+
}
154+
}
155+
156+
# Usage: AssertNotEqualsCaseInsensitive <object1> <object2> <message>
157+
#
158+
function AssertNotEqualsCaseInsensitive
159+
{
160+
Assert ($args.Length -ge 2 -and $args.Length -le 3) "AssertNotEqualsCaseInsensitive takes either two or three parameters."
161+
if ($args.Length -eq 2)
162+
{
163+
Assert ($args[0] -ine $args[1]) ("'" + $args[0] + "' equals '" + $args[1] + "'")
164+
}
165+
else
166+
{
167+
Assert ($args[0] -ine $args[1]) $args[2]
168+
}
169+
}
170+
171+
# Usage: AssertNotNull <object> <message>
172+
#
173+
function AssertNotNull
174+
{
175+
Assert ($args.Length -eq 2) "AssertNotNull takes two parameters."
176+
Assert ($args[0] -ne $()) $args[1]
177+
}
178+
179+
# Usage: AssertNull <object> <message>
180+
#
181+
function AssertNull
182+
{
183+
Assert ($args.Length -eq 2) "AssertNull takes two parameters."
184+
Assert ($args[0] -eq $()) $args[1]
185+
}
186+
187+
# This function waits for either a specified amount of time or for a script block to evaluate to true and throws an exception if the timeout period elapses. Example:
188+
#
189+
# WaitFor {get-process calc} 10000 250 "Calc.exe wasn't started within 10 seconds."
190+
#
191+
function WaitFor(
192+
[Management.Automation.ScriptBlock]$scriptBlock,
193+
$timeoutInMilliseconds = 10000,
194+
$intervalInMilliseconds = 1000,
195+
$exceptionMessage = (throw "Please provide a descriptive exception message to 'WaitFor' so people don't have a hard time debugging it.")
196+
)
197+
{
198+
# Get the current time
199+
$startTime = [DateTime]::Now
200+
201+
# Loop until the script block evaluates to true
202+
while (-not ($scriptBlock.Invoke()))
203+
{
204+
# Sleep for the specified interval
205+
sleep -mil $intervalInMilliseconds
206+
207+
# If the timeout period has passed, throw an exception
208+
if (([DateTime]::Now - $startTime).TotalMilliseconds -gt $timeoutInMilliseconds)
209+
{
210+
throw $exceptionMessage
211+
}
212+
}
213+
}

0 commit comments

Comments
 (0)
0