Skip to content

Commit 3485763

Browse files
author
Kapil Borle
committed
Merge pull request #551 from PowerShell/kapilmb/FixPSv4Tests
Fix build and test issues on PowerShell v4
2 parents 8e2b07d + 6d8c1f8 commit 3485763

18 files changed

+195
-221
lines changed

Engine/ScriptAnalyzer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//
1+

2+
//
23
// Copyright (c) Microsoft Corporation.
34
//
45
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

PSScriptAnalyzer.sln

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ Global
2525
{F4BDE3D0-3EEF-4157-8A3E-722DF7ADEF60}.Release|Any CPU.Build.0 = Release|Any CPU
2626
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.Debug|Any CPU.ActiveCfg = Release|Any CPU
2727
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.Debug|Any CPU.Build.0 = Release|Any CPU
28-
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.PSV3 Debug|Any CPU.ActiveCfg = Debug|Any CPU
29-
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.PSV3 Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.PSV3 Debug|Any CPU.ActiveCfg = PSV3 Debug|Any CPU
29+
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.PSV3 Debug|Any CPU.Build.0 = PSV3 Debug|Any CPU
3030
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.PSV3 Release|Any CPU.ActiveCfg = PSV3 Release|Any CPU
3131
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.PSV3 Release|Any CPU.Build.0 = PSV3 Release|Any CPU
3232
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.Release|Any CPU.ActiveCfg = Release|Any CPU

Tests/Engine/CommunityAnalyzerRules/CommunityAnalyzerRules.psm1

Lines changed: 91 additions & 93 deletions
Large diffs are not rendered by default.

Tests/Engine/CorrectionExtent.tests.ps1

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Describe "Correction Extent" {
88

99
Context "Object construction" {
1010
It "creates the object with correct properties" {
11-
$correctionExtent = $type::new(1, 1, 1, 3, "get-childitem", "newfile", "cool description")
11+
$correctionExtent = $obj = New-Object -TypeName $type -ArgumentList 1, 1, 1, 3, "get-childitem", "newfile", "cool description"
1212

1313
$correctionExtent.StartLineNumber | Should Be 1
1414
$correctionExtent.EndLineNumber | Should Be 1
@@ -18,17 +18,15 @@ Describe "Correction Extent" {
1818
$correctionExtent.File | Should Be "newfile"
1919
$correctionExtent.Description | Should Be "cool description"
2020
}
21-
21+
2222
It "throws if end line number is less than start line number" {
23-
$text = "Get-ChildItem"
24-
{$type::new(2, 1, 1, $text.Length + 1, $text, "newfile")} | Should Throw "start line number"
23+
$text = "Get-ChildItem"
24+
{New-Object -TypeName $type -ArgumentList @(2, 1, 1, $text.Length + 1, $text, "newfile")} | Should Throw "start line number"
2525
}
26-
26+
2727
It "throws if end column number is less than start column number for same line" {
2828
$text = "start-process"
29-
{$type::new(1, 1, 2, 1, $text, "newfile")} | Should Throw "start column number"
29+
{New-Object -TypeName $type -ArgumentList @(1, 1, 2, 1, $text, "newfile")} | Should Throw "start column number"
3030
}
3131
}
32-
}
33-
34-
32+
}

Tests/Engine/CustomizedRule.tests.ps1

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Describe "Test importing correct customized rules" {
108108

109109
It "will show the custom rules when given glob with recurse switch" {
110110
$customizedRulePath = Get-ScriptAnalyzerRule -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule* | Where-Object {$_.RuleName -eq $measure}
111-
$customizedRulePath.Count | Should be 4
111+
$customizedRulePath.Count | Should be 3
112112
}
113113
}
114114

