Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/code/InstallPSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,9 @@ private void RequiredResourceHelper(Hashtable reqResourceHash)
}
}

if (pkgParams.Scope == ScopeType.AllUsers)
if (pkgParams.Scope.HasValue && pkgParams.Scope.Value == ScopeType.AllUsers)
{
_pathsToInstallPkg = Utils.GetAllInstallationPaths(this, pkgParams.Scope);
_pathsToInstallPkg = Utils.GetAllInstallationPaths(this, pkgParams.Scope.Value);
}

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

// When reqResourceParams is provided (via -RequiredResource), use its properties
// instead of the cmdlet-level parameters. Only use the property if it was explicitly set (not null).
bool acceptLicense = reqResourceParams?.AcceptLicense ?? AcceptLicense;
bool quiet = reqResourceParams?.Quiet ?? Quiet;
bool reinstall = reqResourceParams?.Reinstall ?? Reinstall;
bool trustRepository = reqResourceParams?.TrustRepository ?? TrustRepository;
bool noClobber = reqResourceParams?.NoClobber ?? NoClobber;
bool skipDependencyCheck = reqResourceParams?.SkipDependencyCheck ?? SkipDependencyCheck;
ScopeType scope = reqResourceParams?.Scope ?? Scope;

IEnumerable<PSResourceInfo> installedPkgs = _installHelper.BeginInstallPackages(
names: pkgNames,
versionRange: versionRange,
Expand All @@ -573,19 +583,19 @@ private void ProcessInstallHelper(string[] pkgNames, string pkgVersion, bool pkg
versionString: Version,
prerelease: pkgPrerelease,
repository: pkgRepository,
acceptLicense: AcceptLicense,
quiet: Quiet,
reinstall: Reinstall,
acceptLicense: acceptLicense,
quiet: quiet,
reinstall: reinstall,
force: false,
trustRepository: TrustRepository,
noClobber: NoClobber,
trustRepository: trustRepository,
noClobber: noClobber,
asNupkg: false,
includeXml: true,
skipDependencyCheck: SkipDependencyCheck,
skipDependencyCheck: skipDependencyCheck,
authenticodeCheck: AuthenticodeCheck,
savePkg: false,
pathsToInstallPkg: _pathsToInstallPkg,
scope: Scope,
scope: scope,
tmpPath: _tmpPath,
pkgsInstalled: _packagesOnMachine);

Expand Down
50 changes: 31 additions & 19 deletions src/code/InstallPkgParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ public class InstallPkgParams
public string Name { get; set; }
public VersionRange Version { get; set; }
public string Repository { get; set; }
public bool AcceptLicense { get; set; }
public bool? AcceptLicense { get; set; }
public bool Prerelease { get; set; }
public ScopeType Scope { get; set; }
public bool Quiet { get; set; }
public bool Reinstall { get; set; }
public ScopeType? Scope { get; set; }
public bool? Quiet { get; set; }
public bool? Reinstall { get; set; }
public bool Force { get; set; }
public bool TrustRepository { get; set; }
public bool NoClobber { get; set; }
public bool SkipDependencyCheck { get; set; }
public bool? TrustRepository { get; set; }
public bool? NoClobber { get; set; }
public bool? SkipDependencyCheck { get; set; }



Expand Down Expand Up @@ -67,8 +67,10 @@ public void SetProperty(string propertyName, string propertyValue, out ErrorReco
break;

case "acceptlicense":
bool.TryParse(propertyValue, out bool acceptLicenseTmp);
AcceptLicense = acceptLicenseTmp;
if (!string.IsNullOrWhiteSpace(propertyValue) && bool.TryParse(propertyValue, out bool acceptLicenseTmp))
{
AcceptLicense = acceptLicenseTmp;
}
break;

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

case "quiet":
bool.TryParse(propertyValue, out bool quietTmp);
Quiet = quietTmp;
if (!string.IsNullOrWhiteSpace(propertyValue) && bool.TryParse(propertyValue, out bool quietTmp))
{
Quiet = quietTmp;
}
break;

case "reinstall":
bool.TryParse(propertyValue, out bool reinstallTmp);
Reinstall = reinstallTmp;
if (!string.IsNullOrWhiteSpace(propertyValue) && bool.TryParse(propertyValue, out bool reinstallTmp))
{
Reinstall = reinstallTmp;
}
break;

case "trustrepository":
bool.TryParse(propertyValue, out bool trustRepositoryTmp);
TrustRepository = trustRepositoryTmp;
if (!string.IsNullOrWhiteSpace(propertyValue) && bool.TryParse(propertyValue, out bool trustRepositoryTmp))
{
TrustRepository = trustRepositoryTmp;
}
break;

case "noclobber":
bool.TryParse(propertyValue, out bool noClobberTmp);
NoClobber = noClobberTmp;
if (!string.IsNullOrWhiteSpace(propertyValue) && bool.TryParse(propertyValue, out bool noClobberTmp))
{
NoClobber = noClobberTmp;
}
break;

case "skipdependencycheck":
bool.TryParse(propertyValue, out bool skipDependencyCheckTmp);
SkipDependencyCheck = skipDependencyCheckTmp;
if (!string.IsNullOrWhiteSpace(propertyValue) && bool.TryParse(propertyValue, out bool skipDependencyCheckTmp))
{
SkipDependencyCheck = skipDependencyCheckTmp;
}
break;

default:
Expand Down
13 changes: 13 additions & 0 deletions test/InstallPSResourceTests/InstallPSResourceV2Server.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,19 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' {
(Get-InstalledPSResource -Name 'TestModule99').'Prerelease' | Should -Be 'beta2'
}

It "Install module using -RequiredResource with TrustRepository in hashtable" {
# This test verifies that TrustRepository specified in -RequiredResource hashtable is respected
Install-PSResource -RequiredResource @{
'TestModule99' = @{
'repository' = 'PSGallery'
'trustrepository' = 'true'
}
}
$res = Get-InstalledPSResource -Name 'TestModule99'
$res.Name | Should -Be 'TestModule99'
$res.Version | Should -Be '0.0.93'
}

It "Install modules using -RequiredResource with JSON string" {
$rrJSON = "{
'test_module': {
Expand Down
13 changes: 13 additions & 0 deletions test/InstallPSResourceTests/InstallPSResourceV3Server.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,19 @@ Describe 'Test Install-PSResource for V3Server scenarios' -tags 'CI' {
$res3.Version | Should -Be '0.0.93'
}

It 'Install module using -RequiredResource with TrustRepository in hashtable' {
# This test verifies that TrustRepository specified in -RequiredResource hashtable is respected
Install-PSResource -RequiredResource @{
'TestModule99' = @{
'repository' = $NuGetGalleryName
'trustrepository' = 'true'
}
}
$res = Get-InstalledPSResource -Name 'TestModule99'
$res.Name | Should -Be 'TestModule99'
$res.Version | Should -Be '0.0.93'
}

It 'Install modules using -RequiredResource with JSON string' {
$rrJSON = "{
'test_module': {
Expand Down
Loading