From e185f89591ac5fa5286bef60d087d852873df2be Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 12 Nov 2019 08:53:33 -0800 Subject: [PATCH] Handle exception if enumerating files fails when rebuilding path to have correct casing (#11014) --- .../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 720127163..c2e1a92af 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -161,14 +161,22 @@ namespace Microsoft.PowerShell.Commands 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 01f866aaa..26349ad92 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)[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] + 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