Skip to content

Commit 720074a

Browse files
authored
WIP: Add -AcceptLicense to Save-PSResource (#1718)
1 parent 5c4116f commit 720074a

File tree

4 files changed

+73
-47
lines changed

4 files changed

+73
-47
lines changed

src/code/InstallHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ private bool TryInstallToTempPath(
10261026
}
10271027

10281028
// Accept License verification
1029-
if (!_savePkg && !CallAcceptLicense(pkgToInstall, moduleManifest, tempInstallPath, pkgVersion, out error))
1029+
if (!CallAcceptLicense(pkgToInstall, moduleManifest, tempInstallPath, pkgVersion, out error))
10301030
{
10311031
_pkgNamesToInstall.RemoveAll(x => x.Equals(pkgToInstall.Name, StringComparison.InvariantCultureIgnoreCase));
10321032
return false;

src/code/SavePSResource.cs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ public string TemporaryPath
171171
/// </summary>
172172
public SwitchParameter Quiet { get; set; }
173173

174+
/// <summary>
175+
/// For modules that require a license, AcceptLicense automatically accepts the license agreement during installation.
176+
/// </summary>
177+
[Parameter]
178+
public SwitchParameter AcceptLicense { get; set; }
179+
174180
#endregion
175181

176182
#region Method overrides
@@ -207,7 +213,8 @@ protected override void ProcessRecord()
207213
break;
208214

209215
case InputObjectParameterSet:
210-
foreach (var inputObj in InputObject) {
216+
foreach (var inputObj in InputObject)
217+
{
211218
string normalizedVersionString = Utils.GetNormalizedVersionString(inputObj.Version.ToString(), inputObj.Prerelease);
212219
ProcessSaveHelper(
213220
pkgNames: new string[] { inputObj.Name },
@@ -230,15 +237,15 @@ protected override void ProcessRecord()
230237
private void ProcessSaveHelper(string[] pkgNames, string pkgVersion, bool pkgPrerelease, string[] pkgRepository)
231238
{
232239
WriteDebug("In SavePSResource::ProcessSaveHelper()");
233-
var namesToSave = Utils.ProcessNameWildcards(pkgNames, removeWildcardEntries:false, out string[] errorMsgs, out bool nameContainsWildcard);
240+
var namesToSave = Utils.ProcessNameWildcards(pkgNames, removeWildcardEntries: false, out string[] errorMsgs, out bool nameContainsWildcard);
234241
if (nameContainsWildcard)
235242
{
236243
WriteError(new ErrorRecord(
237244
new PSInvalidOperationException("Name with wildcards is not supported for Save-PSResource cmdlet"),
238245
"NameContainsWildcard",
239246
ErrorCategory.InvalidArgument,
240247
this));
241-
248+
242249
return;
243250
}
244251

@@ -276,26 +283,27 @@ private void ProcessSaveHelper(string[] pkgNames, string pkgVersion, bool pkgPre
276283

277284
// figure out if version is a prerelease or not.
278285
// if condition is not met, prerelease is the value passed in via the parameter.
279-
if (!string.IsNullOrEmpty(pkgVersion) && pkgVersion.Contains('-')) {
286+
if (!string.IsNullOrEmpty(pkgVersion) && pkgVersion.Contains('-'))
287+
{
280288
pkgPrerelease = true;
281289
}
282290

283291
var installedPkgs = _installHelper.BeginInstallPackages(
284-
names: namesToSave,
292+
names: namesToSave,
285293
versionRange: versionRange,
286294
nugetVersion: nugetVersion,
287295
versionType: versionType,
288296
versionString: pkgVersion,
289-
prerelease: pkgPrerelease,
290-
repository: pkgRepository,
291-
acceptLicense: true,
292-
quiet: Quiet,
293-
reinstall: true,
294-
force: false,
297+
prerelease: pkgPrerelease,
298+
repository: pkgRepository,
299+
acceptLicense: AcceptLicense,
300+
quiet: Quiet,
301+
reinstall: true,
302+
force: false,
295303
trustRepository: TrustRepository,
296-
noClobber: false,
297-
asNupkg: AsNupkg,
298-
includeXml: IncludeXml,
304+
noClobber: false,
305+
asNupkg: AsNupkg,
306+
includeXml: IncludeXml,
299307
skipDependencyCheck: SkipDependencyCheck,
300308
authenticodeCheck: AuthenticodeCheck,
301309
savePkg: true,

test/SavePSResourceTests/SavePSResourceV2Tests.ps1

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ Describe 'Test HTTP Save-PSResource for V2 Server Protocol' -tags 'CI' {
3030

3131
It "Save specific module resource by name" {
3232
Save-PSResource -Name $testModuleName -Repository $PSGalleryName -Path $SaveDir -TrustRepository
33-
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName
33+
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName
3434
$pkgDir | Should -Not -BeNullOrEmpty
3535
(Get-ChildItem $pkgDir.FullName) | Should -HaveCount 1
3636
}
3737

3838
It "Save specific script resource by name" {
3939
Save-PSResource -Name $testScriptName -Repository $PSGalleryName -Path $SaveDir -TrustRepository
40-
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "test_script.ps1"
40+
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ "test_script.ps1"
4141
$pkgDir | Should -Not -BeNullOrEmpty
4242
(Get-ChildItem $pkgDir.FullName) | Should -HaveCount 1
4343
}
@@ -53,7 +53,7 @@ Describe 'Test HTTP Save-PSResource for V2 Server Protocol' -tags 'CI' {
5353

5454
It "Should not save resource given nonexistant name" {
5555
Save-PSResource -Name NonExistentModule -Repository $PSGalleryName -Path $SaveDir -ErrorVariable err -ErrorAction SilentlyContinue -TrustRepository
56-
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "NonExistentModule"
56+
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ "NonExistentModule"
5757
$pkgDir.Name | Should -BeNullOrEmpty
5858
}
5959

@@ -65,39 +65,39 @@ Describe 'Test HTTP Save-PSResource for V2 Server Protocol' -tags 'CI' {
6565

6666
It "Should save resource given name and exact version" {
6767
Save-PSResource -Name $testModuleName -Version "1.0.0" -Repository $PSGalleryName -Path $SaveDir -TrustRepository
68-
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName
68+
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName
6969
$pkgDir | Should -Not -BeNullOrEmpty
7070
$pkgDirVersion = Get-ChildItem $pkgDir.FullName
7171
$pkgDirVersion.Name | Should -Be "1.0.0.0"
7272
}
7373

7474
It "Should save resource given name and version '3.*'" {
7575
Save-PSResource -Name $testModuleName -Version "3.*" -Repository $PSGalleryName -Path $SaveDir -TrustRepository
76-
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName
76+
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName
7777
$pkgDir | Should -Not -BeNullOrEmpty
7878
$pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName
7979
$pkgDirVersion.Name | Should -Be "3.0.0.0"
8080
}
8181

8282
It "Should save resource given name and exact version with bracket syntax" {
8383
Save-PSResource -Name $testModuleName -Version "[1.0.0]" -Repository $PSGalleryName -Path $SaveDir -TrustRepository
84-
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName
84+
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName
8585
$pkgDir | Should -Not -BeNullOrEmpty
8686
$pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName
8787
$pkgDirVersion.Name | Should -Be "1.0.0.0"
8888
}
8989

9090
It "Should save resource given name and exact range inclusive [1.0.0, 3.0.0]" {
9191
Save-PSResource -Name $testModuleName -Version "[1.0.0, 3.0.0]" -Repository $PSGalleryName -Path $SaveDir -TrustRepository
92-
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName
92+
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName
9393
$pkgDir | Should -Not -BeNullOrEmpty
9494
$pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName
9595
$pkgDirVersion.Name | Should -Be "3.0.0.0"
9696
}
9797

9898
It "Should save resource given name and exact range exclusive (1.0.0, 5.0.0)" {
9999
Save-PSResource -Name $testModuleName -Version "(1.0.0, 5.0.0)" -Repository $PSGalleryName -Path $SaveDir -TrustRepository
100-
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName
100+
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName
101101
$pkgDir | Should -Not -BeNullOrEmpty
102102
$pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName
103103
$pkgDirVersion.Name | Should -Be "3.0.0.0"
@@ -111,15 +111,15 @@ Describe 'Test HTTP Save-PSResource for V2 Server Protocol' -tags 'CI' {
111111
catch
112112
{}
113113

114-
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName
114+
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName
115115
$pkgDir | Should -BeNullOrEmpty
116116
$Error.Count | Should -BeGreaterThan 0
117-
$Error[0].FullyQualifiedErrorId | Should -Be "IncorrectVersionFormat,Microsoft.PowerShell.PSResourceGet.Cmdlets.SavePSResource"
117+
$Error[0].FullyQualifiedErrorId | Should -Be "IncorrectVersionFormat,Microsoft.PowerShell.PSResourceGet.Cmdlets.SavePSResource"
118118
}
119119

120120
It "Save resource with latest (including prerelease) version given Prerelease parameter" {
121121
Save-PSResource -Name $testModuleName -Prerelease -Repository $PSGalleryName -Path $SaveDir -TrustRepository
122-
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName
122+
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName
123123
$pkgDir | Should -Not -BeNullOrEmpty
124124
$pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName
125125
$pkgDirVersion.Name | Should -Be "5.2.5"
@@ -146,24 +146,24 @@ Describe 'Test HTTP Save-PSResource for V2 Server Protocol' -tags 'CI' {
146146
### the input object is of type string (ie "true").
147147
It "Save PSResourceInfo object piped in for prerelease version object" -Pending {
148148
Find-PSResource -Name $testModuleName -Version "5.2.5-alpha001" -Repository $PSGalleryName | Save-PSResource -Path $SaveDir -TrustRepository -Verbose
149-
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName
149+
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName
150150
$pkgDir | Should -Not -BeNullOrEmpty
151151
(Get-ChildItem -Path $pkgDir.FullName) | Should -HaveCount 1
152152
}
153153

154154
It "Save module as a nupkg" {
155155
Save-PSResource -Name $testModuleName -Version "1.0.0" -Repository $PSGalleryName -Path $SaveDir -AsNupkg -TrustRepository
156-
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "test_module.1.0.0.nupkg"
156+
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ "test_module.1.0.0.nupkg"
157157
$pkgDir | Should -Not -BeNullOrEmpty
158158
}
159159

160160
It "Save module and include XML metadata file" {
161161
Save-PSResource -Name $testModuleName -Version "1.0.0" -Repository $PSGalleryName -Path $SaveDir -IncludeXml -TrustRepository
162-
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $testModuleName
162+
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName
163163
$pkgDir | Should -Not -BeNullOrEmpty
164164
$pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName
165165
$pkgDirVersion.Name | Should -Be "1.0.0.0"
166-
$xmlFile = Get-ChildItem -Path $pkgDirVersion.FullName | Where-Object Name -eq "PSGetModuleInfo.xml"
166+
$xmlFile = Get-ChildItem -Path $pkgDirVersion.FullName | Where-Object Name -EQ "PSGetModuleInfo.xml"
167167
$xmlFile | Should -Not -BeNullOrEmpty
168168
}
169169

@@ -184,8 +184,8 @@ Describe 'Test HTTP Save-PSResource for V2 Server Protocol' -tags 'CI' {
184184
Save-PSResource -Name $testScriptName -Repository $PSGalleryName -Path $SaveDir -TrustRepository -IncludeXml
185185

186186
$scriptXML = $testScriptName + "_InstalledScriptInfo.xml"
187-
$savedScriptFile = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "test_script.ps1"
188-
$savedScriptXML = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $scriptXML
187+
$savedScriptFile = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ "test_script.ps1"
188+
$savedScriptXML = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $scriptXML
189189
$savedScriptFile | Should -Not -BeNullOrEmpty
190190
(Get-ChildItem $savedScriptFile.FullName) | Should -HaveCount 1
191191
$savedScriptXML | Should -Not -BeNullOrEmpty
@@ -199,4 +199,13 @@ Describe 'Test HTTP Save-PSResource for V2 Server Protocol' -tags 'CI' {
199199
$err.Count | Should -BeGreaterThan 0
200200
$err[0].FullyQualifiedErrorId | Should -BeExactly "InstallPackageFailure,Microsoft.PowerShell.PSResourceGet.Cmdlets.SavePSResource"
201201
}
202+
203+
# Save resource that requires license
204+
It "Save resource that requires accept license with -AcceptLicense flag" {
205+
Save-PSResource -Repository $TestGalleryName -TrustRepository -Path $SaveDir `
206+
-Name $testModuleName2 -AcceptLicense
207+
$pkg = Get-InstalledPSResource -Path $SaveDir -Name $testModuleName2
208+
$pkg.Name | Should -Be $testModuleName2
209+
$pkg.Version | Should -Be "0.0.1.0"
210+
}
202211
}

0 commit comments

Comments
 (0)