@@ -147,7 +147,7 @@ Describe "Test importing correct customized rules" {
147147

148148
It "will show the custom rules when given glob with recurse switch" {
149149
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule* | Where-Object {$_.Message -eq $message}
150-
$customizedRulePath.Count | Should be 4
150+
$customizedRulePath.Count | Should be 3
151151
}
152152

153153
It "Using IncludeDefaultRules Switch with CustomRulePath" {
@@ -164,23 +164,26 @@ Describe "Test importing correct customized rules" {
164164
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1
165165
$customizedRulePath.Count | Should Be 1
166166
}
167+
168+
if ($PSVersionTable.PSVersion -ge [Version]'5.0')
169+
{
170+
It "loads custom rules that contain version in their path" {
171+
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomRulePath $directory\VersionedSampleRule\SampleRuleWithVersion
172+
$customizedRulePath.Count | Should Be 1
167173

168-
It "loads custom rules that contain version in their path" {
169-
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomRulePath $directory\SampleRuleWithVersion\SampleRuleWithVersion
170-
$customizedRulePath.Count | Should Be 1
171-
172-
$customizedRulePath = Get-ScriptAnalyzerRule -CustomRulePath $directory\SampleRuleWithVersion\SampleRuleWithVersion
173-
$customizedRulePath.Count | Should Be 1
174-
}
174+
$customizedRulePath = Get-ScriptAnalyzerRule -CustomRulePath $directory\VersionedSampleRule\SampleRuleWithVersion
175+
$customizedRulePath.Count | Should Be 1
176+
}
175177

176-
It "loads custom rules that contain version in their path with the RecurseCustomRule switch" {
177-
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomRulePath $directory\SampleRuleWithVersion -RecurseCustomRulePath
178-
$customizedRulePath.Count | Should Be 1
178+
It "loads custom rules that contain version in their path with the RecurseCustomRule switch" {
179+
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomRulePath $directory\VersionedSampleRule -RecurseCustomRulePath
180+
$customizedRulePath.Count | Should Be 1
179181

180-
$customizedRulePath = Get-ScriptAnalyzerRule -CustomRulePath $directory\SampleRuleWithVersion -RecurseCustomRulePath
181-
$customizedRulePath.Count | Should Be 1
182+
$customizedRulePath = Get-ScriptAnalyzerRule -CustomRulePath $directory\VersionedSampleRule -RecurseCustomRulePath
183+
$customizedRulePath.Count | Should Be 1
182184

183-
}
185+
}
186+
}
184187
}
185188

186189
}

Tests/Engine/InvokeScriptAnalyzer.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Describe "Test available parameters" {
6969
}
7070
}
7171

72-
if (!$testingLibraryUsage)
72+
if (!$testingLibraryUsage -and ($PSVersionTable.PSVersion -ge [Version]'5.0'))
7373
{
7474
Context "SaveDscDependency parameter" {
7575
It "has the parameter" {

Tests/Engine/ModuleDependencyHandler.tests.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ if ($testingLibraryUsage)
88
return
99
}
1010

11+
# DSC Module saving is not supported in versions less than PSv5
12+
if (($PSVersionTable.PSVersion -lt [Version]'5.0'))
13+
{
14+
return
15+
}
16+
1117
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
1218
$violationFileName = 'MissingDSCResource.ps1'
1319
$violationFilePath = Join-Path $directory $violationFileName

Tests/Engine/RuleSuppression.tests.ps1

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,13 @@ Describe "RuleSuppressionWithoutScope" {
5858
}
5959
}
6060

61-
Context "Rule suppression within DSC Configuration definition" {
62-
It "Suppresses rule" {
63-
$suppressedRule = Invoke-ScriptAnalyzer -ScriptDefinition $ruleSuppressionInConfiguration -SuppressedOnly
64-
$suppressedRule.Count | Should Be 1
61+
if (($PSVersionTable.PSVersion -ge [Version]'5.0'))
62+
{
63+
Context "Rule suppression within DSC Configuration definition" {
64+
It "Suppresses rule" {
65+
$suppressedRule = Invoke-ScriptAnalyzer -ScriptDefinition $ruleSuppressionInConfiguration -SuppressedOnly
66+
$suppressedRule.Count | Should Be 1
67+
}
6568
}
6669
}
6770

Tests/Engine/SampleRuleWithVersion/SampleRuleWithVersion/1.0.0.0/SampleRuleWithVersion.psm1 renamed to Tests/Engine/VersionedSampleRule/SampleRuleWithVersion/1.0.0.0/SampleRuleWithVersion.psm1

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,10 @@ function Measure-RequiresRunAsAdministrator
2828
[System.Management.Automation.Language.ScriptBlockAst]
2929
$testAst
3030
)
31+
$dr = New-Object `
32+
-Typename "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord" `
33+
-ArgumentList "This is help",$ast.Extent,$PSCmdlet.MyInvocation.InvocationName,Warning,$null
3134

32-
33-
$results = @()
34-
35-
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]]@{"Message" = "this is help";
36-
"Extent" = $ast.Extent;
37-
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
38-
"Severity" = "Warning"}
39-
40-
$results += $result
41-
42-
43-
return $results
44-
45-
35+
return @($dr)
4636
}
4737
Export-ModuleMember -Function Measure*

