From 6c891b6e24d1f79d3ef68b5bfba3e787156241da Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Thu, 6 Jun 2019 18:24:13 +0100 Subject: [PATCH 1/5] UseCorrectCasing: Preserve script paths --- Rules/UseCorrectCasing.cs | 2 +- Tests/Rules/UseCorrectCasing.tests.ps1 | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Rules/UseCorrectCasing.cs b/Rules/UseCorrectCasing.cs index 2bfe322cf..faef65859 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) { continue; } diff --git a/Tests/Rules/UseCorrectCasing.tests.ps1 b/Tests/Rules/UseCorrectCasing.tests.ps1 index e4ec35e9a..f2b123be5 100644 --- a/Tests/Rules/UseCorrectCasing.tests.ps1 +++ b/Tests/Rules/UseCorrectCasing.tests.ps1 @@ -38,4 +38,11 @@ Describe "UseCorrectCasing" { } Invoke-Formatter 'invoke-dummyFunction' | Should -Be 'Invoke-DummyFunction' } + + It "preserves script paths" -Skip:($IsLinux -or $IsMacOS) { + $uncPath = [System.IO.Path]::Combine("\\$(HOSTNAME.EXE)\C$\", $TestDrive, "$(New-Guid).ps1") + New-Item -ItemType File -Path $uncPath + $scriptDefinition = ". $uncPath" + Invoke-Formatter $scriptDefinition | Should -Be $scriptDefinition + } } From 3770023f90dab3d5167b5b329b7014a09409c301 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Thu, 6 Jun 2019 18:54:46 +0100 Subject: [PATCH 2/5] make test ps v4 compatible (new-guid cmdlet does not exists on v4) --- Tests/Rules/UseCorrectCasing.tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Rules/UseCorrectCasing.tests.ps1 b/Tests/Rules/UseCorrectCasing.tests.ps1 index f2b123be5..42feff73b 100644 --- a/Tests/Rules/UseCorrectCasing.tests.ps1 +++ b/Tests/Rules/UseCorrectCasing.tests.ps1 @@ -40,7 +40,7 @@ Describe "UseCorrectCasing" { } It "preserves script paths" -Skip:($IsLinux -or $IsMacOS) { - $uncPath = [System.IO.Path]::Combine("\\$(HOSTNAME.EXE)\C$\", $TestDrive, "$(New-Guid).ps1") + $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 From c8feb9cbea624356a427e537e215e508439e3ae4 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Thu, 6 Jun 2019 21:23:48 +0100 Subject: [PATCH 3/5] Add test case for general paths --- Tests/Rules/UseCorrectCasing.tests.ps1 | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Tests/Rules/UseCorrectCasing.tests.ps1 b/Tests/Rules/UseCorrectCasing.tests.ps1 index 42feff73b..36645ac6c 100644 --- a/Tests/Rules/UseCorrectCasing.tests.ps1 +++ b/Tests/Rules/UseCorrectCasing.tests.ps1 @@ -39,7 +39,14 @@ Describe "UseCorrectCasing" { Invoke-Formatter 'invoke-dummyFunction' | Should -Be 'Invoke-DummyFunction' } - It "preserves script paths" -Skip:($IsLinux -or $IsMacOS) { + 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" From 069d02f2bfd49039ae362328427e1e6c54e65b9b Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Thu, 6 Jun 2019 23:49:46 +0100 Subject: [PATCH 4/5] Add test case for application paths as discussed and do not correct application paths any more for simplicity --- Rules/UseCorrectCasing.cs | 8 +----- Tests/Rules/UseCorrectCasing.tests.ps1 | 34 ++++++++++++++------------ 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/Rules/UseCorrectCasing.cs b/Rules/UseCorrectCasing.cs index faef65859..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 || commandInfo.CommandType == CommandTypes.ExternalScript) + 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 36645ac6c..d4397e0e1 100644 --- a/Tests/Rules/UseCorrectCasing.tests.ps1 +++ b/Tests/Rules/UseCorrectCasing.tests.ps1 @@ -16,37 +16,39 @@ 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 = 'C:\Windows\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" { + 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) { + 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" From 9320a74fd0a391e64e6dfaa4c029be39425d8065 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Tue, 11 Jun 2019 22:04:01 +0100 Subject: [PATCH 5/5] Use environment variable to get Windows directory to address PR feedback --- Tests/Rules/UseCorrectCasing.tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Rules/UseCorrectCasing.tests.ps1 b/Tests/Rules/UseCorrectCasing.tests.ps1 index d4397e0e1..ae716264c 100644 --- a/Tests/Rules/UseCorrectCasing.tests.ps1 +++ b/Tests/Rules/UseCorrectCasing.tests.ps1 @@ -31,7 +31,7 @@ Describe "UseCorrectCasing" { $applicationPath = '. /bin/ls' } else { - $applicationPath = 'C:\Windows\System32\cmd.exe' + $applicationPath = "${env:WINDIR}\System32\cmd.exe" } Invoke-Formatter ". $applicationPath" | Should -Be ". $applicationPath" }