diff --git a/Rules/UseCorrectCasing.cs b/Rules/UseCorrectCasing.cs index 2bfe322cf..7c0c20727 100644 --- a/Rules/UseCorrectCasing.cs +++ b/Rules/UseCorrectCasing.cs @@ -51,7 +51,7 @@ public override IEnumerable AnalyzeScript(Ast ast, string file } var commandInfo = Helper.Instance.GetCommandInfo(commandName); - if (commandInfo == null) + if (commandInfo == null || commandInfo.CommandType == CommandTypes.ExternalScript || commandInfo.CommandType == CommandTypes.Application) { continue; } @@ -60,12 +60,6 @@ public override IEnumerable AnalyzeScript(Ast ast, string file var fullyqualifiedName = $"{commandInfo.ModuleName}\\{shortName}"; var isFullyQualified = commandName.Equals(fullyqualifiedName, StringComparison.OrdinalIgnoreCase); var correctlyCasedCommandName = isFullyQualified ? fullyqualifiedName : shortName; - if (isWindows && commandInfo.CommandType == CommandTypes.Application && !Path.HasExtension(commandName)) - { - // For binaries that could exist on both Windows and Linux like e.g. git we do not want to expand - // git to git.exe to keep the script cross-platform compliant - correctlyCasedCommandName = Path.GetFileNameWithoutExtension(correctlyCasedCommandName); - } if (!commandName.Equals(correctlyCasedCommandName, StringComparison.Ordinal)) { diff --git a/Tests/Rules/UseCorrectCasing.tests.ps1 b/Tests/Rules/UseCorrectCasing.tests.ps1 index e4ec35e9a..ae716264c 100644 --- a/Tests/Rules/UseCorrectCasing.tests.ps1 +++ b/Tests/Rules/UseCorrectCasing.tests.ps1 @@ -16,26 +16,42 @@ Describe "UseCorrectCasing" { Invoke-Formatter '?' | Should -Be '?' } - It "Corrects applications on Windows to not end in .exe" -Skip:($IsLinux -or $IsMacOS) { - Invoke-Formatter 'Cmd' | Should -Be 'cmd' - Invoke-Formatter 'Cmd' | Should -Be 'cmd' - Invoke-Formatter 'MORE' | Should -Be 'more' - Invoke-Formatter 'WinRM' | Should -Be 'winrm' - Invoke-Formatter 'CertMgr' | Should -Be 'certmgr' + It "Does not corrects applications on the PATH" -Skip:($IsLinux -or $IsMacOS) { + Invoke-Formatter 'Cmd' | Should -Be 'Cmd' + Invoke-Formatter 'MORE' | Should -Be 'MORE' } It "Preserves extension of applications on Windows" -Skip:($IsLinux -or $IsMacOS) { - Invoke-Formatter 'Cmd.exe' | Should -Be 'cmd.exe' - Invoke-Formatter 'MORE.com' | Should -Be 'more.com' - Invoke-Formatter 'WinRM.cmd' | Should -Be 'winrm.cmd' - Invoke-Formatter 'CertMgr.MSC' | Should -Be 'certmgr.msc' + Invoke-Formatter 'cmd.exe' | Should -Be 'cmd.exe' + Invoke-Formatter 'more.com' | Should -Be 'more.com' } - It "corrects case of script function" { - function Invoke-DummyFunction - { - + It "Preserves full application path" { + if ($IsLinux -or $IsMacOS) { + $applicationPath = '. /bin/ls' + } + else { + $applicationPath = "${env:WINDIR}\System32\cmd.exe" } + Invoke-Formatter ". $applicationPath" | Should -Be ". $applicationPath" + } + + It "Corrects case of script function" { + function Invoke-DummyFunction { } Invoke-Formatter 'invoke-dummyFunction' | Should -Be '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 -Be $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 -Be $scriptDefinition + } }