-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.Tests.ps1
107 lines (91 loc) · 2.89 KB
/
config.Tests.ps1
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
# Call Invoke-Pester
Set-StrictMode -Version Latest
Import-Module powershell-yaml
Describe "Get-Config" {
It "local config" {
$expected = @{
foo = 'bar'
}
ConvertTo-Yaml $expected | Out-File "$name.yaml"
$actual = Get-Config $name
$actual.Keys | Should -Be $expected.Keys
$actual.foo | Should -Be $expected.foo
}
It "local config (int)" {
$expected = @{
foo = 1
}
ConvertTo-Yaml $expected | Out-File "$name.yaml"
$actual = Get-Config $name
$actual.Keys | Should -Be $expected.Keys
$actual.foo | Should -Be $expected.foo
}
It "user global config" {
$expected = @{
foo = 'bar'
}
ConvertTo-Yaml $expected | Out-File "$Env:APPDATA\$name\config.yaml"
$actual = Get-Config $name
$actual.Keys | Should -Be $expected.Keys
$actual.foo | Should -Be $expected.foo
}
It "system global config" {
$expected = @{
foo = 'bar'
}
ConvertTo-Yaml $expected | Out-File "$Env:ProgramData\$name\config.yaml"
$actual = Get-Config $name
$actual.Keys | Should -Be $expected.Keys
$actual.foo | Should -Be $expected.foo
}
It "joined config" {
$local = @{
bar = 'bar'
}
$user = @{
bar = 'bazz'
}
$system = @{
foo = 'foo'
}
$expected = @{
foo = 'foo'
bar = 'bar'
}
ConvertTo-Yaml $local | Out-File "$name.yaml"
ConvertTo-Yaml $user | Out-File "$Env:APPDATA\$name\config.yaml"
ConvertTo-Yaml $system | Out-File "$Env:ProgramData\$name\config.yaml"
$actual = Get-Config $name
$actual.Keys.Length | Should -Be $expected.Keys.Length
$actual.foo | Should -Be $expected.foo
$actual.bar | Should -Be $expected.bar
}
BeforeAll {
New-Variable -Option Constant, AllScope -Name name -Value 'powershell-config'
New-Variable originalProgramData -Option Constant -Value "$Env:ProgramData"
New-Variable originalAPPDATA -Option Constant -Value "$Env:APPDATA"
New-Variable originalPWD -Option Constant -Value "$PWD"
function New-TemporaryDirectory {
$parent = [System.IO.Path]::GetTempPath()
[String] $temp = New-Guid
New-Item -ItemType Directory -Path (Join-Path $parent $temp)
}
$Env:ProgramData = New-TemporaryDirectory
New-Item -ItemType Directory -Path "$Env:ProgramData\$name"
$Env:APPDATA = New-TemporaryDirectory
New-Item -ItemType Directory -Path "$Env:APPDATA\$name"
Import-Module -Force (Join-Path "$PSScriptRoot" 'config.psm1')
$tempPWD = New-TemporaryDirectory
Set-Location $tempPWD
}
AfterAll {
Remove-Item $Env:ProgramData -Recurse -ErrorAction Ignore
Set-Location $originalPWD
Remove-Item $tempPWD -Recurse
Set-Item Env:\ProgramData -Value $originalProgramData
Set-Item Env:\APPDATA -Value $originalAPPDATA
}
AfterEach {
Remove-Item "$name.yaml", "$Env:APPDATA\$name\config.yaml", "$Env:ProgramData\$name\config.yaml" -ErrorAction Ignore
}
}