diff --git a/CHANGELOG.md b/CHANGELOG.md index 79612b524f..7912cc5d07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/libexec/scoop-alias.ps1 b/libexec/scoop-alias.ps1 index d60884c351..48a956fd64 100644 --- a/libexec/scoop-alias.ps1 +++ b/libexec/scoop-alias.ps1 @@ -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", @@ -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." } } diff --git a/test/Scoop-Alias.Tests.ps1 b/test/Scoop-Alias.Tests.ps1 index bd365718a8..e6fd33e7eb 100644 --- a/test/Scoop-Alias.Tests.ps1 +++ b/test/Scoop-Alias.Tests.ps1 @@ -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 @@ -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'..." } } } diff --git a/test/Scoop-Versions.Tests.ps1 b/test/Scoop-Versions.Tests.ps1 index 536f9cc7c3..7043ee07cf 100644 --- a/test/Scoop-Versions.Tests.ps1 +++ b/test/Scoop-Versions.Tests.ps1 @@ -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