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*

0 commit comments

Comments
 (0)