Skip to content

Commit f038593

Browse files
committed
Replace custom variable expansion with $ExecutionContext.InvokeCommand.ExpandString()
1 parent d381ae9 commit f038593

2 files changed

Lines changed: 57 additions & 36 deletions

File tree

Tests/poshspec.Tests.ps1

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Describe 'Module Manifest Tests' {
1111
Describe 'Get-PoshspecParam' {
1212
InModuleScope PoshSpec {
1313
Context 'One Parameter' {
14-
$results = Get-PoshspecParam -TestName MyTest -TestExpression {Get-Item $Target} -Target Name -Should { Should Exist }
14+
$results = Get-PoshspecParam -TestName MyTest -TestExpression {Get-Item '$Target'} -Target Name -Should { Should Exist }
1515

1616
It "Should return the correct test Name" {
1717
$results.Name | Should Be "MyTest 'Name' Should Exist"
@@ -22,8 +22,20 @@ Describe 'Get-PoshspecParam' {
2222
}
2323
}
2424

25+
Context 'One Parameter with a space' {
26+
$results = Get-PoshspecParam -TestName MyTest -TestExpression {Get-Item '$Target'} -Target "Spaced Value" -Should { Should Exist }
27+
28+
It "Should return the correct test Name" {
29+
$results.Name | Should Be "MyTest 'Spaced Value' Should Exist"
30+
}
31+
32+
It "Should return the correct test Expression" {
33+
$results.Expression | Should Be "Get-Item 'Spaced Value' | Should Exist"
34+
}
35+
}
36+
2537
Context 'Two Parameters' {
26-
$results = Get-PoshspecParam -TestName MyTest -TestExpression {Get-Item $Target $Property} -Target Name -Property Something -Should { Should Exist }
38+
$results = Get-PoshspecParam -TestName MyTest -TestExpression {Get-Item '$Target' '$Property'} -Target Name -Property Something -Should { Should Exist }
2739

2840
It "Should return the correct test Name" {
2941
$results.Name | Should Be "MyTest property 'Something' for 'Name' Should Exist"
@@ -35,7 +47,7 @@ Describe 'Get-PoshspecParam' {
3547
}
3648

3749
Context 'Three Parameters' {
38-
$results = Get-PoshspecParam -TestName MyTest -TestExpression {Get-Item $Target $Property $Qualifier} -Target Name -Property Something -Qualifier 1 -Should { Should Exist }
50+
$results = Get-PoshspecParam -TestName MyTest -TestExpression {Get-Item '$Target' '$Property' '$Qualifier'} -Target Name -Property Something -Qualifier 1 -Should { Should Exist }
3951

4052
It "Should return the correct test Name" {
4153
$results.Name | Should Be "MyTest property 'Something' for 'Name' at '1' Should Exist"
@@ -45,13 +57,6 @@ Describe 'Get-PoshspecParam' {
4557
$results.Expression | Should Be "Get-Item 'name' 'Something' '1' | Select-Object -ExpandProperty 'Something' | Should Exist"
4658
}
4759
}
48-
49-
Context 'Expression handling' {
50-
51-
It "Should thow and error if missing a property" {
52-
{ Get-PoshspecParam -TestName MyTest -TestExpression {Get-Item $Target $Property} -Target Name -Should { Should Exist } } | Should Throw
53-
}
54-
}
5560
}
5661
}
5762

@@ -133,7 +138,7 @@ Describe 'Test Functions' {
133138
}
134139

135140
It "Should return the correct test Expression" {
136-
$results.Expression | Should Be "Test-NetConnection -ComputerName 'localhost' -Port '80' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty 'PingSucceeded' | Should Be `$true"
141+
$results.Expression | Should Be "Test-NetConnection -ComputerName localhost -Port 80 -ErrorAction SilentlyContinue | Select-Object -ExpandProperty 'PingSucceeded' | Should Be `$true"
137142
}
138143
}
139144

@@ -146,7 +151,7 @@ Describe 'Test Functions' {
146151
}
147152

148153
It "Should return the correct test Expression" {
149-
$results.Expression | Should Be "Get-HotFix -Id 'KB1234567' -ErrorAction SilentlyContinue | Should Exist"
154+
$results.Expression | Should Be "Get-HotFix -Id KB1234567 -ErrorAction SilentlyContinue | Should Exist"
150155
}
151156
}
152157

@@ -159,7 +164,7 @@ Describe 'Test Functions' {
159164
}
160165

161166
It "Should return the correct test Expression" {
162-
$results.Expression | Should Be "Get-CimInstance -ClassName 'Win32_OperatingSystem' | Select-Object -ExpandProperty 'SystemDirectory' | Should Be C:\WINDOWS\system32"
167+
$results.Expression | Should Be "Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -ExpandProperty 'SystemDirectory' | Should Be C:\WINDOWS\system32"
163168
}
164169
}
165170
Context 'CimObject with Namespace' {
@@ -171,7 +176,7 @@ Describe 'Test Functions' {
171176
}
172177

173178
It "Should return the correct test Expression" {
174-
$results.Expression | Should Be "Get-CimInstance -ClassName 'MSFT_DSCConfigurationStatus' -Namespace 'root/Microsoft/Windows/DesiredStateConfiguration' | Select-Object -ExpandProperty 'Error' | Should BeNullOrEmpty"
179+
$results.Expression | Should Be "Get-CimInstance -ClassName MSFT_DSCConfigurationStatus -Namespace root/Microsoft/Windows/DesiredStateConfiguration | Select-Object -ExpandProperty 'Error' | Should BeNullOrEmpty"
175180
}
176181
}
177182

@@ -210,7 +215,7 @@ Describe 'Test Functions' {
210215
}
211216

212217
It 'Should return a correct text expression' {
213-
$results.Expression | Should Be 'Get-CimInstance -ClassName Win32_Group -Filter "Name = ''$Target''" | Should Not BeNullOrEmpty'
218+
$results.Expression | Should Be 'Get-CimInstance -ClassName Win32_Group -Filter "Name = ''Administrators''" | Should Not BeNullOrEmpty'
214219
}
215220
}
216221

@@ -258,7 +263,7 @@ Describe 'Test Functions' {
258263
}
259264

260265
It "Should return the correct test Expression" {
261-
$results.Expression | Should Be "Resolve-DnsName -Name 'www.google.com' -DnsOnly -NoHostsFile -ErrorAction SilentlyContinue | Should Not BeNullOrEmpty"
266+
$results.Expression | Should Be "Resolve-DnsName -Name www.google.com -DnsOnly -NoHostsFile -ErrorAction SilentlyContinue | Should Not BeNullOrEmpty"
262267
}
263268
}
264269
}

