From 9a9d4c37537e91657d39aa95ac029c34bedea718 Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Thu, 7 Nov 2019 12:11:46 -0800 Subject: [PATCH 1/2] Handle exception if enumerating files fails when rebuilding path to have correct casing --- .../namespaces/FileSystemProvider.cs | 18 +++++++++++++----- .../FileSystem.Tests.ps1 | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 7201271631c..c2e1a92af84 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -161,14 +161,22 @@ private static string GetCorrectCasedPath(string path) else { // Use GetFileSystemEntries to get the correct casing of this element - var entries = Directory.GetFileSystemEntries(exactPath, item); - if (entries.Length > 0) + try { - exactPath = entries.First(); + var entries = Directory.GetFileSystemEntries(exactPath, item); + if (entries.Length > 0) + { + exactPath = entries.First(); + } + else + { + // If previous call didn't return anything, something failed so we just return the path we were given + return path; + } } - else + catch { - // If previous call didn't return anything, something failed so we just return the path we were given + // If we can't enumerate, we stop and just return the original path return path; } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index 01f866aaaa0..6ea156223be 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -279,6 +279,23 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" { } } + Context "Appx path" { + BeforeAll { + $skipTest = $true + if ($IsWindows -and (Get-Command -Name Get-AppxPackage) -and (Get-AppxPackage Microsoft.WindowsCalculator)) { + $skipTest = $false + } + } + + It "Can get an appx package item" -Skip:$skipTest { + $pkgDir = (Get-AppxPackage Microsoft.WindowsCalculator).InstallLocation + + Get-Item $pkgDir\Calculator.exe -ErrorAction Stop | Should -BeOfType [System.IO.FileInfo] + Get-Item -Path $pkgDir -ErrorAction Stop | Should -BeOfType [System.IO.DirectoryInfo] + Get-ChildItem -Path $pkgDir -ErrorAction Stop | Should -Not -BeNullOrEmpty + } + } + Context "Validate basic host navigation functionality" { BeforeAll { #build semi-complex directory structure to test navigation within From e2485d312b0571db3756d7991f283168c2eb40c8 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 8 Nov 2019 09:00:19 -0800 Subject: [PATCH 2/2] Update test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 Co-Authored-By: Ilya --- .../Microsoft.PowerShell.Management/FileSystem.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index 6ea156223be..26349ad9274 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -288,7 +288,7 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" { } It "Can get an appx package item" -Skip:$skipTest { - $pkgDir = (Get-AppxPackage Microsoft.WindowsCalculator).InstallLocation + $pkgDir = (Get-AppxPackage)[0].InstallLocation Get-Item $pkgDir\Calculator.exe -ErrorAction Stop | Should -BeOfType [System.IO.FileInfo] Get-Item -Path $pkgDir -ErrorAction Stop | Should -BeOfType [System.IO.DirectoryInfo]