Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UseCorrectCasing: Do not correct applications or script paths at all #1255

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
JamesWTruher marked this conversation as resolved.
Show resolved Hide resolved
New-Item -ItemType File -Path $uncPath
$scriptDefinition = ". $uncPath"
Invoke-Formatter $scriptDefinition | Should -Be $scriptDefinition
}
}