poshspec.psm1

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,31 @@ function Get-PoshspecParam {
3535
)
3636

3737
$assertion = $Should.ToString().Trim()
38-
39-
40-
$targetName = $Target
41-
42-
if ($PSBoundParameters.ContainsKey("FriendlyName"))
38+
<#
39+
if ($PSBoundParameters.ContainsKey("Target"))
4340
{
44-
$targetName = $Friendlyname
41+
$Target = '`"' + $Target + '`"'
42+
}
43+
44+
if ($PSBoundParameters.ContainsKey("Property"))
45+
{
46+
$Property = "'" + $Property + "'"
4547
}
4648
47-
$tokens = [System.Management.Automation.PSParser]::Tokenize($TestExpression,[ref]$null)
49+
if ($PSBoundParameters.ContainsKey("Qualifier"))
50+
{
51+
$Qualifier = "'" + $Qualifier + "'"
52+
}
4853
54+
#$targetName = $Target
55+
#>
56+
if (-not $PSBoundParameters.ContainsKey("FriendlyName"))
57+
{
58+
$FriendlyName = $Target
59+
}
60+
61+
$expressionString = $TestExpression.ToString()
62+
<#
4963
$expandedTokens = foreach ($token in $tokens)
5064
{
5165
if ($token.Type -eq 'Variable')
@@ -74,25 +88,27 @@ function Get-PoshspecParam {
7488
}
7589
7690
$expressionString = $expandedTokens -join " "
77-
91+
#>
7892
if ($PSBoundParameters.ContainsKey("Property"))
7993
{
8094
$expressionString += " | Select-Object -ExpandProperty '$Property'"
8195

8296
if ($PSBoundParameters.ContainsKey("Qualifier"))
8397
{
84-
$nameString = "{0} property '{1}' for '{2}' at '{3}' {4}" -f $TestName,$Property, $targetName, $Qualifier, $assertion
98+
$nameString = "{0} property '{1}' for '{2}' at '{3}' {4}" -f $TestName,$Property, $FriendlyName, $Qualifier, $assertion
8599
}
86100
else
87101
{
88-
$nameString = "{0} property '{1}' for '{2}' {3}" -f $TestName, $Property, $targetName, $assertion
102+
$nameString = "{0} property '{1}' for '{2}' {3}" -f $TestName, $Property, $FriendlyName, $assertion
89103
}
90104
}
91105
else
92106
{
93-
$nameString = "{0} '{1}' {2}" -f $TestName, $targetName, $assertion
107+
$nameString = "{0} '{1}' {2}" -f $TestName, $FriendlyName, $assertion
94108
}
95-
109+
110+
$expressionString = $ExecutionContext.InvokeCommand.ExpandString($expressionString)
111+
96112
$expressionString += " | $assertion"
97113

98114
Write-Output -InputObject ([PSCustomObject]@{Name = $nameString; Expression = $expressionString})
@@ -143,7 +159,7 @@ function Service {
143159
[scriptblock]$Should
144160
)
145161

146-
$params = Get-PoshspecParam -TestName Service -TestExpression {Get-Service -Name $Target} @PSBoundParameters
162+
$params = Get-PoshspecParam -TestName Service -TestExpression {Get-Service -Name '$Target'} @PSBoundParameters
147163

148164
Invoke-PoshspecExpression @params
149165
}
@@ -176,7 +192,7 @@ function File {
176192
)
177193

