From 7a84d78614e00094bab7946cd7ca22201996dde5 Mon Sep 17 00:00:00 2001 From: HUMORCE Date: Tue, 11 Jul 2023 14:38:59 +0000 Subject: [PATCH 1/8] fix(scoop-alias): Prevent overwrite existing file when adding alias --- libexec/scoop-alias.ps1 | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libexec/scoop-alias.ps1 b/libexec/scoop-alias.ps1 index d60884c351..a252c96b70 100644 --- a/libexec/scoop-alias.ps1 +++ b/libexec/scoop-alias.ps1 @@ -51,6 +51,9 @@ function add_alias($name, $command) { # generate script $shimdir = shimdir $false + if (Test-Path "$shimdir\$alias_file.ps1") { + abort "File '$name' already exists in shims directory." + } $script = @( "# Summary: $description", From cb39d8d7413e482daa147876411ecb6e4b702ed4 Mon Sep 17 00:00:00 2001 From: HUMORCE Date: Tue, 11 Jul 2023 14:42:32 +0000 Subject: [PATCH 2/8] fix(test): Fix tests of alias --- test/Scoop-Alias.Tests.ps1 | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/test/Scoop-Alias.Tests.ps1 b/test/Scoop-Alias.Tests.ps1 index bd365718a8..d11b72cca4 100644 --- a/test/Scoop-Alias.Tests.ps1 +++ b/test/Scoop-Alias.Tests.ps1 @@ -23,19 +23,18 @@ Describe 'Manipulate Alias' -Tag 'Scoop' { & $alias_file | Should -Be 'hello, world!' } - It 'Does not change existing alias if alias exists' { - $alias_file = "$shimdir\scoop-rm.ps1" - New-Item $alias_file -Type File -Force - $alias_file | Should -Exist + # NOTE: `abort()` in `add_alias()` cause tests error. + # It 'Does not change existing file if its filename same as alias name' { + # $alias_file = "$shimdir\scoop-rm.ps1" + # 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"' + # & $alias_file | Should -Be $null + # } 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' }) } From 7ea03ed091d3ffa8d68c023c5bc6fc4eecdfa88e Mon Sep 17 00:00:00 2001 From: HUMORCE Date: Tue, 11 Jul 2023 14:44:59 +0000 Subject: [PATCH 3/8] fix(test): Fix tests of versions --- test/Scoop-Versions.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 7244c4eb27a49ba5e39539879a3e4d118b8bd1c7 Mon Sep 17 00:00:00 2001 From: HUMORCE Date: Tue, 11 Jul 2023 14:54:00 +0000 Subject: [PATCH 4/8] filename variable --- libexec/scoop-alias.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libexec/scoop-alias.ps1 b/libexec/scoop-alias.ps1 index a252c96b70..542dd3815e 100644 --- a/libexec/scoop-alias.ps1 +++ b/libexec/scoop-alias.ps1 @@ -52,7 +52,7 @@ function add_alias($name, $command) { # generate script $shimdir = shimdir $false if (Test-Path "$shimdir\$alias_file.ps1") { - abort "File '$name' already exists in shims directory." + abort "File '$alias_file.ps1' already exists in shims directory." } $script = @( From 7b3cba11f3d0170eceb2df101280533af1f3fb44 Mon Sep 17 00:00:00 2001 From: HUMORCE Date: Tue, 11 Jul 2023 16:59:14 +0000 Subject: [PATCH 5/8] quotes alias name --- libexec/scoop-alias.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libexec/scoop-alias.ps1 b/libexec/scoop-alias.ps1 index 542dd3815e..74e4e98fb8 100644 --- a/libexec/scoop-alias.ps1 +++ b/libexec/scoop-alias.ps1 @@ -44,7 +44,7 @@ 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" @@ -70,11 +70,11 @@ 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) From 335ba3d122daed1802081f75aee84929240b6e7d Mon Sep 17 00:00:00 2001 From: HUMORCE Date: Tue, 11 Jul 2023 16:59:49 +0000 Subject: [PATCH 6/8] quotes alias name --- libexec/scoop-alias.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libexec/scoop-alias.ps1 b/libexec/scoop-alias.ps1 index 74e4e98fb8..48a956fd64 100644 --- a/libexec/scoop-alias.ps1 +++ b/libexec/scoop-alias.ps1 @@ -74,14 +74,14 @@ function rm_alias($name) { } if ($aliases.$name) { - info "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." } } From 6177839a80153f68aac9c74600b6776ac9cead67 Mon Sep 17 00:00:00 2001 From: Hsiao-nan Cheung Date: Mon, 8 Apr 2024 19:42:14 +0800 Subject: [PATCH 7/8] Tweak tests --- test/Scoop-Alias.Tests.ps1 | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/test/Scoop-Alias.Tests.ps1 b/test/Scoop-Alias.Tests.ps1 index d11b72cca4..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,22 +23,24 @@ Describe 'Manipulate Alias' -Tag 'Scoop' { & $alias_file | Should -Be 'hello, world!' } - # NOTE: `abort()` in `add_alias()` cause tests error. - # It 'Does not change existing file if its filename same as alias name' { - # $alias_file = "$shimdir\scoop-rm.ps1" - # New-Item $alias_file -Type File -Force - # $alias_file | Should -Exist + 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 -Be $null - # } + 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" $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'..." } } } From ff671096bc05babb90832c9cbda497cc4fca0781 Mon Sep 17 00:00:00 2001 From: Hsiao-nan Cheung Date: Mon, 8 Apr 2024 19:49:57 +0800 Subject: [PATCH 8/8] Update chglog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49348cf15f..92b19529b8 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)) ### Performance Improvements