Skip to content

Commit 895cc6f

Browse files
authored
Respect TrustRepository parameter in RequiredResource hashtable (#1910)
1 parent 60b96d9 commit 895cc6f

File tree

4 files changed

+76
-28
lines changed

4 files changed

+76
-28
lines changed

src/code/InstallPSResource.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,9 @@ private void RequiredResourceHelper(Hashtable reqResourceHash)
487487
}
488488
}
489489

490-
if (pkgParams.Scope == ScopeType.AllUsers)
490+
if (pkgParams.Scope.HasValue && pkgParams.Scope.Value == ScopeType.AllUsers)
491491
{
492-
_pathsToInstallPkg = Utils.GetAllInstallationPaths(this, pkgParams.Scope);
492+
_pathsToInstallPkg = Utils.GetAllInstallationPaths(this, pkgParams.Scope.Value);
493493
}
494494

495495
pkgVersion = pkgInstallInfo["version"] == null ? String.Empty : pkgInstallInfo["version"].ToString();
@@ -565,6 +565,16 @@ private void ProcessInstallHelper(string[] pkgNames, string pkgVersion, bool pkg
565565
this));
566566
}
567567

568+
// When reqResourceParams is provided (via -RequiredResource), use its properties
569+
// instead of the cmdlet-level parameters. Only use the property if it was explicitly set (not null).
570+
bool acceptLicense = reqResourceParams?.AcceptLicense ?? AcceptLicense;
571+
bool quiet = reqResourceParams?.Quiet ?? Quiet;
572+
bool reinstall = reqResourceParams?.Reinstall ?? Reinstall;
573+
bool trustRepository = reqResourceParams?.TrustRepository ?? TrustRepository;
574+
bool noClobber = reqResourceParams?.NoClobber ?? NoClobber;
575+
bool skipDependencyCheck = reqResourceParams?.SkipDependencyCheck ?? SkipDependencyCheck;
576+
ScopeType scope = reqResourceParams?.Scope ?? Scope;
577+
568578
IEnumerable<PSResourceInfo> installedPkgs = _installHelper.BeginInstallPackages(
569579
names: pkgNames,
570580
versionRange: versionRange,
@@ -573,19 +583,19 @@ private void ProcessInstallHelper(string[] pkgNames, string pkgVersion, bool pkg
573583
versionString: Version,
574584
prerelease: pkgPrerelease,
575585
repository: pkgRepository,
576-
acceptLicense: AcceptLicense,
577-
quiet: Quiet,
578-
reinstall: Reinstall,
586+
acceptLicense: acceptLicense,
587+
quiet: quiet,
588+
reinstall: reinstall,
579589
force: false,
580-
trustRepository: TrustRepository,
581-
noClobber: NoClobber,
590+
trustRepository: trustRepository,
591+
noClobber: noClobber,
582592
asNupkg: false,
583593
includeXml: true,
584-
skipDependencyCheck: SkipDependencyCheck,
594+
skipDependencyCheck: skipDependencyCheck,
585595
authenticodeCheck: AuthenticodeCheck,
586596
savePkg: false,
587597
pathsToInstallPkg: _pathsToInstallPkg,
588-
scope: Scope,
598+
scope: scope,
589599
tmpPath: _tmpPath,
590600
pkgsInstalled: _packagesOnMachine);
591601

src/code/InstallPkgParams.cs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ public class InstallPkgParams
1111
public string Name { get; set; }
1212
public VersionRange Version { get; set; }
1313
public string Repository { get; set; }
14-
public bool AcceptLicense { get; set; }
14+
public bool? AcceptLicense { get; set; }
1515
public bool Prerelease { get; set; }
16-
public ScopeType Scope { get; set; }
17-
public bool Quiet { get; set; }
18-
public bool Reinstall { get; set; }
16+
public ScopeType? Scope { get; set; }
17+
public bool? Quiet { get; set; }
18+
public bool? Reinstall { get; set; }
1919
public bool Force { get; set; }
20-
public bool TrustRepository { get; set; }
21-
public bool NoClobber { get; set; }
22-
public bool SkipDependencyCheck { get; set; }
20+
public bool? TrustRepository { get; set; }
21+
public bool? NoClobber { get; set; }
22+
public bool? SkipDependencyCheck { get; set; }
2323

2424

2525

@@ -67,8 +67,10 @@ public void SetProperty(string propertyName, string propertyValue, out ErrorReco
6767
break;
6868

6969
case "acceptlicense":
70-
bool.TryParse(propertyValue, out bool acceptLicenseTmp);
71-
AcceptLicense = acceptLicenseTmp;
70+
if (!string.IsNullOrWhiteSpace(propertyValue) && bool.TryParse(propertyValue, out bool acceptLicenseTmp))
71+
{
72+
AcceptLicense = acceptLicenseTmp;
73+
}
7274
break;
7375

7476
case "prerelease":
@@ -82,28 +84,38 @@ public void SetProperty(string propertyName, string propertyValue, out ErrorReco
8284
break;
8385

8486
case "quiet":
85-
bool.TryParse(propertyValue, out bool quietTmp);
86-
Quiet = quietTmp;
87+
if (!string.IsNullOrWhiteSpace(propertyValue) && bool.TryParse(propertyValue, out bool quietTmp))
88+
{
89+
Quiet = quietTmp;
90+
}
8791
break;
8892

8993
case "reinstall":
90-
bool.TryParse(propertyValue, out bool reinstallTmp);
91-
Reinstall = reinstallTmp;
94+
if (!string.IsNullOrWhiteSpace(propertyValue) && bool.TryParse(propertyValue, out bool reinstallTmp))
95+
{
96+
Reinstall = reinstallTmp;
97+
}
9298
break;
9399

94100
case "trustrepository":
95-
bool.TryParse(propertyValue, out bool trustRepositoryTmp);
96-
TrustRepository = trustRepositoryTmp;
101+
if (!string.IsNullOrWhiteSpace(propertyValue) && bool.TryParse(propertyValue, out bool trustRepositoryTmp))
102+
{
103+
TrustRepository = trustRepositoryTmp;
104+
}
97105
break;
98106

99107
case "noclobber":
100-
bool.TryParse(propertyValue, out bool noClobberTmp);
101-
NoClobber = noClobberTmp;
108+
if (!string.IsNullOrWhiteSpace(propertyValue) && bool.TryParse(propertyValue, out bool noClobberTmp))
109+
{
110+
NoClobber = noClobberTmp;
111+
}
102112
break;
103113

104114
case "skipdependencycheck":
105-
bool.TryParse(propertyValue, out bool skipDependencyCheckTmp);
106-
SkipDependencyCheck = skipDependencyCheckTmp;
115+
if (!string.IsNullOrWhiteSpace(propertyValue) && bool.TryParse(propertyValue, out bool skipDependencyCheckTmp))
116+
{
117+
SkipDependencyCheck = skipDependencyCheckTmp;
118+
}
107119
break;
108120

109121
default:

test/InstallPSResourceTests/InstallPSResourceV2Server.Tests.ps1

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,19 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' {
429429
(Get-InstalledPSResource -Name 'TestModule99').'Prerelease' | Should -Be 'beta2'
430430
}
431431

432+
It "Install module using -RequiredResource with TrustRepository in hashtable" {
433+
# This test verifies that TrustRepository specified in -RequiredResource hashtable is respected
434+
Install-PSResource -RequiredResource @{
435+
'TestModule99' = @{
436+
'repository' = 'PSGallery'
437+
'trustrepository' = 'true'
438+
}
439+
}
440+
$res = Get-InstalledPSResource -Name 'TestModule99'
441+
$res.Name | Should -Be 'TestModule99'
442+
$res.Version | Should -Be '0.0.93'
443+
}
444+
432445
It "Install modules using -RequiredResource with JSON string" {
433446
$rrJSON = "{
434447
'test_module': {

test/InstallPSResourceTests/InstallPSResourceV3Server.Tests.ps1

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,19 @@ Describe 'Test Install-PSResource for V3Server scenarios' -tags 'CI' {
310310
$res3.Version | Should -Be '0.0.93'
311311
}
312312

313+
It 'Install module using -RequiredResource with TrustRepository in hashtable' {
314+
# This test verifies that TrustRepository specified in -RequiredResource hashtable is respected
315+
Install-PSResource -RequiredResource @{
316+
'TestModule99' = @{
317+
'repository' = $NuGetGalleryName
318+
'trustrepository' = 'true'
319+
}
320+
}
321+
$res = Get-InstalledPSResource -Name 'TestModule99'
322+
$res.Name | Should -Be 'TestModule99'
323+
$res.Version | Should -Be '0.0.93'
324+
}
325+
313326
It 'Install modules using -RequiredResource with JSON string' {
314327
$rrJSON = "{
315328
'test_module': {

0 commit comments

Comments
 (0)