-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathPSFzf.tests.ps1
More file actions
802 lines (680 loc) · 30.8 KB
/
PSFzf.tests.ps1
File metadata and controls
802 lines (680 loc) · 30.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
#
# This is a PowerShell Unit Test file.
# You need a unit test framework such as Pester to run PowerShell Unit tests.
# You can download Pester from http://go.microsoft.com/fwlink/?LinkID=534084
#
Get-Module PsFzf | Remove-Module
# set env variable so Import-Module doesn't fail:
if ([string]::IsNullOrEmpty($env:GOPATH)) {
$env:GOPATH = "c:\ADirectoryThatShouldNotExist\"
}
Import-Module $(Join-Path $PSScriptRoot PSFzf.psd1) -ErrorAction Stop
Describe "Find-CurrentPath" {
InModuleScope PsFzf {
Context "Function Exists" {
It "Should Return Nothing" {
$line = "" ; $cursor = 0
$leftCursor = $rightCursor = $null
Find-CurrentPath $line $cursor ([ref]$leftCursor) ([ref]$rightCursor) | Should -Be $null
$leftCursor | Should -Be 0
$rightCursor | Should -Be 0
}
It "Should Return Nothing with Spaces Cursor at Beginning" {
$line = " " ; $cursor = 0
$leftCursor = $rightCursor = $null
Find-CurrentPath $line $cursor ([ref]$leftCursor) ([ref]$rightCursor) | Should -Be " "
$leftCursor | Should -Be 0
$rightCursor | Should -Be 0
}
It "Should Return Nothing with Spaces Cursor at End" {
$line = " " ; $cursor = 1
$leftCursor = $rightCursor = $null
Find-CurrentPath $line $cursor ([ref]$leftCursor) ([ref]$rightCursor) | Should -Be " "
$leftCursor | Should -Be 0
$rightCursor | Should -Be 0
}
It "Should Return Path Cursor at Beginning for Single Char" {
$line = "~" ; $cursor = 0
$leftCursor = $rightCursor = $null
Find-CurrentPath $line $cursor ([ref]$leftCursor) ([ref]$rightCursor) | Should -Be "~"
$leftCursor | Should -Be 0
$rightCursor | Should -Be ($line.Length - 1)
}
It "Should Return Path Cursor at Beginning" {
$line = "C:\Windows\" ; $cursor = 0
$leftCursor = $rightCursor = $null
Find-CurrentPath $line $cursor ([ref]$leftCursor) ([ref]$rightCursor) | Should -Be "c:\Windows\"
$leftCursor | Should -Be 0
$rightCursor | Should -Be ($line.Length - 1)
}
It "Should Return Path Cursor at End" {
$line = "C:\Windows\" ; $cursor = $line.Length
$leftCursor = $rightCursor = $null
Find-CurrentPath $line $cursor ([ref]$leftCursor) ([ref]$rightCursor) | Should -Be "c:\Windows\"
$leftCursor | Should -Be 0
$rightCursor | Should -Be ($line.Length - 1)
}
It "Should Return Command and Path Cursor at Beginning" {
$line = "cd C:\Windows\" ; $cursor = 0
$leftCursor = $rightCursor = $null
Find-CurrentPath $line $cursor ([ref]$leftCursor) ([ref]$rightCursor) | Should -Be "cd"
$leftCursor | Should -Be 0
$rightCursor | Should -Be ('cd'.Length - 1)
}
It "Should Return Command and Path Cursor at End" {
$line = "cd C:\Windows\" ; $cursor = $line.Length
$leftCursor = $rightCursor = $null
Find-CurrentPath $line $cursor ([ref]$leftCursor) ([ref]$rightCursor) | Should -Be "c:\Windows\"
$leftCursor | Should -Be 'cd '.Length
$rightCursor | Should -Be ($line.Length - 1)
}
It "Should Return Command and Path Cursor at End" {
$line = "cd C:\Windows\" ; $cursor = $line.Length - 1
$leftCursor = $rightCursor = $null
Find-CurrentPath $line $cursor ([ref]$leftCursor) ([ref]$rightCursor) | Should -Be "c:\Windows\"
$leftCursor | Should -Be 'cd '.Length
$rightCursor | Should -Be ($line.Length - 1)
}
It "Should Return Path With Quotes Cursor at Beginning" -ForEach @(
@{ Quote = '"' }
@{ Quote = "'" }
) {
$line = $quote + 'C:\Program Files\' + $quote ; $cursor = 0
$leftCursor = $rightCursor = $null
Find-CurrentPath $line $cursor ([ref]$leftCursor) ([ref]$rightCursor) | Should -Be 'C:\Program Files\'
$leftCursor | Should -Be 0
$rightCursor | Should -Be ($line.Length - 1)
}
}
}
}
Describe 'Invoke-PsFzfRipgrep' {
InModuleScope PsFzf {
$OriginalPSFZF_RG_PREFIX = $null
$script:CapturedCommand = $null
# $script:MockedFzfDefaultCmd = $null # No longer needed
$script:SystemOriginalFzfDefaultCommand = $null
BeforeEach {
# Store and clear environment variables
$OriginalPSFZF_RG_PREFIX = $env:PSFZF_RG_PREFIX
$env:PSFZF_RG_PREFIX = $null
$script:SystemOriginalFzfDefaultCommand = $env:FZF_DEFAULT_COMMAND
$env:FZF_DEFAULT_COMMAND = "SENTINEL_FZF_COMMAND_FOR_RESTORE_TEST"
# Reset captured command
$script:CapturedCommand = $null
# Mock Invoke-Expression to capture the command
Mock Invoke-Expression {
param($Command)
$script:CapturedCommand = $Command
# Simulate fzf returning no selection to allow the function to complete
return $null
} -ModuleName PsFzf
# Mock Get-EditorLaunch to prevent actual editor launch
Mock Get-EditorLaunch {
param($FileList, $LineNum = 0)
# Do nothing, just prevent original function call
return "MockedEditorLaunch $FileList $LineNum"
} -ModuleName PsFzf
# Mock Resolve-Path for -NoEditor switch
Mock Resolve-Path {
param($Path)
return "Resolved_$Path" # Simulate path resolution
} -ModuleName PsFzf
# No more mocking of FzfDefaultCmd constructor or Restore method
}
AfterEach {
# Restore environment variables
$env:PSFZF_RG_PREFIX = $OriginalPSFZF_RG_PREFIX
$env:FZF_DEFAULT_COMMAND = $script:SystemOriginalFzfDefaultCommand
}
Context 'Default rg command' {
It 'Should use the default rg prefix and restore FZF_DEFAULT_COMMAND' {
Invoke-PsFzfRipgrep -SearchString 'testsearch' | Out-Null
$defaultRgPrefix = "rg --column --line-number --no-heading --color=always --smart-case "
$script:CapturedCommand | Should -Match ([regex]::Escape($defaultRgPrefix))
$env:FZF_DEFAULT_COMMAND | Should -Be "SENTINEL_FZF_COMMAND_FOR_RESTORE_TEST"
}
}
Context 'Custom rg command via PSFZF_RG_PREFIX' {
It 'Should use the custom rg prefix and restore FZF_DEFAULT_COMMAND' {
$customRgPrefix = 'my-custom-rg --awesome '
$env:PSFZF_RG_PREFIX = $customRgPrefix
Invoke-PsFzfRipgrep -SearchString 'testsearch' | Out-Null
$script:CapturedCommand | Should -Match ([regex]::Escape($customRgPrefix))
$script:CapturedCommand | Should -Not -Match ([regex]::Escape("rg --column --line-number"))
$env:FZF_DEFAULT_COMMAND | Should -Be "SENTINEL_FZF_COMMAND_FOR_RESTORE_TEST"
}
}
Context 'NoEditor switch' {
It 'Should call Resolve-Path, not Get-EditorLaunch, and restore FZF_DEFAULT_COMMAND' {
# Override Invoke-Expression mock for this specific test to return a value
Mock Invoke-Expression {
param($Command)
$script:CapturedCommand = $Command
return "somefile.txt:123:content" # Simulate fzf selection
} -ModuleName PsFzf
$result = Invoke-PsFzfRipgrep -SearchString 'testsearch' -NoEditor
$result | Should -Be "Resolved_somefile.txt"
Should -Invoke 'Resolve-Path' -Times 1 -ModuleName PsFzf -ParameterFilter { $Path -eq 'somefile.txt' }
Should -Not -Invoke 'Get-EditorLaunch' -ModuleName PsFzf
$env:FZF_DEFAULT_COMMAND | Should -Be "SENTINEL_FZF_COMMAND_FOR_RESTORE_TEST"
}
}
Context 'Editor launch' {
It 'Should call Get-EditorLaunch, not Resolve-Path, and restore FZF_DEFAULT_COMMAND' {
# Override Invoke-Expression mock for this specific test to return a value
Mock Invoke-Expression {
param($Command)
$script:CapturedCommand = $Command
return "anotherfile.txt:45:foobar" # Simulate fzf selection
} -ModuleName PsFzf
Invoke-PsFzfRipgrep -SearchString 'testsearch' | Out-Null
Should -Invoke 'Get-EditorLaunch' -Times 1 -ModuleName PsFzf -ParameterFilter {
$FileList -eq 'anotherfile.txt' -and $LineNum -eq '45'
}
Should -Not -Invoke 'Resolve-Path' -ModuleName PsFzf
$env:FZF_DEFAULT_COMMAND | Should -Be "SENTINEL_FZF_COMMAND_FOR_RESTORE_TEST"
}
}
}
}
Describe 'Invoke-FuzzySetLocation' {
InModuleScope PsFzf {
BeforeAll {
# Variables for mocks and environment restoration
$Original_FZF_ALT_C_COMMAND = $null
$TempTestDirectory = "temp_test_dir_ifsl" # IFSL for Invoke-FuzzySetLocation
$ResolvedTempTestDirectory = ''
}
BeforeEach {
# Save and clear FZF_ALT_C_COMMAND
$Original_FZF_ALT_C_COMMAND = $env:FZF_ALT_C_COMMAND
$env:FZF_ALT_C_COMMAND = $null
# Create a temporary directory for tests that need filesystem interaction
$ParentPath = if (Test-Path -Path "TestDrive:") { "TestDrive:" } else { $PSScriptRoot }
$ResolvedTempTestDirectory = Join-Path $ParentPath $TempTestDirectory
New-Item -ItemType Directory -Path $ResolvedTempTestDirectory -Force | Out-Null
New-Item -ItemType Directory -Path (Join-Path $ResolvedTempTestDirectory "subdir1") -Force | Out-Null
New-Item -ItemType Directory -Path (Join-Path $ResolvedTempTestDirectory "subdir2") -Force | Out-Null
New-Item -ItemType File -Path (Join-Path $ResolvedTempTestDirectory "somefile.txt") -Force | Out-Null
}
AfterEach {
# Restore FZF_ALT_C_COMMAND
$env:FZF_ALT_C_COMMAND = $Original_FZF_ALT_C_COMMAND
# Remove temporary directory - don't remove due to this error: https://github.com/pester/Pester/issues/1070
Remove-Item -Path $ResolvedTempTestDirectory -Recurse -Force -ErrorAction SilentlyContinue
# Clear specific mocks to avoid interference between tests
# Commenting out Remove-Mock due to persistent CommandNotFoundException in this environment.
# This is a workaround; ideally, Remove-Mock should function correctly.
# Pester\Remove-Mock -CommandName 'Invoke-Fzf' -ModuleName 'PsFzf' -ErrorAction SilentlyContinue
# Pester\Remove-Mock -CommandName 'Set-Location' -ModuleName 'PsFzf' -ErrorAction SilentlyContinue
}
Context 'When FZF_ALT_C_COMMAND is set and not empty' {
It 'Should use FZF_ALT_C_COMMAND, pass its output to Invoke-Fzf, and set location to Invoke-Fzf result' {
# Arrange
$env:FZF_ALT_C_COMMAND = 'Write-Output "custom_path_from_alt_c"' # This command's output goes to Invoke-Fzf
$script:expectedPathForFzfMock = "custom_path_from_alt_c_SELECTED" # What Invoke-Fzf mock will return
Mock Invoke-Fzf {
param([Parameter(ValueFromPipeline = $true)]$InputObject) # Match real signature somewhat
Write-Warning "Invoke-Fzf mock called. Input type: $($InputObject.GetType().Name). Input: $InputObject"
return $script:expectedPathForFzfMock
} -ModuleName PsFzf
Mock Set-Location { param($Path) Write-Warning "Set-Location mock called with: $Path" } -ModuleName PsFzf
# Act
$result = Invoke-FuzzySetLocation
# Assert
$result | Should -Be $script:expectedPathForFzfMock
Should -Invoke 'Invoke-Fzf' -Times 1 -ModuleName 'PsFzf' # Corrected assertion
Should -Invoke 'Set-Location' -Times 1 -ModuleName 'PsFzf' -ParameterFilter { $Path -eq $script:expectedPathForFzfMock } # Corrected assertion
}
}
Context 'When FZF_ALT_C_COMMAND is not set' {
It 'Should use default Get-ChildItem command, pass its output to Invoke-Fzf, and set location to Invoke-Fzf result' {
# Arrange
$env:FZF_ALT_C_COMMAND = $null # Ensure it's null
$script:expectedPathForFzfMock = Join-Path $ResolvedTempTestDirectory "subdir1_SELECTED" # Example selection from fzf
Mock Invoke-Fzf {
param([Parameter(ValueFromPipeline = $true)]$InputObject)
Write-Warning "Invoke-Fzf mock (default case) called. Input type: $($InputObject.GetType().Name). Input count: $(if ($InputObject) {@($InputObject).Count} else {0})"
return $script:expectedPathForFzfMock
} -ModuleName PsFzf
Mock Set-Location { param($Path) Write-Warning "Set-Location mock (default case) called with: $Path" } -ModuleName PsFzf
# Act
$result = Invoke-FuzzySetLocation -Directory $ResolvedTempTestDirectory
# Assert
$result | Should -Be $script:expectedPathForFzfMock
Should -Invoke 'Invoke-Fzf' -Times 1 -ModuleName 'PsFzf' # Corrected assertion
Should -Invoke 'Set-Location' -Times 1 -ModuleName 'PsFzf' -ParameterFilter { $Path -eq $script:expectedPathForFzfMock } # Corrected assertion
}
}
Context 'When FZF_ALT_C_COMMAND is set to an empty string' {
It 'Should use default Get-ChildItem command (like FZF_ALT_C_COMMAND not set)' {
# Arrange
$env:FZF_ALT_C_COMMAND = "" # Empty string
$script:expectedPathForFzfMock = Join-Path $ResolvedTempTestDirectory "subdir2_SELECTED" # Example selection from fzf
Mock Invoke-Fzf {
param([Parameter(ValueFromPipeline = $true)]$InputObject)
Write-Warning "Invoke-Fzf mock (empty alt_c) called. Input type: $($InputObject.GetType().Name). Input count: $(if ($InputObject) {@($InputObject).Count} else {0})"
return $script:expectedPathForFzfMock
} -ModuleName PsFzf
Mock Set-Location { param($Path) Write-Warning "Set-Location mock (empty alt_c) called with: $Path" } -ModuleName PsFzf
# Act
$result = Invoke-FuzzySetLocation -Directory $ResolvedTempTestDirectory
# Assert
$result | Should -Be $script:expectedPathForFzfMock
Should -Invoke 'Invoke-Fzf' -Times 1 -ModuleName 'PsFzf' # Corrected assertion
Should -Invoke 'Set-Location' -Times 1 -ModuleName 'PsFzf' -ParameterFilter { $Path -eq $script:expectedPathForFzfMock } # Corrected assertion
}
}
}
}
Describe 'Invoke-FuzzyZLocation' {
InModuleScope PsFzf {
BeforeAll {
# simulate functions so we don't have to load module:
if ($null -eq ${function:Get-ZLocation}) {
${function:Get-ZLocation} = {}
}
if ($null -eq ${function:Set-ZLocation}) {
${function:Set-ZLocation} = {}
}
# For Invoke-FuzzyZLocation tests, always mock Get-ZLocation to return a consistent dataset.
# This makes tests independent of the actual ZLocation database state.
Mock Get-ZLocation {
Write-Warning "Mocked Get-ZLocation (providing consistent test data)"
return @{
'/path/to/projectA' = 10;
'/path/to/another/projectB' = 5;
'/another/path/to/C' = 15;
}
} -ModuleName PsFzf
# Mock Set-ZLocation if it doesn't exist, or use a simple pass-through mock if it does.
# This command is not directly asserted in these tests but might be called by other ZLocation logic.
if (-not (Get-Command Set-ZLocation -ErrorAction SilentlyContinue)) {
Mock Set-ZLocation {
param($Path)
Write-Warning "Mocked Set-ZLocation (original not found) called with: $Path"
} -ModuleName PsFzf
}
else {
# If it exists, we can still mock it to prevent actual DB writes if desired,
# or let the original run if operations are idempotent and don't affect other tests.
# For simplicity, let's just ensure it's available or minimally mocked.
Mock Set-ZLocation {
param($Path)
Write-Warning "Mocked Set-ZLocation (original MAY exist, simple log) called with: $Path"
# Optionally call original: & (Get-Command Set-ZLocation -ErrorAction Stop) @PSBoundParameters
} -ModuleName PsFzf
}
}
BeforeEach {
# Common mocks for Invoke-FuzzyZLocation tests
# These script-scoped variables are reset in AfterEach
$script:StaticCollectedInputForAllMockCallsInTest = @()
$script:StaticFzfQueryArgValue = $null
$script:StaticFzfNoSortArgValue = $null
$script:StaticActualInvokeFzfBeginCount = 0
$script:StaticActualInvokeFzfProcessCount = 0
$script:StaticActualInvokeFzfEndCount = 0
Mock Invoke-Fzf {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline = $true)] $InputObject,
[string]$Query = $null,
[switch]$NoSort
)
begin {
$script:StaticActualInvokeFzfBeginCount++
# Parameters should be consistent for a single logical pipeline invocation
if ($script:StaticActualInvokeFzfBeginCount -eq 1) {
$script:StaticFzfQueryArgValue = $Query
$script:StaticFzfNoSortArgValue = $NoSort.IsPresent
}
Write-Warning "Invoke-Fzf MOCK ---BEGIN--- CallNo: $($script:StaticActualInvokeFzfBeginCount). Query: '$Query'. NoSort: '$($NoSort.IsPresent)'"
}
process {
$script:StaticActualInvokeFzfProcessCount++
if ($null -ne $InputObject) {
# Ensure robust array accumulation
$script:StaticCollectedInputForAllMockCallsInTest = @($script:StaticCollectedInputForAllMockCallsInTest) + $InputObject
}
Write-Warning "Invoke-Fzf MOCK ---PROCESS--- ItemNo: $($script:StaticActualInvokeFzfProcessCount). Input: '$InputObject'. Static collected now: $($script:StaticCollectedInputForAllMockCallsInTest.Count)"
}
end {
$script:StaticActualInvokeFzfEndCount++
Write-Warning "Invoke-Fzf MOCK ---END--- CallNo: $($script:StaticActualInvokeFzfEndCount). Total static collected: $($script:StaticCollectedInputForAllMockCallsInTest.Count). Query was: '$($script:StaticFzfQueryArgValue)'"
# THE RETURN LOGIC NOW ONLY HAPPENS ON THE *LAST* EXPECTED CALL
# AND OPERATES ON THE FULLY ACCUMULATED LIST
# This assumes Get-ZLocation (mocked) provides 3 items.
if ($script:StaticActualInvokeFzfEndCount -eq 3) {
if ($script:StaticFzfQueryArgValue -eq 'projectA') {
return '/path/to/projectA_SELECTED'
}
elseif ($script:StaticFzfQueryArgValue -eq 'nonexistent') {
return $null
}
if ($script:StaticCollectedInputForAllMockCallsInTest.Count -gt 0 -and (-not $script:StaticFzfQueryArgValue)) {
# First item of the *accumulated and sorted* list
if ($script:StaticCollectedInputForAllMockCallsInTest[0] -eq '/another/path/to/C') {
return '/another/path/to/C_SELECTED'
}
}
# Fallback based on accumulated data if necessary
if ($script:StaticCollectedInputForAllMockCallsInTest.Count -gt 0) {
return $script:StaticCollectedInputForAllMockCallsInTest[0] + "_fallback_SELECTED"
}
return '/path/to/default/empty_input_SELECTED'
}
return $null # Intermediate "end" calls return nothing, so they don't pollute $result in Invoke-FuzzyZLocation
}
} -ModuleName PsFzf
Mock Set-Location { param($Path) Write-Warning "Set-Location mock (Invoke-FuzzyZLocation specific) called with: $Path" } -ModuleName PsFzf
}
AfterEach {
# Clear mocks after each test
# Pester\Remove-Mock -CommandName 'Get-ZLocation' -ModuleName 'PsFzf' -ErrorAction SilentlyContinue
# Pester\Remove-Mock -CommandName 'Invoke-Fzf' -ModuleName 'PsFzf' -ErrorAction SilentlyContinue
# Pester\Remove-Mock -CommandName 'Set-Location' -ModuleName 'PsFzf' -ErrorAction SilentlyContinue
$script:StaticCollectedInputForAllMockCallsInTest = @()
$script:StaticFzfQueryArgValue = $null
$script:StaticFzfNoSortArgValue = $null
$script:StaticActualInvokeFzfBeginCount = 0
$script:StaticActualInvokeFzfProcessCount = 0
$script:StaticActualInvokeFzfEndCount = 0
}
Context 'When no query is provided' {
It 'Should process ZLocation entries, pass them to Invoke-Fzf, and set location to the result' {
# Diagnostic: See what Get-ZLocation returns in this context
$actualZOutput = Get-ZLocation
Write-Warning "Test 'When no query is provided': Get-ZLocation returned: $($actualZOutput | ConvertTo-Json -Depth 3 -Compress)"
# Act
Invoke-FuzzyZLocation
# Assert
Should -Invoke 'Get-ZLocation' -Times 1 -ModuleName 'PsFzf'
# Even if mock is called 3 times, these parameters should be consistent from the first call
$script:StaticFzfQueryArgValue | Should -BeNullOrEmpty
$script:StaticFzfNoSortArgValue | Should -BeTrue
# Verify the items accumulated by Invoke-Fzf mock are the sorted keys
$expectedArray = @('/another/path/to/C', '/path/to/projectA', '/path/to/another/projectB')
$script:StaticCollectedInputForAllMockCallsInTest | Should -Be $expectedArray
# Check that Set-Location was called with the item fzf mock would return on its final call
Should -Invoke 'Set-Location' -Times 1 -ModuleName 'PsFzf' -ParameterFilter { $Path -eq '/another/path/to/C_SELECTED' }
}
}
Context 'When a query is provided' {
It 'Should pass the query to Invoke-Fzf and set location to the result' {
# Arrange
$testQuery = 'projectA'
# Act
Invoke-FuzzyZLocation -Query $testQuery
# Assert
Should -Invoke 'Get-ZLocation' -Times 1 -ModuleName 'PsFzf'
$script:StaticFzfQueryArgValue | Should -Be $testQuery
$script:StaticFzfNoSortArgValue | Should -BeTrue
$script:StaticCollectedInputForAllMockCallsInTest.Count | Should -Be 3 # Still processes all 3 items
Should -Invoke 'Set-Location' -Times 1 -ModuleName 'PsFzf' -ParameterFilter { $Path -eq '/path/to/projectA_SELECTED' }
}
}
Context 'When Invoke-Fzf returns no selection' {
It 'Should not call Set-Location' {
# Arrange
$testQuery = 'nonexistent' # This query will make Invoke-Fzf mock return $null
# Act
Invoke-FuzzyZLocation -Query $testQuery
# Assert
Should -Invoke 'Get-ZLocation' -Times 1 -ModuleName 'PsFzf'
$script:StaticFzfQueryArgValue | Should -Be $testQuery
$script:StaticCollectedInputForAllMockCallsInTest.Count | Should -Be 3
Should -Not -Invoke 'Set-Location' -ModuleName 'PsFzf'
}
}
}
}
Describe "Add-BinaryModuleTypes" {
InModuleScope PsFzf {
Context "Module Loaded" {
It "Be Able to Create Type" {
$filePath = Join-Path ([system.io.path]::GetTempPath()) 'TestFile.txt'
1..100 | Add-Content $filePath
$newObject = New-Object PSFzf.IO.ReverseLineReader -ArgumentList $filePath
$newObject | Should -Not -Be $null
}
}
}
}
Describe "Check FixCompletionResult" {
InModuleScope PsFzf {
Context "Non-quoted Strings Should Not Change" {
It "Check Simple String" {
FixCompletionResult("not_quoted") | Should -Be "not_quoted"
}
It "Check Simple String with quote" {
FixCompletionResult("not_quotedwith'") | Should -Be "not_quotedwith'"
}
}
Context "Non-quoted Strings With Spaces Should Change" {
It "Check Simple String With Space" {
FixCompletionResult("with space") | Should -Be """with space"""
}
It "Check Simple String with quote" {
FixCompletionResult("with space, ' and with'") | Should -Be """with space, ' and with'"""
}
}
Context "Quoted Strings Should Not Change" {
It "Check Simple String With Space and Already Double Quoted" {
FixCompletionResult("""with space and already quoted""") | Should -Be """with space and already quoted"""
}
It "Check Simple String With Space and Already Single Quoted" {
FixCompletionResult("'with space and already quoted'") | Should -Be "'with space and already quoted'"
}
}
Context "AlwaysQuote Parameter Tests" {
It "Should quote simple string when AlwaysQuote is used" {
FixCompletionResult -str "file.txt" -AlwaysQuote | Should -Be """file.txt"""
}
It "Should not double-quote already double-quoted string with AlwaysQuote" {
FixCompletionResult -str """already_quoted.txt""" -AlwaysQuote | Should -Be """already_quoted.txt"""
}
It "Should not double-quote already single-quoted string with AlwaysQuote" {
FixCompletionResult -str "'already_quoted.txt'" -AlwaysQuote | Should -Be "'already_quoted.txt'"
}
It "Should quote string with spaces when AlwaysQuote is used" {
FixCompletionResult -str "file with spaces.txt" -AlwaysQuote | Should -Be """file with spaces.txt"""
}
It "Should not double-quote already quoted string with spaces when AlwaysQuote is used" {
FixCompletionResult -str """file with spaces.txt""" -AlwaysQuote | Should -Be """file with spaces.txt"""
}
}
}
}
Describe "Check Parameters" {
InModuleScope PsFzf {
Context "Parameters Should Fail" {
It "Borders Should -Be Mutally Exclusive" {
{ $_ = '' | Invoke-Fzf -Border -BorderStyle 'sharp' } |
Should -Throw '*are mutally exclusive*'
}
It "Validate Tiebreak" {
{ $_ = '' | Invoke-Fzf -Tiebreak 'Tiebreak' } |
Should -Throw 'Cannot validate argument on parameter ''Tiebreak''*'
}
It "Validate BorderStyle" {
{ $_ = '' | Invoke-Fzf -BorderStyle 'InvalidStyle' } |
Should -Throw 'Cannot validate argument on parameter ''BorderStyle''*'
}
It "Validate Info" {
{ $_ = '' | Invoke-Fzf -Info 'InvalidInfo' } |
Should -Throw 'Cannot validate argument on parameter ''Info''*'
}
It "Validate Height Pattern Percentage" {
{ $_ = '' | Invoke-Fzf -Height '1000%' } |
Should -Throw 'Cannot validate argument on parameter ''Height''*'
}
It "Validate Height Pattern Non-Number" {
{ $_ = '' | Invoke-Fzf -Height 'adf1000' } |
Should -Throw 'Cannot validate argument on parameter ''Height''*'
}
It "Validate Height Pattern Negative" {
{ $_ = '' | Invoke-Fzf -Height '-1' } |
Should -Throw 'Cannot validate argument on parameter ''Height''*'
}
It "Validate MinHeight Pattern Non-Number" {
{ $_ = '' | Invoke-Fzf -MinHeight 'adf1' -Height 10 } |
Should -Throw 'Cannot process argument transformation on parameter ''MinHeight''*'
}
It "Validate MinHeight Pattern Negative" {
{ $_ = '' | Invoke-Fzf -MinHeight '-1' -Height 10 } |
Should -Throw 'Cannot validate argument on parameter ''MinHeight''*'
}
}
}
}
Describe "Set-PsFzfOption PsReadlineHandlerProviderDelimiter" {
InModuleScope PsFzf {
Context "Default delimiter is comma" {
It "Should default to comma" {
# The default delimiter should be ','
$script:PsReadlineHandlerProviderDelimiter | Should -Be ','
}
}
Context "Can override delimiter" {
It "Should allow setting custom delimiter" {
Set-PsFzfOption -PsReadlineHandlerProviderDelimiter ' '
$script:PsReadlineHandlerProviderDelimiter | Should -Be ' '
}
It "Should allow setting to semicolon" {
Set-PsFzfOption -PsReadlineHandlerProviderDelimiter ';'
$script:PsReadlineHandlerProviderDelimiter | Should -Be ';'
}
It "Should allow setting back to comma" {
Set-PsFzfOption -PsReadlineHandlerProviderDelimiter ','
$script:PsReadlineHandlerProviderDelimiter | Should -Be ','
}
}
}
}
Describe "Set-PsFzfOption TabCompletionPreviewWindow" {
InModuleScope PsFzf {
Context "Default preview window configuration" {
It "Should default to 'hidden|down|right|right:hidden'" {
# The default preview window configuration should be 'hidden|down|right|right:hidden'
$script:TabCompletionPreviewWindow | Should -Be 'hidden|down|right|right:hidden'
}
}
Context "Can override preview window configuration" {
It "Should allow setting custom preview window configuration" {
Set-PsFzfOption -TabCompletionPreviewWindow 'visible|up|left'
$script:TabCompletionPreviewWindow | Should -Be 'visible|up|left'
}
It "Should allow setting to hidden with different change options" {
Set-PsFzfOption -TabCompletionPreviewWindow 'hidden|up|down|left:hidden'
$script:TabCompletionPreviewWindow | Should -Be 'hidden|up|down|left:hidden'
}
It "Should allow setting back to default" {
Set-PsFzfOption -TabCompletionPreviewWindow 'hidden|down|right|right:hidden'
$script:TabCompletionPreviewWindow | Should -Be 'hidden|down|right|right:hidden'
}
}
}
}
Describe "Get-EditorLaunch" {
InModuleScope PSFzf {
Context "Vim" {
BeforeEach {
$env:PSFZF_EDITOR_OPTIONS = $null
$env:VSCODE_PID = $null
$env:VISUAL = $null
$env:EDITOR = $null
$testFile1 = Join-Path $TestDrive 'somefile1.txt'
Set-Content -Path $testFile1 -Value "hello 1"
$testFile2 = Join-Path $TestDrive 'somefile2.txt'
Set-Content -Path $testFile2 -Value "hello 2"
}
It "Should Return vim Single" {
$env:EDITOR = 'vim'
Get-EditorLaunch $testFile1 | Should -Be "vim ""$testFile1"" +0"
}
It "Should Return vim Single With Quotes" {
$env:EDITOR = 'vim'
Get-EditorLaunch """$testFile1""" | Should -Be "vim ""$testFile1"" +0"
}
It "Should Return vim Single With Options" {
$env:EDITOR = 'vim'
$env:PSFZF_EDITOR_OPTIONS = "--clean"
Get-EditorLaunch $testFile1 | Should -Be "vim --clean ""$testFile1"" +0"
}
It "Should Return vim Single with Line Number" {
$env:EDITOR = 'vim'
Get-EditorLaunch $testFile1 -LineNum 101 | Should -Be "vim ""$testFile1"" +101"
}
It "Should Return vim Multiple" {
$env:EDITOR = 'vim'
Get-EditorLaunch @($testFile1, $testFile2) | Should -Be "vim ""$testFile1"" ""$testFile2"""
}
It "Should Return vim Multiple With Quotes" {
$env:EDITOR = 'vim'
Get-EditorLaunch @("""$testFile1""", """$testFile2""") | Should -Be "vim ""$testFile1"" ""$testFile2"""
}
It "Should Return code Single" {
$env:EDITOR = 'code'
Get-EditorLaunch $testFile1 | Should -Be $('code --goto "{0}:0"' -f $testFile1)
}
It "Should Return code Single With Quotes" {
$env:EDITOR = 'code'
Get-EditorLaunch """$testFile1""" | Should -Be $('code --goto "{0}:0"' -f $testFile1)
}
It "Should Return code Single Reuse Window" {
$env:EDITOR = 'code'
$env:VSCODE_PID = 100
Get-EditorLaunch $testFile1 | Should -Be $('code --reuse-window --goto "{0}:0"' -f $testFile1)
}
It "Should Return code Single with Line Number" {
$env:EDITOR = 'code'
Get-EditorLaunch $testFile1 -LineNum 100 | Should -Be $('code --goto "{0}:100"' -f $testFile1)
}
It "Should Return code Multiple" {
$env:EDITOR = 'code'
Get-EditorLaunch @($testFile1, $testFile2) | Should -Be "code ""$testFile1"" ""$testFile2"""
}
It "Should Return code Multiple With Quotes" {
$env:EDITOR = 'code'
Get-EditorLaunch @("""$testFile1""", """$testFile2""") | Should -Be "code ""$testFile1"" ""$testFile2"""
}
}
}
}
# CI seems to have problems on GitHub CI - timing issues?
if ( $false ) {
Describe "Invoke-Fzf" {
InModuleScope PsFzf {
Context "Function Exists" {
It "Should Return Nothing" {
$result = '' | Invoke-Fzf -Query 'file1.txt' -Select1 -Exit0 -Filter ' '
$result | Should -Be $null
}
It "Should Return 1 Item, 1 Element" {
$result = 'file1.txt' | Invoke-Fzf -Select1 -Exit0 -Filter 'file1.txt'
$result | Should -Be 'file1.txt'
}
It "Should Return 1 Item, Case Insensitive" {
$result = 'file1.txt' | Invoke-Fzf -Select1 -Exit0 -CaseInsensitive -Filter 'FILE1.TXT'
$result | Should -Be 'file1.txt'
}
It "Should Return Nothing, Case Sensitive" {
$result = 'file1.txt' | Invoke-Fzf -Select1 -Exit0 -CaseSensitive -Filter 'FILE1.TXT'
$result | Should -Be $null
}
It "Should Return 1 Item, No Multi" {
$result = 'file1.txt', 'file2.txt' | Invoke-Fzf -Multi -Select1 -Exit0 -Filter "file1"
$result | Should -Be 'file1.txt'
}
It "Should Return 2 Item, Multi" {
$result = 'file1.txt', 'file2.txt' | Invoke-Fzf -Multi -Select1 -Exit0 -Filter "file"
$result.Length | Should -Be 2
$result[0] | Should -Be 'file1.txt'
$result[1] | Should -Be 'file2.txt'
}
}
}
}
}