diff --git a/src/code/InstallPSResource.cs b/src/code/InstallPSResource.cs index 83943eef9..feca62d50 100644 --- a/src/code/InstallPSResource.cs +++ b/src/code/InstallPSResource.cs @@ -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(); @@ -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 installedPkgs = _installHelper.BeginInstallPackages( names: pkgNames, versionRange: versionRange, @@ -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); diff --git a/src/code/InstallPkgParams.cs b/src/code/InstallPkgParams.cs index 7a1b8e986..19cdd076b 100644 --- a/src/code/InstallPkgParams.cs +++ b/src/code/InstallPkgParams.cs @@ -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; } @@ -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": @@ -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: diff --git a/test/InstallPSResourceTests/InstallPSResourceV2Server.Tests.ps1 b/test/InstallPSResourceTests/InstallPSResourceV2Server.Tests.ps1 index 4285e0479..e269b2628 100644 --- a/test/InstallPSResourceTests/InstallPSResourceV2Server.Tests.ps1 +++ b/test/InstallPSResourceTests/InstallPSResourceV2Server.Tests.ps1 @@ -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': { diff --git a/test/InstallPSResourceTests/InstallPSResourceV3Server.Tests.ps1 b/test/InstallPSResourceTests/InstallPSResourceV3Server.Tests.ps1 index e18b9272d..835e0c0c6 100644 --- a/test/InstallPSResourceTests/InstallPSResourceV3Server.Tests.ps1 +++ b/test/InstallPSResourceTests/InstallPSResourceV3Server.Tests.ps1 @@ -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': {