Tests/Unit/Get-ScriptConfig.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 |
$modulePath = Resolve-Path -Path "$PSScriptRoot\..\..\.." | Select-Object -ExpandProperty Path $moduleName = Resolve-Path -Path "$PSScriptRoot\..\.." | Get-Item | Select-Object -ExpandProperty BaseName Remove-Module -Name $moduleName -Force -ErrorAction SilentlyContinue Import-Module -Name "$modulePath\$moduleName" -Force Describe 'Get-ScriptConfig' { Context 'Test Data' { It 'shloud be able to load a valid INI configuration file' { # Arrange $expectedArray = @( 'Lorem', 'Ipsum' ) $expectedHashtable = @{ Foo = 'Bar'; Hello = 'World' } # Act $config = Get-ScriptConfig -Path "$PSScriptRoot\TestData\config.ini" -Format 'INI' # Assert $config | Should -Not -BeNullOrEmpty $config.MyString | Should -Be 'This is a test INI config file!' $config.MyIntegerPositive | Should -Be 42 $config.MyIntegerNegative | Should -Be -153 $config.MyBooleanTrue | Should -BeTrue $config.MyBooleanFalse | Should -BeFalse $config.MyArray | Should -Be $expectedArray $config.MyHashtable.Keys -as [string[]] | Should -Be ($expectedHashtable.Keys -as [string[]]) $config.MyHashtable.Values -as [string[]] | Should -Be ($expectedHashtable.Values -as [string[]]) } It 'shloud be able to load a valid JSON configuration file' { # Arrange $expectedArray = @( 'Lorem', 'Ipsum' ) $expectedHashtable = @{ Foo = 'Bar'; Hello = 'World' } # Act $config = Get-ScriptConfig -Path "$PSScriptRoot\TestData\config.json" -Format 'JSON' # Assert $config | Should -Not -BeNullOrEmpty $config.MyString | Should -Be 'This is a test JSON config file!' $config.MyIntegerPositive | Should -Be 42 $config.MyIntegerNegative | Should -Be -153 $config.MyBooleanTrue | Should -BeTrue $config.MyBooleanFalse | Should -BeFalse $config.MyArray | Should -Be $expectedArray $config.MyHashtable.Keys -as [string[]] | Should -Be ($expectedHashtable.Keys -as [string[]]) $config.MyHashtable.Values -as [string[]] | Should -Be ($expectedHashtable.Values -as [string[]]) } It 'shloud be able to load a valid XML configuration file' { # Arrange $expectedArray = @( 'Lorem', 'Ipsum' ) $expectedHashtable = @{ Foo = 'Bar'; Hello = 'World' } # Act $config = Get-ScriptConfig -Path "$PSScriptRoot\TestData\config.xml" -Format 'XML' # Assert $config | Should -Not -BeNullOrEmpty $config.MyString | Should -Be 'This is a test XML config file!' $config.MyIntegerPositive | Should -Be 42 $config.MyIntegerNegative | Should -Be -153 $config.MyBooleanTrue | Should -BeTrue $config.MyBooleanFalse | Should -BeFalse $config.MyArray | Should -Be $expectedArray $config.MyHashtable.Keys -as [string[]] | Should -Be ($expectedHashtable.Keys -as [string[]]) $config.MyHashtable.Values -as [string[]] | Should -Be ($expectedHashtable.Values -as [string[]]) } } Context 'Mocked Convert Script' { Mock ConvertFrom-ScriptConfigIni { return @{} } -ModuleName 'ScriptConfig' -Verifiable Mock ConvertFrom-ScriptConfigJson { return @{} } -ModuleName 'ScriptConfig' -Verifiable Mock ConvertFrom-ScriptConfigXml { return @{} } -ModuleName 'ScriptConfig' -Verifiable It 'should call the INI function if a .ini file is specified' { # Act Get-ScriptConfig -Path "$PSScriptRoot\TestData\config.ini" | Out-Null # Assert Assert-MockCalled 'ConvertFrom-ScriptConfigIni' -ModuleName 'ScriptConfig' -Scope 'It' -Times 1 -Exactly } It 'should call the JSON function if a .json file is specified' { # Act Get-ScriptConfig -Path "$PSScriptRoot\TestData\config.json" | Out-Null # Assert Assert-MockCalled 'ConvertFrom-ScriptConfigJson' -ModuleName 'ScriptConfig' -Scope 'It' -Times 1 -Exactly } It 'should call the XML function if a .xml file is specified' { # Act Get-ScriptConfig -Path "$PSScriptRoot\TestData\config.xml" | Out-Null # Assert Assert-MockCalled 'ConvertFrom-ScriptConfigXml' -ModuleName 'ScriptConfig' -Scope 'It' -Times 1 -Exactly } It 'should call the INI function if INI format is specified' { # Act Get-ScriptConfig -Path "$PSScriptRoot\TestData\config.ini" -Format 'INI' | Out-Null # Assert Assert-MockCalled 'ConvertFrom-ScriptConfigIni' -ModuleName 'ScriptConfig' -Scope 'It' -Times 1 -Exactly } It 'should call the JSON function if JSON format is specified' { # Act Get-ScriptConfig -Path "$PSScriptRoot\TestData\config.json" -Format 'JSON' | Out-Null # Assert Assert-MockCalled 'ConvertFrom-ScriptConfigJson' -ModuleName 'ScriptConfig' -Scope 'It' -Times 1 -Exactly } It 'should call the XML function if XML format is specified' { # Act Get-ScriptConfig -Path "$PSScriptRoot\TestData\config.xml" -Format 'XML' | Out-Null # Assert Assert-MockCalled 'ConvertFrom-ScriptConfigXml' -ModuleName 'ScriptConfig' -Scope 'It' -Times 1 -Exactly } } } |