Tests/Engine/samplerule/samplerule.psm1

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
.SYNOPSIS
55
Uses #Requires -RunAsAdministrator instead of your own methods.
66
.DESCRIPTION
7-
The #Requires statement prevents a script from running unless the Windows PowerShell version, modules, snap-ins, and module and snap-in version prerequisites are met.
8-
From Windows PowerShell 4.0, the #Requires statement let script developers require that sessions be run with elevated user rights (run as Administrator).
7+
The #Requires statement prevents a script from running unless the Windows PowerShell version, modules, snap-ins, and module and snap-in version prerequisites are met.
8+
From Windows PowerShell 4.0, the #Requires statement let script developers require that sessions be run with elevated user rights (run as Administrator).
99
Script developers does not need to write their own methods any more.
1010
To fix a violation of this rule, please consider to use #Requires -RunAsAdministrator instead of your own methods.
1111
.EXAMPLE
@@ -27,21 +27,10 @@ function Measure-RequiresRunAsAdministrator
2727
[ValidateNotNullOrEmpty()]
2828
[System.Management.Automation.Language.ScriptBlockAst]
2929
$testAst
30-
)
31-
32-
33-
$results = @()
34-
35-
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]]@{"Message" = "this is help";
36-
"Extent" = $ast.Extent;
37-
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
38-
"Severity" = "Warning"}
39-
40-
$results += $result
41-
42-
43-
return $results
44-
45-
30+
)
31+
$dr = New-Object `
32+
-Typename "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord" `
33+
-ArgumentList "This is help",$ast.Extent,$PSCmdlet.MyInvocation.InvocationName,Warning,$null
34+
return @($dr)
4635
}
4736
Export-ModuleMember -Function Measure*

Tests/Engine/samplerule/samplerule1.psm1

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,11 @@ function Measure-RequiresRunAsAdministrator
2727
[ValidateNotNullOrEmpty()]
2828
[System.Management.Automation.Language.ScriptBlockAst]
2929
$testAst
30-
)
31-
32-
33-
$results = @()
34-
35-
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]]@{"Message" = "this is help";
36-
"Extent" = $ast.Extent;
37-
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
38-
"Severity" = "Warning"}
39-
40-
$results += $result
41-
42-
43-
return $results
44-
45-
30+
)
31+
$dr = New-Object `
32+
-Typename "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord" `
33+
-ArgumentList "This is help",$ast.Extent,$PSCmdlet.MyInvocation.InvocationName,Warning,$null
34+
35+
return @($dr)
4636
}
4737
Export-ModuleMember -Function Measure*

Tests/Engine/samplerule/samplerule2/samplerule2.psm1

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,10 @@ function Measure-RequiresRunAsAdministrator
2828
[System.Management.Automation.Language.ScriptBlockAst]
2929
$testAst
3030
)
31+
$dr = New-Object `
32+
-Typename "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord" `
33+
-ArgumentList "This is help",$ast.Extent,$PSCmdlet.MyInvocation.InvocationName,Warning,$null
3134

32-
33-
$results = @()
34-
35-
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]]@{"Message" = "this is help";
36-
"Extent" = $ast.Extent;
37-
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
38-
"Severity" = "Warning"}
39-
40-
$results += $result
41-
42-
43-
return $results
44-
45-
35+
return @($dr)
4636
}
4737
Export-ModuleMember -Function Measure*

