1- Import-Module PSScriptAnalyzer
2- $ruleName = " PSUseCompatibleCmdlets"
1+ $ruleName = " PSUseCompatibleCmdlets"
32$directory = Split-Path $MyInvocation.MyCommand.Path - Parent
3+ $testRootDirectory = Split-Path - Parent $directory
44$ruleTestDirectory = Join-Path $directory ' UseCompatibleCmdlets'
55
6+ Import-Module PSScriptAnalyzer
7+ Import-Module (Join-Path $testRootDirectory ' PSScriptAnalyzerTestHelper.psm1' )
8+
69Describe " UseCompatibleCmdlets" {
710 Context " script has violation" {
811 It " detects violation" {
@@ -12,4 +15,44 @@ Describe "UseCompatibleCmdlets" {
1215 $diagnosticRecords.Count | Should Be 1
1316 }
1417 }
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+ }
1558}
0 commit comments