diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index 300c5b6c9..64c42dddd 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -332,16 +332,11 @@ private List InstallPackage( if (!_quiet) { int activityId = 0; - string activity = ""; - string statusDescription = ""; - - // Installing parent package (one whose name was passed in to install) - activityId = 0; - activity = string.Format("Installing {0}...", pkg.Name); - statusDescription = string.Format("{0}% Complete:", Math.Round(((double)totalInstalledPkgCount / totalPkgs) * 100), 2); - - var progressRecord = new ProgressRecord(activityId, activity, statusDescription); - _cmdletPassedIn.WriteProgress(progressRecord); + int percentComplete = ((totalInstalledPkgCount * 100) / totalPkgs); + string activity = string.Format("Installing {0}...", pkg.Name); + string statusDescription = string.Format("{0}% Complete", percentComplete); + _cmdletPassedIn.WriteProgress( + new ProgressRecord(activityId, activity, statusDescription)); } // Create PackageIdentity in order to download diff --git a/test/GetInstalledPSResource.Tests.ps1 b/test/GetInstalledPSResource.Tests.ps1 index b7901be78..63d44e241 100644 --- a/test/GetInstalledPSResource.Tests.ps1 +++ b/test/GetInstalledPSResource.Tests.ps1 @@ -1,6 +1,7 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. +$ProgressPreference = "SilentlyContinue" Import-Module "$psscriptroot\PSGetTestUtils.psm1" -Force Describe 'Test Get-PSResource for Module' { diff --git a/test/InstallPSResource.Tests.ps1 b/test/InstallPSResource.Tests.ps1 index b87fd9868..b5cf93604 100644 --- a/test/InstallPSResource.Tests.ps1 +++ b/test/InstallPSResource.Tests.ps1 @@ -1,13 +1,12 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. +$ProgressPreference = "SilentlyContinue" Import-Module "$psscriptroot\PSGetTestUtils.psm1" -Force Describe 'Test Install-PSResource for Module' { BeforeAll { - $OldProgressPreference = $ProgressPreference - $ProgressPreference = "SilentlyContinue" $TestGalleryName = Get-PoshTestGalleryName $PSGalleryName = Get-PSGalleryName $NuGetGalleryName = Get-NuGetGalleryName @@ -23,7 +22,6 @@ Describe 'Test Install-PSResource for Module' { } AfterAll { - $ProgressPreference = $OldProgressPreference Get-RevertPSResourceRepositoryFile } diff --git a/test/SavePSResource.Tests.ps1 b/test/SavePSResource.Tests.ps1 index 067baecbb..6704ee72b 100644 --- a/test/SavePSResource.Tests.ps1 +++ b/test/SavePSResource.Tests.ps1 @@ -1,261 +1,262 @@ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. - -Import-Module "$psscriptroot\PSGetTestUtils.psm1" -Force - -Describe 'Test Save-PSResource for PSResources' { - - BeforeAll{ - $TestGalleryName = Get-PoshTestGalleryName - $PSGalleryName = Get-PSGalleryName - $NuGetGalleryName = Get-NuGetGalleryName - $testModuleName = "test_module" - $testModuleName2 = "TestModule" - $testScriptName = "TestTestScript" - Get-NewPSResourceRepositoryFile - Register-LocalRepos - - $SaveDir = Join-Path $TestDrive 'SavedResources' - New-Item -Item Directory $SaveDir -Force - } - - AfterEach { - # Delte contents of save directory - Remove-Item -Path (Join-Path $SaveDir '*') -Recurse -Force -ErrorAction SilentlyContinue - } - - AfterAll { - Get-RevertPSResourceRepositoryFile - } - - It "Save specific module resource by name" { - Save-PSResource -Name $testModuleName2 -Repository $TestGalleryName -Path $SaveDir - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName2 - $pkgDir | Should -Not -BeNullOrEmpty - (Get-ChildItem $pkgDir.FullName).Count | Should -Be 1 - } - - It "Save specific script resource by name" { - Save-PSResource -Name $testScriptName -Repository $TestGalleryName -Path $SaveDir - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "TestTestScript.ps1" - $pkgDir | Should -Not -BeNullOrEmpty - (Get-ChildItem $pkgDir.FullName).Count | Should -Be 1 - } - - It "Save multiple resources by name" { - $pkgNames = @("TestModule","TestModule99") - Save-PSResource -Name $pkgNames -Repository $TestGalleryName -Path $SaveDir - $pkgDirs = Get-ChildItem -Path $SaveDir | Where-Object { $_.Name -eq "TestModule" -or $_.Name -eq "TestModule99" } - $pkgDirs.Count | Should -Be 2 - (Get-ChildItem $pkgDirs[0].FullName).Count | Should -Be 1 - (Get-ChildItem $pkgDirs[1].FullName).Count | Should -Be 1 - } - - It "Should not save resource given nonexistant name" { - Save-PSResource -Name NonExistentModule -Repository $TestGalleryName -Path $SaveDir -ErrorVariable err -ErrorAction SilentlyContinue - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "NonExistentModule" - $pkgDir.Name | Should -BeNullOrEmpty - $err.Count | Should -Not -Be 0 - $err[0].FullyQualifiedErrorId | Should -BeExactly "ResourceNotFoundError,Microsoft.PowerShell.PowerShellGet.Cmdlets.SavePSResource" - } - - It "Not Save module with Name containing wildcard" { - Save-PSResource -Name "TestModule*" -Repository $TestGalleryName -Path $SaveDir -ErrorVariable err -ErrorAction SilentlyContinue - $err.Count | Should -Not -Be 0 - $err[0].FullyQualifiedErrorId | Should -BeExactly "NameContainsWildcard,Microsoft.PowerShell.PowerShellGet.Cmdlets.SavePSResource" - } - - # Do some version testing, but Find-PSResource should be doing thorough testing - It "Should save resource given name and exact version" { - Save-PSResource -Name $testModuleName2 -Version "1.2.0" -Repository $TestGalleryName -Path $SaveDir - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName2 - $pkgDir | Should -Not -BeNullOrEmpty - $pkgDirVersion = Get-ChildItem $pkgDir.FullName - $pkgDirVersion.Name | Should -Be "1.2.0" - } - - It "Should save resource given name and exact version with bracket syntax" { - Save-PSResource -Name $testModuleName2 -Version "[1.2.0]" -Repository $TestGalleryName -Path $SaveDir - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName2 - $pkgDir | Should -Not -BeNullOrEmpty - $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName - $pkgDirVersion.Name | Should -Be "1.2.0" - } - - It "Should save resource given name and exact range inclusive [1.0.0, 1.1.1]" { - Save-PSResource -Name $testModuleName2 -Version "[1.0.0, 1.1.1]" -Repository $TestGalleryName -Path $SaveDir - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName2 - $pkgDir | Should -Not -BeNullOrEmpty - $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName - $pkgDirVersion.Name | Should -Be "1.1.1" - } - - It "Should save resource given name and exact range exclusive (1.0.0, 1.1.1)" { - Save-PSResource -Name $testModuleName2 -Version "(1.0.0, 1.1.1)" -Repository $TestGalleryName -Path $SaveDir - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName2 - $pkgDir | Should -Not -BeNullOrEmpty - $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName - $pkgDirVersion.Name | Should -Be "1.1" - } - - It "Should not save resource with incorrectly formatted version such as " -TestCases @( - @{Version='(1.2.0.0)'; Description="exclusive version (2.10.0.0)"}, - @{Version='[1-2-0-0]'; Description="version formatted with invalid delimiter [1-2-0-0]"} - ) { - param($Version, $Description) - - Save-PSResource -Name $testModuleName2 -Version $Version -Repository $TestGalleryName -Path $SaveDir -ErrorVariable err -ErrorAction SilentlyContinue - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName2 - $pkgDir | Should -BeNullOrEmpty - $err.Count | Should -Not -Be 0 - $err[0].FullyQualifiedErrorId | Should -BeExactly "ResourceNotFoundError,Microsoft.PowerShell.PowerShellGet.Cmdlets.SavePSResource" - } - - It "Save resource when given Name, Version '*', should install the latest version" { - Save-PSResource -Name $testModuleName2 -Version "*" -Repository $TestGalleryName -Path $SaveDir - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName2 - $pkgDir | Should -Not -BeNullOrEmpty - $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName - $pkgDirVersion.Name | Should -Be "1.3.0" - } - - It "Save resource with latest (including prerelease) version given Prerelease parameter" { - Save-PSResource -Name "TestModulePrerelease" -Prerelease -Repository $TestGalleryName -Path $SaveDir - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "TestModulePrerelease" - $pkgDir | Should -Not -BeNullOrEmpty - $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName - $pkgDirVersion.Name | Should -Be "0.0.1" - } - - It "Save a module with a dependency" { - Save-PSResource -Name "PSGetTestModule" -Prerelease -Repository $TestGalleryName -Path $SaveDir - $pkgDirs = Get-ChildItem -Path $SaveDir | Where-Object { $_.Name -eq "PSGetTestModule" -or $_.Name -eq "PSGetTestDependency1" } - $pkgDirs.Count | Should -Be 2 - (Get-ChildItem $pkgDirs[0].FullName).Count | Should -Be 1 - (Get-ChildItem $pkgDirs[1].FullName).Count | Should -Be 1 - } - - It "Save a module with a dependency and skip saving the dependency" { - Save-PSResource -Name "PSGetTestModule" -Prerelease -Repository $TestGalleryName -Path $SaveDir -SkipDependencyCheck - $pkgDirs = Get-ChildItem -Path $SaveDir | Where-Object { $_.Name -eq "PSGetTestModule" -or $_.Name -eq "PSGetTestDependency1" } - $pkgDirs.Count | Should -Be 1 - (Get-ChildItem $pkgDirs[0].FullName).Count | Should -Be 1 - } - - It "Save resource via InputObject by piping from Find-PSresource" { - Find-PSResource -Name $testModuleName2 -Repository $TestGalleryName | Save-PSResource -Path $SaveDir - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName2 - $pkgDir | Should -Not -BeNullOrEmpty - $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName - $pkgDirVersion.Name | Should -Be "1.3.0" - } - - It "Save resource should not prompt 'trust repository' if repository is not trusted but -TrustRepository is used" { - try { - Set-PSResourceRepository PoshTestGallery -Trusted:$false - Save-PSResource -Name $testModuleName2 -Repository $TestGalleryName -TrustRepository -Path $SaveDir - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName2 - $pkgDir | Should -Not -BeNullOrEmpty - $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName - $pkgDirVersion.Name | Should -Be "1.3.0" - } - finally { - Set-PSResourceRepository PoshTestGallery -Trusted - } - } - - It "Save resource from local repository given Repository parameter" { - $publishModuleName = "TestFindModule" - $repoName = "psgettestlocal" - Get-ModuleResourcePublishedToLocalRepoTestDrive $publishModuleName $repoName - Set-PSResourceRepository "psgettestlocal" -Trusted:$true - - Save-PSResource -Name $publishModuleName -Repository $repoName -Path $SaveDir - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $publishModuleName - $pkgDir | Should -Not -BeNullOrEmpty - (Get-ChildItem -Path $pkgDir.FullName).Count | Should -Be 1 - } - - It "Save specific module resource by name when no repository is specified" { - Set-PSResourceRepository "PoshTestGallery" -Trusted:$True - Set-PSResourceRepository "PSGallery" -Trusted:$True - Set-PSResourceRepository "psgettestlocal2" -Trusted:$True - - Save-PSResource -Name "TestModule" -Path $SaveDir - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName2 - $pkgDir | Should -Not -BeNullOrEmpty - (Get-ChildItem -Path $pkgDir.FullName).Count | Should -Be 1 - } - - It "Save PSResourceInfo object piped in" { - Find-PSResource -Name $testModuleName2 -Version "1.1.0.0" -Repository $TestGalleryName | Save-PSResource -Path $SaveDir - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName2 - $pkgDir | Should -Not -BeNullOrEmpty - (Get-ChildItem -Path $pkgDir.FullName).Count | Should -Be 1 - } - - It "Save PSResourceInfo object piped in for prerelease version object" { - Find-PSResource -Name $testModuleName -Version "4.5.2-alpha001" -Repository $TestGalleryName | Save-PSResource -Path $SaveDir - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName - $pkgDir | Should -Not -BeNullOrEmpty - (Get-ChildItem -Path $pkgDir.FullName).Count | Should -Be 1 - } - - It "Save module as a nupkg" { - Save-PSResource -Name $testModuleName2 -Version "1.3.0" -Repository $TestGalleryName -Path $SaveDir -AsNupkg - write-host $SaveDir - write-host - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "testmodule.1.3.0.nupkg" - $pkgDir | Should -Not -BeNullOrEmpty - } - - It "Save script as a nupkg" { - Save-PSResource -Name $testScriptName -Version "1.3.1" -Repository $TestGalleryName -Path $SaveDir -AsNupkg - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "testtestscript.1.3.1.nupkg" - $pkgDir | Should -Not -BeNullOrEmpty - } - - It "Save module and include XML metadata file" { - Save-PSResource -Name $testModuleName2 -Version "1.3.0" -Repository $TestGalleryName -Path $SaveDir -IncludeXML - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName2 - $pkgDir | Should -Not -BeNullOrEmpty - $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName - $pkgDirVersion.Name | Should -Be "1.3.0" - $xmlFile = Get-ChildItem -Path $pkgDirVersion.FullName | Where-Object Name -eq "PSGetModuleInfo.xml" - $xmlFile | Should -Not -BeNullOrEmpty - } - - It "Save module using -PassThru" { - $res = Save-PSResource -Name $testModuleName2 -Version "1.3.0" -Repository $TestGalleryName -Path $SaveDir -PassThru - $res.Name | Should -Be $testModuleName2 - $res.Version | Should -Be "1.3.0.0" - } -<# - # Tests should not write to module directory - It "Save specific module resource by name if no -Path param is specifed" { - Save-PSResource -Name $testModuleName2 -Repository $TestGalleryName - $pkgDir = Get-ChildItem -Path . | Where-Object Name -eq $testModuleName2 - $pkgDir | Should -Not -BeNullOrEmpty - (Get-ChildItem $pkgDir.FullName).Count | Should -Be 1 - - # Delete all files and subdirectories in the current , but keep the directory $SaveDir - if (Test-Path -Path $pkgDir.FullName) { - Remove-Item -Path $pkgDir.FullName -Recurse -Force -ErrorAction SilentlyContinue - } - } -#> - -<# - # This needs to be manually tested due to prompt - It "Install resource should prompt 'trust repository' if repository is not trusted" { - Set-PSResourceRepository PoshTestGallery -Trusted:$false - - Install-PSResource -Name $testModuleName2 -Repository $TestGalleryName -confirm:$false - - $pkg = Get-Module $testModuleName2 -ListAvailable - $pkg.Name | Should -Be $testModuleName2 - - Set-PSResourceRepository PoshTestGallery -Trusted - } -#> +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +$ProgressPreference = "SilentlyContinue" +Import-Module "$psscriptroot\PSGetTestUtils.psm1" -Force + +Describe 'Test Save-PSResource for PSResources' { + + BeforeAll { + $TestGalleryName = Get-PoshTestGalleryName + $PSGalleryName = Get-PSGalleryName + $NuGetGalleryName = Get-NuGetGalleryName + $testModuleName = "test_module" + $testModuleName2 = "TestModule" + $testScriptName = "TestTestScript" + Get-NewPSResourceRepositoryFile + Register-LocalRepos + + $SaveDir = Join-Path $TestDrive 'SavedResources' + New-Item -Item Directory $SaveDir -Force + } + + AfterEach { + # Delte contents of save directory + Remove-Item -Path (Join-Path $SaveDir '*') -Recurse -Force -ErrorAction SilentlyContinue + } + + AfterAll { + Get-RevertPSResourceRepositoryFile + } + + It "Save specific module resource by name" { + Save-PSResource -Name $testModuleName2 -Repository $TestGalleryName -Path $SaveDir + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName2 + $pkgDir | Should -Not -BeNullOrEmpty + (Get-ChildItem $pkgDir.FullName).Count | Should -Be 1 + } + + It "Save specific script resource by name" { + Save-PSResource -Name $testScriptName -Repository $TestGalleryName -Path $SaveDir + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "TestTestScript.ps1" + $pkgDir | Should -Not -BeNullOrEmpty + (Get-ChildItem $pkgDir.FullName).Count | Should -Be 1 + } + + It "Save multiple resources by name" { + $pkgNames = @("TestModule","TestModule99") + Save-PSResource -Name $pkgNames -Repository $TestGalleryName -Path $SaveDir + $pkgDirs = Get-ChildItem -Path $SaveDir | Where-Object { $_.Name -eq "TestModule" -or $_.Name -eq "TestModule99" } + $pkgDirs.Count | Should -Be 2 + (Get-ChildItem $pkgDirs[0].FullName).Count | Should -Be 1 + (Get-ChildItem $pkgDirs[1].FullName).Count | Should -Be 1 + } + + It "Should not save resource given nonexistant name" { + Save-PSResource -Name NonExistentModule -Repository $TestGalleryName -Path $SaveDir -ErrorVariable err -ErrorAction SilentlyContinue + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "NonExistentModule" + $pkgDir.Name | Should -BeNullOrEmpty + $err.Count | Should -Not -Be 0 + $err[0].FullyQualifiedErrorId | Should -BeExactly "ResourceNotFoundError,Microsoft.PowerShell.PowerShellGet.Cmdlets.SavePSResource" + } + + It "Not Save module with Name containing wildcard" { + Save-PSResource -Name "TestModule*" -Repository $TestGalleryName -Path $SaveDir -ErrorVariable err -ErrorAction SilentlyContinue + $err.Count | Should -Not -Be 0 + $err[0].FullyQualifiedErrorId | Should -BeExactly "NameContainsWildcard,Microsoft.PowerShell.PowerShellGet.Cmdlets.SavePSResource" + } + + # Do some version testing, but Find-PSResource should be doing thorough testing + It "Should save resource given name and exact version" { + Save-PSResource -Name $testModuleName2 -Version "1.2.0" -Repository $TestGalleryName -Path $SaveDir + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName2 + $pkgDir | Should -Not -BeNullOrEmpty + $pkgDirVersion = Get-ChildItem $pkgDir.FullName + $pkgDirVersion.Name | Should -Be "1.2.0" + } + + It "Should save resource given name and exact version with bracket syntax" { + Save-PSResource -Name $testModuleName2 -Version "[1.2.0]" -Repository $TestGalleryName -Path $SaveDir + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName2 + $pkgDir | Should -Not -BeNullOrEmpty + $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName + $pkgDirVersion.Name | Should -Be "1.2.0" + } + + It "Should save resource given name and exact range inclusive [1.0.0, 1.1.1]" { + Save-PSResource -Name $testModuleName2 -Version "[1.0.0, 1.1.1]" -Repository $TestGalleryName -Path $SaveDir + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName2 + $pkgDir | Should -Not -BeNullOrEmpty + $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName + $pkgDirVersion.Name | Should -Be "1.1.1" + } + + It "Should save resource given name and exact range exclusive (1.0.0, 1.1.1)" { + Save-PSResource -Name $testModuleName2 -Version "(1.0.0, 1.1.1)" -Repository $TestGalleryName -Path $SaveDir + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName2 + $pkgDir | Should -Not -BeNullOrEmpty + $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName + $pkgDirVersion.Name | Should -Be "1.1" + } + + It "Should not save resource with incorrectly formatted version such as " -TestCases @( + @{Version='(1.2.0.0)'; Description="exclusive version (2.10.0.0)"}, + @{Version='[1-2-0-0]'; Description="version formatted with invalid delimiter [1-2-0-0]"} + ) { + param($Version, $Description) + + Save-PSResource -Name $testModuleName2 -Version $Version -Repository $TestGalleryName -Path $SaveDir -ErrorVariable err -ErrorAction SilentlyContinue + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName2 + $pkgDir | Should -BeNullOrEmpty + $err.Count | Should -Not -Be 0 + $err[0].FullyQualifiedErrorId | Should -BeExactly "ResourceNotFoundError,Microsoft.PowerShell.PowerShellGet.Cmdlets.SavePSResource" + } + + It "Save resource when given Name, Version '*', should install the latest version" { + Save-PSResource -Name $testModuleName2 -Version "*" -Repository $TestGalleryName -Path $SaveDir -ErrorAction SilentlyContinue + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName2 + $pkgDir | Should -Not -BeNullOrEmpty + $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName + $pkgDirVersion.Name | Should -Be "1.3.0" + } + + It "Save resource with latest (including prerelease) version given Prerelease parameter" { + Save-PSResource -Name "TestModulePrerelease" -Prerelease -Repository $TestGalleryName -Path $SaveDir + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "TestModulePrerelease" + $pkgDir | Should -Not -BeNullOrEmpty + $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName + $pkgDirVersion.Name | Should -Be "0.0.1" + } + + It "Save a module with a dependency" { + Save-PSResource -Name "PSGetTestModule" -Prerelease -Repository $TestGalleryName -Path $SaveDir + $pkgDirs = Get-ChildItem -Path $SaveDir | Where-Object { $_.Name -eq "PSGetTestModule" -or $_.Name -eq "PSGetTestDependency1" } + $pkgDirs.Count | Should -Be 2 + (Get-ChildItem $pkgDirs[0].FullName).Count | Should -Be 1 + (Get-ChildItem $pkgDirs[1].FullName).Count | Should -Be 1 + } + + It "Save a module with a dependency and skip saving the dependency" { + Save-PSResource -Name "PSGetTestModule" -Prerelease -Repository $TestGalleryName -Path $SaveDir -SkipDependencyCheck + $pkgDirs = Get-ChildItem -Path $SaveDir | Where-Object { $_.Name -eq "PSGetTestModule" -or $_.Name -eq "PSGetTestDependency1" } + $pkgDirs.Count | Should -Be 1 + (Get-ChildItem $pkgDirs[0].FullName).Count | Should -Be 1 + } + + It "Save resource via InputObject by piping from Find-PSresource" { + Find-PSResource -Name $testModuleName2 -Repository $TestGalleryName | Save-PSResource -Path $SaveDir + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName2 + $pkgDir | Should -Not -BeNullOrEmpty + $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName + $pkgDirVersion.Name | Should -Be "1.3.0" + } + + It "Save resource should not prompt 'trust repository' if repository is not trusted but -TrustRepository is used" { + try { + Set-PSResourceRepository PoshTestGallery -Trusted:$false + Save-PSResource -Name $testModuleName2 -Repository $TestGalleryName -TrustRepository -Path $SaveDir + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName2 + $pkgDir | Should -Not -BeNullOrEmpty + $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName + $pkgDirVersion.Name | Should -Be "1.3.0" + } + finally { + Set-PSResourceRepository PoshTestGallery -Trusted + } + } + + It "Save resource from local repository given Repository parameter" { + $publishModuleName = "TestFindModule" + $repoName = "psgettestlocal" + Get-ModuleResourcePublishedToLocalRepoTestDrive $publishModuleName $repoName + Set-PSResourceRepository "psgettestlocal" -Trusted:$true + + Save-PSResource -Name $publishModuleName -Repository $repoName -Path $SaveDir + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $publishModuleName + $pkgDir | Should -Not -BeNullOrEmpty + (Get-ChildItem -Path $pkgDir.FullName).Count | Should -Be 1 + } + + It "Save specific module resource by name when no repository is specified" { + Set-PSResourceRepository "PoshTestGallery" -Trusted:$True + Set-PSResourceRepository "PSGallery" -Trusted:$True + Set-PSResourceRepository "psgettestlocal2" -Trusted:$True + + Save-PSResource -Name "TestModule" -Path $SaveDir + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName2 + $pkgDir | Should -Not -BeNullOrEmpty + (Get-ChildItem -Path $pkgDir.FullName).Count | Should -Be 1 + } + + It "Save PSResourceInfo object piped in" { + Find-PSResource -Name $testModuleName2 -Version "1.1.0.0" -Repository $TestGalleryName | Save-PSResource -Path $SaveDir + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName2 + $pkgDir | Should -Not -BeNullOrEmpty + (Get-ChildItem -Path $pkgDir.FullName).Count | Should -Be 1 + } + + It "Save PSResourceInfo object piped in for prerelease version object" { + Find-PSResource -Name $testModuleName -Version "4.5.2-alpha001" -Repository $TestGalleryName | Save-PSResource -Path $SaveDir + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName + $pkgDir | Should -Not -BeNullOrEmpty + (Get-ChildItem -Path $pkgDir.FullName).Count | Should -Be 1 + } + + It "Save module as a nupkg" { + Save-PSResource -Name $testModuleName2 -Version "1.3.0" -Repository $TestGalleryName -Path $SaveDir -AsNupkg + write-host $SaveDir + write-host + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "testmodule.1.3.0.nupkg" + $pkgDir | Should -Not -BeNullOrEmpty + } + + It "Save script as a nupkg" { + Save-PSResource -Name $testScriptName -Version "1.3.1" -Repository $TestGalleryName -Path $SaveDir -AsNupkg + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "testtestscript.1.3.1.nupkg" + $pkgDir | Should -Not -BeNullOrEmpty + } + + It "Save module and include XML metadata file" { + Save-PSResource -Name $testModuleName2 -Version "1.3.0" -Repository $TestGalleryName -Path $SaveDir -IncludeXML + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName2 + $pkgDir | Should -Not -BeNullOrEmpty + $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName + $pkgDirVersion.Name | Should -Be "1.3.0" + $xmlFile = Get-ChildItem -Path $pkgDirVersion.FullName | Where-Object Name -eq "PSGetModuleInfo.xml" + $xmlFile | Should -Not -BeNullOrEmpty + } + + It "Save module using -PassThru" { + $res = Save-PSResource -Name $testModuleName2 -Version "1.3.0" -Repository $TestGalleryName -Path $SaveDir -PassThru + $res.Name | Should -Be $testModuleName2 + $res.Version | Should -Be "1.3.0.0" + } +<# + # Tests should not write to module directory + It "Save specific module resource by name if no -Path param is specifed" { + Save-PSResource -Name $testModuleName2 -Repository $TestGalleryName + $pkgDir = Get-ChildItem -Path . | Where-Object Name -eq $testModuleName2 + $pkgDir | Should -Not -BeNullOrEmpty + (Get-ChildItem $pkgDir.FullName).Count | Should -Be 1 + + # Delete all files and subdirectories in the current , but keep the directory $SaveDir + if (Test-Path -Path $pkgDir.FullName) { + Remove-Item -Path $pkgDir.FullName -Recurse -Force -ErrorAction SilentlyContinue + } + } +#> + +<# + # This needs to be manually tested due to prompt + It "Install resource should prompt 'trust repository' if repository is not trusted" { + Set-PSResourceRepository PoshTestGallery -Trusted:$false + + Install-PSResource -Name $testModuleName2 -Repository $TestGalleryName -confirm:$false + + $pkg = Get-Module $testModuleName2 -ListAvailable + $pkg.Name | Should -Be $testModuleName2 + + Set-PSResourceRepository PoshTestGallery -Trusted + } +#> } \ No newline at end of file diff --git a/test/UninstallPSResource.Tests.ps1 b/test/UninstallPSResource.Tests.ps1 index eba8cfbc5..b4542254a 100644 --- a/test/UninstallPSResource.Tests.ps1 +++ b/test/UninstallPSResource.Tests.ps1 @@ -1,222 +1,225 @@ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. - -Import-Module "$psscriptroot\PSGetTestUtils.psm1" -Force - -Describe 'Test Uninstall-PSResource for Modules' { - - BeforeAll{ - $TestGalleryName = Get-PoshTestGalleryName - $PSGalleryName = Get-PSGalleryName - $testModuleName = "test_module" - $testScriptName = "test_script" - Get-NewPSResourceRepositoryFile - Uninstall-PSResource -name ContosoServer -Version "*" - } - BeforeEach{ - $null = Install-PSResource ContosoServer -Repository $TestGalleryName -TrustRepository -WarningAction SilentlyContinue - } - AfterAll { - Get-RevertPSResourceRepositoryFile - } - - It "Uninstall a specific module by name" { - Uninstall-PSResource -name ContosoServer - Get-Module ContosoServer -ListAvailable | Should -Be $null - } - - $testCases = @{Name="Test?Module"; ErrorId="ErrorFilteringNamesForUnsupportedWildcards"}, - @{Name="Test[Module"; ErrorId="ErrorFilteringNamesForUnsupportedWildcards"} - - It "not uninstall module given Name with invalid wildcard characters" -TestCases $testCases { - param($Name, $ErrorId) - Uninstall-PSResource -Name $Name -ErrorVariable err -ErrorAction SilentlyContinue - $err.Count | Should -Not -Be 0 - $err[0].FullyQualifiedErrorId | Should -BeExactly "$ErrorId,Microsoft.PowerShell.PowerShellGet.Cmdlets.UninstallPSResource" - } - - It "Uninstall a list of modules by name" { - $null = Install-PSResource BaseTestPackage -Repository $TestGalleryName -TrustRepository -WarningAction SilentlyContinue - - Uninstall-PSResource -Name BaseTestPackage, ContosoServer - Get-Module ContosoServer, BaseTestPackage -ListAvailable | Should -be $null - } - - It "Uninstall a specific script by name" { - $null = Install-PSResource "test_script" -Repository $TestGalleryName -TrustRepository - $res = Get-PSResource -Name "test_script" - $res.Name | Should -Be "test_script" - - Uninstall-PSResource -name "test_script" - $res = Get-PSResource -Name "test_script" - $res | Should -BeNullOrEmpty - } - - It "Uninstall a list of scripts by name" { - $null = Install-PSResource "test_script", "TestTestScript" -Repository $TestGalleryName -TrustRepository - $res = Get-PSResource -Name "test_script" - $res2 = Get-PSResource -Name "TestTestScript" - $res.Name | Should -Be "test_script" - $res2.Name | Should -Be "TestTestScript" - - Uninstall-PSResource -Name "test_script", "TestTestScript" - $res = Get-PSResource -Name "test_script" - $res2 = Get-PSResource -Name "TestTestScript" - $res | Should -BeNullOrEmpty - $res2 | Should -BeNullOrEmpty - } - - It "Uninstall a module when given name and specifying all versions" { - $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.0.0" -TrustRepository -WarningAction SilentlyContinue - $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.5.0" -TrustRepository -WarningAction SilentlyContinue - $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "2.0.0" -TrustRepository -WarningAction SilentlyContinue - - Uninstall-PSResource -Name ContosoServer -version "*" - $pkgs = Get-Module ContosoServer -ListAvailable - $pkgs.Version | Should -Not -Contain "1.0.0" - $pkgs.Version | Should -Not -Contain "1.5.0" - $pkgs.Version | Should -Not -Contain "2.0.0" - $pkgs.Version | Should -Not -Contain "2.5.0" - } - - It "Uninstall a module when given name and using the default version (ie all versions, not explicitly specified)" { - $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.0.0" -TrustRepository -WarningAction SilentlyContinue - $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.5.0" -TrustRepository -WarningAction SilentlyContinue - $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "2.0.0" -TrustRepository -WarningAction SilentlyContinue - - Uninstall-PSResource -Name ContosoServer - $pkgs = Get-Module ContosoServer -ListAvailable - $pkgs.Version | Should -Not -Contain "1.0.0" - $pkgs.Version | Should -Not -Contain "1.5.0" - $pkgs.Version | Should -Not -Contain "2.0.0" - $pkgs.Version | Should -Not -Contain "2.5.0" - } - - It "Uninstall module when given Name and specifying exact version" { - $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.0.0" -TrustRepository -WarningAction SilentlyContinue - $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.5.0" -TrustRepository -WarningAction SilentlyContinue - $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "2.0.0" -TrustRepository -WarningAction SilentlyContinue - - Uninstall-PSResource -Name "ContosoServer" -Version "1.0.0" - $pkgs = Get-Module ContosoServer -ListAvailable - $pkgs.Version | Should -Not -Contain "1.0.0" - } - - $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"; Reason="validate version, exact range inclusive"}, - @{Version="(1.0.0.0, 2.5.0.0)"; ExpectedVersion="2.0.0.0"; Reason="validate version, exact range exclusive"}, - @{Version="(1.0.0.0,)"; ExpectedVersion="2.5.0.0"; Reason="validate version, minimum version exclusive"}, - @{Version="[1.0.0.0,)"; ExpectedVersion="2.5.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"; Reason="validate version, maximum version inclusive"}, - @{Version="[1.0.0.0, 2.5.0.0)"; ExpectedVersion="2.0.0.0"; Reason="validate version, mixed inclusive minimum and exclusive maximum version"} - - It "Uninstall module when given Name to " -TestCases $testCases { - param($Version, $ExpectedVersion) - $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.0.0" -TrustRepository -WarningAction SilentlyContinue - $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.5.0" -TrustRepository -WarningAction SilentlyContinue - $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "2.0.0" -TrustRepository -WarningAction SilentlyContinue - - Uninstall-PSResource -Name ContosoServer -Version $Version - $pkgs = Get-Module ContosoServer -ListAvailable - $pkgs.Version | Should -Not -Contain $Version - } - - $testCases2 = @{Version='[1.*.0]'; Description="version with wilcard in middle"}, - @{Version='[*.5.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 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"} - - It "Do not uninstall module with incorrectly formatted version such as " -TestCases $testCases2 { - param($Version, $Description) - - {Uninstall-PSResource -Name "ContosoServer" -Version $Version} | Should -Throw "Argument for -Version parameter is not in the proper format." - } - - $testCases3 = @{Version='(2.5.0.0)'; Description="exclusive version (8.1.0.0)"}, - @{Version='[2-5-0-0]'; Description="version formatted with invalid delimiter"} - - It "Do not uninstall module with incorrectly formatted version such as " -TestCases $testCases3 { - param($Version, $Description) - - Uninstall-PSResource -Name "ContosoServer" -Version $Version - - $pkg = Get-Module ContosoServer -ListAvailable - $pkg.Version | Should -Be "2.5" - } - - It "Uninstall prerelease version module when prerelease version specified" { - Install-PSResource -Name $testModuleName -Version "5.2.5-alpha001" -Repository $TestGalleryName - Uninstall-PSResource -Name $testModuleName -Version "5.2.5-alpha001" - $res = Get-PSResource $testModuleName -Version "5.2.5-alpha001" - $res | Should -BeNullOrEmpty - } - - It "Not uninstall non-prerelease version module when similar prerelease version is specified" { - Install-PSResource -Name $testModuleName -Version "5.0.0.0" -Repository $TestGalleryName - Uninstall-PSResource -Name $testModuleName -Version "5.0.0-preview" - $res = Get-PSResource -Name $testModuleName -Version "5.0.0.0" - $res.Name | Should -Be $testModuleName - $res.Version | Should -Be "5.0.0.0" - } - - It "Uninstall prerelease version script when prerelease version specified" { - Install-PSResource -Name $testScriptName -Version "3.0.0-alpha001" -Repository $TestGalleryName - Uninstall-PSResource -Name $testScriptName -Version "3.0.0-alpha001" - $res = Get-PSResource -Name $testScriptName - $res | Should -BeNullOrEmpty - } - - It "Not uninstall non-prerelease version module when prerelease version specified" { - Install-PSResource -Name $testScriptName -Version "2.5.0.0" -Repository $TestGalleryName - Uninstall-PSResource -Name $testScriptName -Version "2.5.0-alpha001" - $res = Get-PSResource -Name $testScriptName -Version "2.5.0.0" - $res.Name | Should -Be $testScriptName - $res.Version | Should -Be "2.5.0.0" - } - - It "Uninstall module using -WhatIf, should not uninstall the module" { - Uninstall-PSResource -Name "ContosoServer" -WhatIf - $pkg = Get-Module ContosoServer -ListAvailable - $pkg.Version | Should -Be "2.5" - } - - It "Do not Uninstall module that is a dependency for another module" { - $null = Install-PSResource $testModuleName -Repository $TestGalleryName -TrustRepository -WarningAction SilentlyContinue - - Uninstall-PSResource -Name "RequiredModule1" -ErrorVariable ev -ErrorAction SilentlyContinue - - $pkg = Get-Module "RequiredModule1" -ListAvailable - $pkg | Should -Not -Be $null - - $ev.FullyQualifiedErrorId | Should -BeExactly 'UninstallPSResourcePackageIsaDependency,Microsoft.PowerShell.PowerShellGet.Cmdlets.UninstallPSResource' - } - - It "Uninstall module that is a dependency for another module using -SkipDependencyCheck" { - $null = Install-PSResource $testModuleName -Repository $TestGalleryName -TrustRepository -WarningAction SilentlyContinue - - Uninstall-PSResource -Name "RequiredModule1" -SkipDependencyCheck - - $pkg = Get-Module "RequiredModule1" -ListAvailable - $pkg | Should -BeNullOrEmpty - } - - It "Uninstall PSResourceInfo object piped in" { - Install-PSResource -Name "ContosoServer" -Version "1.5.0.0" -Repository $TestGalleryName - Get-PSResource -Name "ContosoServer" -Version "1.5.0.0" | Uninstall-PSResource - $res = Get-PSResource -Name "ContosoServer" -Version "1.5.0.0" - $res | Should -BeNullOrEmpty - } - - It "Uninstall PSResourceInfo object piped in for prerelease version object" { - Install-PSResource -Name $testModuleName -Version "4.5.2-alpha001" -Repository $TestGalleryName - Get-PSResource -Name $testModuleName -Version "4.5.2-alpha001" | Uninstall-PSResource - $res = Get-PSResource -Name $testModuleName -Version "4.5.2-alpha001" - $res | Should -BeNullOrEmpty - } -} +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +$ProgressPreference = "SilentlyContinue" +Import-Module "$psscriptroot\PSGetTestUtils.psm1" -Force + +Describe 'Test Uninstall-PSResource for Modules' { + + BeforeAll{ + $TestGalleryName = Get-PoshTestGalleryName + $PSGalleryName = Get-PSGalleryName + $testModuleName = "test_module" + $testScriptName = "test_script" + Get-NewPSResourceRepositoryFile + Uninstall-PSResource -name ContosoServer -Version "*" + } + + BeforeEach { + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -TrustRepository -WarningAction SilentlyContinue + } + + AfterAll { + Get-RevertPSResourceRepositoryFile + } + + It "Uninstall a specific module by name" { + Uninstall-PSResource -name ContosoServer + Get-Module ContosoServer -ListAvailable | Should -Be $null + } + + $testCases = @{Name="Test?Module"; ErrorId="ErrorFilteringNamesForUnsupportedWildcards"}, + @{Name="Test[Module"; ErrorId="ErrorFilteringNamesForUnsupportedWildcards"} + + It "not uninstall module given Name with invalid wildcard characters" -TestCases $testCases { + param($Name, $ErrorId) + Uninstall-PSResource -Name $Name -ErrorVariable err -ErrorAction SilentlyContinue + $err.Count | Should -Not -Be 0 + $err[0].FullyQualifiedErrorId | Should -BeExactly "$ErrorId,Microsoft.PowerShell.PowerShellGet.Cmdlets.UninstallPSResource" + } + + It "Uninstall a list of modules by name" { + $null = Install-PSResource BaseTestPackage -Repository $TestGalleryName -TrustRepository -WarningAction SilentlyContinue -SkipDependencyCheck + + Uninstall-PSResource -Name BaseTestPackage, ContosoServer + Get-Module ContosoServer, BaseTestPackage -ListAvailable | Should -be $null + } + + It "Uninstall a specific script by name" { + $null = Install-PSResource "test_script" -Repository $TestGalleryName -TrustRepository + $res = Get-PSResource -Name "test_script" + $res.Name | Should -Be "test_script" + + Uninstall-PSResource -name "test_script" + $res = Get-PSResource -Name "test_script" + $res | Should -BeNullOrEmpty + } + + It "Uninstall a list of scripts by name" { + $null = Install-PSResource "test_script", "TestTestScript" -Repository $TestGalleryName -TrustRepository + $res = Get-PSResource -Name "test_script" + $res2 = Get-PSResource -Name "TestTestScript" + $res.Name | Should -Be "test_script" + $res2.Name | Should -Be "TestTestScript" + + Uninstall-PSResource -Name "test_script", "TestTestScript" + $res = Get-PSResource -Name "test_script" + $res2 = Get-PSResource -Name "TestTestScript" + $res | Should -BeNullOrEmpty + $res2 | Should -BeNullOrEmpty + } + + It "Uninstall a module when given name and specifying all versions" { + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.0.0" -TrustRepository -WarningAction SilentlyContinue + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.5.0" -TrustRepository -WarningAction SilentlyContinue + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "2.0.0" -TrustRepository -WarningAction SilentlyContinue + + Uninstall-PSResource -Name ContosoServer -version "*" + $pkgs = Get-Module ContosoServer -ListAvailable + $pkgs.Version | Should -Not -Contain "1.0.0" + $pkgs.Version | Should -Not -Contain "1.5.0" + $pkgs.Version | Should -Not -Contain "2.0.0" + $pkgs.Version | Should -Not -Contain "2.5.0" + } + + It "Uninstall a module when given name and using the default version (ie all versions, not explicitly specified)" { + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.0.0" -TrustRepository -WarningAction SilentlyContinue + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.5.0" -TrustRepository -WarningAction SilentlyContinue + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "2.0.0" -TrustRepository -WarningAction SilentlyContinue + + Uninstall-PSResource -Name ContosoServer + $pkgs = Get-Module ContosoServer -ListAvailable + $pkgs.Version | Should -Not -Contain "1.0.0" + $pkgs.Version | Should -Not -Contain "1.5.0" + $pkgs.Version | Should -Not -Contain "2.0.0" + $pkgs.Version | Should -Not -Contain "2.5.0" + } + + It "Uninstall module when given Name and specifying exact version" { + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.0.0" -TrustRepository -WarningAction SilentlyContinue + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.5.0" -TrustRepository -WarningAction SilentlyContinue + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "2.0.0" -TrustRepository -WarningAction SilentlyContinue + + Uninstall-PSResource -Name "ContosoServer" -Version "1.0.0" + $pkgs = Get-Module ContosoServer -ListAvailable + $pkgs.Version | Should -Not -Contain "1.0.0" + } + + $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"; Reason="validate version, exact range inclusive"}, + @{Version="(1.0.0.0, 2.5.0.0)"; ExpectedVersion="2.0.0.0"; Reason="validate version, exact range exclusive"}, + @{Version="(1.0.0.0,)"; ExpectedVersion="2.5.0.0"; Reason="validate version, minimum version exclusive"}, + @{Version="[1.0.0.0,)"; ExpectedVersion="2.5.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"; Reason="validate version, maximum version inclusive"}, + @{Version="[1.0.0.0, 2.5.0.0)"; ExpectedVersion="2.0.0.0"; Reason="validate version, mixed inclusive minimum and exclusive maximum version"} + + It "Uninstall module when given Name to " -TestCases $testCases { + param($Version, $ExpectedVersion) + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.0.0" -TrustRepository -WarningAction SilentlyContinue + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.5.0" -TrustRepository -WarningAction SilentlyContinue + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "2.0.0" -TrustRepository -WarningAction SilentlyContinue + + Uninstall-PSResource -Name ContosoServer -Version $Version + $pkgs = Get-Module ContosoServer -ListAvailable + $pkgs.Version | Should -Not -Contain $Version + } + + $testCases2 = @{Version='[1.*.0]'; Description="version with wilcard in middle"}, + @{Version='[*.5.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 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"} + + It "Do not uninstall module with incorrectly formatted version such as " -TestCases $testCases2 { + param($Version, $Description) + + {Uninstall-PSResource -Name "ContosoServer" -Version $Version} | Should -Throw "Argument for -Version parameter is not in the proper format." + } + + $testCases3 = @{Version='(2.5.0.0)'; Description="exclusive version (8.1.0.0)"}, + @{Version='[2-5-0-0]'; Description="version formatted with invalid delimiter"} + + It "Do not uninstall module with incorrectly formatted version such as " -TestCases $testCases3 { + param($Version, $Description) + + Uninstall-PSResource -Name "ContosoServer" -Version $Version + + $pkg = Get-Module ContosoServer -ListAvailable + $pkg.Version | Should -Be "2.5" + } + + It "Uninstall prerelease version module when prerelease version specified" { + Install-PSResource -Name $testModuleName -Version "5.2.5-alpha001" -Repository $TestGalleryName + Uninstall-PSResource -Name $testModuleName -Version "5.2.5-alpha001" + $res = Get-PSResource $testModuleName -Version "5.2.5-alpha001" + $res | Should -BeNullOrEmpty + } + + It "Not uninstall non-prerelease version module when similar prerelease version is specified" { + Install-PSResource -Name $testModuleName -Version "5.0.0.0" -Repository $TestGalleryName + Uninstall-PSResource -Name $testModuleName -Version "5.0.0-preview" + $res = Get-PSResource -Name $testModuleName -Version "5.0.0.0" + $res.Name | Should -Be $testModuleName + $res.Version | Should -Be "5.0.0.0" + } + + It "Uninstall prerelease version script when prerelease version specified" { + Install-PSResource -Name $testScriptName -Version "3.0.0-alpha001" -Repository $TestGalleryName + Uninstall-PSResource -Name $testScriptName -Version "3.0.0-alpha001" + $res = Get-PSResource -Name $testScriptName + $res | Should -BeNullOrEmpty + } + + It "Not uninstall non-prerelease version module when prerelease version specified" { + Install-PSResource -Name $testScriptName -Version "2.5.0.0" -Repository $TestGalleryName + Uninstall-PSResource -Name $testScriptName -Version "2.5.0-alpha001" + $res = Get-PSResource -Name $testScriptName -Version "2.5.0.0" + $res.Name | Should -Be $testScriptName + $res.Version | Should -Be "2.5.0.0" + } + + It "Uninstall module using -WhatIf, should not uninstall the module" { + Uninstall-PSResource -Name "ContosoServer" -WhatIf + $pkg = Get-Module ContosoServer -ListAvailable + $pkg.Version | Should -Be "2.5" + } + + It "Do not Uninstall module that is a dependency for another module" { + $null = Install-PSResource $testModuleName -Repository $TestGalleryName -TrustRepository -WarningAction SilentlyContinue + + Uninstall-PSResource -Name "RequiredModule1" -ErrorVariable ev -ErrorAction SilentlyContinue + + $pkg = Get-Module "RequiredModule1" -ListAvailable + $pkg | Should -Not -Be $null + + $ev.FullyQualifiedErrorId | Should -BeExactly 'UninstallPSResourcePackageIsaDependency,Microsoft.PowerShell.PowerShellGet.Cmdlets.UninstallPSResource' + } + + It "Uninstall module that is a dependency for another module using -SkipDependencyCheck" { + $null = Install-PSResource $testModuleName -Repository $TestGalleryName -TrustRepository -WarningAction SilentlyContinue + + Uninstall-PSResource -Name "RequiredModule1" -SkipDependencyCheck + + $pkg = Get-Module "RequiredModule1" -ListAvailable + $pkg | Should -BeNullOrEmpty + } + + It "Uninstall PSResourceInfo object piped in" { + Install-PSResource -Name "ContosoServer" -Version "1.5.0.0" -Repository $TestGalleryName + Get-PSResource -Name "ContosoServer" -Version "1.5.0.0" | Uninstall-PSResource + $res = Get-PSResource -Name "ContosoServer" -Version "1.5.0.0" + $res | Should -BeNullOrEmpty + } + + It "Uninstall PSResourceInfo object piped in for prerelease version object" { + Install-PSResource -Name $testModuleName -Version "4.5.2-alpha001" -Repository $TestGalleryName + Get-PSResource -Name $testModuleName -Version "4.5.2-alpha001" | Uninstall-PSResource + $res = Get-PSResource -Name $testModuleName -Version "4.5.2-alpha001" + $res | Should -BeNullOrEmpty + } +} diff --git a/test/UpdatePSResource.Tests.ps1 b/test/UpdatePSResource.Tests.ps1 index c2449a276..cb11a8b59 100644 --- a/test/UpdatePSResource.Tests.ps1 +++ b/test/UpdatePSResource.Tests.ps1 @@ -1,12 +1,13 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. +$ProgressPreference = "SilentlyContinue" Import-Module "$psscriptroot\PSGetTestUtils.psm1" -Force Describe 'Test Update-PSResource' { - BeforeAll{ + BeforeAll { $TestGalleryName = Get-PoshTestGalleryName $NuGetGalleryName = Get-NuGetGalleryName $testModuleName = "TestModule"