1
- Import-Module PSScriptAnalyzer
2
- $ruleName = " PSUseCompatibleCmdlets"
1
+ $ruleName = " PSUseCompatibleCmdlets"
3
2
$directory = Split-Path $MyInvocation.MyCommand.Path - Parent
3
+ $testRootDirectory = Split-Path - Parent $directory
4
4
$ruleTestDirectory = Join-Path $directory ' UseCompatibleCmdlets'
5
5
6
+ Import-Module PSScriptAnalyzer
7
+ Import-Module (Join-Path $testRootDirectory ' PSScriptAnalyzerTestHelper.psm1' )
8
+
6
9
Describe " UseCompatibleCmdlets" {
7
10
Context " script has violation" {
8
11
It " detects violation" {
@@ -12,4 +15,44 @@ Describe "UseCompatibleCmdlets" {
12
15
$diagnosticRecords.Count | Should Be 1
13
16
}
14
17
}
18
+
19
+ Function Test-Command
20
+ {
21
+ param (
22
+ [Parameter (ValueFromPipeline )]
23
+ $command ,
24
+ $settings ,
25
+ $expectedViolations
26
+ )
27
+ process
28
+ {
29
+ It (" found {0} violations for '{1}'" -f $expectedViolations , $command ) {
30
+ Invoke-ScriptAnalyzer - ScriptDefinition $command - IncludeRule $ruleName - Settings $settings | `
31
+ Get-Count | `
32
+ Should Be $expectedViolations
33
+ }
34
+ }
35
+ }
36
+
37
+ $settings = @ {rules = @ {PSUseCompatibleCmdlets = @ {compatibility = @ (" core-6.0.0-alpha-windows" )}}}
38
+
39
+ Context " Microsoft.PowerShell.Core" {
40
+ @ (' Enter-PSSession' , ' Foreach-Object' , ' Get-Command' ) | `
41
+ Test-Command - Settings $settings - ExpectedViolations 0
42
+ }
43
+
44
+ Context " Non-builtin commands" {
45
+ @ (' get-foo' , ' get-bar' , ' get-baz' ) | `
46
+ Test-Command - Settings $settings - ExpectedViolations 0
47
+ }
48
+
49
+ Context " Aliases" {
50
+ @ (' where' , ' select' , ' cd' ) | `
51
+ Test-Command - Settings $settings - ExpectedViolations 0
52
+ }
53
+
54
+ Context " Commands present in reference platform but not in target platform" {
55
+ @ (" Start-VM" , " New-SmbShare" , " Get-Disk" ) | `
56
+ Test-Command - Settings $settings - ExpectedViolations 1
57
+ }
15
58
}
0 commit comments