Skip to content

Commit

Permalink
fix(scoop-alias): Prevent overwrite existing file when adding alias (#…
Browse files Browse the repository at this point in the history
…5577)

Co-authored-by: Hsiao-nan Cheung <niheaven@gmail.com>
  • Loading branch information
HUMORCE and niheaven committed Apr 8, 2024
1 parent b008fe5 commit 81e7dec
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
- **update/uninstall:** Remove items from PATH correctly ([#5833](https://github.com/ScoopInstaller/Scoop/issues/5833))
- **shim:** Allow GUI applications to attach to the shell's console when launched using the GUI shim ([#5721](https://github.com/ScoopInstaller/Scoop/issues/5721))
- **core:** Fix arguments parsing method of `Invoke-ExternalCommand()` ([#5839](https://github.com/ScoopInstaller/Scoop/issues/5839))
- **scoop-alias:** Prevent overwrite existing file when adding alias ([#5577](https://github.com/ScoopInstaller/Scoop/issues/5577))
- **scoop-virustotal:** Fix the issue that escape character not available in PowerShell 5.1 ([#5870](https://github.com/ScoopInstaller/Scoop/issues/5870))

### Performance Improvements
Expand Down
11 changes: 7 additions & 4 deletions libexec/scoop-alias.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ function add_alias($name, $command) {
# get current aliases from config
$aliases = init_alias_config
if ($aliases.$name) {
abort "Alias $name already exists."
abort "Alias '$name' already exists."
}

$alias_file = "scoop-$name"

# generate script
$shimdir = shimdir $false
if (Test-Path "$shimdir\$alias_file.ps1") {
abort "File '$alias_file.ps1' already exists in shims directory."
}
$script =
@(
"# Summary: $description",
Expand All @@ -67,18 +70,18 @@ function add_alias($name, $command) {
function rm_alias($name) {
$aliases = init_alias_config
if (!$name) {
abort 'Which alias should be removed?'
abort 'Alias to be removed has not been specified!'
}

if ($aliases.$name) {
"Removing alias $name..."
info "Removing alias '$name'..."

rm_shim $aliases.$name (shimdir $false)

$aliases.PSObject.Properties.Remove($name)
set_config $script:config_alias $aliases | Out-Null
} else {
abort "Alias $name doesn't exist."
abort "Alias '$name' doesn't exist."
}
}

Expand Down
13 changes: 7 additions & 6 deletions test/Scoop-Alias.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ BeforeAll {
Describe 'Manipulate Alias' -Tag 'Scoop' {
BeforeAll {
Mock shimdir { "$TestDrive\shims" }
Mock set_config { }
Mock set_config {}
Mock get_config { @{} }

$shimdir = shimdir
Expand All @@ -23,23 +23,24 @@ Describe 'Manipulate Alias' -Tag 'Scoop' {
& $alias_file | Should -Be 'hello, world!'
}

It 'Does not change existing alias if alias exists' {
It 'Does not change existing file if its filename same as alias name' {
$alias_file = "$shimdir\scoop-rm.ps1"
Mock abort {}
New-Item $alias_file -Type File -Force
$alias_file | Should -Exist

add_alias 'rm' 'test'
& $alias_file | Should -Not -Be 'test'
add_alias 'rm' '"test"'
Should -Invoke -CommandName abort -Times 1 -ParameterFilter { $msg -eq "File 'scoop-rm.ps1' already exists in shims directory." }
}

It 'Removes an existing alias' {
$alias_file = "$shimdir\scoop-rm.ps1"
add_alias 'rm' '"hello, world!"'

$alias_file | Should -Exist
Mock get_config { @(@{'rm' = 'scoop-rm' }) }
Mock info {}

rm_alias 'rm'
$alias_file | Should -Not -Exist
Should -Invoke -CommandName info -Times 1 -ParameterFilter { $msg -eq "Removing alias 'rm'..." }
}
}
2 changes: 1 addition & 1 deletion test/Scoop-Versions.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Describe 'versions comparison' -Tag 'Scoop' {
Compare-Version 'nightly-20190801' 'nightly-20200801' | Should -Be 0
}

It 'handles nightly versions with `update_nightly`' {
It "handles nightly versions with 'update_nightly'" {
function get_config { $true }
Mock Get-Date { '20200801' }
Compare-Version 'nightly-20200801' 'nightly' | Should -Be 0
Expand Down

0 comments on commit 81e7dec

Please sign in to comment.