Skip to content

Commit

Permalink
WIP: Add -AcceptLicense to Save-PSResource (#1718)
Browse files Browse the repository at this point in the history
  • Loading branch information
o-l-a-v authored Oct 21, 2024
1 parent 5c4116f commit 720074a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/code/InstallHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 22 additions & 14 deletions src/code/SavePSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ public string TemporaryPath
/// </summary>
public SwitchParameter Quiet { get; set; }

/// <summary>
/// For modules that require a license, AcceptLicense automatically accepts the license agreement during installation.
/// </summary>
[Parameter]
public SwitchParameter AcceptLicense { get; set; }

#endregion

#region Method overrides
Expand Down Expand Up @@ -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 },
Expand All @@ -230,15 +237,15 @@ 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(
new PSInvalidOperationException("Name with wildcards is not supported for Save-PSResource cmdlet"),
"NameContainsWildcard",
ErrorCategory.InvalidArgument,
this));

return;
}

Expand Down Expand Up @@ -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,
Expand Down
43 changes: 26 additions & 17 deletions test/SavePSResourceTests/SavePSResourceV2Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}

Expand All @@ -65,39 +65,39 @@ 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"
}

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"
}

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"
}

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"
}

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"
Expand All @@ -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"
Expand All @@ -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
}

Expand All @@ -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
Expand All @@ -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"
}
}
Loading

0 comments on commit 720074a

Please sign in to comment.