Skip to content

Commit 0c52638

Browse files
JamesWTruherdaxian-dbw
authored andcommitted
Improve package management acceptance tests by not going to the gallery (#19412)
1 parent e1aca8f commit 0c52638

File tree

2 files changed

+41
-31
lines changed

2 files changed

+41
-31
lines changed

test/powershell/Modules/PackageManagement/PackageManagement.Tests.ps1

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,36 @@
1313
#
1414
# ------------------ PackageManagement Test -----------------------------------
1515

16-
$gallery = "https://www.powershellgallery.com/api/v2"
17-
$source = 'OneGetTestSource'
18-
1916
Describe "PackageManagement Acceptance Test" -Tags "Feature" {
2017

21-
BeforeAll{
22-
Register-PackageSource -Name Nugettest -provider NuGet -Location https://www.nuget.org/api/v2 -Force
18+
BeforeAll {
19+
# the package name for testing
20+
$packageName = "PowerShell.TestPackage"
21+
22+
# register the asset directory
23+
$localSourceName = [Guid]::NewGuid().ToString("n")
24+
$localSourceLocation = Join-Path $PSScriptRoot assets
25+
Register-PackageSource -Name $localSourceName -provider NuGet -Location $localSourceLocation -Force -Trusted
26+
27+
# register the gallery location
28+
$galleryLocation = "https://www.powershellgallery.com/api/v2"
29+
$gallerySourceName = [Guid]::newGuid().ToString("n")
30+
Register-PackageSource -Name $gallerySourceName -Location $galleryLocation -ProviderName 'PowerShellGet' -Trusted -ErrorAction SilentlyContinue
31+
32+
$SavedProgressPreference = $ProgressPreference
33+
$ProgressPreference = "SilentlyContinue"
34+
}
2335

24-
$packageSource = Get-PackageSource -Location $gallery -ErrorAction SilentlyContinue
25-
if ($packageSource) {
26-
$source = $packageSource.Name
27-
Set-PackageSource -Name $source -Trusted
28-
} else {
29-
Register-PackageSource -Name $source -Location $gallery -ProviderName 'PowerShellGet' -Trusted -ErrorAction SilentlyContinue
36+
AfterAll {
37+
$ProgressPreference = $SavedProgressPreference
38+
Unregister-PackageSource -Source $localSourceName -ErrorAction Ignore
39+
Unregister-PackageSource -Name $gallerySourceName -ErrorAction Ignore
40+
Uninstall-Module NanoServerPackage -ErrorAction Ignore -WarningAction SilentlyContinue
3041
}
3142

32-
$SavedProgressPreference = $ProgressPreference
33-
$ProgressPreference = "SilentlyContinue"
34-
}
35-
AfterAll {
36-
$ProgressPreference = $SavedProgressPreference
37-
}
3843
It "get-packageprovider" {
39-
4044
$gpp = Get-PackageProvider
41-
4245
$gpp.Name | Should -Contain 'NuGet'
43-
4446
$gpp.Name | Should -Contain 'PowerShellGet'
4547
}
4648

@@ -50,32 +52,40 @@ Describe "PackageManagement Acceptance Test" -Tags "Feature" {
5052
}
5153

5254
It "install-packageprovider, Expect succeed" {
53-
$ipp = (Install-PackageProvider -Name NanoServerPackage -Force -Source $source -Scope CurrentUser).name
55+
Set-ItResult -Pending -Because "local test package provider not installable"
56+
$ippArgs = @{
57+
Name = "NanoServerPackage"
58+
Force = $true
59+
Source = $galleryLocation
60+
Scope = "CurrentUser"
61+
WarningAction = "SilentlyContinue"
62+
}
63+
$ipp = (Install-PackageProvider @ippArgs).name
5464
$ipp | Should -Contain "NanoServerPackage"
5565
}
5666

5767
It "Find-package" {
58-
$f = Find-Package -ProviderName NuGet -Name jquery -Source Nugettest
59-
$f.Name | Should -Contain "jquery"
68+
$f = Find-Package -ProviderName NuGet -Name $packageName -Source $localSourceName
69+
$f.Name | Should -Contain "$packageName"
6070
}
6171

6272
It "Install-package" {
63-
$i = Install-Package -ProviderName NuGet -Name jquery -Force -Source Nugettest -Scope CurrentUser
64-
$i.Name | Should -Contain "jquery"
73+
$i = Install-Package -ProviderName NuGet -Name $packageName -Force -Source $localSourceName -Scope CurrentUser
74+
$i.Name | Should -Contain "$packageName"
6575
}
6676

6777
It "Get-package" {
68-
$g = Get-Package -ProviderName NuGet -Name jquery
69-
$g.Name | Should -Contain "jquery"
78+
$g = Get-Package -ProviderName NuGet -Name $packageName
79+
$g.Name | Should -Contain "$packageName"
7080
}
7181

7282
It "save-package" {
73-
$s = Save-Package -ProviderName NuGet -Name jquery -Path $TestDrive -Force -Source Nugettest
74-
$s.Name | Should -Contain "jquery"
83+
$s = Save-Package -ProviderName NuGet -Name $packageName -Path $TestDrive -Force -Source $localSourceName
84+
$s.Name | Should -Contain "$packageName"
7585
}
7686

7787
It "uninstall-package" {
78-
$u = Uninstall-Package -ProviderName NuGet -Name jquery
79-
$u.Name | Should -Contain "jquery"
88+
$u = Uninstall-Package -ProviderName NuGet -Name $packageName
89+
$u.Name | Should -Contain "$packageName"
8090
}
8191
}

0 commit comments

Comments
 (0)