Skip to content

Commit 185e7f6

Browse files
author
Kapil Borle
committed
Fix rule tests for CoreCLR
1 parent c8769c8 commit 185e7f6

File tree

2 files changed

+45
-37
lines changed

2 files changed

+45
-37
lines changed

Tests/Rules/UseSingularNounsReservedVerbs.tests.ps1

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
Import-Module PSScriptAnalyzer
2+
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
3+
$testRootDirectory = Split-Path -Parent $directory
4+
Import-Module (Join-Path $testRootDirectory 'PSScriptAnalyzerTestHelper.psm1')
5+
26
$nounViolationMessage = "The cmdlet 'Verb-Files' uses a plural noun. A singular noun should be used instead."
37
$verbViolationMessage = "The cmdlet 'Verb-Files' uses an unapproved verb."
48
$nounViolationName = "PSUseSingularNouns"
59
$verbViolationName = "PSUseApprovedVerbs"
6-
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
710
$violations = Invoke-ScriptAnalyzer $directory\BadCmdlet.ps1
811
$nounViolations = $violations | Where-Object {$_.RuleName -eq $nounViolationName}
912
$verbViolations = $violations | Where-Object {$_.RuleName -eq $verbViolationName}
1013
$noViolations = Invoke-ScriptAnalyzer $directory\GoodCmdlet.ps1
1114
$nounNoViolations = $noViolations | Where-Object {$_.RuleName -eq $nounViolationName}
1215
$verbNoViolations = $noViolations | Where-Object {$_.RuleName -eq $verbViolationName}
1316

