Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/code/GetInstalledPSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
37 changes: 25 additions & 12 deletions test/GetInstalledPSResource/GetInstalledPSResource.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,27 @@ 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
Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -Version "1.0"
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" {
Expand Down Expand Up @@ -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 <Reason> <Version>" -TestCases $testCases {
param($Version, $ExpectedVersion)
Expand Down Expand Up @@ -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
Expand Down
Loading