Skip to content

Refactor tests to use test modules from PSGallery, not PoshTestGallery #628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Apr 14, 2022
Merged
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
226 changes: 138 additions & 88 deletions test/FindPSResource.Tests.ps1

Large diffs are not rendered by default.

82 changes: 42 additions & 40 deletions test/GetInstalledPSResource.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ Import-Module "$psscriptroot\PSGetTestUtils.psm1" -Force
Describe 'Test Get-PSResource for Module' {

BeforeAll{
$TestGalleryName = Get-PoshTestGalleryName
$PSGalleryName = Get-PSGalleryName
$testModuleName = "test_module"
$testScriptName = "test_script"
Get-NewPSResourceRepositoryFile

Install-PSResource -Name ContosoServer -Repository $TestGalleryName -Verbose
Install-PSResource ContosoServer -Repository $TestGalleryName -TrustRepository -Version "2.0"
Install-PSResource ContosoServer -Repository $TestGalleryName -TrustRepository -Version "1.5"
Install-PSResource ContosoServer -Repository $TestGalleryName -TrustRepository -Version "1.0"
Install-PSResource TestTestScript -Repository $TestGalleryName -TrustRepository
Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -Verbose
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
}

AfterAll {
Uninstall-PSResource -Name $testModuleName -Version "*"
Uninstall-PSResource -Name $testScriptName -Version "*"
Get-RevertPSResourceRepositoryFile
}

Expand All @@ -29,59 +31,59 @@ Describe 'Test Get-PSResource for Module' {
}

It "Get specific module resource by name" {
$pkg = Get-PSResource -Name ContosoServer
$pkg.Name | Should -Contain "ContosoServer"
$pkg = Get-PSResource -Name $testModuleName
$pkg.Name | Should -Contain $testModuleName
}

It "Get specific script resource by name" {
$pkg = Get-PSResource -Name TestTestScript
$pkg.Name | Should -Be "TestTestScript"
$pkg = Get-PSResource -Name $testScriptName
$pkg.Name | Should -Be $testScriptName
}

It "Get resource when given Name to <Reason> <Version>" -TestCases @(
@{Name="*tosoSer*"; ExpectedName="ContosoServer"; Reason="validate name, with wildcard at beginning and end of name: *tosoSer*"},
@{Name="ContosoSer*"; ExpectedName="ContosoServer"; Reason="validate name, with wildcard at end of name: ContosoSer*"},
@{Name="*tosoServer"; ExpectedName="ContosoServer"; Reason="validate name, with wildcard at beginning of name: *tosoServer"},
@{Name="Cont*erver"; ExpectedName="ContosoServer"; Reason="validate name, with wildcard in middle of name: Cont*erver"}
@{Name="*est_modul*"; ExpectedName=$testModuleName; Reason="validate name, with wildcard at beginning and end of name: *est_modul*"},
@{Name="test_mod*"; ExpectedName=$testModuleName; Reason="validate name, with wildcard at end of name: test_mod*"},
@{Name="*est_module"; ExpectedName=$testModuleName; Reason="validate name, with wildcard at beginning of name: *est_module"},
@{Name="tes*ule"; ExpectedName=$testModuleName; Reason="validate name, with wildcard in middle of name: tes*ule"}
) {
param($Version, $ExpectedVersion)
$pkgs = Get-PSResource -Name $Name
$pkgs.Name | Should -Contain "ContosoServer"
$pkgs.Name | Should -Contain $testModuleName
}

$testCases =
@{Version="[2.0.0.0]"; ExpectedVersion="2.0.0.0"; Reason="validate version, exact match"},
@{Version="2.0.0.0"; ExpectedVersion="2.0.0.0"; Reason="validate version, exact match without bracket syntax"},
@{Version="[1.0.0.0, 2.5.0.0]"; ExpectedVersion=@("2.5.0.0", "2.0.0.0", "1.5.0.0", "1.0.0.0"); Reason="validate version, exact range inclusive"},
@{Version="(1.0.0.0, 2.5.0.0)"; ExpectedVersion=@("2.0.0.0", "1.5.0.0"); Reason="validate version, exact range exclusive"},
@{Version="(1.0.0.0,)"; ExpectedVersion=@("2.5.0.0", "2.0.0.0", "1.5.0.0"); Reason="validate version, minimum version exclusive"},
@{Version="[1.0.0.0,)"; ExpectedVersion=@("2.5.0.0", "2.0.0.0", "1.5.0.0", "1.0.0.0"); Reason="validate version, minimum version inclusive"},
@{Version="(,1.5.0.0)"; ExpectedVersion="1.0.0.0"; Reason="validate version, maximum version exclusive"},
@{Version="(,1.5.0.0]"; ExpectedVersion=@("1.5.0.0", "1.0.0.0"); Reason="validate version, maximum version inclusive"},
@{Version="[1.0.0.0, 2.5.0.0)"; ExpectedVersion=@("2.0.0.0", "1.5.0.0", "1.0.0.0"); Reason="validate version, mixed inclusive minimum and exclusive maximum version"}
@{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)
$pkgs = Get-PSResource -Name "ContosoServer" -Version $Version
$pkgs.Name | Should -Contain "ContosoServer"
$pkgs = Get-PSResource -Name $testModuleName -Version $Version
$pkgs.Name | Should -Contain $testModuleName
$pkgs.Version | Should -Be $ExpectedVersion
}

