Skip to content

Commit b6bd0bf

Browse files
authored
Merge pull request #47 from MatrTech/46-alias-file-path
46 alias file path
2 parents 5df079f + b9c86e4 commit b6bd0bf

3 files changed

Lines changed: 33 additions & 10 deletions

File tree

private/Alias-Helper.ps1

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
. $PSScriptRoot\..\classes\AliasPathMapping.ps1
22

33
function Get-Script-Path {
4-
$moduleName = "quickpath"
5-
$modulePath = (Get-Module -Name $moduleName -ListAvailable | Select-Object -First 1 -ExpandProperty Path)
6-
$rootPath = Split-Path $modulePath -Parent
7-
$rootPath = Split-Path $rootPath -Parent
8-
9-
"$rootPath\aliases.json"
4+
$appData = Join-Path $env:LOCALAPPDATA 'quickpath'
5+
if (-not (Test-Path $appData)) {
6+
New-Item -Path $appData -ItemType Directory -Force | Out-Null
7+
}
8+
9+
$file = Join-Path $appData 'aliases.json'
10+
11+
if (-not (Test-Path $file)) {
12+
'[]' | New-Item -Path $file -ItemType File -Force | Out-Null
13+
}
14+
15+
return $file
1016
}
1117

1218

quickpath.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'quickpath.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '0.0.0'
15+
ModuleVersion = '0.0.3'
1616

1717
# Supported PSEditions
1818
# CompatiblePSEditions = @()

tests/Alias-Helper.Tests.ps1

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Describe 'Alias-Helper' {
1111
$script:JSON_FILE_PATH = "$PSScriptRoot\aliases-test.json"
1212
$script:JSON_CONTENT = Get-Content -Path $script:JSON_FILE_PATH | ConvertFrom-Json
1313

14+
$env:LOCALAPPDATA = "localappdata"
15+
1416
Mock New-Item
1517
Mock Test-Path { return $true }
1618
}
@@ -61,11 +63,26 @@ Describe 'Alias-Helper' {
6163
}
6264
}
6365
context 'Get-Script-Path' {
64-
It 'Returns correct path' {
65-
Mock Get-Module { @{Path = "moduleinstallfolder/moduleversion/modulepath" } }
66+
It 'Creates directory and file if not exist' {
67+
Mock Test-Path { return $false }
68+
Mock New-Item
6669

6770
Get-Script-Path
68-
| Should -Be "moduleinstallfolder\aliases.json"
71+
72+
Assert-MockCalled -CommandName New-Item -Times 2 -Exactly -Scope It
73+
Assert-MockCalled -CommandName New-Item -Times 1 -Exactly -Scope It -ParameterFilter {
74+
$Path -eq (Join-Path $env:LOCALAPPDATA 'quickpath') -and $ItemType -eq 'Directory'
75+
}
76+
Assert-MockCalled -CommandName New-Item -Times 1 -Exactly -Scope It -ParameterFilter {
77+
$Path -eq (Join-Path (Join-Path $env:LOCALAPPDATA 'quickpath') 'aliases.json') -and $ItemType -eq 'File'
78+
}
79+
}
80+
It 'Returns correct file path' {
81+
$expectedPath = Join-Path (Join-Path $env:LOCALAPPDATA 'quickpath') 'aliases.json'
82+
83+
$result = Get-Script-Path
84+
85+
$result | Should -Be $expectedPath
6986
}
7087
}
7188
context 'Get-Alias' {

0 commit comments

Comments
 (0)