forked from PowerShell/PSScriptAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUseCorrectCasing.tests.ps1
More file actions
133 lines (113 loc) · 5.93 KB
/
Copy pathUseCorrectCasing.tests.ps1
File metadata and controls
133 lines (113 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
Describe "UseCorrectCasing" {
It "corrects case of simple cmdlet" {
Invoke-Formatter 'get-childitem' | Should -BeExactly 'Get-ChildItem'
}
It "corrects case of fully qualified cmdlet" {
Invoke-Formatter 'Microsoft.PowerShell.management\get-childitem' | Should -BeExactly 'Microsoft.PowerShell.Management\Get-ChildItem'
}
It "corrects case of of cmdlet inside interpolated string" {
Invoke-Formatter '"$(get-childitem)"' | Should -BeExactly '"$(Get-ChildItem)"'
}
It "Corrects alias correctly" {
Invoke-Formatter 'Gci' | Should -BeExactly 'gci'
Invoke-Formatter '?' | Should -BeExactly '?'
}
It "Does not corrects applications on the PATH" -Skip:($IsLinux -or $IsMacOS) {
Invoke-Formatter 'Git' | Should -BeExactly 'Git'
Invoke-Formatter 'SSH' | Should -BeExactly 'SSH'
}
It "Preserves extension of applications on Windows" -Skip:($IsLinux -or $IsMacOS) {
Invoke-Formatter 'cmd.exe' | Should -BeExactly 'cmd.exe'
Invoke-Formatter 'more.com' | Should -BeExactly 'more.com'
}
It "Preserves full application path" {
if ($IsLinux -or $IsMacOS) {
$applicationPath = '. /bin/ls'
}
else {
$applicationPath = "${env:WINDIR}\System32\cmd.exe"
}
Invoke-Formatter ". $applicationPath" | Should -BeExactly ". $applicationPath"
}
# TODO: Can we make this work?
# There is a limitation in the Helper's CommandCache: it doesn't see commands that are (only temporarily) defined in the current scope
It "Corrects case of script function" -Skip {
function global:Invoke-DummyFunction { }
Invoke-Formatter 'invoke-dummyFunction' | Should -BeExactly 'Invoke-DummyFunction'
}
It "Preserves script path" {
$path = Join-Path $TestDrive "$([guid]::NewGuid()).ps1"
New-Item -ItemType File -Path $path
$scriptDefinition = ". $path"
Invoke-Formatter $scriptDefinition | Should -BeExactly $scriptDefinition
}
It "Preserves UNC script path" -Skip:($IsLinux -or $IsMacOS) {
$uncPath = [System.IO.Path]::Combine("\\$(HOSTNAME.EXE)\C$\", $TestDrive, "$([guid]::NewGuid()).ps1")
New-Item -ItemType File -Path $uncPath
$scriptDefinition = ". $uncPath"
Invoke-Formatter $scriptDefinition | Should -BeExactly $scriptDefinition
}
It "Corrects parameter casing" {
# Without messing up the spacing or use of semicolons
Invoke-Formatter 'Get-ChildItem -literalpath $parameterValue' |
Should -BeExactly 'Get-ChildItem -LiteralPath $parameterValue'
Invoke-Formatter 'Get-ChildItem -literalpath:$parameterValue' |
Should -BeExactly 'Get-ChildItem -LiteralPath:$parameterValue'
Invoke-Formatter 'Get-ChildItem -literalpath: $parameterValue' |
Should -BeExactly 'Get-ChildItem -LiteralPath: $parameterValue'
}
It "Should not throw when using parameter name that does not exist" {
Invoke-Formatter 'Get-Process -NonExistingParameterName' -ErrorAction Stop
}
It "Does not throw when correcting certain cmdlets (issue 1516)" {
$scriptDefinition = 'Get-Content;Test-Path;Get-ChildItem;Get-Content;Test-Path;Get-ChildItem'
$settings = @{ 'Rules' = @{ 'PSUseCorrectCasing' = @{ 'Enable' = $true; CheckCommands = $true; CheckKeywords = $true; CheckOperators = $true } } }
{
1..100 |
ForEach-Object { $null = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -Settings $settings -ErrorAction Stop }
} |
Should -Not -Throw
}
It "Corrects uppercase operators" {
Invoke-Formatter '$ENV:PATH -SPLIT ";"' |
Should -BeExactly '$ENV:PATH -split ";"'
}
It "Corrects mixed case operators" {
Invoke-Formatter '$ENV:PATH -Split ";" -Join ":"' |
Should -BeExactly '$ENV:PATH -split ";" -join ":"'
}
It "Corrects unary operators" {
Invoke-Formatter '-Split "Hello World"' |
Should -BeExactly '-split "Hello World"'
}
It "Does not break PlusPlus or MinusMinus" {
Invoke-Formatter '$A++; $B--' |
Should -BeExactly '$A++; $B--'
}
It "Shows relevant diagnostic message for function/command name casing" {
$settings = @{ 'Rules' = @{ 'PSUseCorrectCasing' = @{ 'Enable' = $true; CheckCommands = $true; CheckKeywords = $true; CheckOperators = $true } } }
$violations = Invoke-ScriptAnalyzer -ScriptDefinition 'WHERE-OBJECT Name -EQ "Value"' -Settings $settings
$violations.Count | Should -Be 1
$violations[0].Message | Should -Be "Function/Cmdlet 'WHERE-OBJECT' does not match its exact casing 'Where-Object'."
}
It "Shows relevant diagnostic message for parameter casing" {
$settings = @{ 'Rules' = @{ 'PSUseCorrectCasing' = @{ 'Enable' = $true; CheckCommands = $true; CheckKeywords = $true; CheckOperators = $true } } }
$violations = Invoke-ScriptAnalyzer -ScriptDefinition 'Where-Object Name -eq "Value"' -Settings $settings
$violations.Count | Should -Be 1
$violations[0].Message | Should -Be "Parameter '-eq' of function/cmdlet 'Where-Object' does not match its exact casing 'EQ'."
}
It "Shows relevant diagnostic message for operator casing" {
$settings = @{ 'Rules' = @{ 'PSUseCorrectCasing' = @{ 'Enable' = $true; CheckCommands = $true; CheckKeywords = $true; CheckOperators = $true } } }
$violations = Invoke-ScriptAnalyzer -ScriptDefinition '$a -EQ 1' -Settings $settings
$violations.Count | Should -Be 1
$violations[0].Message | Should -Be "Operator '-EQ' does not match the expected case '-eq'."
}
Context "Inconsistent Keywords" {
It "Corrects keyword case" {
Invoke-Formatter 'ForEach ($x IN $y) { $x }' |
Should -BeExactly 'foreach ($x in $y) { $x }'
}
}
}