Tests/PSConfig.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
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
$ParentPath = Split-Path -Path (Split-Path -Path $MyInvocation.MyCommand.Definition -Parent) -Parent
Import-Module -Name (Join-Path -Path $ParentPath -ChildPath "PSConfig.psm1")


Describe Add-DefaultConfigurationSource {
    Clear-ConfigurationSource
    InModuleScope PSConfig {
        It "Creates a Default Configuration Source" {
            $Script:ConfigurationSources.Count | Should Be 0
            Add-DefaultConfigurationSource -InputObject @{
                Data = "Hello, World!"
            }
            $Script:ConfigurationSources.Count | Should Be 1
            $Script:ConfigurationSources[0].Name | Should BeExactly "Default Values"
            $Script:ConfigurationSources[0].Type | Should BeExactly "Default"
            $Script:ConfigurationSources[0].Data.PSObject.Properties.Count | Should Be 1
            $Script:ConfigurationSources[0].Data.Data | Should BeExactly "Hello, World!"
        }
    }
}


Describe Add-EnvironmentConfigurationSource {
    Clear-ConfigurationSource
    InModuleScope PSConfig {
        It "Creates an Environment Variable Configuration Source" {
            $Script:ConfigurationSources.Count | Should Be 0
            Add-EnvironmentConfigurationSource
            $Script:ConfigurationSources.Count | Should Be 1
            $Script:ConfigurationSources[0].Name | Should BeExactly "Environment Variables"
            $Script:ConfigurationSources[0].Type | Should BeExactly "Environment"
        }
    }
}


Describe Add-FileConfigurationSource {
    Clear-ConfigurationSource
    It "Creates a Configuration Source from a string data file" {
        Mock -ModuleName PSConfig Get-Content { return 'Data = Hello, World!' }
        Mock -ModuleName PSConfig Test-Path { return $true }

        InModuleScope PSConfig {
            $Script:ConfigurationSources.Count | Should Be 0
            Add-FileConfigurationSource -Path "C:\NotARealPath\Test.txt" -Format "StringData"
            $Script:ConfigurationSources.Count | Should Be 1
            $Script:ConfigurationSources[0].Name | Should BeExactly "C:\NotARealPath\Test.txt"
            $Script:ConfigurationSources[0].Type | Should BeExactly "File/StringData"
            $Script:ConfigurationSources[0].Data.PSObject.Properties.Count | Should Be 1
            $Script:ConfigurationSources[0].Data.Data | Should BeExactly "Hello, World!"
        }
    }

    Clear-ConfigurationSource
    It "Creates a Configuration Source from a Json file" {
        Mock -ModuleName PSConfig Get-Content { return '{"Data": "Hello, World!"}' }
        Mock -ModuleName PSConfig Test-Path { return $true }

        InModuleScope PSConfig {
            $Script:ConfigurationSources.Count | Should Be 0
            Add-FileConfigurationSource -Path "C:\NotARealPath\Test.json" -Format "Json"
            $Script:ConfigurationSources.Count | Should Be 1
            $Script:ConfigurationSources[0].Name | Should BeExactly "C:\NotARealPath\Test.json"
            $Script:ConfigurationSources[0].Type | Should BeExactly "File/Json"
            $Script:ConfigurationSources[0].Data.PSObject.Properties.Count | Should Be 1
            $Script:ConfigurationSources[0].Data.Data | Should BeExactly "Hello, World!"
        }
    }

    Clear-ConfigurationSource
    It "Creates a Configuration Source from a Csv file" {
        Mock -ModuleName PSConfig Import-Csv { return "Data`n" + '"Hello, World!"' | ConvertFrom-Csv }
        Mock -ModuleName PSConfig Test-Path { return $true }

        InModuleScope PSConfig {
            $Script:ConfigurationSources.Count | Should Be 0
            Add-FileConfigurationSource -Path "C:\NotARealPath\Test.csv" -Format "Csv"
            $Script:ConfigurationSources.Count | Should Be 1
            $Script:ConfigurationSources[0].Name | Should BeExactly "C:\NotARealPath\Test.csv"
            $Script:ConfigurationSources[0].Type | Should BeExactly "File/Csv"
            $Script:ConfigurationSources[0].Data.PSObject.Properties.Count | Should Be 1
            $Script:ConfigurationSources[0].Data.Data | Should BeExactly "Hello, World!"
        }
    }
}

