Skip to content

Install should implement ShouldProcess #425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 29, 2021
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
9 changes: 8 additions & 1 deletion src/code/InstallPSResource.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Specialized;
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
Expand All @@ -15,7 +16,7 @@ namespace Microsoft.PowerShell.PowerShellGet.Cmdlets
/// It returns nothing.
/// </summary>

[Cmdlet(VerbsLifecycle.Install, "PSResource", DefaultParameterSetName = "NameParameterSet", SupportsShouldProcess = true, HelpUri = "<add>")]
[Cmdlet(VerbsLifecycle.Install, "PSResource", DefaultParameterSetName = "NameParameterSet", SupportsShouldProcess = true)]
public sealed
class InstallPSResource : PSCmdlet
{
Expand Down Expand Up @@ -124,6 +125,12 @@ protected override void BeginProcessing()

protected override void ProcessRecord()
{
if (!ShouldProcess(string.Format("package to install: '{0}'", String.Join(", ", Name))))
{
WriteVerbose(string.Format("Install operation cancelled by user for packages: {0}", String.Join(", ", Name)));
return;
}

var installHelper = new InstallHelper(updatePkg: false, savePkg: false, cmdletPassedIn: this);

switch (ParameterSetName)
Expand Down
7 changes: 7 additions & 0 deletions test/InstallPSResource.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down