Skip to content

Commit 2cc40ec

Browse files
authored
Merge pull request #1255 from bergmeister/DoNotCorrectCasingOfFilePaths
UseCorrectCasing: Do not correct applications or script paths at all
2 parents b09eb33 + 9320a74 commit 2cc40ec

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

Rules/UseCorrectCasing.cs

+1-7
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
5151
}
5252

5353
var commandInfo = Helper.Instance.GetCommandInfo(commandName);
54-
if (commandInfo == null)
54+
if (commandInfo == null || commandInfo.CommandType == CommandTypes.ExternalScript || commandInfo.CommandType == CommandTypes.Application)
5555
{
5656
continue;
5757
}
@@ -60,12 +60,6 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
6060
var fullyqualifiedName = $"{commandInfo.ModuleName}\\{shortName}";
6161
var isFullyQualified = commandName.Equals(fullyqualifiedName, StringComparison.OrdinalIgnoreCase);
6262
var correctlyCasedCommandName = isFullyQualified ? fullyqualifiedName : shortName;
63-
if (isWindows && commandInfo.CommandType == CommandTypes.Application && !Path.HasExtension(commandName))
64-
{
65-
// For binaries that could exist on both Windows and Linux like e.g. git we do not want to expand
66-
// git to git.exe to keep the script cross-platform compliant
67-
correctlyCasedCommandName = Path.GetFileNameWithoutExtension(correctlyCasedCommandName);
68-
}
6963

7064
if (!commandName.Equals(correctlyCasedCommandName, StringComparison.Ordinal))
7165
{

Tests/Rules/UseCorrectCasing.tests.ps1

+30-14
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,42 @@ Describe "UseCorrectCasing" {
1616
Invoke-Formatter '?' | Should -Be '?'
1717
}
1818

19-
It "Corrects applications on Windows to not end in .exe" -Skip:($IsLinux -or $IsMacOS) {
20-
Invoke-Formatter 'Cmd' | Should -Be 'cmd'
21-
Invoke-Formatter 'Cmd' | Should -Be 'cmd'
22-
Invoke-Formatter 'MORE' | Should -Be 'more'
23-
Invoke-Formatter 'WinRM' | Should -Be 'winrm'
24-
Invoke-Formatter 'CertMgr' | Should -Be 'certmgr'
19+
It "Does not corrects applications on the PATH" -Skip:($IsLinux -or $IsMacOS) {
20+
Invoke-Formatter 'Cmd' | Should -Be 'Cmd'
21+
Invoke-Formatter 'MORE' | Should -Be 'MORE'
2522
}
2623

2724
It "Preserves extension of applications on Windows" -Skip:($IsLinux -or $IsMacOS) {
28-
Invoke-Formatter 'Cmd.exe' | Should -Be 'cmd.exe'
29-
Invoke-Formatter 'MORE.com' | Should -Be 'more.com'
30-
Invoke-Formatter 'WinRM.cmd' | Should -Be 'winrm.cmd'
31-
Invoke-Formatter 'CertMgr.MSC' | Should -Be 'certmgr.msc'
25+
Invoke-Formatter 'cmd.exe' | Should -Be 'cmd.exe'
26+
Invoke-Formatter 'more.com' | Should -Be 'more.com'
3227
}
3328

34-
It "corrects case of script function" {
35-
function Invoke-DummyFunction
36-
{
37-
29+
It "Preserves full application path" {
30+
if ($IsLinux -or $IsMacOS) {
31+
$applicationPath = '. /bin/ls'
32+
}
33+
else {
34+
$applicationPath = "${env:WINDIR}\System32\cmd.exe"
3835
}
36+
Invoke-Formatter ". $applicationPath" | Should -Be ". $applicationPath"
37+
}
38+
39+
It "Corrects case of script function" {
40+
function Invoke-DummyFunction { }
3941
Invoke-Formatter 'invoke-dummyFunction' | Should -Be 'Invoke-DummyFunction'
4042
}
43+
44+
It "Preserves script path" {
45+
$path = Join-Path $TestDrive "$([guid]::NewGuid()).ps1"
46+
New-Item -ItemType File -Path $path
47+
$scriptDefinition = ". $path"
48+
Invoke-Formatter $scriptDefinition | Should -Be $scriptDefinition
49+
}
50+
51+
It "Preserves UNC script path" -Skip:($IsLinux -or $IsMacOS) {
52+
$uncPath = [System.IO.Path]::Combine("\\$(HOSTNAME.EXE)\C$\", $TestDrive, "$([guid]::NewGuid()).ps1")
53+
New-Item -ItemType File -Path $uncPath
54+
$scriptDefinition = ". $uncPath"
55+
Invoke-Formatter $scriptDefinition | Should -Be $scriptDefinition
56+
}
4157
}

0 commit comments

Comments
 (0)