Describe Add-FileConfigurationSourceMultiline {
    Clear-ConfigurationSource
    It "Creates a Configuration Source from a string data file with multiple lines" {
        Mock -ModuleName PSConfig Get-Content { return "Data = Hello, World!`r`nSecondLine = Some more`r`nThirdLine = This is the Third Line" }
        Mock -ModuleName PSConfig Test-Path { return $true }

        InModuleScope PSConfig {
            $Script:ConfigurationSources.Count | Should Be 0
            Add-FileConfigurationSource -Path "C:\NotARealPath\Test.txt" -Format "StringData"
            $Script:ConfigurationSources.Count | Should Be 1
            $Script:ConfigurationSources[0].Name | Should BeExactly "C:\NotARealPath\Test.txt"
            $Script:ConfigurationSources[0].Type | Should BeExactly "File/StringData"
            ($Script:ConfigurationSources[0].Data.PSObject.Properties | Measure-Object).Count | Should Be 3
            $Script:ConfigurationSources[0].Data.Data | Should BeExactly "Hello, World!"
            $Script:ConfigurationSources[0].Data.SecondLine | Should BeExactly "Some more"
            $Script:ConfigurationSources[0].Data.ThirdLine | Should BeExactly "This is the Third Line"
        }
    }
}

Describe Clear-ConfigurationSource {
    Clear-ConfigurationSource
    Add-DefaultConfigurationSource -InputObject @{
        Data = "Hello, World!"
    }
    InModuleScope PSConfig {
        It "Clears the configuration" {
            $Script:ConfigurationSources.Count | Should Be 1
            Clear-ConfigurationSource
            $Script:ConfigurationSources.Count | Should Be 0
        }
    }
}


Describe Get-ConfigurationItem {
    Clear-ConfigurationSource
    Context "No data loaded" {
        It "Returns null" {
            Get-ConfigurationItem -Key "Nonexistent" | Should BeNullOrEmpty
        }
    }

    Add-EnvironmentConfigurationSource
    Context "Environment Variables" {
        It "Returns null when nothing is found" {
            Get-ConfigurationItem -Key "Data" | Should BeNullOrEmpty
        }

        It "Returns environment variables" {
            Get-ConfigurationItem -Key "PSModulePath" | Should BeExactly $env:PSModulePath
        }
    }

    Clear-ConfigurationSource
    Add-DefaultConfigurationSource -InputObject @{
        Data = "Hello, World!"
    }
    Context "Object Values" {
        It "Returns null when nothing is found" {
            Get-ConfigurationItem -Key "Nonexistent" | Should BeNullOrEmpty
        }

        It "Returns data" {
            Get-ConfigurationItem -Key "Data" | Should BeExactly "Hello, World!"
        }
    }

    Clear-ConfigurationSource
    Add-EnvironmentConfigurationSource
    Add-DefaultConfigurationSource -InputObject @{
        Data = "Hello, World!"
    }
    Context "Multiple Values" {
        It "Returns null when nothing is found" {
            Get-ConfigurationItem -Key "Nonexistent" | Should BeNullOrEmpty
        }

        It "Returns data when found in first configuration source" {
            Get-ConfigurationItem -Key "PSModulePath" | Should BeExactly $env:PSModulePath
        }

        It "Returns data when found in second configuration source" {
            Get-ConfigurationItem -Key "Data" | Should BeExactly "Hello, World!"
        }
    }
}


Remove-Module -Name "PSConfig"