Skip to content

Commit

Permalink
Merge pull request PowerShell#1255 from bergmeister/DoNotCorrectCasin…
Browse files Browse the repository at this point in the history
…gOfFilePaths

UseCorrectCasing: Do not correct applications or script paths at all
  • Loading branch information
JamesWTruher committed Jun 12, 2019
2 parents b09eb33 + 9320a74 commit 2cc40ec
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
8 changes: 1 addition & 7 deletions Rules/UseCorrectCasing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public override IEnumerable<DiagnosticRecord> 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;
}
Expand All @@ -60,12 +60,6 @@ public override IEnumerable<DiagnosticRecord> 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))
{
Expand Down
44 changes: 30 additions & 14 deletions Tests/Rules/UseCorrectCasing.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 2cc40ec

Please sign in to comment.