From 720074a17acb550898ed181baf7099674bde2879 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olav=20R=C3=B8nnestad=20Birkeland?= <6450056+o-l-a-v@users.noreply.github.com> Date: Mon, 21 Oct 2024 22:34:59 +0200 Subject: [PATCH] WIP: Add `-AcceptLicense` to `Save-PSResource` (#1718) --- src/code/InstallHelper.cs | 2 +- src/code/SavePSResource.cs | 36 ++++++++++------ .../SavePSResourceV2Tests.ps1 | 43 +++++++++++-------- .../SavePSResourceV3Tests.ps1 | 39 ++++++++++------- 4 files changed, 73 insertions(+), 47 deletions(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index 8cc212eb7..4d86af911 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -1026,7 +1026,7 @@ private bool TryInstallToTempPath( } // Accept License verification - if (!_savePkg && !CallAcceptLicense(pkgToInstall, moduleManifest, tempInstallPath, pkgVersion, out error)) + if (!CallAcceptLicense(pkgToInstall, moduleManifest, tempInstallPath, pkgVersion, out error)) { _pkgNamesToInstall.RemoveAll(x => x.Equals(pkgToInstall.Name, StringComparison.InvariantCultureIgnoreCase)); return false; diff --git a/src/code/SavePSResource.cs b/src/code/SavePSResource.cs index 48d6f4891..ce5581ad7 100644 --- a/src/code/SavePSResource.cs +++ b/src/code/SavePSResource.cs @@ -171,6 +171,12 @@ public string TemporaryPath /// public SwitchParameter Quiet { get; set; } + /// + /// For modules that require a license, AcceptLicense automatically accepts the license agreement during installation. + /// + [Parameter] + public SwitchParameter AcceptLicense { get; set; } + #endregion #region Method overrides @@ -207,7 +213,8 @@ protected override void ProcessRecord() break; case InputObjectParameterSet: - foreach (var inputObj in InputObject) { + foreach (var inputObj in InputObject) + { string normalizedVersionString = Utils.GetNormalizedVersionString(inputObj.Version.ToString(), inputObj.Prerelease); ProcessSaveHelper( pkgNames: new string[] { inputObj.Name }, @@ -230,7 +237,7 @@ protected override void ProcessRecord() private void ProcessSaveHelper(string[] pkgNames, string pkgVersion, bool pkgPrerelease, string[] pkgRepository) { WriteDebug("In SavePSResource::ProcessSaveHelper()"); - var namesToSave = Utils.ProcessNameWildcards(pkgNames, removeWildcardEntries:false, out string[] errorMsgs, out bool nameContainsWildcard); + var namesToSave = Utils.ProcessNameWildcards(pkgNames, removeWildcardEntries: false, out string[] errorMsgs, out bool nameContainsWildcard); if (nameContainsWildcard) { WriteError(new ErrorRecord( @@ -238,7 +245,7 @@ private void ProcessSaveHelper(string[] pkgNames, string pkgVersion, bool pkgPre "NameContainsWildcard", ErrorCategory.InvalidArgument, this)); - + return; } @@ -276,26 +283,27 @@ private void ProcessSaveHelper(string[] pkgNames, string pkgVersion, bool pkgPre // figure out if version is a prerelease or not. // if condition is not met, prerelease is the value passed in via the parameter. - if (!string.IsNullOrEmpty(pkgVersion) && pkgVersion.Contains('-')) { + if (!string.IsNullOrEmpty(pkgVersion) && pkgVersion.Contains('-')) + { pkgPrerelease = true; } var installedPkgs = _installHelper.BeginInstallPackages( - names: namesToSave, + names: namesToSave, versionRange: versionRange, nugetVersion: nugetVersion, versionType: versionType, versionString: pkgVersion, - prerelease: pkgPrerelease, - repository: pkgRepository, - acceptLicense: true, - quiet: Quiet, - reinstall: true, - force: false, + prerelease: pkgPrerelease, + repository: pkgRepository, + acceptLicense: AcceptLicense, + quiet: Quiet, + reinstall: true, + force: false, trustRepository: TrustRepository, - noClobber: false, - asNupkg: AsNupkg, - includeXml: IncludeXml, + noClobber: false, + asNupkg: AsNupkg, + includeXml: IncludeXml, skipDependencyCheck: SkipDependencyCheck, authenticodeCheck: AuthenticodeCheck, savePkg: true, diff --git a/test/SavePSResourceTests/SavePSResourceV2Tests.ps1 b/test/SavePSResourceTests/SavePSResourceV2Tests.ps1 index 8f7d540c5..f00656b06 100644 --- a/test/SavePSResourceTests/SavePSResourceV2Tests.ps1 +++ b/test/SavePSResourceTests/SavePSResourceV2Tests.ps1 @@ -30,14 +30,14 @@ Describe 'Test HTTP Save-PSResource for V2 Server Protocol' -tags 'CI' { It "Save specific module resource by name" { Save-PSResource -Name $testModuleName -Repository $PSGalleryName -Path $SaveDir -TrustRepository - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName $pkgDir | Should -Not -BeNullOrEmpty (Get-ChildItem $pkgDir.FullName) | Should -HaveCount 1 } It "Save specific script resource by name" { Save-PSResource -Name $testScriptName -Repository $PSGalleryName -Path $SaveDir -TrustRepository - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "test_script.ps1" + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ "test_script.ps1" $pkgDir | Should -Not -BeNullOrEmpty (Get-ChildItem $pkgDir.FullName) | Should -HaveCount 1 } @@ -53,7 +53,7 @@ Describe 'Test HTTP Save-PSResource for V2 Server Protocol' -tags 'CI' { It "Should not save resource given nonexistant name" { Save-PSResource -Name NonExistentModule -Repository $PSGalleryName -Path $SaveDir -ErrorVariable err -ErrorAction SilentlyContinue -TrustRepository - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "NonExistentModule" + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ "NonExistentModule" $pkgDir.Name | Should -BeNullOrEmpty } @@ -65,7 +65,7 @@ Describe 'Test HTTP Save-PSResource for V2 Server Protocol' -tags 'CI' { It "Should save resource given name and exact version" { Save-PSResource -Name $testModuleName -Version "1.0.0" -Repository $PSGalleryName -Path $SaveDir -TrustRepository - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName $pkgDir | Should -Not -BeNullOrEmpty $pkgDirVersion = Get-ChildItem $pkgDir.FullName $pkgDirVersion.Name | Should -Be "1.0.0.0" @@ -73,7 +73,7 @@ Describe 'Test HTTP Save-PSResource for V2 Server Protocol' -tags 'CI' { It "Should save resource given name and version '3.*'" { Save-PSResource -Name $testModuleName -Version "3.*" -Repository $PSGalleryName -Path $SaveDir -TrustRepository - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName $pkgDir | Should -Not -BeNullOrEmpty $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName $pkgDirVersion.Name | Should -Be "3.0.0.0" @@ -81,7 +81,7 @@ Describe 'Test HTTP Save-PSResource for V2 Server Protocol' -tags 'CI' { It "Should save resource given name and exact version with bracket syntax" { Save-PSResource -Name $testModuleName -Version "[1.0.0]" -Repository $PSGalleryName -Path $SaveDir -TrustRepository - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName $pkgDir | Should -Not -BeNullOrEmpty $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName $pkgDirVersion.Name | Should -Be "1.0.0.0" @@ -89,7 +89,7 @@ Describe 'Test HTTP Save-PSResource for V2 Server Protocol' -tags 'CI' { It "Should save resource given name and exact range inclusive [1.0.0, 3.0.0]" { Save-PSResource -Name $testModuleName -Version "[1.0.0, 3.0.0]" -Repository $PSGalleryName -Path $SaveDir -TrustRepository - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName $pkgDir | Should -Not -BeNullOrEmpty $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName $pkgDirVersion.Name | Should -Be "3.0.0.0" @@ -97,7 +97,7 @@ Describe 'Test HTTP Save-PSResource for V2 Server Protocol' -tags 'CI' { It "Should save resource given name and exact range exclusive (1.0.0, 5.0.0)" { Save-PSResource -Name $testModuleName -Version "(1.0.0, 5.0.0)" -Repository $PSGalleryName -Path $SaveDir -TrustRepository - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName $pkgDir | Should -Not -BeNullOrEmpty $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName $pkgDirVersion.Name | Should -Be "3.0.0.0" @@ -111,15 +111,15 @@ Describe 'Test HTTP Save-PSResource for V2 Server Protocol' -tags 'CI' { catch {} - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName $pkgDir | Should -BeNullOrEmpty $Error.Count | Should -BeGreaterThan 0 - $Error[0].FullyQualifiedErrorId | Should -Be "IncorrectVersionFormat,Microsoft.PowerShell.PSResourceGet.Cmdlets.SavePSResource" + $Error[0].FullyQualifiedErrorId | Should -Be "IncorrectVersionFormat,Microsoft.PowerShell.PSResourceGet.Cmdlets.SavePSResource" } It "Save resource with latest (including prerelease) version given Prerelease parameter" { Save-PSResource -Name $testModuleName -Prerelease -Repository $PSGalleryName -Path $SaveDir -TrustRepository - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName $pkgDir | Should -Not -BeNullOrEmpty $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName $pkgDirVersion.Name | Should -Be "5.2.5" @@ -146,24 +146,24 @@ Describe 'Test HTTP Save-PSResource for V2 Server Protocol' -tags 'CI' { ### the input object is of type string (ie "true"). It "Save PSResourceInfo object piped in for prerelease version object" -Pending { Find-PSResource -Name $testModuleName -Version "5.2.5-alpha001" -Repository $PSGalleryName | Save-PSResource -Path $SaveDir -TrustRepository -Verbose - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName $pkgDir | Should -Not -BeNullOrEmpty (Get-ChildItem -Path $pkgDir.FullName) | Should -HaveCount 1 } It "Save module as a nupkg" { Save-PSResource -Name $testModuleName -Version "1.0.0" -Repository $PSGalleryName -Path $SaveDir -AsNupkg -TrustRepository - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "test_module.1.0.0.nupkg" + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ "test_module.1.0.0.nupkg" $pkgDir | Should -Not -BeNullOrEmpty } It "Save module and include XML metadata file" { Save-PSResource -Name $testModuleName -Version "1.0.0" -Repository $PSGalleryName -Path $SaveDir -IncludeXml -TrustRepository - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName $pkgDir | Should -Not -BeNullOrEmpty $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName $pkgDirVersion.Name | Should -Be "1.0.0.0" - $xmlFile = Get-ChildItem -Path $pkgDirVersion.FullName | Where-Object Name -eq "PSGetModuleInfo.xml" + $xmlFile = Get-ChildItem -Path $pkgDirVersion.FullName | Where-Object Name -EQ "PSGetModuleInfo.xml" $xmlFile | Should -Not -BeNullOrEmpty } @@ -184,8 +184,8 @@ Describe 'Test HTTP Save-PSResource for V2 Server Protocol' -tags 'CI' { Save-PSResource -Name $testScriptName -Repository $PSGalleryName -Path $SaveDir -TrustRepository -IncludeXml $scriptXML = $testScriptName + "_InstalledScriptInfo.xml" - $savedScriptFile = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "test_script.ps1" - $savedScriptXML = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $scriptXML + $savedScriptFile = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ "test_script.ps1" + $savedScriptXML = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $scriptXML $savedScriptFile | Should -Not -BeNullOrEmpty (Get-ChildItem $savedScriptFile.FullName) | Should -HaveCount 1 $savedScriptXML | Should -Not -BeNullOrEmpty @@ -199,4 +199,13 @@ Describe 'Test HTTP Save-PSResource for V2 Server Protocol' -tags 'CI' { $err.Count | Should -BeGreaterThan 0 $err[0].FullyQualifiedErrorId | Should -BeExactly "InstallPackageFailure,Microsoft.PowerShell.PSResourceGet.Cmdlets.SavePSResource" } + + # Save resource that requires license + It "Save resource that requires accept license with -AcceptLicense flag" { + Save-PSResource -Repository $TestGalleryName -TrustRepository -Path $SaveDir ` + -Name $testModuleName2 -AcceptLicense + $pkg = Get-InstalledPSResource -Path $SaveDir -Name $testModuleName2 + $pkg.Name | Should -Be $testModuleName2 + $pkg.Version | Should -Be "0.0.1.0" + } } diff --git a/test/SavePSResourceTests/SavePSResourceV3Tests.ps1 b/test/SavePSResourceTests/SavePSResourceV3Tests.ps1 index c82a11b54..cb414ce50 100644 --- a/test/SavePSResourceTests/SavePSResourceV3Tests.ps1 +++ b/test/SavePSResourceTests/SavePSResourceV3Tests.ps1 @@ -28,7 +28,7 @@ Describe 'Test HTTP Save-PSResource for V3 Server Protocol' -tags 'CI' { It "Save specific module resource by name" { Save-PSResource -Name $testModuleName -Repository $NuGetGalleryName -Path $SaveDir -TrustRepository - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName $pkgDir | Should -Not -BeNullOrEmpty (Get-ChildItem $pkgDir.FullName) | Should -HaveCount 1 } @@ -44,7 +44,7 @@ Describe 'Test HTTP Save-PSResource for V3 Server Protocol' -tags 'CI' { It "Should not save resource given nonexistant name" { Save-PSResource -Name NonExistentModule -Repository $NuGetGalleryName -Path $SaveDir -ErrorVariable err -ErrorAction SilentlyContinue -TrustRepository - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "NonExistentModule" + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ "NonExistentModule" $pkgDir.Name | Should -BeNullOrEmpty } @@ -56,7 +56,7 @@ Describe 'Test HTTP Save-PSResource for V3 Server Protocol' -tags 'CI' { It "Should save resource given name and exact version" { Save-PSResource -Name $testModuleName -Version "1.0.0" -Repository $NuGetGalleryName -Path $SaveDir -TrustRepository - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName $pkgDir | Should -Not -BeNullOrEmpty $pkgDirVersion = Get-ChildItem $pkgDir.FullName $pkgDirVersion.Name | Should -Be "1.0.0" @@ -64,7 +64,7 @@ Describe 'Test HTTP Save-PSResource for V3 Server Protocol' -tags 'CI' { It "Should save resource given name and exact version with bracket syntax" { Save-PSResource -Name $testModuleName -Version "[1.0.0]" -Repository $NuGetGalleryName -Path $SaveDir -TrustRepository - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName $pkgDir | Should -Not -BeNullOrEmpty $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName $pkgDirVersion.Name | Should -Be "1.0.0" @@ -72,7 +72,7 @@ Describe 'Test HTTP Save-PSResource for V3 Server Protocol' -tags 'CI' { It "Should save resource given name and exact range inclusive [1.0.0, 3.0.0]" { Save-PSResource -Name $testModuleName -Version "[1.0.0, 3.0.0]" -Repository $NuGetGalleryName -Path $SaveDir -TrustRepository - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName $pkgDir | Should -Not -BeNullOrEmpty $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName $pkgDirVersion.Name | Should -Be "3.0.0" @@ -80,7 +80,7 @@ Describe 'Test HTTP Save-PSResource for V3 Server Protocol' -tags 'CI' { It "Should save resource given name and exact range exclusive (1.0.0, 5.0.0)" { Save-PSResource -Name $testModuleName -Version "(1.0.0, 5.0.0)" -Repository $NuGetGalleryName -Path $SaveDir -TrustRepository - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName $pkgDir | Should -Not -BeNullOrEmpty $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName $pkgDirVersion.Name | Should -Be "3.0.0" @@ -94,15 +94,15 @@ Describe 'Test HTTP Save-PSResource for V3 Server Protocol' -tags 'CI' { catch {} - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName $pkgDir | Should -BeNullOrEmpty $Error.Count | Should -BeGreaterThan 0 - $Error[0].FullyQualifiedErrorId | Should -Be "IncorrectVersionFormat,Microsoft.PowerShell.PSResourceGet.Cmdlets.SavePSResource" + $Error[0].FullyQualifiedErrorId | Should -Be "IncorrectVersionFormat,Microsoft.PowerShell.PSResourceGet.Cmdlets.SavePSResource" } It "Save resource with latest (including prerelease) version given Prerelease parameter" { Save-PSResource -Name $testModuleName -Prerelease -Repository $NuGetGalleryName -Path $SaveDir -TrustRepository - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName $pkgDir | Should -Not -BeNullOrEmpty $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName $pkgDirVersion.Name | Should -Be "5.2.5" @@ -112,24 +112,24 @@ Describe 'Test HTTP Save-PSResource for V3 Server Protocol' -tags 'CI' { ### the input object is of type string (ie "true"). It "Save PSResourceInfo object piped in for prerelease version object" -Pending{ Find-PSResource -Name $testModuleName -Version "5.2.5-alpha001" -Repository $NuGetGalleryName | Save-PSResource -Path $SaveDir -TrustRepository - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName $pkgDir | Should -Not -BeNullOrEmpty - (Get-ChildItem -Path $pkgDir.FullName) | Should -HaveCount 1 + (Get-ChildItem -Path $pkgDir.FullName) | Should -HaveCount 1 } It "Save module as a nupkg" { Save-PSResource -Name $testModuleName -Version "1.0.0" -Repository $NuGetGalleryName -Path $SaveDir -AsNupkg -TrustRepository - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "test_module.1.0.0.nupkg" + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ "test_module.1.0.0.nupkg" $pkgDir | Should -Not -BeNullOrEmpty } It "Save module and include XML metadata file" { Save-PSResource -Name $testModuleName -Version "1.0.0" -Repository $NuGetGalleryName -Path $SaveDir -IncludeXml -TrustRepository - $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName $pkgDir | Should -Not -BeNullOrEmpty $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName $pkgDirVersion.Name | Should -Be "1.0.0" - $xmlFile = Get-ChildItem -Path $pkgDirVersion.FullName | Where-Object Name -eq "PSGetModuleInfo.xml" + $xmlFile = Get-ChildItem -Path $pkgDirVersion.FullName | Where-Object Name -EQ "PSGetModuleInfo.xml" $xmlFile | Should -Not -BeNullOrEmpty } @@ -146,4 +146,13 @@ Describe 'Test HTTP Save-PSResource for V3 Server Protocol' -tags 'CI' { $err.Count | Should -BeGreaterThan 0 $err[0].FullyQualifiedErrorId | Should -BeExactly "InstallPackageFailure,Microsoft.PowerShell.PSResourceGet.Cmdlets.SavePSResource" } -} \ No newline at end of file + + # Save resource that requires license + It "Install resource that requires accept license with -AcceptLicense flag" { + Save-PSResource -Repository $NuGetGalleryName -TrustRepository -Path $SaveDir ` + -Name "test_module_with_license" -AcceptLicense + $pkg = Get-InstalledPSResource -Path $SaveDir "test_module_with_license" + $pkg.Name | Should -Be "test_module_with_license" + $pkg.Version | Should -Be "2.0.0" + } +}