@@ -7,9 +7,16 @@ $script:TargetProfileConfigKey = 'TargetProfiles'
7
7
8
8
$script :Srv2012_3_profile = ' win-8_x64_6.2.9200.0_3.0_x64_4.0.30319.42000_framework'
9
9
$script :Srv2012r2_4_profile = ' win-8_x64_6.3.9600.0_4.0_x64_4.0.30319.42000_framework'
10
+ $script :Srv2012r2_6_1_profile = ' win-8_x64_6.3.9600.0_6.1.2_x64_4.0.30319.42000_core'
11
+ $script :Srv2016_5_profile = ' win-8_x64_10.0.14393.0_5.1.14393.2636_x64_4.0.30319.42000_framework'
12
+ $script :Srv2016_6_1_profile = ' win-8_x64_10.0.14393.0_6.1.2_x64_4.0.30319.42000_core'
10
13
$script :Srv2019_5_profile = ' win-8_x64_10.0.17763.0_5.1.17763.134_x64_4.0.30319.42000_framework'
11
14
$script :Srv2019_6_1_profile = ' win-8_x64_10.0.17763.0_6.1.2_x64_4.0.30319.42000_core'
15
+ $script :Win10_5_profile = ' win-48_x64_10.0.17763.0_5.1.17763.134_x64_4.0.30319.42000_framework'
16
+ $script :Win10_6_1_profile = ' win-48_x64_10.0.17763.0_6.1.2_x64_4.0.30319.42000_core'
12
17
$script :Ubuntu1804_6_1_profile = ' ubuntu_x64_18.04_6.1.2_x64_4.0.30319.42000_core'
18
+ $script :Debian8_6_1_profile = ' debian_x64_8_6.1.2_x64_4.0.30319.42000_core'
19
+ $script :Rhel76_6_1_profile = ' rhel_x64_7.6_6.1.2_x64_4.0.30319.42000_core'
13
20
14
21
$script :TypeCompatibilityTestCases = @ (
15
22
@ { Target = $script :Srv2012_3_profile ; Script = ' [System.Management.Automation.ModuleIntrinsics]::GetModulePath("here", "there", "everywhere")' ; Types = @ (' System.Management.Automation.ModuleIntrinsics' ); Version = " 3.0" ; OS = ' Windows' ; ProblemCount = 1 }
@@ -18,14 +25,37 @@ $script:TypeCompatibilityTestCases = @(
18
25
@ { Target = $script :Srv2012_3_profile ; Script = ' $kw = New-Object "System.Management.Automation.Language.DynamicKeyword"' ; Types = @ (' System.Management.Automation.Language.DynamicKeyword' ); Version = " 3.0" ; OS = ' Windows' ; ProblemCount = 1 }
19
26
@ { Target = $script :Srv2012_3_profile ; Script = ' & { param([Parameter(Position=0)][ArgumentCompleter({"Banana"})][string]$Hello) $Hello } "Banana"' ; Types = @ (' ArgumentCompleter' ); Version = " 3.0" ; OS = ' Windows' ; ProblemCount = 1 }
20
27
28
+ @ { Target = $script :Srv2012r2_4_profile ; Script = ' [WildcardPattern]"bicycle*"' ; Types = @ (' WildcardPattern' ); Version = " 4.0" ; OS = ' Windows' ; ProblemCount = 1 }
29
+ @ { Target = $script :Srv2012r2_4_profile ; Script = ' $client = [System.Net.Http.HttpClient]::new()' ; Types = @ (' System.Net.Http.HttpClient' ); Version = " 4.0" ; OS = ' Windows' ; ProblemCount = 1 }
30
+ @ { Target = $script :Srv2012r2_4_profile ; Script = ' [Microsoft.PowerShell.EditMode]"Vi"' ; Types = @ (' Microsoft.PowerShell.EditMode' ); Version = " 4.0" ; OS = ' Windows' ; ProblemCount = 1 }
31
+
21
32
@ { Target = $script :Srv2019_5_profile ; Script = ' [Microsoft.PowerShell.Commands.WebSslProtocol]::Default -eq "Tls12"' ; Types = @ (' Microsoft.PowerShell.Commands.WebSslProtocol' ); Version = " 5.1" ; OS = ' Windows' ; ProblemCount = 1 }
33
+ @ { Target = $script :Srv2019_5_profile ; Script = ' [System.Collections.Immutable.ImmutableList[string]]::Empty' ; Types = @ (' System.Collections.Immutable.ImmutableList' ); Version = " 5.1" ; OS = ' Windows' ; ProblemCount = 1 }
34
+ @ { Target = $script :Srv2019_5_profile ; Script = ' [System.Collections.Generic.TreeSet[string]]::new(@("duck", "goose", "banana"))' ; Types = @ (' System.Collections.Generic.TreeSet' ); Version = " 5.1" ; OS = ' Windows' ; ProblemCount = 1 }
35
+
36
+ @ { Target = $script :Srv2019_6_1_profile ; Script = ' function CertFunc { param([System.Net.ICertificatePolicy]$Policy) Do-Something $Policy }' ; Types = @ (' System.Net.ICertificatePolicy' ); Version = " 6.1" ; OS = ' Windows' ; ProblemCount = 1 }
22
37
23
38
@ { Target = $script :Ubuntu1804_6_1_profile ; Script = ' [System.Management.Automation.Security.SystemPolicy]::GetSystemLockdownPolicy()' ; Types = @ (' System.Management.Automation.Security.SystemPolicy' ); Version = " 6.1.2" ; OS = ' Linux' ; ProblemCount = 1 }
24
39
@ { Target = $script :Ubuntu1804_6_1_profile ; Script = ' [System.Management.Automation.Security.SystemPolicy]::GetSystemLockdownPolicy()' ; Types = @ (' System.Management.Automation.Security.SystemPolicy' ); Version = " 6.1.2" ; OS = ' Linux' ; ProblemCount = 1 }
25
40
@ { Target = $script :Ubuntu1804_6_1_profile ; Script = ' [System.Management.Automation.Security.SystemEnforcementMode]$enforcementMode = "Audit"' ; Types = @ (' System.Management.Automation.Security.SystemEnforcementMode' ); Version = " 6.1.2" ; OS = ' Linux' ; ProblemCount = 1 }
26
41
@ { Target = $script :Ubuntu1804_6_1_profile ; Script = ' $ci = New-Object "Microsoft.PowerShell.Commands.ComputerInfo"' ; Types = @ (' Microsoft.PowerShell.Commands.ComputerInfo' ); Version = " 6.1.2" ; OS = ' Linux' ; ProblemCount = 1 }
27
42
)
28
43
44
+ $script :MemberCompatibilityTestCases = @ (
45
+ @ { Target = $script :Srv2012_3_profile ; Script = ' [System.Management.Automation.LanguagePrimitives]::ConvertTypeNameToPSTypeName("System.String")' ; Types = @ (' System.Management.Automation.LanguagePrimitives' ); Members = @ (' ConvertTypeNameToPSTypeName' ); Version = " 3.0" ; OS = ' Windows' ; ProblemCount = 1 }
46
+ @ { Target = $script :Srv2012_3_profile ; Script = ' [System.Management.Automation.WildcardPattern]::Get("banana*", "None").IsMatch("bananaduck")' ; Types = @ (' System.Management.Automation.WildcardPattern' ); Members = @ (' Get' ); Version = " 3.0" ; OS = ' Windows' ; ProblemCount = 1 }
47
+
48
+ @ { Target = $script :Srv2012r2_4_profile ; Script = ' if (-not [Microsoft.PowerShell.Commands.ModuleSpecification]::TryParse($msStr, [ref]$modSpec)){ throw "Bad!" }' ; Types = @ (' Microsoft.PowerShell.Commands.ModuleSpecification' ); Members = @ (' TryParse' ); Version = " 4.0" ; OS = ' Windows' ; ProblemCount = 1 }
49
+ @ { Target = $script :Srv2012r2_4_profile ; Script = ' [System.Management.Automation.LanguagePrimitives]::IsObjectEnumerable($obj)' ; Types = @ (' System.Management.Automation.LanguagePrimitives' ); Members = @ (' IsObjectEnumerable' ); Version = " 4.0" ; OS = ' Windows' ; ProblemCount = 1 }
50
+
51
+ @ { Target = $script :Srv2019_5_profile ; Script = ' $socket = [System.Net.WebSockets.WebSocket]::CreateFromStream($stream, $true, "http", [timespan]::FromMinutes(10))' ; Types = @ (' System.Net.WebSockets.WebSocket' ); Members = @ (' CreateFromStream' ); Version = " 5.1" ; OS = ' Windows' ; ProblemCount = 1 }
52
+ @ { Target = $script :Srv2019_5_profile ; Script = ' [System.Management.Automation.HostUtilities]::InvokeOnRunspace($command, $runspace)' ; Types = @ (' System.Management.Automation.HostUtilities' ); Members = @ (' InvokeOnRunspace' ); Version = " 5.1" ; OS = ' Windows' ; ProblemCount = 1 }
53
+
54
+ @ { Target = $script :Srv2019_6_1_profile ; Script = ' [Microsoft.PowerShell.ToStringCodeMethods]::PropertyValueCollection($obj)' ; Types = @ (' Microsoft.PowerShell.ToStringCodeMethods' ); Members = @ (' PropertyValueCollection' ); Version = " 6.1" ; OS = ' Windows' ; ProblemCount = 1 }
55
+
56
+ @ { Target = $script :Ubuntu1804_6_1_profile ; Script = ' [System.Management.Automation.Tracing.Tracer]::GetExceptionString($e)' ; Types = @ (' System.Management.Automation.Tracing.Tracer' ); Members = @ (' GetExceptionString' ); Version = " 6.1" ; OS = ' Linux' ; ProblemCount = 1 }
57
+ )
58
+
29
59
Describe ' UseCompatibleTypes' {
30
60
Context ' Targeting a single profile' {
31
61
It " Reports <ProblemCount> problem(s) with <Script> on <OS> with PowerShell <Version> targeting <Target>" - TestCases $script :TypeCompatibilityTestCases {
@@ -52,5 +82,94 @@ Describe 'UseCompatibleTypes' {
52
82
$diagnostics [$i ].TargetPlatform.PowerShell.Version.Minor | Should - Be $Version.Minor
53
83
}
54
84
}
85
+
86
+ It " Reports <ProblemCount> problem(s) with <Script> on <OS> with PowerShell <Version> targeting <Target>" - TestCases $script :MemberCompatibilityTestCases {
87
+ param ($Script , [string ]$Target , [string []]$Types , [string []]$Members , [version ]$Version , [string ]$OS , [int ]$ProblemCount )
88
+
89
+ $settings = @ {
90
+ Rules = @ {
91
+ $script :RuleName = @ {
92
+ Enable = $true
93
+ $script :TargetProfileConfigKey = @ ($Target )
94
+ }
95
+ }
96
+ }
97
+
98
+ $diagnostics = Invoke-ScriptAnalyzer - IncludeRule $script :RuleName - ScriptDefinition $Script - Settings $settings
99
+
100
+ $diagnostics.Count | Should - Be $ProblemCount
101
+
102
+ for ($i = 0 ; $i -lt $diagnostics.Count ; $i ++ )
103
+ {
104
+ $diagnostics [$i ].Type | Should - BeExactly $Types [$i ]
105
+ $diagnostics [$i ].Member | Should - BeExactly $Members [$i ]
106
+ $diagnostics [$i ].TargetPlatform.OperatingSystem.Family | Should - Be $OS
107
+ $diagnostics [$i ].TargetPlatform.PowerShell.Version.Major | Should - Be $Version.Major
108
+ $diagnostics [$i ].TargetPlatform.PowerShell.Version.Minor | Should - Be $Version.Minor
109
+ }
110
+ }
111
+ }
112
+
113
+ Context " Full file checking against all targets" {
114
+ It " Finds all incompatibilities in the script" {
115
+ $settings = @ {
116
+ Rules = @ {
117
+ $script :RuleName = @ {
118
+ Enable = $true
119
+ $script :TargetProfileConfigKey = @ (
120
+ $script :Srv2012_3_profile
121
+ $script :Srv2012r2_4_profile
122
+ $script :Srv2012r2_6_1_profile
123
+ $script :Srv2016_5_profile
124
+ $script :Srv2016_6_1_profile
125
+ $script :Srv2019_5_profile
126
+ $script :Srv2019_6_1_profile
127
+ $script :Win10_5_profile
128
+ $script :Win10_6_1_profile
129
+ $script :Ubuntu1804_6_1_profile
130
+ $script :Debian8_6_1_profile
131
+ $script :Rhel76_6_1_profile
132
+ )
133
+ }
134
+ }
135
+ }
136
+
137
+ $diagnostics = Invoke-ScriptAnalyzer - Path " $PSScriptRoot /CompatibilityRuleAssets/IncompatibleScript.ps1" - Settings $settings - IncludeRule PSUseCompatibleTypes
138
+ $diagnostics.Count | Should - Be 2
139
+ foreach ($diagnostic in $diagnostics )
140
+ {
141
+ $diagnostic.Member | Should - BeExactly ' TryParse'
142
+ $diagnostic.Type | Should - BeExactly ' Microsoft.PowerShell.Commands.ModuleSpecification'
143
+ }
144
+ }
145
+ }
146
+
147
+ Context " PSSA repository code checking" {
148
+ It " Checks that there are no incompatibilities in PSSA build scripts" {
149
+ $settings = @ {
150
+ Rules = @ {
151
+ $script :RuleName = @ {
152
+ Enable = $true
153
+ $script :TargetProfileConfigKey = @ (
154
+ $script :Srv2012_3_profile
155
+ $script :Srv2012r2_4_profile
156
+ $script :Srv2012r2_6_1_profile
157
+ $script :Srv2016_5_profile
158
+ $script :Srv2016_6_1_profile
159
+ $script :Srv2019_5_profile
160
+ $script :Srv2019_6_1_profile
161
+ $script :Win10_5_profile
162
+ $script :Win10_6_1_profile
163
+ $script :Ubuntu1804_6_1_profile
164
+ $script :Debian8_6_1_profile
165
+ $script :Rhel76_6_1_profile
166
+ )
167
+ }
168
+ }
169
+ }
170
+
171
+ $diagnostics = Invoke-ScriptAnalyzer - Path " $PSScriptRoot /../../" - Settings $settings - IncludeRule PSUseCompatibleTypes
172
+ $diagnostics.Count | Should - Be 0
173
+ }
55
174
}
56
175
}
0 commit comments