Skip to content
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

Add Remove-Alias Command #5143

Merged
merged 11 commits into from
Nov 3, 2017
Merged

Add Remove-Alias Command #5143

merged 11 commits into from
Nov 3, 2017

Conversation

PowershellNinja
Copy link
Contributor

Hi Powershell Team!

My boss visited a particular Powershell session at MS Ignite 2017 where a particular person said he would like a Pull Request containing the Remove-Alias Cmdlet.

We thought this would be a good point to start contributing to Powershell!

The PR contains the Remove-Alias Cmdlet and some tests for it (and a reference in the .psd1).
As this is my first contribution to Powershell I am looking forward to all thoughts, comments and suggestions, please tell me if there is anything I need to improve.

Regards,
Raphael

Copy link
Collaborator

@iSazonov iSazonov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PowershellNinja Thanks for your contribution!

Alternative is Remove-Item alias:\AliasName

@SteveL-MSFT Can we approve the new cmdlet?

@@ -0,0 +1,117 @@
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we should remove the copyright.

namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// The implementation of the "remove-alias" cmdlet
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use right case and dot:

/// The implementation of the "Remove-Alias" cmdlet.

/// The implementation of the "remove-alias" cmdlet
/// </summary>
///
[Cmdlet(VerbsCommon.Remove, "Alias", DefaultParameterSetName = "Default", HelpUri = "")]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daxian-dbw @HemantMahawar @SteveL-MSFT We need new HelpUri.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue opened: #15625

/// The Name parameter for the command
/// </summary>
///
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add ValueFromPipeline too.

#region Parameters

/// <summary>
/// The Name parameter for the command
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add dot.

#endregion Command code

} // class GetAliasCommand
}//Microsoft.PowerShell.Commands
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting.
I'd remove such comments at all - VS manipulates blocks very well.


