-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Add Remove-Alias Command #5143
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
Add Remove-Alias Command #5143
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4a2a5a7
Add Remove-Alias Command. Add Remove-Alias Test. Add Remove-Alias to …
PowershellNinja a8f7752
Fix code-formatting. Fix ErrorAction on Remove-Alias instead of prefe…
PowershellNinja 234c436
Remove empty lines. Add Remove-Alias to Unix Module .psd1. Add Remove…
PowershellNinja 79f396d
Add ErrorAction Stop to all Get-Alias and Set-Alias Cmdlets in all te…
PowershellNinja f06f593
Changed Remove-Alias parameter Name to String-Array. Changed Cmdlet P…
PowershellNinja 1ff1986
Remove "ral"-alias test as this is covered in DefaultCommands.Tests.p…
PowershellNinja 7fb1868
Remove $FullCLR in DefaultCommands.Tests.ps1 for Remove-Alias and "ra…
PowershellNinja 88fd895
Add Alias "ral" to Remove-Alias Cmdlet.
PowershellNinja 7ad77ed
Remove "ral" alias as this is handled by using the [Alias()] attribut…
PowershellNinja f1c67a9
Merge branch 'master' into master
iSazonov 33230b6
Replace alias names used in Remove-Alias Cmdlet tests with GUIDs to a…
PowershellNinja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
src/Microsoft.PowerShell.Commands.Utility/commands/utility/RemoveAliasCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| using System; | ||
| using System.Management.Automation; | ||
| using System.Management.Automation.Internal; | ||
|
|
||
| namespace Microsoft.PowerShell.Commands | ||
| { | ||
| /// <summary> | ||
| /// The implementation of the "Remove-Alias" cmdlet. | ||
| /// </summary> | ||
| /// | ||
| [Cmdlet(VerbsCommon.Remove, "Alias", DefaultParameterSetName = "Default", HelpUri = "")] | ||
| [Alias("ral")] | ||
| public class RemoveAliasCommand : PSCmdlet | ||
| { | ||
| #region Parameters | ||
|
|
||
| /// <summary> | ||
| /// The alias name to remove. | ||
| /// </summary> | ||
| [Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)] | ||
| public string[] Name { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// The scope parameter for the command determines | ||
| /// which scope the alias is removed from. | ||
| /// </summary> | ||
| [Parameter] | ||
| public string Scope { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// If set to true and an existing alias of the same name exists | ||
| /// and is ReadOnly, it will still be deleted. | ||
| /// </summary> | ||
| [Parameter] | ||
| public SwitchParameter Force { get; set; } | ||
|
|
||
| #endregion Parameters | ||
|
|
||
| #region Command code | ||
|
|
||
| /// <summary> | ||
| /// The main processing loop of the command. | ||
| /// </summary> | ||
| protected override void ProcessRecord() | ||
| { | ||
| foreach(string aliasName in Name) | ||
| { | ||
| AliasInfo existingAlias = null; | ||
| if (String.IsNullOrEmpty(Scope)) | ||
| { | ||
| existingAlias = SessionState.Internal.GetAlias(aliasName); | ||
| } | ||
| else | ||
| { | ||
| existingAlias = SessionState.Internal.GetAliasAtScope(aliasName, Scope); | ||
| } | ||
|
|
||
| if (existingAlias != null) | ||
| { | ||
| SessionState.Internal.RemoveAlias(aliasName, Force); | ||
| } | ||
| else | ||
| { | ||
| ItemNotFoundException notAliasFound = new ItemNotFoundException(StringUtil.Format(AliasCommandStrings.NoAliasFound, "name", aliasName)); | ||
| ErrorRecord error = new ErrorRecord(notAliasFound, "ItemNotFoundException",ErrorCategory.ObjectNotFound, aliasName); | ||
| WriteError(error); | ||
| } | ||
| } | ||
| } | ||
| #endregion Command code | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
test/powershell/Modules/Microsoft.PowerShell.Utility/Remove-Alias.Tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| Describe "Remove-Alias" -Tags "CI" { | ||
|
|
||
| BeforeAll { | ||
| $testAliasName = (New-Guid).Guid | ||
| } | ||
|
|
||
| It "Remove-Alias should remove a non-readonly alias"{ | ||
| { | ||
| Set-Alias -Name $testAliasName -Value "Remove-Alias" -ErrorAction Stop | ||
| Remove-Alias -Name $testAliasName -ErrorAction Stop | ||
| Get-Alias -Name $testAliasName -ErrorAction Stop | ||
| } | ShouldBeErrorId 'ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand' | ||
| } | ||
|
|
||
| It "Remove-Alias should throw on a readonly alias"{ | ||
| { | ||
| Set-Alias -Name $testAliasName -Value "Remove-Alias" -Option ReadOnly -ErrorAction Stop | ||
| Remove-Alias -Name $testAliasName -ErrorAction Stop | ||
| } | ShouldBeErrorId 'AliasNotRemovable,Microsoft.PowerShell.Commands.RemoveAliasCommand' | ||
| } | ||
|
|
||
| It "Remove-Alias should remove a non-readonly alias with force"{ | ||
| { | ||
| Set-Alias -Name $testAliasName -Value "Remove-Alias" -ErrorAction Stop | ||
| Remove-Alias -Name $testAliasName -Force -ErrorAction Stop | ||
| Get-Alias -Name $testAliasName -ErrorAction Stop | ||
| } | ShouldBeErrorId 'ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand' | ||
| } | ||
|
|
||
| It "Remove-Alias should remove a readonly alias with force"{ | ||
| { | ||
| Set-Alias -Name $testAliasName -Value "Remove-Alias" -Option ReadOnly -ErrorAction Stop | ||
| Remove-Alias -Name $testAliasName -Force -ErrorAction Stop | ||
| Get-Alias -Name $testAliasName -ErrorAction Stop | ||
| } | ShouldBeErrorId 'ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand' | ||
| } | ||
|
|
||
| It "Remove-Alias should throw if alias does not exist"{ | ||
| { | ||
| Get-Alias -Name $testAliasName -ErrorAction SilentlyContinue | Should BeNullorEmpty | ||
| Remove-Alias -Name $testAliasName -ErrorAction Stop | ||
| } | ShouldBeErrorId 'ItemNotFoundException,Microsoft.PowerShell.Commands.RemoveAliasCommand' | ||
| } | ||
|
|
||
| It "Remove-Alias should remove multiple alias at once"{ | ||
| { | ||
| Set-Alias -Name "$testAliasName" -Value "Remove-Alias" -ErrorAction Stop | ||
| Set-Alias -Name "$testAliasName-2" -Value "Remove-Alias" -ErrorAction Stop | ||
| Set-Alias -Name "$testAliasName-3" -Value "Remove-Alias" -ErrorAction Stop | ||
| Remove-Alias -Name "$testAliasName","$testAliasName-2","$testAliasName-3" -ErrorAction Stop | ||
| Get-Alias -Name "$testAliasName" -ErrorAction Stop | ShouldBeErrorId 'ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand' | ||
| Get-Alias -Name "$testAliasName-2" -ErrorAction Stop | ShouldBeErrorId 'ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand' | ||
| Get-Alias -Name "$testAliasName-3" -ErrorAction Stop | ShouldBeErrorId 'ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand' | ||
| } | ||
| } | ||
|
|
||
| It "Remove-Alias should throw on out-of-range scope"{ | ||
| { | ||
| Set-Alias -Name $testAliasName -Value "Remove-Alias" -ErrorAction Stop | ||
| Remove-Alias -Name $testAliasName -Scope 99999 -ErrorAction Stop | ||
| } | ShouldBeErrorId "ArgumentOutOfRange,Microsoft.PowerShell.Commands.RemoveAliasCommand" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
tralcould 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: