Skip to content

Fix build and test issues on PowerShell v4 #551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 2, 2016
Merged
3 changes: 2 additions & 1 deletion Engine/ScriptAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//

//
// Copyright (c) Microsoft Corporation.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Expand Down
4 changes: 2 additions & 2 deletions PSScriptAnalyzer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ Global
{F4BDE3D0-3EEF-4157-8A3E-722DF7ADEF60}.Release|Any CPU.Build.0 = Release|Any CPU
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.Debug|Any CPU.ActiveCfg = Release|Any CPU
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.Debug|Any CPU.Build.0 = Release|Any CPU
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.PSV3 Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.PSV3 Debug|Any CPU.Build.0 = Debug|Any CPU
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.PSV3 Debug|Any CPU.ActiveCfg = PSV3 Debug|Any CPU
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.PSV3 Debug|Any CPU.Build.0 = PSV3 Debug|Any CPU
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.PSV3 Release|Any CPU.ActiveCfg = PSV3 Release|Any CPU
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.PSV3 Release|Any CPU.Build.0 = PSV3 Release|Any CPU
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
184 changes: 91 additions & 93 deletions Tests/Engine/CommunityAnalyzerRules/CommunityAnalyzerRules.psm1

Large diffs are not rendered by default.

16 changes: 7 additions & 9 deletions Tests/Engine/CorrectionExtent.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Describe "Correction Extent" {

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

$correctionExtent.StartLineNumber | Should Be 1
$correctionExtent.EndLineNumber | Should Be 1
Expand All @@ -18,17 +18,15 @@ Describe "Correction Extent" {
$correctionExtent.File | Should Be "newfile"
$correctionExtent.Description | Should Be "cool description"
}

It "throws if end line number is less than start line number" {
$text = "Get-ChildItem"
{$type::new(2, 1, 1, $text.Length + 1, $text, "newfile")} | Should Throw "start line number"
$text = "Get-ChildItem"
{New-Object -TypeName $type -ArgumentList @(2, 1, 1, $text.Length + 1, $text, "newfile")} | Should Throw "start line number"
}

It "throws if end column number is less than start column number for same line" {
$text = "start-process"
{$type::new(1, 1, 2, 1, $text, "newfile")} | Should Throw "start column number"
{New-Object -TypeName $type -ArgumentList @(1, 1, 2, 1, $text, "newfile")} | Should Throw "start column number"
}
}
}


}
33 changes: 18 additions & 15 deletions Tests/Engine/CustomizedRule.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Describe "Test importing correct customized rules" {

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

Expand Down Expand Up @@ -147,7 +147,7 @@ Describe "Test importing correct customized rules" {

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

It "Using IncludeDefaultRules Switch with CustomRulePath" {
Expand All @@ -164,23 +164,26 @@ Describe "Test importing correct customized rules" {
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1
$customizedRulePath.Count | Should Be 1
}

if ($PSVersionTable.PSVersion -ge [Version]'5.0')
{
It "loads custom rules that contain version in their path" {
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomRulePath $directory\VersionedSampleRule\SampleRuleWithVersion
$customizedRulePath.Count | Should Be 1

It "loads custom rules that contain version in their path" {
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomRulePath $directory\SampleRuleWithVersion\SampleRuleWithVersion
$customizedRulePath.Count | Should Be 1

$customizedRulePath = Get-ScriptAnalyzerRule -CustomRulePath $directory\SampleRuleWithVersion\SampleRuleWithVersion
$customizedRulePath.Count | Should Be 1
}
$customizedRulePath = Get-ScriptAnalyzerRule -CustomRulePath $directory\VersionedSampleRule\SampleRuleWithVersion
$customizedRulePath.Count | Should Be 1
}

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

$customizedRulePath = Get-ScriptAnalyzerRule -CustomRulePath $directory\SampleRuleWithVersion -RecurseCustomRulePath
$customizedRulePath.Count | Should Be 1
$customizedRulePath = Get-ScriptAnalyzerRule -CustomRulePath $directory\VersionedSampleRule -RecurseCustomRulePath
$customizedRulePath.Count | Should Be 1

}
}
}
}

}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Engine/InvokeScriptAnalyzer.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Describe "Test available parameters" {
}
}