#Store ErrorActionPreference Value
$storedEA = $ErrorActionPreference
$ErrorActionPreference = "Stop"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove and use -ErrorAction Stop` explicitly in a cmdlet.

}
catch{
$_.FullyQualifiedErrorId | Should Be 'ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand'
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the pattern:

   { ... } | ShouldBeErrorId 'ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand' 

Use -ErrorAction Stop.

Below too.

$ErrorActionPreference = "Stop"
Describe "Remove-Alias" -Tags "CI" {

It "Remove-Alias Should Remove Non-ReadOnly Alias"{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space skipped.
And please use normal case for normal words:
"Remove-Alias should remove a non-readonly alias" {

Below too.

}
}
#Reset ErrorActionPreference to old Value
$ErrorActionPreference = $storedEA
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove $ErrorActionPreference - see my comment above.
Please add new line at EOF.

@SteveL-MSFT SteveL-MSFT added the Review - Committee The PR/Issue needs a review from the PowerShell Committee label Oct 18, 2017
@SteveL-MSFT
Copy link
Member

I don't think adding Remove-Alias should be an issue, but I'll bring it up today at the committee meeting.

@SteveL-MSFT SteveL-MSFT removed the Review - Committee The PR/Issue needs a review from the PowerShell Committee label Oct 18, 2017
@joeyaiello
Copy link
Contributor

I'm good with it!

@SteveL-MSFT SteveL-MSFT added the Committee-Reviewed PS-Committee has reviewed this and made a decision label Oct 18, 2017
@SteveL-MSFT
Copy link
Member

@PowerShell/powershell-committee no concerns from PS-Committee on the addition of this cmdlet. Thanks for the contribution!

…rence variable. Remove unnecessary Get-Alias calls. Switch Force parameter to auto property. Add ValueFromPipeline to Remove-Alias Name parameter.
@PowershellNinja
Copy link
Contributor Author

@iSazonov Wow, thanks for all your feedback! I tried to incorporate it all in the last commit. Is there anything else I can / should do?

Copy link
Collaborator

@iSazonov iSazonov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#region Parameters

/// <summary>
/// The Name parameter for the command.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe "The alias name to remove."

existingAlias = SessionState.Internal.GetAliasAtScope(Name, Scope);
}
}
catch (SessionStateException sessionStateException)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exception is already our PowerShell exception so please remove the try-catch and WriteError.

try{
SessionState.Internal.RemoveAlias(Name, Force);
}
catch (SessionStateException sessionStateException)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same please remove the try-catch.

else
{
ItemNotFoundException notAliasFound = new ItemNotFoundException(StringUtil.Format(AliasCommandStrings.NoAliasFound, "name", Name));
ErrorRecord error = new ErrorRecord(notAliasFound, "ItemNotFoundException",ErrorCategory.ObjectNotFound, Name);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add WriteError(error);
And please add new test for the case.

/// <summary>
/// The Name parameter for the command.
/// </summary>
///
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the extra line in all file.

…-Alias to approved Commands test. Remove Try/Catch for SessionstateException. Add WriteError for aliasnotfound exception.
@PowershellNinja
Copy link
Contributor Author

Thanks for the additional suggestions, implemented them all. Are the empty lines between the tests and between the #region and parameter statements ok or should I remove them too?

Copy link
Collaborator

@iSazonov iSazonov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PowershellNinja Thanks for your contribution! We are near to merge.

}

It "Remove-Alias should throw if alias does not exist"{
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should protect themselves and add Get-Alias -Name "foo" | Should BeNullorEmpty before trying to remove.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean as a separate tests before the "Remove-Alias should throw if alias does not exist" test or just like this:

It "Remove-Alias should throw if alias does not exist"{
        {
            Get-Alias -Name "foo" | Should BeNullorEmpty
            Remove-Alias -Name "foo" -ErrorAction Stop            
        } | ShouldBeErrorId 'ItemNotFoundException,Microsoft.PowerShell.Commands.RemoveAliasCommand'
    }

Copy link
Collaborator

@iSazonov iSazonov Oct 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Only with -ErrorAction SilentlyContinue
Get-Alias -Name "foo" -ErrorAction SilentlyContinue | Should BeNullorEmpty

{
Set-Alias -Name "foo" -Value "bar"
Remove-Alias -Name "foo"
Get-Alias -Name "foo" -ErrorAction Stop
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep a consistency and add -ErrorAction Stop to previous two lines - that will exclude false positives.
Please add this in tests below.

@iSazonov
Copy link
Collaborator

Are the empty lines between the tests and between the #region and parameter statements ok or should I remove them too?

It is ok for new files.
Main rule is - we must follow the pattern of the surrounding code to keep readability.

@SteveL-MSFT SteveL-MSFT added the Documentation Needed in this repo Documentation is needed in this repo label Oct 22, 2017
…sts for consistency. Add Should BeNullOrEmpty to "should throw if alias does not exist" test to verify that foo alias really is not there before removing it.
@iSazonov
Copy link
Collaborator

@adityapatwardhan @anmenaga Please review the PR.

@adityapatwardhan
Copy link
Member

@PowershellNinja restarted the macOS build for Travis-CI.

/// </summary>
///
[Cmdlet(VerbsCommon.Remove, "Alias", DefaultParameterSetName = "Default", HelpUri = "")]
public class RemoveAliasCommand : PSCmdlet
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an alias for Remove-Alias at : https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/InitialSessionState.cs#L4656
I recommend ral so we are consistent with gal,sal and nal

@@ -363,6 +363,7 @@ Describe "Verify approved aliases list" -Tags "CI" {
"Cmdlet", "Register-ObjectEvent", , $($FullCLR -or $CoreWindows -or $CoreUnix)
"Cmdlet", "Register-PSSessionConfiguration", , $($FullCLR -or $CoreWindows -or $CoreUnix)
"Cmdlet", "Register-WmiEvent", , $($FullCLR )
"Cmdlet", "Remove-Alias", , $($FullCLR -or $CoreWindows -or $CoreUnix)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly add an entry here for ral

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove $FullCLR.

/// The alias name to remove.
/// </summary>
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
public string Name { get; set; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a string[]. All the Remove* cmdlets that have a Name parameter support having a array for names.

/// </summary>
protected override void ProcessRecord()
{
AliasInfo existingAlias = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic here should be updated to remove multiple aliases when the parameter for Name changes to string[].

Describe "Remove-Alias" -Tags "CI" {
It "Remove-Alias should remove a non-readonly alias"{
{
Set-Alias -Name "foo" -Value "bar" -ErrorAction Stop
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the strings so that they use better names than foo and bar.

Set-Alias -Name "foo" -Value "bar" -ErrorAction Stop
Remove-Alias -Name "foo" -Scope 99999 -ErrorAction Stop
} | ShouldBeErrorId "ArgumentOutOfRange,Microsoft.PowerShell.Commands.RemoveAliasCommand"
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a case for ral

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adityapatwardhan We have a test set for aliases in separate file and shouldn't test aliases here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. Please add tests here: https://github.com/PowerShell/PowerShell/tree/master/test/powershell/Modules/Microsoft.PowerShell.Utility

There are test files for Get-Alias and Set-Alias over there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adityapatwardhan @iSazonov sorry, I saw this conversation just now, I'll move the test with the alias, but I'm not sure I understood where I should put them. Currently I have the test together with the other tests unter https://github.com/PowerShell/PowerShell/tree/master/test/powershell/Modules/Microsoft.PowerShell.Utility in a separate file called "Remove-Alias.Tests.ps1". Should I move the whole file or just the test testing for the ral alias?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PowershellNinja You are right. Changes in DefaultCommands.Tests.ps1 should have you covered to testing alias. Sorry for the confusion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adityapatwardhan absolutely no problem, I just was not sure if I have to put the test elsewhere. I removed it in the recent commit.

…rocess Logic to work with String Array. Added Alias named "ral". Added test case for Alias "ral". Added "ral" Alias to list of approved Aliases. Replaced foo and bar values in all tests with better names.
@iSazonov
Copy link
Collaborator

@PowershellNinja Please resolve merge conflict.

@PowershellNinja
Copy link
Contributor Author

@iSazonov The conflict seems to be in DefaultCommands.Tests.ps1 and is caused by my addition of the Test for the "ral" alias. Do I have to remove it there now that I removed the Alias itself from "\src\System.Management.Automation\engine\InitialSessionState.cs"?
This is also the cause of the CI failing, the "ral" alias does not seem to work by just adding the [Alias()] attribute on the Cmdlet, do I need to add something else there?

@iSazonov
Copy link
Collaborator

@PowershellNinja I did this.

If you plan work with Git - please learn topics about resolving conflicts in Git.
Here we'll always try to help you.

@PowershellNinja
Copy link
Contributor Author

PowershellNinja commented Oct 31, 2017

@iSazonov Sorry, I think I did not clearly formulate that, thanks for taking care of the conflict. My question was not about the merge conflict, but about the fact that when I just have the "ral" alias added using the [Alias()] attribute on the Cmdlet, the alias does not seem to exist/work. Do you have any Idea why this might be?

@iSazonov
Copy link
Collaborator

@adityapatwardhan Can we merge without waiting CI MacOS?

@iSazonov
Copy link
Collaborator

@PowershellNinja Tests show that all works well.
When you tried to add a duplicate alias I believe the session initialization failed - both are in the same method, you got a broken environment.

@PowershellNinja
Copy link
Contributor Author

@iSazonov Ah I see, that may explain it. I retested with only one alias and all worked as expected.

@adityapatwardhan
Copy link
Member

@PowershellNinja I restarted the macOS build and it passes now.

Copy link
Member

@JamesWTruher JamesWTruher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

none of my comments are blocking

@@ -0,0 +1,58 @@
Describe "Remove-Alias" -Tags "CI" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one of the things that could happen on dev boxes is that an alias tral could exist which could cause some test failures. Since it doesn't matter, should this be something more unlikely to have a collision (say a generated guid)?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PowershellNinja Could you please address the request?
Sample:

Describe "Remove-Alias" -Tags "CI" { 
    BeforeAll {
        $testAliasName = (New-Guid).Guid
    }
...

} | ShouldBeErrorId 'ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand'
}

It "Remove-Alias should throw if alias does not exist"{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i assume that this is a non-terminating error - should there be a test for that? Something like:

Remove-Alias -Name "tral" -ErrorVariable RemoveAliasError 2>$null
$RemoveAliasError.FullyQualifiedErrorId | ShouldBe "ItemNotFoundException,Microsoft.PowerShell.Commands.RemoveAliasCommand"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have tons of such "incorrect" tests. I think we should refactor them all in time or after resolving #4781

}
}

It "Remove-Alias should throw on out-of-range scope"{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might be worth adding some tests with explicit scope probes. Say create an alias in scope 3, then remove it without a scope, it should still be in scope 3, etc. I don't think we have many tests which explicitly loot at testing our scopes, so it would be a welcome change

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that CodeCov reports internal void RemoveAlias falls under tests. Formally we don't get increasing code coverage with the change.
Although I found up to 34 test files with "scope" word in the repo the SessionStateAliasAPIs.cs code coverage is only 68%. I've view these tests before, some look like tricky so I didn't venture to make improvements. I think we need to open new tracking Issue if we want have better scope tests.

@iSazonov
Copy link
Collaborator

iSazonov commented Nov 2, 2017

@PowershellNinja Please address the comment about tests and I'll merge.

@PowershellNinja
Copy link
Contributor Author

@iSazonov @JamesWTruher I implemented the suggestion about the tests in the latest commit

@iSazonov iSazonov merged commit c9f83fe into PowerShell:master Nov 3, 2017
@iSazonov
Copy link
Collaborator

iSazonov commented Nov 3, 2017

@PowershellNinja Many thanks for your contribution! Welcome back with new PRs!

@PowershellNinja
Copy link
Contributor Author

PowershellNinja commented Nov 3, 2017 via email

Copy link
Contributor

@jpsnover jpsnover left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to remove the ValueFromPipeline for the NAME param or this won't work properly.
You can see that if you do
PS> gal E* |Select Name
PS> gal E* |Select Name |Remove-aLias

@jpsnover
Copy link
Contributor

jpsnover commented Nov 6, 2017

We should probably have a PSAnalyzer rule which warns people away from having ValueFromPipeline=TRUE on a STRING. This is almost always the wrong thing to do.

@iSazonov
Copy link
Collaborator

iSazonov commented Nov 8, 2017

Do we expect the behavior?

gal E* |Select Name |Remove-aLias -Force

@PowershellNinja
Copy link
Contributor Author

@iSazonov I was able to reproduce this, as long as you use -Force, it removes the Aliases, without -Force it throws at the first removal because "ebp" is Read-Only:

gal E* | Select Name
Name
----
ebp
echo
epal
epcsv
erase
etsn
exsn

gal E* | Select Name | Remove-Alias
Remove-Alias : Alias was not removed because alias ebp is constant or read-only.

gal E* | Select Name | Remove-Alias -Force
gal E* | Select Name
*Nothing Here*

@iSazonov
Copy link
Collaborator

iSazonov commented Nov 8, 2017

We can easily fix this with try-catch and writing non-terminating error.

@joeyaiello joeyaiello removed Documentation Needed in this repo Documentation is needed in this repo labels Oct 15, 2018
@rjmholt rjmholt added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Jul 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Committee-Reviewed PS-Committee has reviewed this and made a decision
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants