MegaBites-AI/Windows-powershell
0372
1#2# Copyright (c) Microsoft Corporation.3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6# https://www.apache.org/licenses/LICENSE-2.07#8# Unless required by applicable law or agreed to in writing, software9# distributed under the License is distributed on an "AS IS" BASIS,10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11# See the License for the specific language governing permissions and12# limitations under the License.13#14# ------------------ PackageManagement Test -----------------------------------15 16Describe "PackageManagement Acceptance Test" -Tags "Feature" {17 18 BeforeAll {19 # the package name for testing20 $packageName = "PowerShell.TestPackage"21 22 # register the asset directory23 $localSourceName = [Guid]::NewGuid().ToString("n")24 $localSourceLocation = Join-Path $PSScriptRoot assets25 $newSourceResult = Register-PackageSource -Name $localSourceName -provider NuGet -Location $localSourceLocation -Force -Trusted -Verbose26 Write-Verbose -Verbose -Message "Register-PackageSource -Name $localSourceName -provider NuGet -Location $localSourceLocation -Force -Trusted"27 $newSourceResult | Out-String -Stream | Write-Verbose -Verbose28 $localSourceCheck = Get-PackageSource -Name $localSourceName -ErrorAction Ignore29 30 $skipPackageTests = $false31 # It is possible that Register-PackageSource appears to succeed but the source is not actually registered32 # collect as much information as possible to help diagnose the problem33 if (($newSourceResult.Location -ne $localSourceLocation) -or ($localSourceCheck.Location -ne $localSourceLocation)) {34 Write-Verbose -Verbose "Skipping tests because local source could not be correctly created"35 if ( $newSourceResult ) {36 $newSourceResult | out-string -str | Write-Verbose -Verbose37 }38 else {39 Write-Verbose "newSourceResult is null"40 }41 42 if ( $localSourceCheck ) {43 $localSourceCheck | out-string -str | Write-Verbose -Verbose44 }45 else {46 Write-Verbose "LocalSourceCheck is null"47 }48 49 if (-not $IsWindows) {50 Get-ChildItem "$HOME/.config" -Recurse -File | out-string -str | Write-Verbose -Verbose51 }52 53 $skipPackageTests = $true54 }55 56 # register the gallery location57 $galleryLocation = "https://www.powershellgallery.com/api/v2"58 $gallerySourceName = [Guid]::newGuid().ToString("n")59 # this is expected to fail as there should already be a source pointing to the gallery url60 Register-PackageSource -Name $gallerySourceName -Location $galleryLocation -ProviderName 'PowerShellGet' -Trusted -ErrorAction SilentlyContinue61 62 $SavedProgressPreference = $ProgressPreference63 $ProgressPreference = "SilentlyContinue"64 65 Get-PackageSource | Out-String -Stream | Write-Verbose -Verbose66 }67 68 AfterAll {69 $ProgressPreference = $SavedProgressPreference70 try {71 # non-fatal errors72 Unregister-PackageSource -Source $localSourceName -ErrorAction Ignore73 Unregister-PackageSource -Name $gallerySourceName -ErrorAction Ignore74 Uninstall-Module NanoServerPackage -ErrorAction Ignore -WarningAction SilentlyContinue75 }76 catch {77 Write-Warning "Failure in AfterAll: $_"78 }79 }80 81 It "get-packageprovider" {82 $gpp = Get-PackageProvider83 $gpp.Name | Should -Contain 'NuGet'84 $gpp.Name | Should -Contain 'PowerShellGet'85 }86 87 It "find-packageprovider PowerShellGet" {88 $fpp = (Find-PackageProvider -Name "PowerShellGet" -Force).name89 $fpp | Should -Contain "PowerShellGet"90 }91 92 It "install-packageprovider, Expect succeed" {93 Set-ItResult -Pending -Because "local test package provider not installable"94 $ippArgs = @{95 Name = "NanoServerPackage"96 Force = $true97 Source = $galleryLocation98 Scope = "CurrentUser"99 WarningAction = "SilentlyContinue"100 }101 $ipp = (Install-PackageProvider @ippArgs).name102 $ipp | Should -Contain "NanoServerPackage"103 }104 105 It "Find-package" -skip:$skipPackageTests {106 $f = Find-Package -ProviderName NuGet -Name $packageName -Source $localSourceName107 $f.Name | Should -Contain "$packageName" -Because "PackageSource $localSourceLocation not created"108 }109 110 It "Install-package" -skip:$skipPackageTests {111 $i = Install-Package -ProviderName NuGet -Name $packageName -Force -Source $localSourceName -Scope CurrentUser112 $i.Name | Should -Contain "$packageName" -Because "PackageSource $localSourceLocation not created"113 }114 115 It "Get-package" -skip:$skipPackageTests {116 $g = Get-Package -ProviderName NuGet -Name $packageName117 $g.Name | Should -Contain "$packageName"118 }119 120 It "save-package" -skip:$skipPackageTests {121 $s = Save-Package -ProviderName NuGet -Name $packageName -Path $TestDrive -Force -Source $localSourceName122 $s.Name | Should -Contain "$packageName" -Because (Get-ChildItem $TestDrive)123 }124 125 It "uninstall-package" -skip:$skipPackageTests {126 $u = Uninstall-Package -ProviderName NuGet -Name $packageName127 $u.Name | Should -Contain "$packageName"128 }129}130 