Tests/Engine/samplerule/samplerule2/samplerule3/samplerule3.psm1

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,10 @@ function Measure-RequiresRunAsAdministrator
2828
[System.Management.Automation.Language.ScriptBlockAst]
2929
$testAst
3030
)
31+
$dr = New-Object `
32+
-Typename "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord" `
33+
-ArgumentList "This is help",$ast.Extent,$PSCmdlet.MyInvocation.InvocationName,Warning,$null
3134

32-
33-
$results = @()
34-
35-
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]]@{"Message" = "this is help";
36-
"Extent" = $ast.Extent;
37-
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
38-
"Severity" = "Warning"}
39-
40-
$results += $result
41-
42-
43-
return $results
44-
45-
35+
return @($dr)
4636
}
4737
Export-ModuleMember -Function Measure*

Tests/Rules/AvoidUnloadableModuleOrMissingRequiredFieldInManifest.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ $noHashtableFilepath = Join-Path $directory "TestBadModule\NoHashtable.psd1"
99

1010
Describe "MissingRequiredFieldModuleManifest" {
1111
BeforeAll {
12-
Import-Module (Join-Path $directory "PSScriptAnalyzerTestHelper.psm1")
12+
Import-Module (Join-Path $directory "PSScriptAnalyzerTestHelper.psm1") -Force
1313
}
1414

1515
AfterAll{

Tests/Rules/PSCredentialType.tests.ps1

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Import-Module PSScriptAnalyzer
1+
Import-Module PSScriptAnalyzer
22
$violationMessage = "The Credential parameter in 'Credential' must be of type PSCredential. For PowerShell 4.0 and earlier, please define a credential transformation attribute, e.g. [System.Management.Automation.Credential()], after the PSCredential type attribute."
33
$violationName = "PSUsePSCredentialType"
44
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
@@ -16,9 +16,14 @@ Describe "PSCredentialType" {
1616
}
1717
}
1818

19-
Context "When there are no violations" {
20-
It "returns no violations" {
21-
$noViolations.Count | Should Be 0
19+
$expectedViolationCount = 0
20+
if ($PSVersionTable.PSVersion -lt [Version]'5.0')
21+
{
22+
$expectedViolationCount = 1
23+
}
24+
Context ("When there are {0} violations" -f $expectedViolationCount) {
25+
It ("returns {0} violations" -f $expectedViolationCount) {
26+
$noViolations.Count | Should Be $expectedViolationCount
2227
}
2328
}
2429
}

Tests/Rules/PSScriptAnalyzerTestHelper.psm1

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@ Function Get-ExtentText
55
[string] $scriptPath
66
)
77
$scriptContent = Get-Content -Path $scriptPath
8-
$start = [System.Management.Automation.Language.ScriptPosition]::new($scriptPath, $violation.StartLineNumber, $violation.StartColumnNumber, $scriptContent[$violation.StartLineNumber - 1])
9-
$end = [System.Management.Automation.Language.ScriptPosition]::new($scriptPath, $violation.EndLineNumber, $violation.EndColumnNumber, $scriptContent[$violation.EndLineNumber - 1])
10-
$extent = [System.Management.Automation.Language.ScriptExtent]::new($start, $end)
8+
$typeScriptPos = 'System.Management.Automation.Language.ScriptPosition'
9+
$start = New-Object -TypeName $typeScriptPos -ArgumentList @($scriptPath, $violation.StartLineNumber, $violation.StartColumnNumber, $scriptContent[$violation.StartLineNumber - 1])
10+
$end = New-Object -TypeName $typeScriptPos -ArgumentList @($scriptPath, $violation.EndLineNumber, $violation.EndColumnNumber, $scriptContent[$violation.EndLineNumber - 1])
11+
$extent = New-Object -TypeName 'System.Management.Automation.Language.ScriptExtent' -ArgumentList @($start, $end)
1112
return($extent.Text)
1213
}
1314

1415
Function Test-CorrectionExtent
1516
{
1617
Param(
1718
[string] $violationFilepath,
18-
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord] $diagnosticRecord,
19-
[int] $correctionsCount,
20-
[string] $violationText,
19+
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord] $diagnosticRecord,
20+
[int] $correctionsCount,
21+
[string] $violationText,
2122
[string] $correctionText
2223
)
2324
$corrections = $diagnosticRecord.SuggestedCorrections

0 commit comments

Comments
 (0)