It "Throw invalid version error when passing incorrectly formatted version such as <Description>" -TestCases @(
@{Version='[1.*.0]'; Description="version with wilcard in middle"},
@{Version='[*.5.0.0]'; Description="version with wilcard at start"},
@{Version='[*.0.0.0]'; Description="version with wilcard at start"},
@{Version='[1.*.0.0]'; Description="version with wildcard at second digit"},
@{Version='[1.5.*.0]'; Description="version with wildcard at third digit"}
@{Version='[1.5.0.*'; Description="version with wildcard at end"},
@{Version='[1.0.*.0]'; Description="version with wildcard at third digit"}
@{Version='[1.0.0.*'; Description="version with wildcard at end"},
@{Version='[1..0.0]'; Description="version with missing digit in middle"},
@{Version='[1.5.0.]'; Description="version with missing digit at end"},
@{Version='[1.5.0.0.0]'; Description="version with more than 4 digits"}
@{Version='[1.0.0.]'; Description="version with missing digit at end"},
@{Version='[1.0.0.0.0]'; Description="version with more than 4 digits"}
) {
param($Version, $Description)

$res = $null
try {
$res = Find-PSResource -Name "ContosoServer" -Version $Version -Repository $TestGalleryName -ErrorAction Ignore
$res = Find-PSResource -Name $testModuleName -Version $Version -Repository $PSGalleryName -ErrorAction Ignore
}
catch {}

Expand All @@ -90,28 +92,28 @@ $testCases =

# These versions technically parse into proper NuGet versions, but will not return the version expected
It "Does not return resource when passing incorrectly formatted version such as <Description>, does not throw error" -TestCases @(
@{Version='(1.5.0.0)'; Description="exlcusive version (8.1.0.0)"},
@{Version='[1-5-0-0]'; Description="version formatted with invalid delimiter"}
@{Version='(1.0.0.0)'; Description="exlcusive version (8.1.0.0)"},
@{Version='[1-0-0-0]'; Description="version formatted with invalid delimiter"}

) {
param($Version, $Description)

$res = $null
try {
$res = Find-PSResource -Name "ContosoServer" -Version $Version -Repository $TestGalleryName -ErrorAction Ignore
$res = Find-PSResource -Name $testModuleName -Version $Version -Repository $PSGalleryName -ErrorAction Ignore
}
catch {}

$res | Should -BeNullOrEmpty
}

It "Get resources when given Name, and Version is '*'" {
$pkgs = Get-PSResource -Name ContosoServer -Version "*"
$pkgs = Get-PSResource -Name $testModuleName -Version "*"
$pkgs.Count | Should -BeGreaterOrEqual 2
}

It "Get prerelease version module when version with correct prerelease label is specified" {
Install-PSResource -Name $testModuleName -Version "5.2.5-alpha001" -Repository $TestGalleryName
Install-PSResource -Name $testModuleName -Version "5.2.5-alpha001" -Repository $PSGalleryName -TrustRepository
$res = Get-PSResource -Name $testModuleName -Version "5.2.5"
$res | Should -BeNullOrEmpty
$res = Get-PSResource -Name $testModuleName -Version "5.2.5-alpha001"
Expand All @@ -121,12 +123,12 @@ $testCases =
}

It "Get prerelease version script when version with correct prerelease label is specified" {
Install-PSResource -Name $testScriptName -Version "3.0.0-alpha001" -Repository $TestGalleryName
Install-PSResource -Name $testScriptName -Version "3.0.0-alpha" -Repository $PSGalleryName -TrustRepository
$res = Get-PSResource -Name $testScriptName -Version "3.0.0"
$res | Should -BeNullOrEmpty
$res = Get-PSResource -Name $testScriptName -Version "3.0.0-alpha001"
$res = Get-PSResource -Name $testScriptName -Version "3.0.0-alpha"
$res.Name | Should -Be $testScriptName
$res.Version | Should -Be "3.0.0"
$res.Prerelease | Should -Be "alpha001"
$res.Prerelease | Should -Be "alpha"
}
}
Loading