Skip to content

Commit 844d47d

Browse files
author
Quoc Truong
committed
Add Test for profile
1 parent 6beb251 commit 844d47d

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

Tests/Engine/GlobalSuppression.ps1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
$a
2+
3+
gcm "blah"
4+
5+
$a = "//internal"
6+
7+
Get-Alias -ComputerName dfd
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Check if PSScriptAnalyzer is already loaded so we don't
2+
# overwrite a test version of Invoke-ScriptAnalyzer by
3+
# accident
4+
if (!(Get-Module PSScriptAnalyzer) -and !$testingLibraryUsage)
5+
{
6+
Import-Module -Verbose PSScriptAnalyzer
7+
}
8+
9+
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
10+
$violations = Invoke-ScriptAnalyzer $directory\GlobalSuppression.ps1
11+
$suppression = Invoke-ScriptAnalyzer $directory\GlobalSuppression.ps1 -Profile $directory\Profile.ps1
12+
13+
Describe "GlobalSuppression" {
14+
Context "Exclude Rule" {
15+
It "Raises 1 violation for uninitialized variable and 1 for cmdlet alias" {
16+
$withoutProfile = $violations | Where-Object { $_.RuleName -eq "PSAvoidUsingCmdletAliases" -or $_.RuleName -eq "PSAvoidUninitializedVariable" }
17+
$withoutProfile.Count | Should Be 2
18+
}
19+
20+
It "Does not raise any violations for uninitialized variable and cmdlet alias with profile" {
21+
$withProfile = $suppression | Where-Object { $_.RuleName -eq "PSAvoidUsingCmdletAliases" -or $_.RuleName -eq "PSAvoidUninitializedVariable" }
22+
$withProfile.Count | Should be 0
23+
}
24+
}
25+
26+
Context "Include Rule" {
27+
It "Raises 1 violation for computername hard-coded" {
28+
$withoutProfile = $violations | Where-Object { $_.RuleName -eq "PSAvoidUsingComputerNameHardcoded" }
29+
$withoutProfile.Count | Should Be 1
30+
}
31+
32+
It "Does not raise any violations for computername hard-coded" {
33+
$withProfile = $suppression | Where-Object { $_.RuleName -eq "PSAvoidUsingComputerNameHardcoded" }
34+
$withProfile.Count | Should be 0
35+
}
36+
}
37+
38+
Context "Severity" {
39+
It "Raises 1 violation for internal url without profile" {
40+
$withoutProfile = $violations | Where-Object { $_.RuleName -eq "PSAvoidUsingInternalURLs" }
41+
$withoutProfile.Count | Should Be 1
42+
}
43+
44+
It "Does not raise any violations for internal urls with profile" {
45+
$withProfile = $suppression | Where-Object { $_.RuleName -eq "PSAvoidUsingInternalURLs" }
46+
$withProfile.Count | Should be 0
47+
}
48+
}
49+
}

Tests/Engine/Profile.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@{
2+
Severity='Warning'
3+
IncludeRules=@('PSAvoidUsingCmdletAliases',
4+
'PSAvoidUsingPositionalParameters',
5+
'PSAvoidUsingInternalURLs'
6+
'PSAvoidUninitializedVariable')
7+
ExcludeRules=@('PSAvoidUsingCmdletAliases'
8+
'PSAvoidUninitializedVariable')
9+
}

0 commit comments

Comments
 (0)