if (!$testingLibraryUsage)
if (!$testingLibraryUsage -and ($PSVersionTable.PSVersion -ge [Version]'5.0'))
{
Context "SaveDscDependency parameter" {
It "has the parameter" {
Expand Down
6 changes: 6 additions & 0 deletions Tests/Engine/ModuleDependencyHandler.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ if ($testingLibraryUsage)
return
}

# DSC Module saving is not supported in versions less than PSv5
if (($PSVersionTable.PSVersion -lt [Version]'5.0'))
{
return
}

$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
$violationFileName = 'MissingDSCResource.ps1'
$violationFilePath = Join-Path $directory $violationFileName
Expand Down
11 changes: 7 additions & 4 deletions Tests/Engine/RuleSuppression.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ Describe "RuleSuppressionWithoutScope" {
}
}

Context "Rule suppression within DSC Configuration definition" {
It "Suppresses rule" {
$suppressedRule = Invoke-ScriptAnalyzer -ScriptDefinition $ruleSuppressionInConfiguration -SuppressedOnly
$suppressedRule.Count | Should Be 1
if (($PSVersionTable.PSVersion -ge [Version]'5.0'))
{
Context "Rule suppression within DSC Configuration definition" {
It "Suppresses rule" {
$suppressedRule = Invoke-ScriptAnalyzer -ScriptDefinition $ruleSuppressionInConfiguration -SuppressedOnly
$suppressedRule.Count | Should Be 1
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,10 @@ function Measure-RequiresRunAsAdministrator
[System.Management.Automation.Language.ScriptBlockAst]
$testAst
)
$dr = New-Object `
-Typename "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord" `
-ArgumentList "This is help",$ast.Extent,$PSCmdlet.MyInvocation.InvocationName,Warning,$null


$results = @()

$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]]@{"Message" = "this is help";
"Extent" = $ast.Extent;
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
"Severity" = "Warning"}

$results += $result


return $results


return @($dr)
}
Export-ModuleMember -Function Measure*
25 changes: 7 additions & 18 deletions Tests/Engine/samplerule/samplerule.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
.SYNOPSIS
Uses #Requires -RunAsAdministrator instead of your own methods.
.DESCRIPTION
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.
From Windows PowerShell 4.0, the #Requires statement let script developers require that sessions be run with elevated user rights (run as Administrator).
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.
From Windows PowerShell 4.0, the #Requires statement let script developers require that sessions be run with elevated user rights (run as Administrator).
Script developers does not need to write their own methods any more.
To fix a violation of this rule, please consider to use #Requires -RunAsAdministrator instead of your own methods.
.EXAMPLE
Expand All @@ -27,21 +27,10 @@ function Measure-RequiresRunAsAdministrator
[ValidateNotNullOrEmpty()]
[System.Management.Automation.Language.ScriptBlockAst]
$testAst
)


$results = @()

$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]]@{"Message" = "this is help";
"Extent" = $ast.Extent;
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
"Severity" = "Warning"}

$results += $result


return $results


)
$dr = New-Object `
-Typename "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord" `
-ArgumentList "This is help",$ast.Extent,$PSCmdlet.MyInvocation.InvocationName,Warning,$null
return @($dr)
}
Export-ModuleMember -Function Measure*
22 changes: 6 additions & 16 deletions Tests/Engine/samplerule/samplerule1.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,11 @@ function Measure-RequiresRunAsAdministrator
[ValidateNotNullOrEmpty()]
[System.Management.Automation.Language.ScriptBlockAst]
$testAst
)


$results = @()

$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]]@{"Message" = "this is help";
"Extent" = $ast.Extent;
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
"Severity" = "Warning"}

$results += $result


return $results


)
$dr = New-Object `
-Typename "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord" `
-ArgumentList "This is help",$ast.Extent,$PSCmdlet.MyInvocation.InvocationName,Warning,$null

