Skip to content

Commit 35828df

Browse files
committed
fixes init tests
1 parent 0d014a8 commit 35828df

3 files changed

Lines changed: 32 additions & 47 deletions

File tree

private/Get-Commands.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function Get-Commands {
1919
[Command]::new("alias", @(
2020
[Command]::new("add", "Add-Alias" ),
2121
[Command]::new("remove", "Remove-Alias" )
22-
[Command]::new("list", { Write-Host ($script:ALIASES | Format-Table | Out-String) })
22+
[Command]::new("list", { Write-Host (Get-Aliases | Format-Table | Out-String) })
2323
)
2424
)
2525
[Command]::new("todo", @(

private/Initialize-QuickPath.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function EnsureAliasFile {
2727
}
2828

2929
if (-not (Test-Path $aliasFilePath)) {
30-
'[]' | Out-File -FilePath $aliasFilePath -Encoding UTF8
30+
'[]' | Out-File -FilePath $aliasFilePath
3131
}
3232

3333
return $aliasFilePath

tests/Initialize-QuickPath.Tests.ps1

Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,76 +4,61 @@ BeforeAll {
44

55
Describe "Initialize-QuickPath" {
66
BeforeEach {
7-
# Route Get-AliasFilePath to TestDrive by overriding LOCALAPPDATA just for this test
8-
$script:__origLocalAppData = $env:LOCALAPPDATA
9-
$env:LOCALAPPDATA = 'TestDrive:'
10-
11-
# Compute the expected paths using the real function
12-
$testFile = Get-AliasFilePath
13-
$testRoot = Split-Path -Path $testFile -Parent
14-
15-
# Avoid building commands during init in these tests
16-
Mock Get-Commands { @() }
17-
}
18-
19-
AfterEach {
20-
if ($script:__origLocalAppData) { $env:LOCALAPPDATA = $script:__origLocalAppData }
21-
Remove-Variable -Name __origLocalAppData -Scope Script -ErrorAction SilentlyContinue
7+
Mock Import-Aliases { @() }
8+
Mock Get-AliasFilePath { return "quickpath\\aliases.json" }
9+
Mock Test-Path { return $false }
10+
Mock Out-File {}
11+
Mock New-Item {}
12+
Mock Get-Commands { @([pscustomobject]@{ Name = 'test' }) }
2213
}
2314

2415
It 'File exists, does not recreate' {
25-
# Arrange existing directory and file
26-
New-Item -Path $testRoot -ItemType Directory -Force | Out-Null
27-
'[]' | Out-File -FilePath $testFile -Encoding utf8
28-
$before = (Get-Item $testFile).LastWriteTimeUtc
16+
Mock Test-Path { return $true }
2917

3018
Initialize-QuickPath
3119

32-
# File should remain untouched
33-
Test-Path $testRoot | Should -BeTrue
34-
Test-Path $testFile | Should -BeTrue
35-
(Get-Item $testFile).LastWriteTimeUtc | Should -Be $before
20+
Assert-MockCalled Out-File -Exactly 0
21+
Assert-MockCalled New-Item -Exactly 0
3622
}
3723

38-
It 'Directory exists but file does not, creates file' {
39-
New-Item -Path $testRoot -ItemType Directory -Force | Out-Null
24+
It 'Directory does not exist, creates directory and file' {
25+
Mock Test-Path { $false }
4026

4127
Initialize-QuickPath
4228

43-
Test-Path $testRoot | Should -BeTrue
44-
Test-Path $testFile | Should -BeTrue
29+
Assert-MockCalled New-Item -Exactly 1
30+
Assert-MockCalled Out-File -Exactly 1
4531
}
4632

47-
It 'Directory does not exist, creates directory and file' {
33+
It 'Directory exists but file does not, creates file' {
34+
Mock Test-Path { param($Path) if ($Path -like "*aliases.json") { return $false } else { return $true } }
35+
4836
Initialize-QuickPath
4937

50-
Test-Path $testRoot | Should -BeTrue
51-
Test-Path $testFile | Should -BeTrue
38+
Assert-MockCalled New-Item -Exactly 0
39+
Assert-MockCalled Out-File -Exactly 1
5240
}
5341

5442
It 'Loads commands into $script:COMMANDS' {
55-
Mock Get-Commands { @([pscustomobject]@{ Name = 'test' }) }
43+
5644
Initialize-QuickPath
5745

5846
$script:COMMANDS | Should -Not -Be $null
5947
$script:COMMANDS.Count | Should -BeGreaterThan 0
6048
}
6149

6250
It 'Loads aliases into $script:ALIASES' {
63-
# Arrange: write a minimal valid JSON with one alias mapping
64-
New-Item -Path $testRoot -ItemType Directory -Force | Out-Null
65-
$json = @'
66-
[
67-
{"aliases": ["proj"], "windowsPath": "C:\\Temp\\Proj" }
68-
]
69-
'@
70-
$json | Out-File -FilePath $testFile -Encoding utf8
71-
51+
# Arrange: Mock Import-Aliases to return test data
52+
Mock Import-Aliases {
53+
@([pscustomobject]@{
54+
Aliases = @('proj')
55+
WindowsPath = 'C:\Temp\Proj'
56+
})
57+
}
58+
7259
Initialize-QuickPath
7360

74-
$aliases = Get-Aliases
75-
$aliases | Should -Not -Be $null
76-
$aliases.Count | Should -Be 1
77-
$aliases[0].Aliases | Should -Contain 'proj'
61+
$script:ALIASES | Should -Not -Be $null
62+
$script:ALIASES.Count | Should -BeGreaterThan 0
7863
}
79-
}
64+
}

0 commit comments

Comments
 (0)