From a144c2fdbfc540a108651ed6e4ba53f67adc4c3c Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Sun, 3 Mar 2024 19:09:01 +0900 Subject: [PATCH 1/6] Start to use Pester for unit tests --- Taskfile.yml | 4 ++-- tests/PSFzfHistory.Tests.ps1 | 9 +++++++++ tools/lint-fix.ps1 | 2 +- tools/test-all.ps1 | 6 +++++- 4 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 tests/PSFzfHistory.Tests.ps1 diff --git a/Taskfile.yml b/Taskfile.yml index bcd6cf1..d93c723 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -15,8 +15,8 @@ tasks: test: cmds: # pwsh exit with 0 even if ReadLine made errors, the 1+ code is special for an example as parser error - # So testing here with the "silentness" - - pwsh --File ./tools/test-all.ps1 2>&1 | wc --bytes | grep -Fxq '0' + # So returns 1+ with the result by yourself in each cmd + - pwsh --File ./tools/test-all.ps1 - pwsh --Command 'Test-ModuleManifest -Path *.psd1' fmt: cmds: diff --git a/tests/PSFzfHistory.Tests.ps1 b/tests/PSFzfHistory.Tests.ps1 new file mode 100644 index 0000000..3436dcc --- /dev/null +++ b/tests/PSFzfHistory.Tests.ps1 @@ -0,0 +1,9 @@ +Import-Module -Name .\src\PSFzfHistory.psm1 + +InModuleScope PSFzfHistory { + Describe 'Reverse' { + It 'reverses given list' { + (1, 2, 3, 42, 987654321, 42) | Reverse | Should -BeExactly (42, 987654321, 42, 3, 2, 1) + } + } +} diff --git a/tools/lint-fix.ps1 b/tools/lint-fix.ps1 index 73e5549..16d460a 100644 --- a/tools/lint-fix.ps1 +++ b/tools/lint-fix.ps1 @@ -1,6 +1,6 @@ # Do NOT use 'Invoke-ScriptAnalyzer -Recurse -Path .' It includes dotfiles as .direnv # https://github.com/PowerShell/PSScriptAnalyzer/issues/561 -Get-ChildItem -Recurse -Path . -Include "*.psm1" | +Get-ChildItem -Recurse -Path src -Include "*.ps*1" | ForEach-Object { Write-Output $_.FullName Invoke-ScriptAnalyzer -Recurse -ReportSummary -EnableExit -Settings CodeFormatting -Fix -Path $_.FullName diff --git a/tools/test-all.ps1 b/tools/test-all.ps1 index 0ab8d8c..efa7517 100644 --- a/tools/test-all.ps1 +++ b/tools/test-all.ps1 @@ -1 +1,5 @@ -Import-Module -Name .\src\PSFzfHistory.psm1 +# https://pester.dev/docs/commands/New-PesterConfiguration +$config = New-PesterConfiguration +$config.Run.Exit = $true + +Invoke-Pester -Configuration $config From 8651354bd175725f250b62e2320776c8b652773e Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Sun, 3 Mar 2024 19:24:07 +0900 Subject: [PATCH 2/6] Add test and fucntionize current uniq logic --- src/PSFzfHistory.psm1 | 23 +++++++++++++++-------- tests/PSFzfHistory.Tests.ps1 | 6 ++++++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/PSFzfHistory.psm1 b/src/PSFzfHistory.psm1 index efdaa29..26bd944 100644 --- a/src/PSFzfHistory.psm1 +++ b/src/PSFzfHistory.psm1 @@ -1,16 +1,23 @@ # Do not add --height option for fzf, it shows nothing in keybind use function Invoke-FzfHistory ([String]$fuzzy) { - $reversedCommandSet = [ordered]@{} - [Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryItems() | & Reverse | - ForEach-Object { - if (!$reversedCommandSet.Contains($_.CommandLine)) { - $reversedCommandSet.Add($_.CommandLine, $true) | Out-Null - } - } + Select-Object -ExpandProperty CommandLine | + AsOrderedSet | + fzf --scheme=history --no-sort --no-height --query $fuzzy +} - $reversedCommandSet.Keys | fzf --scheme=history --no-sort --no-height --query $fuzzy +# Avoid System.Collections.Generic.SortedSet from following points +# - the sort order is "index", not the character dictionary order +# - No creation intermediate objects and just used in pipe +function AsOrderedSet { + $set = [ordered]@{} + $input | ForEach-Object { + if (!$set.Contains($_)) { + $set.Add($_, $true) | Out-Null + } + } + $set.Keys } function Reverse { diff --git a/tests/PSFzfHistory.Tests.ps1 b/tests/PSFzfHistory.Tests.ps1 index 3436dcc..bc25085 100644 --- a/tests/PSFzfHistory.Tests.ps1 +++ b/tests/PSFzfHistory.Tests.ps1 @@ -1,6 +1,12 @@ Import-Module -Name .\src\PSFzfHistory.psm1 InModuleScope PSFzfHistory { + Describe 'AsOrderedSet' { + It 'returns unique elements they are keep first appeared order in original' { + (1, 2, 3, 42, 987654321, 42) | AsOrderedSet | Should -BeExactly (1, 2, 3, 42, 987654321) + } + } + Describe 'Reverse' { It 'reverses given list' { (1, 2, 3, 42, 987654321, 42) | Reverse | Should -BeExactly (42, 987654321, 42, 3, 2, 1) From 002870779a94cafe337ce5e46b40f79ea7164314 Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Sun, 3 Mar 2024 19:31:03 +0900 Subject: [PATCH 3/6] Improve test as actual string type --- tests/PSFzfHistory.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PSFzfHistory.Tests.ps1 b/tests/PSFzfHistory.Tests.ps1 index bc25085..789e7ec 100644 --- a/tests/PSFzfHistory.Tests.ps1 +++ b/tests/PSFzfHistory.Tests.ps1 @@ -3,7 +3,7 @@ InModuleScope PSFzfHistory { Describe 'AsOrderedSet' { It 'returns unique elements they are keep first appeared order in original' { - (1, 2, 3, 42, 987654321, 42) | AsOrderedSet | Should -BeExactly (1, 2, 3, 42, 987654321) + ("foo", "bar", "foo", "baz", "f", "BAR", "bar") | AsOrderedSet | Should -BeExactly ('foo', 'bar', 'baz', 'f', 'BAR') } } From b8620322178124188aba37e2402650811633e4e0 Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Sun, 3 Mar 2024 19:31:34 +0900 Subject: [PATCH 4/6] Refactor AsOrderedSet to be more simple --- src/PSFzfHistory.psm1 | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/PSFzfHistory.psm1 b/src/PSFzfHistory.psm1 index 26bd944..1c06c3f 100644 --- a/src/PSFzfHistory.psm1 +++ b/src/PSFzfHistory.psm1 @@ -11,13 +11,8 @@ function Invoke-FzfHistory ([String]$fuzzy) { # - the sort order is "index", not the character dictionary order # - No creation intermediate objects and just used in pipe function AsOrderedSet { - $set = [ordered]@{} - $input | ForEach-Object { - if (!$set.Contains($_)) { - $set.Add($_, $true) | Out-Null - } - } - $set.Keys + $set = New-Object System.Collections.Generic.HashSet[string]; + $input | Where-Object { $set.Add($_) } } function Reverse { From a1eb8744db13b2e4d296f15b4a3957ca9223ac7d Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Sun, 3 Mar 2024 19:43:07 +0900 Subject: [PATCH 5/6] Reverse is not a script block now --- src/PSFzfHistory.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PSFzfHistory.psm1 b/src/PSFzfHistory.psm1 index 1c06c3f..6b288cc 100644 --- a/src/PSFzfHistory.psm1 +++ b/src/PSFzfHistory.psm1 @@ -1,7 +1,7 @@ # Do not add --height option for fzf, it shows nothing in keybind use function Invoke-FzfHistory ([String]$fuzzy) { [Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryItems() | - & Reverse | + Reverse | Select-Object -ExpandProperty CommandLine | AsOrderedSet | fzf --scheme=history --no-sort --no-height --query $fuzzy From d75588a3898020d31fd51555f5d5db21686e179d Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Sun, 3 Mar 2024 19:45:05 +0900 Subject: [PATCH 6/6] Trancate property early --- src/PSFzfHistory.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PSFzfHistory.psm1 b/src/PSFzfHistory.psm1 index 6b288cc..fdc8f2a 100644 --- a/src/PSFzfHistory.psm1 +++ b/src/PSFzfHistory.psm1 @@ -1,8 +1,8 @@ # Do not add --height option for fzf, it shows nothing in keybind use function Invoke-FzfHistory ([String]$fuzzy) { [Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryItems() | - Reverse | Select-Object -ExpandProperty CommandLine | + Reverse | AsOrderedSet | fzf --scheme=history --no-sort --no-height --query $fuzzy }