178194
$name = Split-Path -Path $Target -Leaf
179-
$params = Get-PoshspecParam -TestName File -TestExpression {$Target} -FriendlyName $name @PSBoundParameters
195+
$params = Get-PoshspecParam -TestName File -TestExpression {'$Target'} -FriendlyName $name @PSBoundParameters
180196

181197
Invoke-PoshspecExpression @params
182198
}
@@ -221,11 +237,11 @@ function Registry {
221237

222238
if ($PSBoundParameters.ContainsKey("Property"))
223239
{
224-
$expression = {Get-ItemProperty -Path $Target}
240+
$expression = {Get-ItemProperty -Path '$Target'}
225241
}
226242
else
227243
{
228-
$expression = {$Target}
244+
$expression = {'$Target'}
229245
}
230246

231247
$params = Get-PoshspecParam -TestName Registry -TestExpression $expression -FriendlyName $name @PSBoundParameters
@@ -268,7 +284,7 @@ function Http {
268284
[scriptblock]$Should
269285
)
270286

271-
$params = Get-PoshspecParam -TestName Http -TestExpression {Invoke-WebRequest -Uri $Target -ErrorAction SilentlyContinue} @PSBoundParameters
287+
$params = Get-PoshspecParam -TestName Http -TestExpression {Invoke-WebRequest -Uri '$Target' -ErrorAction SilentlyContinue} @PSBoundParameters
272288

273289
Invoke-PoshspecExpression @params
274290
}
@@ -447,7 +463,7 @@ function Package {
447463
[scriptblock]$Should
448464
)
449465

450-
$expression = {Get-Package -Name $Target -ErrorAction SilentlyContinue}
466+
$expression = {Get-Package -Name '$Target' -ErrorAction SilentlyContinue}
451467

452468
$params = Get-PoshspecParam -TestName Package -TestExpression $expression @PSBoundParameters
453469

@@ -526,7 +542,7 @@ function Interface {
526542
[scriptblock]$Should
527543
)
528544

529-
$expression = {Get-NetAdapter -Name $Target -ErrorAction SilentlyContinue}
545+
$expression = {Get-NetAdapter -Name '$Target' -ErrorAction SilentlyContinue}
530546

531547
$params = Get-PoshspecParam -TestName Interface -TestExpression $expression @PSBoundParameters
532548

@@ -560,7 +576,7 @@ function Folder {
560576
[scriptblock]$Should
561577
)
562578

563-
$params = Get-PoshspecParam -TestName Folder -TestExpression {$Target} @PSBoundParameters
579+
$params = Get-PoshspecParam -TestName Folder -TestExpression {'$Target'} @PSBoundParameters
564580

565581
Invoke-PoshspecExpression @params
566582
}

0 commit comments

Comments
 (0)