Skip to content

Commit

Permalink
Add test case for application paths as discussed and do not correct a…
Browse files Browse the repository at this point in the history
…pplication paths any more for simplicity
  • Loading branch information
bergmeister committed Jun 6, 2019
1 parent c8feb9c commit 069d02f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 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 || commandInfo.CommandType == CommandTypes.ExternalScript)
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
34 changes: 18 additions & 16 deletions Tests/Rules/UseCorrectCasing.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 069d02f

Please sign in to comment.