-
Notifications
You must be signed in to change notification settings - Fork 7.1k
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
Add Remove-Alias Command #5143
Conversation
02d9721
to
4a2a5a7
Compare
There was a problem hiding this 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. | |||
--********************************************************************/ |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 = "")] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)] |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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' | ||
} |
There was a problem hiding this comment.
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"{ |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
I don't think adding |
I'm good with it! |
@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.
@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? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add "Remove-Alias"
#region Parameters | ||
|
||
/// <summary> | ||
/// The Name parameter for the command. |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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> | ||
/// |
There was a problem hiding this comment.
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.
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? |
There was a problem hiding this 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"{ | ||
{ |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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'
}
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
It is ok for new files. |
…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.
@adityapatwardhan @anmenaga Please review the PR. |
@PowershellNinja restarted the macOS build for Travis-CI. |
/// </summary> | ||
/// | ||
[Cmdlet(VerbsCommon.Remove, "Alias", DefaultParameterSetName = "Default", HelpUri = "")] | ||
public class RemoveAliasCommand : PSCmdlet |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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; } |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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" | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
…e on the Remove-Alias Cmdlet.
@PowershellNinja Please resolve merge conflict. |
@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"? |
@PowershellNinja I did this. If you plan work with Git - please learn topics about resolving conflicts in Git. |
@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? |
@adityapatwardhan Can we merge without waiting CI MacOS? |
@PowershellNinja Tests show that all works well. |
@iSazonov Ah I see, that may explain it. I retested with only one alias and all worked as expected. |
@PowershellNinja I restarted the macOS build and it passes now. |
There was a problem hiding this 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" { |
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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"{ |
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
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"{ |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
@PowershellNinja Please address the comment about tests and I'll merge. |
@iSazonov @JamesWTruher I implemented the suggestion about the tests in the latest commit |
@PowershellNinja Many thanks for your contribution! Welcome back with new PRs! |
@iSazonov Awesome! Thanks all reviewers for your feedback and suggestions! I am looking forward to working on Powershell again in the future!
Von meinem iPhone gesendet
Am 03.11.2017 um 09:09 schrieb Ilya <notifications@github.com<mailto:notifications@github.com>>:
@PowershellNinja<https://github.com/powershellninja> Many thanks for your contribution! Welcome back with new PRs!
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#5143 (comment)>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AMGT2FQ-OwxyeUqb3SQeSrT9oivob6Xzks5syspAgaJpZM4P8bNW>.
|
There was a problem hiding this 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
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. |
Do we expect the behavior? gal E* |Select Name |Remove-aLias -Force |
@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* |
We can easily fix this with try-catch and writing non-terminating error. |
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