From df9795c3a12c26b1a057975aa5426f3103cf3aa0 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Wed, 28 Jul 2021 13:16:16 -0400 Subject: [PATCH 1/3] Install should impelement ShouldProcess --- src/code/InstallPSResource.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/code/InstallPSResource.cs b/src/code/InstallPSResource.cs index 51a7a3fc8..39e159bc6 100644 --- a/src/code/InstallPSResource.cs +++ b/src/code/InstallPSResource.cs @@ -15,7 +15,7 @@ namespace Microsoft.PowerShell.PowerShellGet.Cmdlets /// It returns nothing. /// - [Cmdlet(VerbsLifecycle.Install, "PSResource", DefaultParameterSetName = "NameParameterSet", SupportsShouldProcess = true, HelpUri = "")] + [Cmdlet(VerbsLifecycle.Install, "PSResource", DefaultParameterSetName = "NameParameterSet", SupportsShouldProcess = true)] public sealed class InstallPSResource : PSCmdlet { @@ -124,6 +124,12 @@ protected override void BeginProcessing() protected override void ProcessRecord() { + if (!ShouldProcess(string.Format("package to install: '{0}'", String.Join(", ", Name)))) + { + WriteVerbose("ShouldProcess was set to false."); + return; + } + var installHelper = new InstallHelper(updatePkg: false, savePkg: false, cmdletPassedIn: this); switch (ParameterSetName) From 188e61b963c8d4eb8d4319f2afeaead181a53e58 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Thu, 29 Jul 2021 10:29:27 -0400 Subject: [PATCH 2/3] add whatif test to install --- test/InstallPSResource.Tests.ps1 | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/InstallPSResource.Tests.ps1 b/test/InstallPSResource.Tests.ps1 index 33bc966af..7ea30c743 100644 --- a/test/InstallPSResource.Tests.ps1 +++ b/test/InstallPSResource.Tests.ps1 @@ -226,6 +226,13 @@ Describe 'Test Install-PSResource for Module' { $pkg | Should -Not -BeNullOrEmpty $pkg.Name | Should -Be $publishModuleName } + + It "Install module using -WhatIf, should not install the module" { + Install-PSResource -Name "TestModule" -WhatIf + + $res = Get-Module "TestModule" -ListAvailable + $res | Should -BeNullOrEmpty + } } <# Temporarily commented until -Tag is implemented for this Describe block From fd7fe7471d61b46aae83fb2f55f54a85f8102822 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Thu, 29 Jul 2021 15:03:23 -0400 Subject: [PATCH 3/3] change verbose message if ShouldProcess is false --- src/code/InstallPSResource.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/code/InstallPSResource.cs b/src/code/InstallPSResource.cs index 39e159bc6..dee8e3e4e 100644 --- a/src/code/InstallPSResource.cs +++ b/src/code/InstallPSResource.cs @@ -1,3 +1,4 @@ +using System.Collections.Specialized; // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. using System; @@ -126,7 +127,7 @@ protected override void ProcessRecord() { if (!ShouldProcess(string.Format("package to install: '{0}'", String.Join(", ", Name)))) { - WriteVerbose("ShouldProcess was set to false."); + WriteVerbose(string.Format("Install operation cancelled by user for packages: {0}", String.Join(", ", Name))); return; }