From 78b8a8cd17883f34d09aa57ccdaf47a92a9de486 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olav=20R=C3=B8nnestad=20Birkeland?= <6450056+o-l-a-v@users.noreply.github.com> Date: Sun, 21 Sep 2025 12:21:21 +0200 Subject: [PATCH 1/2] Create test for not throwing on erroraction ignore --- .../GetInstalledPSResource.Tests.ps1 | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/test/GetInstalledPSResource/GetInstalledPSResource.Tests.ps1 b/test/GetInstalledPSResource/GetInstalledPSResource.Tests.ps1 index 1f6feab66..6e76741a1 100644 --- a/test/GetInstalledPSResource/GetInstalledPSResource.Tests.ps1 +++ b/test/GetInstalledPSResource/GetInstalledPSResource.Tests.ps1 @@ -11,6 +11,8 @@ Describe 'Test Get-InstalledPSResource for Module' -tags 'CI' { $PSGalleryName = Get-PSGalleryName $testModuleName = "test_module" $testScriptName = "test_script" + $TestEmptyDirectoryPath = [System.IO.Path]::Combine($env:TEMP,'EmptyDir') + Get-NewPSResourceRepositoryFile Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository @@ -18,12 +20,18 @@ Describe 'Test Get-InstalledPSResource for Module' -tags 'CI' { Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -Version "3.0" Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -Version "5.0" Install-PSResource -Name $testScriptName -Repository $PSGalleryName -TrustRepository + + $null = New-Item -Path $TestEmptyDirectoryPath -ItemType 'Directory' } AfterAll { Uninstall-PSResource -Name $testModuleName -Version "*" -ErrorAction SilentlyContinue Uninstall-PSResource -Name $testScriptName -Version "*" -ErrorAction SilentlyContinue Get-RevertPSResourceRepositoryFile + + if (Test-Path -Path $TestEmptyDirectoryPath -PathType 'Container') { + Remove-Item -Path $TestEmptyDirectoryPath -Recurse -Force + } } It "Get resources without any parameter values" { @@ -54,16 +62,17 @@ Describe 'Test Get-InstalledPSResource for Module' -tags 'CI' { $pkgs.Name | Should -Contain $testModuleName } -$testCases = - @{Version="[1.0.0.0]"; ExpectedVersion="1.0.0.0"; Reason="validate version, exact match"}, - @{Version="1.0.0.0"; ExpectedVersion="1.0.0.0"; Reason="validate version, exact match without bracket syntax"}, - @{Version="[1.0.0.0, 5.0.0.0]"; ExpectedVersion=@("5.0.0.0", "3.0.0.0", "1.0.0.0"); Reason="validate version, exact range inclusive"}, - @{Version="(1.0.0.0, 5.0.0.0)"; ExpectedVersion=@("3.0.0.0"); Reason="validate version, exact range exclusive"}, - @{Version="(1.0.0.0,)"; ExpectedVersion=@("5.0.0.0", "3.0.0.0"); Reason="validate version, minimum version exclusive"}, - @{Version="[1.0.0.0,)"; ExpectedVersion=@("5.0.0.0", "3.0.0.0", "1.0.0.0"); Reason="validate version, minimum version inclusive"}, - @{Version="(,5.0.0.0)"; ExpectedVersion=@("3.0.0.0", "1.0.0.0"); Reason="validate version, maximum version exclusive"}, - @{Version="(,5.0.0.0]"; ExpectedVersion=@("5.0.0.0", "3.0.0.0", "1.0.0.0"); Reason="validate version, maximum version inclusive"}, - @{Version="[1.0.0.0, 5.0.0.0)"; ExpectedVersion=@("3.0.0.0", "1.0.0.0"); Reason="validate version, mixed inclusive minimum and exclusive maximum version"} + $testCases = [array]( + @{Version="[1.0.0.0]"; ExpectedVersion="1.0.0.0"; Reason="validate version, exact match"}, + @{Version="1.0.0.0"; ExpectedVersion="1.0.0.0"; Reason="validate version, exact match without bracket syntax"}, + @{Version="[1.0.0.0, 5.0.0.0]"; ExpectedVersion=@("5.0.0.0", "3.0.0.0", "1.0.0.0"); Reason="validate version, exact range inclusive"}, + @{Version="(1.0.0.0, 5.0.0.0)"; ExpectedVersion=@("3.0.0.0"); Reason="validate version, exact range exclusive"}, + @{Version="(1.0.0.0,)"; ExpectedVersion=@("5.0.0.0", "3.0.0.0"); Reason="validate version, minimum version exclusive"}, + @{Version="[1.0.0.0,)"; ExpectedVersion=@("5.0.0.0", "3.0.0.0", "1.0.0.0"); Reason="validate version, minimum version inclusive"}, + @{Version="(,5.0.0.0)"; ExpectedVersion=@("3.0.0.0", "1.0.0.0"); Reason="validate version, maximum version exclusive"}, + @{Version="(,5.0.0.0]"; ExpectedVersion=@("5.0.0.0", "3.0.0.0", "1.0.0.0"); Reason="validate version, maximum version inclusive"}, + @{Version="[1.0.0.0, 5.0.0.0)"; ExpectedVersion=@("3.0.0.0", "1.0.0.0"); Reason="validate version, mixed inclusive minimum and exclusive maximum version"} + ) It "Get resource when given Name to " -TestCases $testCases { param($Version, $ExpectedVersion) @@ -144,8 +153,12 @@ $testCases = (Get-Alias Get-PSResource).Definition | Should -BeExactly 'Get-InstalledPSResource' } - # Windows only - It "Get resource under CurrentUser scope - Windows only" -Skip:(!(Get-IsWindows)) { + It "Should not throw on ErrorAction ignore when no subdirectories are found" { + Get-InstalledPSResource -Path $TestEmptyDirectoryPath -ErrorAction 'Ignore' | Should -Not -Throw + } + + # Windows only + It "Get resource under CurrentUser scope - Windows only" -Skip:(!(Get-IsWindows)) { $pkg = Get-InstalledPSResource -Name $testModuleName -Scope CurrentUser $pkg[0].Name | Should -Be $testModuleName $pkg[0].InstalledLocation.ToString().Contains("Documents") | Should -Be $true @@ -181,4 +194,4 @@ $testCases = $pkg.Name | Should -contain "testmodule99" $pkg.InstalledLocation.ToString().Contains("/.local") | Should -Be $true } -} \ No newline at end of file +} From 0509bd4efd7a483946001a18452250c2ff4820b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olav=20R=C3=B8nnestad=20Birkeland?= <6450056+o-l-a-v@users.noreply.github.com> Date: Sun, 21 Sep 2025 12:43:16 +0200 Subject: [PATCH 2/2] This should not be a terminating error --- src/code/GetInstalledPSResource.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/code/GetInstalledPSResource.cs b/src/code/GetInstalledPSResource.cs index dafd36400..749728196 100644 --- a/src/code/GetInstalledPSResource.cs +++ b/src/code/GetInstalledPSResource.cs @@ -101,14 +101,16 @@ protected override void BeginProcessing() var versionPaths = Utils.GetSubDirectories(resolvedPath); if (versionPaths.Length == 0) { - ThrowTerminatingError(new ErrorRecord( + WriteError(new ErrorRecord( new PSInvalidOperationException($"Error cannot find expected subdirectories in provided path: {Path}"), "PathMissingExpectedSubdirectories", ErrorCategory.InvalidOperation, this)); } - - _pathsToSearch.AddRange(versionPaths); + else + { + _pathsToSearch.AddRange(versionPaths); + } } else {