14-
Describe "UseSingularNouns" {
15-
Context "When there are violations" {
16-
It "has a cmdlet singular noun violation" {
17-
$nounViolations.Count | Should Be 1
18-
}
17+
# this rule doesn't exist in the non desktop version of PSScriptAnalyzer
18+
if (-not (Test-PSEditionCoreCLR))
19+
{
20+
Describe "UseSingularNouns" {
21+
Context "When there are violations" {
22+
It "has a cmdlet singular noun violation" {
23+
$nounViolations.Count | Should Be 1
24+
}
1925

20-
It "has the correct description message" {
21-
$nounViolations[0].Message | Should Match $nounViolationMessage
22-
}
26+
It "has the correct description message" {
27+
$nounViolations[0].Message | Should Match $nounViolationMessage
28+
}
2329

24-
It "has the correct extent" {
25-
$nounViolations[0].Extent.Text | Should be "Verb-Files"
30+
It "has the correct extent" {
31+
$nounViolations[0].Extent.Text | Should be "Verb-Files"
32+
}
2633
}
2734
}
2835

@@ -59,9 +66,9 @@ Describe "UseApprovedVerbs" {
5966
$verbViolations[0].Message | Should Match $verbViolationMessage
6067
}
6168

62-
It "has the correct extent" {
63-
$verbViolations[0].Extent.Text | Should be "Verb-Files"
64-
}
69+
It "has the correct extent" {
70+
$verbViolations[0].Extent.Text | Should be "Verb-Files"
71+
}
6572
}
6673

6774
Context "When there are no violations" {

Tests/Rules/UseToExportFieldsInManifest.tests.ps1

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
Import-Module PSScriptAnalyzer
22
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
3+
$testRootDirectory = Split-Path -Parent $directory
4+
Import-Module (Join-Path $testRootDirectory 'PSScriptAnalyzerTestHelper.psm1')
5+
36
$testManifestPath = Join-Path $directory "TestManifest"
47
$testManifestBadFunctionsWildcardPath = "ManifestBadFunctionsWildcard.psd1"
58
$testManifestBadFunctionsWildcardInArrayPath = "ManifestBadFunctionsWildcardInArray.psd1"
@@ -12,7 +15,7 @@ $testManifestGoodPath = "ManifestGood.psd1"
1215
$testManifestInvalidPath = "ManifestInvalid.psd1"
1316
Import-Module (Join-Path $directory "PSScriptAnalyzerTestHelper.psm1")
1417

15-
Function Run-PSScriptAnalyzerRule
18+
Function Run-PSScriptAnalyzerRule
1619
{
1720
Param(
1821
[Parameter(Mandatory)]
@@ -28,37 +31,36 @@ Describe "UseManifestExportFields" {
2831
Context "Invalid manifest file" {
2932
It "does not process the manifest" {
3033
$results = Run-PSScriptAnalyzerRule $testManifestInvalidPath
31-
$results | Should BeNullOrEmpty
32-
}
34+
$results | Should BeNullOrEmpty
35+
}
3336
}
3437

3538
Context "Manifest contains violations" {
36-
3739
It "detects FunctionsToExport with wildcard" {
3840
$results = Run-PSScriptAnalyzerRule $testManifestBadFunctionsWildcardPath
3941
$results.Count | Should be 1
4042
$results[0].Extent.Text | Should be "'*'"
4143
}
4244

43-
It "suggests corrections for FunctionsToExport with wildcard" {
44-
$violations = Run-PSScriptAnalyzerRule $testManifestBadFunctionsWildcardPath
45-
$violationFilepath = Join-path $testManifestPath $testManifestBadFunctionsWildcardPath
46-
Test-CorrectionExtent $violationFilepath $violations[0] 1 "'*'" "@('Get-Bar', 'Get-Foo')"
47-
$violations[0].SuggestedCorrections[0].Description | Should Be "Replace '*' with @('Get-Bar', 'Get-Foo')"
48-
}
45+
It "suggests corrections for FunctionsToExport with wildcard" {
46+
$violations = Run-PSScriptAnalyzerRule $testManifestBadFunctionsWildcardPath
47+
$violationFilepath = Join-path $testManifestPath $testManifestBadFunctionsWildcardPath
48+
Test-CorrectionExtent $violationFilepath $violations[0] 1 "'*'" "@('Get-Bar', 'Get-Foo')"
49+
$violations[0].SuggestedCorrections[0].Description | Should Be "Replace '*' with @('Get-Bar', 'Get-Foo')"
50+
}
4951

5052
It "detects FunctionsToExport with null" {
5153
$results = Run-PSScriptAnalyzerRule $testManifestBadFunctionsNullPath
5254
$results.Count | Should be 1
5355
$results[0].Extent.Text | Should be '$null'
5456
}
5557

56-
It "suggests corrections for FunctionsToExport with null and line wrapping" {
57-
$violations = Run-PSScriptAnalyzerRule $testManifestBadFunctionsNullPath
58-
$violationFilepath = Join-path $testManifestPath $testManifestBadFunctionsNullPath
59-
$expectedCorrectionExtent = "@('Get-Foo1', 'Get-Foo10', 'Get-Foo11', 'Get-Foo12', 'Get-Foo2', 'Get-Foo3', {0}`t`t'Get-Foo4', 'Get-Foo5', 'Get-Foo6', 'Get-Foo7', 'Get-Foo8', {0}`t`t'Get-Foo9')" -f [System.Environment]::NewLine
60-
Test-CorrectionExtent $violationFilepath $violations[0] 1 '$null' $expectedCorrectionExtent
61-
}
58+
It "suggests corrections for FunctionsToExport with null and line wrapping" {
59+
$violations = Run-PSScriptAnalyzerRule $testManifestBadFunctionsNullPath
60+
$violationFilepath = Join-path $testManifestPath $testManifestBadFunctionsNullPath
61+
$expectedCorrectionExtent = "@('Get-Foo1', 'Get-Foo10', 'Get-Foo11', 'Get-Foo12', 'Get-Foo2', 'Get-Foo3', {0}`t`t'Get-Foo4', 'Get-Foo5', 'Get-Foo6', 'Get-Foo7', 'Get-Foo8', {0}`t`t'Get-Foo9')" -f [System.Environment]::NewLine
62+
Test-CorrectionExtent $violationFilepath $violations[0] 1 '$null' $expectedCorrectionExtent
63+
}
6264

6365
It "detects array element containing wildcard" {
6466
# if more than two elements contain wildcard we can show only the first one as of now.
@@ -69,7 +71,6 @@ Describe "UseManifestExportFields" {
6971

7072
}
7173

72-
7374
It "detects CmdletsToExport with wildcard" {
7475
$results = Run-PSScriptAnalyzerRule $testManifestBadCmdletsWildcardPath
7576
$results.Count | Should be 1
@@ -82,11 +83,11 @@ Describe "UseManifestExportFields" {
8283
$results[0].Extent.Text | Should be "'*'"
8384
}
8485

85-
It "suggests corrections for AliasesToExport with wildcard" {
86-
$violations = Run-PSScriptAnalyzerRule $testManifestBadAliasesWildcardPath
87-
$violationFilepath = Join-path $testManifestPath $testManifestBadAliasesWildcardPath
88-
Test-CorrectionExtent $violationFilepath $violations[0] 1 "'*'" "@('gbar', 'gfoo')"
89-
}
86+
It "suggests corrections for AliasesToExport with wildcard" {
87+
$violations = Run-PSScriptAnalyzerRule $testManifestBadAliasesWildcardPath
88+
$violationFilepath = Join-path $testManifestPath $testManifestBadAliasesWildcardPath
89+
Test-CorrectionExtent $violationFilepath $violations[0] 1 "'*'" "@('gbar', 'gfoo')"
90+
}
9091

9192
It "detects all the *ToExport violations" {
9293
$results = Run-PSScriptAnalyzerRule $testManifestBadAllPath
@@ -98,7 +99,7 @@ Describe "UseManifestExportFields" {
9899
It "detects all the *ToExport fields explicitly stating lists" {
99100
$results = Run-PSScriptAnalyzerRule $testManifestGoodPath
100101
$results.Count | Should be 0
101-
}
102+
}
102103
}
103104

104105
Context "When given a non module manifest file" {

0 commit comments

Comments
 (0)