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

fix(scoop-alias): Prevent overwrite existing file when adding alias #5577

Merged
merged 10 commits into from
Apr 8, 2024
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))

### 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."
rashil2000 marked this conversation as resolved.
Show resolved Hide resolved
}
$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." }
}
niheaven marked this conversation as resolved.
Show resolved Hide resolved

It 'Removes an existing alias' {
$alias_file = "$shimdir\scoop-rm.ps1"
add_alias 'rm' '"hello, world!"'
niheaven marked this conversation as resolved.
Show resolved Hide resolved

$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