return @($dr)
}
Export-ModuleMember -Function Measure*
18 changes: 4 additions & 14 deletions Tests/Engine/samplerule/samplerule2/samplerule2.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,10 @@ function Measure-RequiresRunAsAdministrator
[System.Management.Automation.Language.ScriptBlockAst]
$testAst
)
$dr = New-Object `
-Typename "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord" `
-ArgumentList "This is help",$ast.Extent,$PSCmdlet.MyInvocation.InvocationName,Warning,$null


$results = @()

$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]]@{"Message" = "this is help";
"Extent" = $ast.Extent;
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
"Severity" = "Warning"}

$results += $result


return $results


return @($dr)
}
Export-ModuleMember -Function Measure*
18 changes: 4 additions & 14 deletions Tests/Engine/samplerule/samplerule2/samplerule3/samplerule3.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,10 @@ function Measure-RequiresRunAsAdministrator
[System.Management.Automation.Language.ScriptBlockAst]
$testAst
)
$dr = New-Object `
-Typename "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord" `
-ArgumentList "This is help",$ast.Extent,$PSCmdlet.MyInvocation.InvocationName,Warning,$null


$results = @()

$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]]@{"Message" = "this is help";
"Extent" = $ast.Extent;
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
"Severity" = "Warning"}

$results += $result


return $results


return @($dr)
}
Export-ModuleMember -Function Measure*
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ $noHashtableFilepath = Join-Path $directory "TestBadModule\NoHashtable.psd1"

Describe "MissingRequiredFieldModuleManifest" {
BeforeAll {
Import-Module (Join-Path $directory "PSScriptAnalyzerTestHelper.psm1")
Import-Module (Join-Path $directory "PSScriptAnalyzerTestHelper.psm1") -Force
}

AfterAll{
Expand Down
13 changes: 9 additions & 4 deletions Tests/Rules/PSCredentialType.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Import-Module PSScriptAnalyzer
Import-Module PSScriptAnalyzer
$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."
$violationName = "PSUsePSCredentialType"
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
Expand All @@ -16,9 +16,14 @@ Describe "PSCredentialType" {
}
}

Context "When there are no violations" {
It "returns no violations" {
$noViolations.Count | Should Be 0
$expectedViolationCount = 0
if ($PSVersionTable.PSVersion -lt [Version]'5.0')
{
$expectedViolationCount = 1
}
Context ("When there are {0} violations" -f $expectedViolationCount) {
It ("returns {0} violations" -f $expectedViolationCount) {
$noViolations.Count | Should Be $expectedViolationCount
}
}
}
13 changes: 7 additions & 6 deletions Tests/Rules/PSScriptAnalyzerTestHelper.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ Function Get-ExtentText
[string] $scriptPath
)
$scriptContent = Get-Content -Path $scriptPath
$start = [System.Management.Automation.Language.ScriptPosition]::new($scriptPath, $violation.StartLineNumber, $violation.StartColumnNumber, $scriptContent[$violation.StartLineNumber - 1])
$end = [System.Management.Automation.Language.ScriptPosition]::new($scriptPath, $violation.EndLineNumber, $violation.EndColumnNumber, $scriptContent[$violation.EndLineNumber - 1])
$extent = [System.Management.Automation.Language.ScriptExtent]::new($start, $end)
$typeScriptPos = 'System.Management.Automation.Language.ScriptPosition'
$start = New-Object -TypeName $typeScriptPos -ArgumentList @($scriptPath, $violation.StartLineNumber, $violation.StartColumnNumber, $scriptContent[$violation.StartLineNumber - 1])
$end = New-Object -TypeName $typeScriptPos -ArgumentList @($scriptPath, $violation.EndLineNumber, $violation.EndColumnNumber, $scriptContent[$violation.EndLineNumber - 1])
$extent = New-Object -TypeName 'System.Management.Automation.Language.ScriptExtent' -ArgumentList @($start, $end)
return($extent.Text)
}

Function Test-CorrectionExtent
{
Param(
[string] $violationFilepath,
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord] $diagnosticRecord,
[int] $correctionsCount,
[string] $violationText,
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord] $diagnosticRecord,
[int] $correctionsCount,
[string] $violationText,
[string] $correctionText
)
$corrections = $diagnosticRecord.SuggestedCorrections
Expand Down
Loading