diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index d804da16a..8cc212eb7 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -60,7 +60,7 @@ internal class InstallHelper public InstallHelper(PSCmdlet cmdletPassedIn, NetworkCredential networkCredential) { - CancellationTokenSource source = new CancellationTokenSource(); + CancellationTokenSource source = new(); _cancellationToken = source.Token; _cmdletPassedIn = cmdletPassedIn; _networkCredential = networkCredential; @@ -183,7 +183,7 @@ private List ProcessRepositories( ScopeType scope) { _cmdletPassedIn.WriteDebug("In InstallHelper::ProcessRepositories()"); - List allPkgsInstalled = new List(); + List allPkgsInstalled = new(); if (repository != null && repository.Length != 0) { // Write error and disregard repository entries containing wildcards. @@ -262,7 +262,7 @@ private List ProcessRepositories( var noToAll = false; var findHelper = new FindHelper(_cancellationToken, _cmdletPassedIn, _networkCredential); - List repositoryNamesToSearch = new List(); + List repositoryNamesToSearch = new(); bool sourceTrusted = false; // Loop through all the repositories provided (in priority order) until there no more packages to install. @@ -330,7 +330,7 @@ private List ProcessRepositories( allPkgsInstalled.AddRange(installedPkgs); } - if (_pkgNamesToInstall.Count > 0) + if (!_cmdletPassedIn.MyInvocation.BoundParameters.ContainsKey("WhatIf") && _pkgNamesToInstall.Count > 0) { string repositoryWording = repositoryNamesToSearch.Count > 1 ? "registered repositories" : "repository"; _cmdletPassedIn.WriteError(new ErrorRecord( @@ -547,7 +547,7 @@ private List InstallPackages( FindHelper findHelper) { _cmdletPassedIn.WriteDebug("In InstallHelper::InstallPackages()"); - List pkgsSuccessfullyInstalled = new List(); + List pkgsSuccessfullyInstalled = new(); // Install parent package to the temp directory, // Get the dependencies from the installed package, @@ -658,7 +658,7 @@ private List InstallPackages( } // If -WhatIf is passed in, early out. - if (!_cmdletPassedIn.ShouldProcess("Exit ShouldProcess")) + if (_cmdletPassedIn.MyInvocation.BoundParameters.ContainsKey("WhatIf")) { return pkgsSuccessfullyInstalled; } @@ -1328,7 +1328,7 @@ private bool CallAcceptLicense(PSResourceInfo p, string moduleManifest, string t if (File.Exists(moduleManifest)) { - using (StreamReader sr = new StreamReader(moduleManifest)) + using (StreamReader sr = new(moduleManifest)) { var text = sr.ReadToEnd(); @@ -1336,9 +1336,9 @@ private bool CallAcceptLicense(PSResourceInfo p, string moduleManifest, string t var patternToSkip1 = "#\\s*RequireLicenseAcceptance\\s*=\\s*\\$true"; var patternToSkip2 = "\\*\\s*RequireLicenseAcceptance\\s*=\\s*\\$true"; - Regex rgx = new Regex(pattern); - Regex rgxComment1 = new Regex(patternToSkip1); - Regex rgxComment2 = new Regex(patternToSkip2); + Regex rgx = new(pattern); + Regex rgxComment1 = new(patternToSkip1); + Regex rgxComment2 = new(patternToSkip2); if (rgx.IsMatch(text) && !rgxComment1.IsMatch(text) && !rgxComment2.IsMatch(text)) { requireLicenseAcceptance = true; @@ -1409,14 +1409,14 @@ private bool DetectClobber(string pkgName, Hashtable parsedMetadataHashtable, ou // Get installed modules, then get all possible paths // selectPrereleaseOnly is false because even if Prerelease is true we want to include both stable and prerelease, would never select prerelease only. - GetHelper getHelper = new GetHelper(_cmdletPassedIn); + GetHelper getHelper = new(_cmdletPassedIn); IEnumerable pkgsAlreadyInstalled = getHelper.GetPackagesFromPath( name: new string[] { "*" }, versionRange: VersionRange.All, pathsToSearch: _pathsToSearch, selectPrereleaseOnly: false); - List listOfCmdlets = new List(); + List listOfCmdlets = new(); if (parsedMetadataHashtable.ContainsKey("CmdletsToExport")) { if (parsedMetadataHashtable["CmdletsToExport"] is object[] cmdletsToExport) @@ -1430,8 +1430,8 @@ private bool DetectClobber(string pkgName, Hashtable parsedMetadataHashtable, ou foreach (var pkg in pkgsAlreadyInstalled) { - List duplicateCmdlets = new List(); - List duplicateCmds = new List(); + List duplicateCmdlets = new(); + List duplicateCmds = new(); // See if any of the cmdlets or commands in the pkg we're trying to install exist within a package that's already installed if (pkg.Includes.Cmdlet != null && pkg.Includes.Cmdlet.Length != 0) { diff --git a/test/InstallPSResourceTests/InstallPSResourceLocal.Tests.ps1 b/test/InstallPSResourceTests/InstallPSResourceLocal.Tests.ps1 index 90b264983..493495282 100644 --- a/test/InstallPSResourceTests/InstallPSResourceLocal.Tests.ps1 +++ b/test/InstallPSResourceTests/InstallPSResourceLocal.Tests.ps1 @@ -1,16 +1,18 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. +[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] +Param() + $ProgressPreference = "SilentlyContinue" $modPath = "$psscriptroot/../PSGetTestUtils.psm1" Import-Module $modPath -Force -Verbose $psmodulePaths = $env:PSModulePath -split ';' -Write-Verbose -Verbose "Current module search paths: $psmodulePaths" +Write-Verbose -Verbose -Message "Current module search paths: $psmodulePaths" Describe 'Test Install-PSResource for local repositories' -tags 'CI' { - BeforeAll { $localRepo = "psgettestlocal" $localUNCRepo = "psgettestlocal3" @@ -54,7 +56,7 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' { $res.Version | Should -Be "5.0.0" } - It "Install resource given Name parameter from UNC repository" { + It "Install resource given Name parameter from UNC repository" { Install-PSResource -Name $testModuleName -Repository $localUNCRepo -TrustRepository $res = Get-InstalledPSResource -Name $testModuleName $res.Name | Should -Be $testModuleName @@ -70,7 +72,7 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' { It "Install multiple resources by name" { $pkgNames = @($testModuleName, $testModuleName2) - Install-PSResource -Name $pkgNames -Repository $localRepo -TrustRepository + Install-PSResource -Name $pkgNames -Repository $localRepo -TrustRepository $pkg = Get-InstalledPSResource $pkgNames $pkg.Name | Should -Be $pkgNames } @@ -83,7 +85,7 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' { } It "Should install resource given name and exact version with bracket syntax" { - Install-PSResource -Name $testModuleName -Version "[1.0.0.0]" -Repository $localRepo -TrustRepository + Install-PSResource -Name $testModuleName -Version "[1.0.0.0]" -Repository $localRepo -TrustRepository $res = Get-InstalledPSResource $testModuleName $res.Name | Should -Be $testModuleName $res.Version | Should -Be "1.0.0" @@ -97,7 +99,7 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' { } It "Should install resource given name and exact range exclusive (1.0.0.0, 5.0.0.0)" { - Install-PSResource -Name $testModuleName -Version "(1.0.0.0, 5.0.0.0)" -Repository $localRepo -TrustRepository + Install-PSResource -Name $testModuleName -Version "(1.0.0.0, 5.0.0.0)" -Repository $localRepo -TrustRepository $res = Get-InstalledPSResource $testModuleName $res.Name | Should -Be $testModuleName $res.Version | Should -Be "3.0.0" @@ -110,7 +112,7 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' { } catch {} - $Error[0].FullyQualifiedErrorId | Should -be "IncorrectVersionFormat,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource" + $Error[0].FullyQualifiedErrorId | Should -Be "IncorrectVersionFormat,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource" $res = Get-InstalledPSResource $testModuleName -ErrorAction SilentlyContinue $res | Should -BeNullOrEmpty @@ -131,7 +133,7 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' { } It "Install resource with latest (including prerelease) version given Prerelease parameter" { - Install-PSResource -Name $testModuleName -Prerelease -Repository $localRepo -TrustRepository + Install-PSResource -Name $testModuleName -Prerelease -Repository $localRepo -TrustRepository $pkg = Get-InstalledPSResource $testModuleName $pkg.Name | Should -Be $testModuleName $pkg.Version | Should -Be "5.2.5" @@ -171,14 +173,14 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' { } It "Install resource via InputObject by piping from Find-PSresource" { - Find-PSResource -Name $testModuleName -Repository $localRepo | Install-PSResource -TrustRepository + Find-PSResource -Name $testModuleName -Repository $localRepo | Install-PSResource -TrustRepository $pkg = Get-InstalledPSResource $testModuleName $pkg.Name | Should -Be $testModuleName $pkg.Version | Should -Be "5.0.0" } It "Install resource via InputObject by piping from Find-PSResource" { - $modules = Find-PSResource -Name "*" -Repository $localRepo + $modules = Find-PSResource -Name "*" -Repository $localRepo $modules.Count | Should -BeGreaterThan 1 Install-PSResource -TrustRepository -InputObject $modules @@ -190,7 +192,7 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' { It "Install resource under location specified in PSModulePath" { Install-PSResource -Name $testModuleName -Repository $localRepo -TrustRepository $pkg = Get-InstalledPSResource $testModuleName - $pkg.Name | Should -Be $testModuleName + $pkg.Name | Should -Be $testModuleName ($env:PSModulePath).Contains($pkg.InstalledLocation) } @@ -212,7 +214,7 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' { # Windows only It "Install resource under no specified scope - Windows only" -Skip:(!(Get-IsWindows)) { - Install-PSResource -Name $testModuleName -Repository $localRepo -TrustRepository + Install-PSResource -Name $testModuleName -Repository $localRepo -TrustRepository $pkg = Get-InstalledPSResource $testModuleName $pkg.Name | Should -Be $testModuleName $pkg.InstalledLocation.ToString().Contains("Documents") | Should -Be $true @@ -240,7 +242,7 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' { Install-PSResource -Name $testModuleName -Repository $localRepo -TrustRepository $pkg = Get-InstalledPSResource $testModuleName $pkg.Name | Should -Be $testModuleName - Install-PSResource -Name $testModuleName -Repository $localRepo -TrustRepository -WarningVariable WarningVar -warningaction SilentlyContinue + Install-PSResource -Name $testModuleName -Repository $localRepo -TrustRepository -WarningVariable WarningVar -WarningAction SilentlyContinue $WarningVar | Should -Not -BeNullOrEmpty } @@ -257,6 +259,8 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' { It "Install module using -WhatIf, should not install the module" { Install-PSResource -Name $testModuleName -Version "1.0.0.0" -Repository $localRepo -TrustRepository -WhatIf + $? | Should -BeTrue + $res = Get-InstalledPSResource -Name $testModuleName -ErrorAction SilentlyContinue $res | Should -BeNullOrEmpty } @@ -270,11 +274,11 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' { It "Get definition for alias 'isres'" { (Get-Alias isres).Definition | Should -BeExactly 'Install-PSResource' } - + It "Not install resource that lists dependency packages which cannot be found" { $localRepoUri = Join-Path -Path $TestDrive -ChildPath "testdir" Save-PSResource -Name "test_script" -Repository "PSGallery" -TrustRepository -Path $localRepoUri -AsNupkg -SkipDependencyCheck - Write-Host $localRepoUri + Write-Information -InformationAction Continue -MessageData $localRepoUri $res = Install-PSResource -Name "test_script" -Repository $localRepo -TrustRepository -PassThru -ErrorVariable err -ErrorAction SilentlyContinue $res | Should -BeNullOrEmpty $err.Count | Should -Not -Be 0 diff --git a/test/InstallPSResourceTests/InstallPSResourceV2Server.Tests.ps1 b/test/InstallPSResourceTests/InstallPSResourceV2Server.Tests.ps1 index b64244dcd..0b03a984b 100644 --- a/test/InstallPSResourceTests/InstallPSResourceV2Server.Tests.ps1 +++ b/test/InstallPSResourceTests/InstallPSResourceV2Server.Tests.ps1 @@ -1,12 +1,15 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. +[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] +Param() + $ProgressPreference = "SilentlyContinue" $modPath = "$psscriptroot/../PSGetTestUtils.psm1" Import-Module $modPath -Force -Verbose $psmodulePaths = $env:PSModulePath -split ';' -Write-Verbose -Verbose "Current module search paths: $psmodulePaths" +Write-Verbose -Verbose -Message "Current module search paths: $psmodulePaths" Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' { @@ -28,17 +31,19 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' { AfterEach { Uninstall-PSResource "test_module", "test_module2", "test_script", "TestModule99", "testModuleWithlicense", ` - "TestFindModule","ClobberTestModule1", "ClobberTestModule2", "PackageManagement", "TestTestScript", ` - "TestModuleWithDependency", "TestModuleWithPrereleaseDep", "PrereleaseModule" -SkipDependencyCheck -ErrorAction SilentlyContinue + "TestFindModule","ClobberTestModule1", "ClobberTestModule2", "PackageManagement", "TestTestScript", ` + "TestModuleWithDependency", "TestModuleWithPrereleaseDep", "PrereleaseModule" -SkipDependencyCheck -ErrorAction SilentlyContinue } AfterAll { Get-RevertPSResourceRepositoryFile } - $testCases = @{Name="*"; ErrorId="NameContainsWildcard"}, - @{Name="Test_Module*"; ErrorId="NameContainsWildcard"}, - @{Name="Test?Module","Test[Module"; ErrorId="ErrorFilteringNamesForUnsupportedWildcards"} + $testCases = [array]( + @{Name="*"; ErrorId="NameContainsWildcard"}, + @{Name="Test_Module*"; ErrorId="NameContainsWildcard"}, + @{Name="Test?Module","Test[Module"; ErrorId="ErrorFilteringNamesForUnsupportedWildcards"} + ) It "Should not install resource with wildcard in name" -TestCases $testCases { param($Name, $ErrorId) @@ -63,7 +68,7 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' { It "Install multiple resources by name" { $pkgNames = @($testModuleName, $testModuleName2) - Install-PSResource -Name $pkgNames -Repository $PSGalleryName -TrustRepository + Install-PSResource -Name $pkgNames -Repository $PSGalleryName -TrustRepository $pkg = Get-InstalledPSResource $pkgNames $pkg.Name | Should -Be $pkgNames } @@ -73,7 +78,7 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' { $pkg = Get-InstalledPSResource "NonExistantModule" $pkg.Name | Should -BeNullOrEmpty $err.Count | Should -BeGreaterThan 0 - $err[0].FullyQualifiedErrorId | Should -BeExactly "InstallPackageFailure,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource" + $err[0].FullyQualifiedErrorId | Should -BeExactly "InstallPackageFailure,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource" } # Do some version testing, but Find-PSResource should be doing thorough testing @@ -83,30 +88,30 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' { $pkg.Name | Should -Be $testModuleName $pkg.Version | Should -Be "1.0.0.0" } - + It "Should install resource given name and exact version with bracket syntax" { - Install-PSResource -Name $testModuleName -Version "3.*" -Repository $PSGalleryName -TrustRepository + Install-PSResource -Name $testModuleName -Version "3.*" -Repository $PSGalleryName -TrustRepository $pkg = Get-InstalledPSResource $testModuleName $pkg.Name | Should -Be $testModuleName $pkg.Version | Should -Be "3.0.0.0" } It "Should install resource given name and exact version with bracket syntax" { - Install-PSResource -Name $testModuleName -Version "[1.0.0]" -Repository $PSGalleryName -TrustRepository + Install-PSResource -Name $testModuleName -Version "[1.0.0]" -Repository $PSGalleryName -TrustRepository $pkg = Get-InstalledPSResource $testModuleName $pkg.Name | Should -Be $testModuleName $pkg.Version | Should -Be "1.0.0.0" } It "Should install resource given name and exact range inclusive [1.0.0, 5.0.0]" { - Install-PSResource -Name $testModuleName -Version "[1.0.0, 5.0.0]" -Repository $PSGalleryName -TrustRepository + Install-PSResource -Name $testModuleName -Version "[1.0.0, 5.0.0]" -Repository $PSGalleryName -TrustRepository $pkg = Get-InstalledPSResource $testModuleName $pkg.Name | Should -Be $testModuleName $pkg.Version | Should -Be "5.0.0.0" } It "Should install resource given name and exact range exclusive (1.0.0, 5.0.0)" { - Install-PSResource -Name $testModuleName -Version "(1.0.0, 5.0.0)" -Repository $PSGalleryName -TrustRepository + Install-PSResource -Name $testModuleName -Version "(1.0.0, 5.0.0)" -Repository $PSGalleryName -TrustRepository $pkg = Get-InstalledPSResource $testModuleName $pkg.Name | Should -Be $testModuleName $pkg.Version | Should -Be "3.0.0.0" @@ -118,9 +123,9 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' { try { Install-PSResource -Name $testModuleName -Version $Version -Repository $PSGalleryName -TrustRepository -ErrorAction SilentlyContinue } - catch - {} - $Error[0].FullyQualifiedErrorId | Should -be "IncorrectVersionFormat,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource" + catch { + } + $Error[0].FullyQualifiedErrorId | Should -Be "IncorrectVersionFormat,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource" $res = Get-InstalledPSResource $testModuleName $res | Should -BeNullOrEmpty @@ -134,7 +139,7 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' { } It "Install resource with latest (including prerelease) version given Prerelease parameter" { - Install-PSResource -Name $testModuleName -Prerelease -Repository $PSGalleryName -TrustRepository + Install-PSResource -Name $testModuleName -Prerelease -Repository $PSGalleryName -TrustRepository $pkg = Get-InstalledPSResource $testModuleName $pkg.Name | Should -Be $testModuleName $pkg.Version | Should -Be "5.2.5" @@ -143,7 +148,7 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' { It "Install a module with a dependency" { Uninstall-PSResource -Name "TestModuleWithDependency*" -Version "*" -SkipDependencyCheck -ErrorAction SilentlyContinue - Install-PSResource -Name "TestModuleWithDependencyC" -Version "5.0.0.0" -Repository $PSGalleryName -TrustRepository + Install-PSResource -Name "TestModuleWithDependencyC" -Version "5.0.0.0" -Repository $PSGalleryName -TrustRepository $pkg = Get-InstalledPSResource "TestModuleWithDependencyC" $pkg.Name | Should -Be "TestModuleWithDependencyC" @@ -159,7 +164,7 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' { } It "Install a module with a prerelease dependency" { - Install-PSResource -Name "TestModuleWithPrereleaseDep" -Repository $PSGalleryName -TrustRepository + Install-PSResource -Name "TestModuleWithPrereleaseDep" -Repository $PSGalleryName -TrustRepository $pkg = Get-InstalledPSResource "TestModuleWithPrereleaseDep" $pkg.Name | Should -Be "TestModuleWithPrereleaseDep" @@ -183,16 +188,16 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' { } It "Install resource via InputObject by piping from Find-PSresource" { - Find-PSResource -Name $testModuleName -Repository $PSGalleryName | Install-PSResource -TrustRepository + Find-PSResource -Name $testModuleName -Repository $PSGalleryName | Install-PSResource -TrustRepository $pkg = Get-InstalledPSResource $testModuleName - $pkg.Name | Should -Be $testModuleName + $pkg.Name | Should -Be $testModuleName $pkg.Version | Should -Be "5.0.0.0" } It "Install resource under specified in PSModulePath" { Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository $pkg = Get-InstalledPSResource $testModuleName - $pkg.Name | Should -Be $testModuleName + $pkg.Name | Should -Be $testModuleName ($env:PSModulePath).Contains($pkg.InstalledLocation) } @@ -237,7 +242,7 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' { # Windows only It "Install resource under no specified scope - Windows only" -Skip:(!(Get-IsWindows)) { - Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository + Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository $pkg = Get-InstalledPSResource $testModuleName $pkg.Name | Should -Be $testModuleName $pkg.InstalledLocation.ToString().Contains("Documents") | Should -Be $true @@ -265,7 +270,7 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' { Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository $pkg = Get-InstalledPSResource $testModuleName $pkg.Name | Should -Be $testModuleName - Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -WarningVariable WarningVar -warningaction SilentlyContinue + Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -WarningVariable WarningVar -WarningAction SilentlyContinue $WarningVar | Should -Not -BeNullOrEmpty } @@ -309,7 +314,7 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' { # It "Install resource that requires accept license with -AcceptLicense flag" { # Install-PSResource -Name "testModuleWithlicense" -Repository $TestGalleryName -AcceptLicense # $pkg = Get-InstalledPSResource "testModuleWithlicense" - # $pkg.Name | Should -Be "testModuleWithlicense" + # $pkg.Name | Should -Be "testModuleWithlicense" # $pkg.Version | Should -Be "0.0.3.0" # } @@ -347,6 +352,7 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' { It "Install module using -WhatIf, should not install the module" { Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -WhatIf + $? | Should -BeTrue $res = Get-InstalledPSResource $testModuleName $res | Should -BeNullOrEmpty @@ -384,34 +390,32 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' { It "Install modules using -RequiredResource with hashtable" { $rrHash = @{ - test_module = @{ - version = "[1.0.0,5.0.0)" - repository = $PSGalleryName + test_module = @{ + version = "[1.0.0,5.0.0)" + repository = $PSGalleryName } - test_module2 = @{ - version = "[1.0.0,3.0.0)" - repository = $PSGalleryName - prerelease = "true" + version = "[1.0.0,3.0.0)" + repository = $PSGalleryName + prerelease = "true" } - TestModule99 = @{} - } + } - Install-PSResource -RequiredResource $rrHash -TrustRepository + Install-PSResource -RequiredResource $rrHash -TrustRepository - $res1 = Get-InstalledPSResource $testModuleName - $res1.Name | Should -Be $testModuleName - $res1.Version | Should -Be "3.0.0.0" + $res1 = Get-InstalledPSResource $testModuleName + $res1.Name | Should -Be $testModuleName + $res1.Version | Should -Be "3.0.0.0" - $res2 = Get-InstalledPSResource "test_module2" -Version "2.5.0-beta" - $res2.Name | Should -Be "test_module2" - $res2.Version | Should -Be "2.5.0" - $res2.Prerelease | Should -Be "beta" + $res2 = Get-InstalledPSResource "test_module2" -Version "2.5.0-beta" + $res2.Name | Should -Be "test_module2" + $res2.Version | Should -Be "2.5.0" + $res2.Prerelease | Should -Be "beta" - $res3 = Get-InstalledPSResource $testModuleName2 - $res3.Name | Should -Be $testModuleName2 - $res3.Version | Should -Be "0.0.93" + $res3 = Get-InstalledPSResource $testModuleName2 + $res3.Name | Should -Be $testModuleName2 + $res3.Version | Should -Be "0.0.93" } It "Install modules using -RequiredResource with JSON string" { @@ -430,20 +434,20 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' { } }" - Install-PSResource -RequiredResource $rrJSON -TrustRepository + Install-PSResource -RequiredResource $rrJSON -TrustRepository - $res1 = Get-InstalledPSResource $testModuleName - $res1.Name | Should -Be $testModuleName - $res1.Version | Should -Be "3.0.0.0" + $res1 = Get-InstalledPSResource $testModuleName + $res1.Name | Should -Be $testModuleName + $res1.Version | Should -Be "3.0.0.0" - $res2 = Get-InstalledPSResource "test_module2" -Version "2.5.0-beta" - $res2.Name | Should -Be "test_module2" - $res2.Version | Should -Be "2.5.0" - $res2.Prerelease | Should -Be "beta" + $res2 = Get-InstalledPSResource "test_module2" -Version "2.5.0-beta" + $res2.Name | Should -Be "test_module2" + $res2.Version | Should -Be "2.5.0" + $res2.Prerelease | Should -Be "beta" - $res3 = Get-InstalledPSResource $testModuleName2 - $res3.Name | Should -Be $testModuleName2 - $res3.Version | Should -Be "0.0.93" + $res3 = Get-InstalledPSResource $testModuleName2 + $res3.Name | Should -Be $testModuleName2 + $res3.Version | Should -Be "0.0.93" } It "Install modules using -RequiredResourceFile with PSD1 file" { @@ -485,7 +489,7 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' { } # Install module 1.4.3 (is authenticode signed and has catalog file) - # Should install successfully + # Should install successfully It "Install modules with catalog file using publisher validation" -Skip:(!(Get-IsWindows)) { Install-PSResource -Name $PackageManagement -Version "1.4.3" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository @@ -509,7 +513,7 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' { It "Install module that is not authenticode signed" -Skip:(!(Get-IsWindows)) { Install-PSResource -Name $testModuleName -Version "5.0.0" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorVariable err -ErrorAction SilentlyContinue $err.Count | Should -BeGreaterThan 0 - $err[0].FullyQualifiedErrorId | Should -BeExactly "GetAuthenticodeSignatureError,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource" + $err[0].FullyQualifiedErrorId | Should -BeExactly "GetAuthenticodeSignatureError,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource" } # # Install 1.4.4.1 (with incorrect catalog file) @@ -532,11 +536,11 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' { # Should throw and fail to install It "Install script that is not signed" -Skip:(!(Get-IsWindows)) { Install-PSResource -Name "TestTestScript" -Version "1.3.1.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorVariable err -ErrorAction SilentlyContinue - write-host $err.Count + Write-Information -InformationAction Continue -MessageData $err.Count $err.Count | Should -HaveCount 1 - write-Host $err - write-Host $err[0] - $err[0].FullyQualifiedErrorId | Should -BeExactly "GetAuthenticodeSignatureError,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource" + Write-Information -InformationAction Continue -MessageData $err + Write-Information -InformationAction Continue -MessageData $err[0] + $err[0].FullyQualifiedErrorId | Should -BeExactly "GetAuthenticodeSignatureError,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource" } # Unix test for installing scripts @@ -557,10 +561,10 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' { $res | Should -BeNullOrEmpty } - # This is testing FindVersionGlobbing when it creates the 'maxString' variable, - # specifically when the prerelease versions need to be accounted for since that's when PSResourceGet + # This is testing FindVersionGlobbing when it creates the 'maxString' variable, + # specifically when the prerelease versions need to be accounted for since that's when PSResourceGet # modifies the version range to also account for prerelease versions. - # + # It "install resource when version has a nine in the digit and -Prerelease parameter is passed in" { $moduleName = "TestModuleVersionWithNine" $version = "1.9.9" @@ -596,7 +600,7 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'ManualValidati It "Install resource under AllUsers scope - Unix only" -Skip:(Get-IsWindows) { Install-PSResource -Name $testModuleName -Repository $TestGalleryName -Scope AllUsers $pkg = Get-Module $testModuleName -ListAvailable - $pkg.Name | Should -Be $testModuleName + $pkg.Name | Should -Be $testModuleName $pkg.Path.Contains("/usr/") | Should -Be $true } @@ -604,7 +608,7 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'ManualValidati It "Install resource that requires accept license without -AcceptLicense flag" { Install-PSResource -Name $testModuleName2 -Repository $TestGalleryName $pkg = Get-InstalledPSResource $testModuleName2 - $pkg.Name | Should -Be $testModuleName2 + $pkg.Name | Should -Be $testModuleName2 $pkg.Version | Should -Be "0.0.1.0" } @@ -612,10 +616,10 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'ManualValidati It "Install resource should prompt 'trust repository' if repository is not trusted" { Set-PSResourceRepository PoshTestGallery -Trusted:$false - Install-PSResource -Name $testModuleName -Repository $TestGalleryName -confirm:$false - + Install-PSResource -Name $testModuleName -Repository $TestGalleryName -Confirm:$false + $pkg = Get-Module $testModuleName -ListAvailable - $pkg.Name | Should -Be $testModuleName + $pkg.Name | Should -Be $testModuleName Set-PSResourceRepository PoshTestGallery -Trusted } diff --git a/test/InstallPSResourceTests/InstallPSResourceV3Server.Tests.ps1 b/test/InstallPSResourceTests/InstallPSResourceV3Server.Tests.ps1 index 7c9ee682f..3e4f82823 100644 --- a/test/InstallPSResourceTests/InstallPSResourceV3Server.Tests.ps1 +++ b/test/InstallPSResourceTests/InstallPSResourceV3Server.Tests.ps1 @@ -1,12 +1,15 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. +[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] +Param() + $ProgressPreference = "SilentlyContinue" $modPath = "$psscriptroot/../PSGetTestUtils.psm1" Import-Module $modPath -Force -Verbose $psmodulePaths = $env:PSModulePath -split ';' -Write-Verbose -Verbose "Current module search paths: $psmodulePaths" +Write-Verbose -Verbose -Message "Current module search paths: $psmodulePaths" Describe 'Test Install-PSResource for V3Server scenarios' -tags 'CI' { @@ -32,8 +35,8 @@ Describe 'Test Install-PSResource for V3Server scenarios' -tags 'CI' { } $testCases = @{Name="*"; ErrorId="NameContainsWildcard"}, - @{Name="Test_Module*"; ErrorId="NameContainsWildcard"}, - @{Name="Test?Module","Test[Module"; ErrorId="ErrorFilteringNamesForUnsupportedWildcards"} + @{Name="Test_Module*"; ErrorId="NameContainsWildcard"}, + @{Name="Test?Module","Test[Module"; ErrorId="ErrorFilteringNamesForUnsupportedWildcards"} It "Should not install resource with wildcard in name" -TestCases $testCases { param($Name, $ErrorId) @@ -60,7 +63,7 @@ Describe 'Test Install-PSResource for V3Server scenarios' -tags 'CI' { It "Install multiple resources by name" { $pkgNames = @($testModuleName, $testModuleName2) - Install-PSResource -Name $pkgNames -Repository $NuGetGalleryName -TrustRepository + Install-PSResource -Name $pkgNames -Repository $NuGetGalleryName -TrustRepository $pkg = Get-InstalledPSResource $pkgNames $pkg.Name | Should -Be $pkgNames } @@ -70,7 +73,15 @@ Describe 'Test Install-PSResource for V3Server scenarios' -tags 'CI' { $pkg = Get-InstalledPSResource "NonExistantModule" -ErrorAction SilentlyContinue $pkg.Name | Should -BeNullOrEmpty $err.Count | Should -BeGreaterThan 0 - $err[0].FullyQualifiedErrorId | Should -BeExactly "InstallPackageFailure,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource" + $err[0].FullyQualifiedErrorId | Should -BeExactly "InstallPackageFailure,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource" + } + + It "Install module using -WhatIf, should not install the module" { + Install-PSResource -Name $testModuleName -Repository $NuGetGalleryName -TrustRepository -WhatIf + $? | Should -BeTrue + + $res = Get-InstalledPSResource $testModuleName + $res | Should -BeNullOrEmpty } # Do some version testing, but Find-PSResource should be doing thorough testing @@ -82,21 +93,21 @@ Describe 'Test Install-PSResource for V3Server scenarios' -tags 'CI' { } It "Should install resource given name and exact version with bracket syntax" { - Install-PSResource -Name $testModuleName -Version "[1.0.0]" -Repository $NuGetGalleryName -TrustRepository + Install-PSResource -Name $testModuleName -Version "[1.0.0]" -Repository $NuGetGalleryName -TrustRepository $pkg = Get-InstalledPSResource $testModuleName $pkg.Name | Should -Be $testModuleName $pkg.Version | Should -Be "1.0.0" } It "Should install resource given name and exact range inclusive [1.0.0, 5.0.0]" { - Install-PSResource -Name $testModuleName -Version "[1.0.0, 5.0.0]" -Repository $NuGetGalleryName -TrustRepository + Install-PSResource -Name $testModuleName -Version "[1.0.0, 5.0.0]" -Repository $NuGetGalleryName -TrustRepository $pkg = Get-InstalledPSResource $testModuleName $pkg.Name | Should -Be $testModuleName $pkg.Version | Should -Be "5.0.0" } It "Should install resource given name and exact range exclusive (1.0.0, 5.0.0)" { - Install-PSResource -Name $testModuleName -Version "(1.0.0, 5.0.0)" -Repository $NuGetGalleryName -TrustRepository + Install-PSResource -Name $testModuleName -Version "(1.0.0, 5.0.0)" -Repository $NuGetGalleryName -TrustRepository $pkg = Get-InstalledPSResource $testModuleName $pkg.Name | Should -Be $testModuleName $pkg.Version | Should -Be "3.0.0" @@ -110,7 +121,7 @@ Describe 'Test Install-PSResource for V3Server scenarios' -tags 'CI' { } catch {} - $Error[0].FullyQualifiedErrorId | Should -be "IncorrectVersionFormat,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource" + $Error[0].FullyQualifiedErrorId | Should -Be "IncorrectVersionFormat,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource" $res = Get-InstalledPSResource $testModuleName -ErrorAction SilentlyContinue $res | Should -BeNullOrEmpty @@ -124,7 +135,7 @@ Describe 'Test Install-PSResource for V3Server scenarios' -tags 'CI' { } It "Install resource with latest (including prerelease) version given Prerelease parameter" { - Install-PSResource -Name $testModuleName -Prerelease -Repository $NuGetGalleryName -TrustRepository + Install-PSResource -Name $testModuleName -Prerelease -Repository $NuGetGalleryName -TrustRepository $pkg = Get-InstalledPSResource $testModuleName $pkg.Name | Should -Be $testModuleName $pkg.Version | Should -Be "5.2.5" @@ -132,7 +143,7 @@ Describe 'Test Install-PSResource for V3Server scenarios' -tags 'CI' { } It "Install resource via InputObject by piping from Find-PSresource" { - Find-PSResource -Name $testModuleName -Repository $NuGetGalleryName | Install-PSResource -TrustRepository + Find-PSResource -Name $testModuleName -Repository $NuGetGalleryName | Install-PSResource -TrustRepository $pkg = Get-InstalledPSResource $testModuleName $pkg.Name | Should -Be $testModuleName $pkg.Version | Should -Be "5.0.0" @@ -203,7 +214,7 @@ Describe 'Test Install-PSResource for V3Server scenarios' -tags 'CI' { Install-PSResource -Name $testModuleName -Repository $NuGetGalleryName -TrustRepository $pkg = Get-InstalledPSResource $testModuleName $pkg.Name | Should -Be $testModuleName - Install-PSResource -Name $testModuleName -Repository $NuGetGalleryName -TrustRepository -WarningVariable WarningVar -warningaction SilentlyContinue + Install-PSResource -Name $testModuleName -Repository $NuGetGalleryName -TrustRepository -WarningVariable WarningVar -WarningAction SilentlyContinue $WarningVar | Should -Not -BeNullOrEmpty } @@ -265,35 +276,35 @@ Describe 'Test Install-PSResource for V3Server scenarios' -tags 'CI' { It "Install modules using -RequiredResource with hashtable" { $rrHash = @{ - test_module = @{ - version = "[1.0.0,5.0.0)" - repository = $NuGetGalleryName + test_module = @{ + version = "[1.0.0,5.0.0)" + repository = $NuGetGalleryName } - test_module2 = @{ - version = "[1.0.0,5.0.0]" - repository = $NuGetGalleryName - prerelease = "true" + test_module2 = @{ + version = "[1.0.0,5.0.0]" + repository = $NuGetGalleryName + prerelease = "true" } - TestModule99 = @{ + TestModule99 = @{ repository = $NuGetGalleryName } - } + } - Install-PSResource -RequiredResource $rrHash -TrustRepository + Install-PSResource -RequiredResource $rrHash -TrustRepository - $res1 = Get-InstalledPSResource $testModuleName - $res1.Name | Should -Be $testModuleName - $res1.Version | Should -Be "3.0.0" + $res1 = Get-InstalledPSResource $testModuleName + $res1.Name | Should -Be $testModuleName + $res1.Version | Should -Be "3.0.0" - $res2 = Get-InstalledPSResource $testModuleName2 - $res2.Name | Should -Be $testModuleName2 - $res2.Version | Should -Be "5.0.0" + $res2 = Get-InstalledPSResource $testModuleName2 + $res2.Name | Should -Be $testModuleName2 + $res2.Version | Should -Be "5.0.0" - $res3 = Get-InstalledPSResource "TestModule99" - $res3.Name | Should -Be "TestModule99" - $res3.Version | Should -Be "0.0.93" + $res3 = Get-InstalledPSResource "TestModule99" + $res3.Name | Should -Be "TestModule99" + $res3.Version | Should -Be "0.0.93" } It "Install modules using -RequiredResource with JSON string" { @@ -312,19 +323,19 @@ Describe 'Test Install-PSResource for V3Server scenarios' -tags 'CI' { } }" - Install-PSResource -RequiredResource $rrJSON -TrustRepository + Install-PSResource -RequiredResource $rrJSON -TrustRepository - $res1 = Get-InstalledPSResource $testModuleName - $res1.Name | Should -Be $testModuleName - $res1.Version | Should -Be "3.0.0" + $res1 = Get-InstalledPSResource $testModuleName + $res1.Name | Should -Be $testModuleName + $res1.Version | Should -Be "3.0.0" - $res2 = Get-InstalledPSResource $testModuleName2 - $res2.Name | Should -Be $testModuleName2 - $res2.Version | Should -Be "5.0.0.0" + $res2 = Get-InstalledPSResource $testModuleName2 + $res2.Name | Should -Be $testModuleName2 + $res2.Version | Should -Be "5.0.0.0" - $res3 = Get-InstalledPSResource "testModule99" - $res3.Name | Should -Be "testModule99" - $res3.Version | Should -Be "0.0.93" + $res3 = Get-InstalledPSResource "testModule99" + $res3.Name | Should -Be "testModule99" + $res3.Version | Should -Be "0.0.93" } It "Install modules using -RequiredResourceFile with PSD1 file" { @@ -388,15 +399,15 @@ Describe 'Test Install-PSResource for V3Server scenarios' -tags 'ManualValidatio It "Install resource under AllUsers scope - Unix only" -Skip:(Get-IsWindows) { Install-PSResource -Name $testModuleName -Repository $TestGalleryName -Scope AllUsers $pkg = Get-Module $testModuleName -ListAvailable - $pkg.Name | Should -Be $testModuleName + $pkg.Name | Should -Be $testModuleName $pkg.Path.Contains("/usr/") | Should -Be $true } # This needs to be manually tested due to prompt It "Install resource that requires accept license without -AcceptLicense flag" { Install-PSResource -Name $testModuleName2 -Repository $TestGalleryName - $pkg = Get-InstalledPSResource $testModuleName2 - $pkg.Name | Should -Be $testModuleName2 + $pkg = Get-InstalledPSResource $testModuleName2 + $pkg.Name | Should -Be $testModuleName2 $pkg.Version | Should -Be "2.0.0" } @@ -404,8 +415,8 @@ Describe 'Test Install-PSResource for V3Server scenarios' -tags 'ManualValidatio It "Install resource should prompt 'trust repository' if repository is not trusted" { Set-PSResourceRepository PoshTestGallery -Trusted:$false - Install-PSResource -Name $testModuleName -Repository $TestGalleryName -confirm:$false - + Install-PSResource -Name $testModuleName -Repository $TestGalleryName -Confirm:$false + $pkg = Get-Module $testModuleName -ListAvailable $pkg.Name | Should -Be $testModuleName