1+ # Copyright (c) Microsoft Corporation. All rights reserved.
2+ # Licensed under the MIT License.
3+
4+ [Diagnostics.CodeAnalysis.SuppressMessage (' PSUseDeclaredVarsMoreThanAssignments' , ' ' , Justification = ' False positive' )]
5+ param ()
6+
7+ BeforeAll {
8+ $ruleName = " PSInvalidMultiDotValue"
9+ $ruleMessage = " The unquoted '{0}' expression is not a valid syntax. Types with multiple dots need to be constructed from either a quoted string or individual components."
10+ $correctionDescription = ' Quote the value that contains multiple dots'
11+ }
12+
13+ Describe " InvalidMultiDotValue" {
14+
15+ BeforeAll {
16+ $Settings = @ {
17+ IncludeRules = @ ($ruleName )
18+ Rules = @ { $ruleName = @ { Enable = $true } }
19+ }
20+ }
21+
22+ Context " Violates" {
23+ It " 3 version components" {
24+ $scriptDefinition = { $version = 1.2 .3 }.ToString()
25+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
26+ $violations.Count | Should - Be 1
27+ $violations.Severity | Should - Be Error
28+ $violations.Extent.Text | Should - Be ' 1.2.3'
29+ $violations.Message | Should - Be ($ruleMessage -f ' 1.2.3' )
30+ $violations.RuleSuppressionID | Should - Be ' 1.2.3'
31+ $violations.SuggestedCorrections.Text | Should - Be " '1.2.3'"
32+ $violations.SuggestedCorrections.Description | Should - Be $correctionDescription
33+ }
34+
35+ It " 4 version components" {
36+ $scriptDefinition = { $version = 1.2 .3.4 }.ToString()
37+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
38+ $violations.Count | Should - Be 1
39+ $violations.Severity | Should - Be Error
40+ $violations.Extent.Text | Should - Be ' 1.2.3.4'
41+ $violations.Message | Should - Be ($ruleMessage -f ' 1.2.3.4' )
42+ $violations.RuleSuppressionID | Should - Be ' 1.2.3.4'
43+ $violations.SuggestedCorrections.Text | Should - Be " '1.2.3.4'"
44+ $violations.SuggestedCorrections.Description | Should - Be $correctionDescription
45+ }
46+
47+
48+ It " With class initializer" {
49+ $scriptDefinition = { $version = [Version ]1.2 .3 }.ToString()
50+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
51+ $violations.Count | Should - Be 1
52+ $violations.Severity | Should - Be Error
53+ $violations.Extent.Text | Should - Be ' 1.2.3'
54+ $violations.Message | Should - Be ($ruleMessage -f ' 1.2.3' )
55+ $violations.RuleSuppressionID | Should - Be ' 1.2.3'
56+ $violations.SuggestedCorrections.Text | Should - Be " '1.2.3'"
57+ $violations.SuggestedCorrections.Description | Should - Be $correctionDescription
58+ }
59+
60+ It " As parameter" {
61+ $scriptDefinition = {
62+ param (
63+ [Version ]$version = 1.2 .3
64+ )
65+ Write-Verbose $version
66+ }.ToString()
67+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
68+ $violations.Count | Should - Be 1
69+ $violations.Severity | Should - Be Error
70+ $violations.Extent.Text | Should - Be ' 1.2.3'
71+ $violations.Message | Should - Be ($ruleMessage -f ' 1.2.3' )
72+ $violations.RuleSuppressionID | Should - Be ' 1.2.3'
73+ $violations.SuggestedCorrections.Text | Should - Be " '1.2.3'"
74+ $violations.SuggestedCorrections.Description | Should - Be $correctionDescription
75+ }
76+
77+ # Even an IP address is apparently expect below.
78+ # The violation message and description presume a version
79+ # is expected because this is the more commonly used type.
80+ It " IP Address" {
81+ $scriptDefinition = { $IP = [System.Net.IPAddress ]127.0 .0.1 }.ToString()
82+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
83+ $violations.Count | Should - Be 1
84+ $violations.Severity | Should - Be Error
85+ $violations.Extent.Text | Should - Be ' 127.0.0.1'
86+ $violations.Message | Should - Be ($ruleMessage -f ' 127.0.0.1' )
87+ $violations.RuleSuppressionID | Should - Be ' 127.0.0.1'
88+ $violations.SuggestedCorrections.Text | Should - Be " '127.0.0.1'"
89+ $violations.SuggestedCorrections.Description | Should - Be $correctionDescription
90+ }
91+ }
92+
93+ Context " Compliant" {
94+ It " From string" {
95+ $scriptDefinition = { $Version = [Version ]' 1.2.3' }.ToString()
96+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
97+ $violations | Should - BeNullOrEmpty
98+ }
99+
100+ It " From version components" {
101+ $scriptDefinition = { $Version = [Version ]::new(1 , 2 , 3 , 4 ) }.ToString()
102+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
103+ $violations | Should - BeNullOrEmpty
104+ }
105+
106+ It " From (bare) double" {
107+ $scriptDefinition = { $Version = [Version ]1.2 }.ToString()
108+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
109+ $violations | Should - BeNullOrEmpty
110+ }
111+
112+
113+ It " Dot notation" { # PowerShell:27356
114+ $scriptDefinition = {
115+ $1.2.3.4
116+ $intKeys = @ { 1 = @ { 2 = @ { 3 = @ { 4 = ' test' } } } }
117+ $intKeys.1.2.3.4
118+ }.ToString()
119+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
120+ $violations | Should - BeNullOrEmpty
121+ }
122+ }
123+
124+ Context " Disabled" {
125+
126+ BeforeAll {
127+ $Settings = @ {
128+ IncludeRules = @ ($ruleName )
129+ Rules = @ { $ruleName = @ { Enable = $false } }
130+ }
131+ }
132+
133+ It " ConvertFrom-SecureString -AsPlainText" {
134+ $scriptDefinition = { $version = 1.2 .3 }.ToString()
135+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
136+ $violations | Should - BeNullOrEmpty
137+ }
138+ }
139+
140+ Context " Suppressed" {
141+ It " All" {
142+ $scriptDefinition = {
143+ [Diagnostics.CodeAnalysis.SuppressMessage (' PSInvalidMultiDotValue' , ' ' , Justification = ' Test' )]
144+ param ()
145+ $version = 1.2 .3
146+ $IP = [System.Net.IPAddress ]127.0 .0.1
147+ }.ToString()
148+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
149+ $violations | Should - BeNullOrEmpty
150+ }
151+
152+ It " 1.2.3" {
153+ $scriptDefinition = {
154+ [Diagnostics.CodeAnalysis.SuppressMessage (' PSInvalidMultiDotValue' , ' 1.2.3' , Justification = ' Test' )]
155+ param ()
156+ $version = 1.2 .3
157+ }.ToString()
158+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
159+ $violations | Should - BeNullOrEmpty
160+ }
161+
162+ It " 127.0.0.1" {
163+ $scriptDefinition = {
164+ [Diagnostics.CodeAnalysis.SuppressMessage (' PSInvalidMultiDotValue' , ' 127.0.0.1' , Justification = ' Test' )]
165+ param ()
166+ $IP = [System.Net.IPAddress ]127.0 .0.1
167+ }.ToString()
168+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
169+ $violations | Should - BeNullOrEmpty
170+ }
171+ }
172+
173+ Context " Fixing" {
174+
175+ BeforeAll { # See request: #1938
176+ $tempFile = Join-Path $TestDrive ' TestScript.ps1'
177+ }
178+
179+ It " Version" {
180+ Set-Content - LiteralPath $tempFile - Value {$version = 1.2 .3 }.ToString() - NoNewLine
181+ $violations = Invoke-ScriptAnalyzer - Path $tempFile - Settings $Settings - fix
182+ Get-Content - LiteralPath $tempFile - Raw | Should - Be {$version = ' 1.2.3' }.ToString()
183+ }
184+
185+ It " IP Address" {
186+ Set-Content - LiteralPath $tempFile - Value {$IP = [System.Net.IPAddress ]127.0 .0.1 }.ToString() - NoNewLine
187+ $violations = Invoke-ScriptAnalyzer - Path $tempFile - Settings $Settings - fix
188+ Get-Content - LiteralPath $tempFile - Raw | Should - Be {$IP = [System.Net.IPAddress ]' 127.0.0.1' }.ToString()
189+ }
190+ }
191+ }
0 commit comments