diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b43518..3171c11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,30 +6,36 @@ The format is mainly based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## Unreleased + +* Changed: Get the default config path from the PS call stack +* Changed: The default configuration file type is now JSON (BREAKING CHANGE) + + ## 2.0.0 -- Changed: Convert module to new deployment model -- Changed: Rework code against high quality module guidelines by Microsoft -- Changed: Remove positional parameters (BREAKING CHANGE) +* Changed: Convert module to new deployment model +* Changed: Rework code against high quality module guidelines by Microsoft +* Changed: Remove positional parameters (BREAKING CHANGE) ## 1.0.3 -- Added: Formats and types resources +* Added: Formats and types resources ## 1.0.2 -- Fixed: Default path issue for Get-ScriptConfig +* Fixed: Default path issue for Get-ScriptConfig ## 1.0.1 -- Updated: File encoding to UTF8 w/o BOM -- Updated: Demo and help -- Added: Enhance parameters for Get-ScriptConfig function +* Updated: File encoding to UTF8 w/o BOM +* Updated: Demo and help +* Added: Enhance parameters for Get-ScriptConfig function ## 1.0.0 -- Added: Initial public release +* Added: Initial public release diff --git a/Modules/ScriptConfig/Functions/Get-ScriptConfig.ps1 b/Modules/ScriptConfig/Functions/Get-ScriptConfig.ps1 index c77d747..52acafa 100644 --- a/Modules/ScriptConfig/Functions/Get-ScriptConfig.ps1 +++ b/Modules/ScriptConfig/Functions/Get-ScriptConfig.ps1 @@ -1,34 +1,33 @@ <# .SYNOPSIS - Load a script configuration from a config file. + Load a script configuration from a config file. .DESCRIPTION - Load a script configuration from a config file. By default, the config file - next to the script is loaded. So for the script MyScript.ps1, the config - file MyScript.ps1.config will be loaded. The config file format will be - detected by it's extension (.xml, .ini, .json). If the file format can't be - detected, the default format (XML) will be used. + Load a script configuration from a config file. By default, the config + file next to the script is loaded. So for the script MyScript.ps1, the + config file MyScript.ps1.config will be loaded. The config file format + will be detected by it's extension (.xml, .ini, .json). If the file + format can't be detected, the default format (JSON) will be used. .EXAMPLE - PS C:\> $config = Get-ScriptConfig - Loads the default XML formatted configuration file. + PS C:\> $config = Get-ScriptConfig + Loads the default JSON formatted configuration file. .EXAMPLE - PS C:\> $config = Get-ScriptConfig -Path 'C:\MyApp\global.config' - Loads a custom configuration file, with default XML format. + PS C:\> $config = Get-ScriptConfig -Path 'C:\MyApp\global.config' + Loads a custom configuration file, with default JSON format. .EXAMPLE - PS C:\> $config = Get-ScriptConfig -Format JSON - Loads the default configuration file but in JSON format. + PS C:\> $config = Get-ScriptConfig -Format XML + Loads the default configuration file but in XML format. .NOTES - Author : Claudio Spizzi - License : MIT License + Author : Claudio Spizzi + License : MIT License .LINK - https://github.com/claudiospizzi/ScriptConfig + https://github.com/claudiospizzi/ScriptConfig #> - function Get-ScriptConfig { [CmdletBinding()] @@ -37,9 +36,8 @@ function Get-ScriptConfig # Specify the path to the configuration file. By default, the current # script file path will be used with an appended '.config' extension. [Parameter(Mandatory = $false)] - [ValidateScript({Test-Path -Path $_})] [System.String] - $Path = ([String] $Global:MyInvocation.MyCommand.Definition).Trim() + '.config', + $Path, # Override the format detection and default value. [Parameter(Mandatory = $false)] @@ -48,18 +46,38 @@ function Get-ScriptConfig $Format ) - # Only work with absolute path, makes error handling easier - $Path = (Resolve-Path -Path $Path).Path + # If the Path parameter was not specified, add a default value. If possible, + # use the last script called this function. Else throw an exception. + if (-not $PSBoundParameters.ContainsKey('Path')) + { + $lastScriptPath = Get-PSCallStack | Select-Object -Skip 1 -First 1 -ExpandProperty 'ScriptName' - # If no format was specified, try to detect it - if ([String]::IsNullOrEmpty($Format)) + if (-not [System.String]::IsNullOrEmpty($lastScriptPath)) + { + $Path = $lastScriptPath + '.config' + } + else + { + throw "Configuration file not specified" + } + } + + # Now check, if the configuration file exists + if (-not (Test-Path -Path $Path)) + { + throw "Configuration file not found: $Path" + } + + # If the Format parameter was not specified, try to detect by the file + # extension or use the default format JSON. + if (-not $PSBoundParameters.ContainsKey('Format')) { switch -Wildcard ($Path) { '*.xml' { $Format = 'XML' } '*.json' { $Format = 'JSON' } '*.ini' { $Format = 'INI' } - default { $Format = 'XML' } + default { $Format = 'JSON' } } } @@ -68,17 +86,11 @@ function Get-ScriptConfig # Load raw content, parse it later $content = Get-Content -Path $Path -ErrorAction Stop - # Use custom functions to parse the files + # Use custom functions to parse the files and return the config object switch ($Format) { - 'XML' { $configHashtable = ConvertFrom-ScriptConfigXml -Content $content } - 'JSON' { $configHashtable = ConvertFrom-ScriptConfigJson -Content $content } - 'INI' { $configHashtable = ConvertFrom-ScriptConfigIni -Content $content } + 'XML' { ConvertFrom-ScriptConfigXml -Content $content } + 'JSON' { ConvertFrom-ScriptConfigJson -Content $content } + 'INI' { ConvertFrom-ScriptConfigIni -Content $content } } - - # Create a config object with a custom type - $config = New-Object -TypeName PSObject -Property $configHashtable - $config.PSTypeNames.Insert(0, 'ScriptConfig.Configuration') - - Write-Output $config } diff --git a/Modules/ScriptConfig/Helpers/ConvertFrom-ScriptConfigIni.ps1 b/Modules/ScriptConfig/Helpers/ConvertFrom-ScriptConfigIni.ps1 index a543cdd..ba30322 100644 --- a/Modules/ScriptConfig/Helpers/ConvertFrom-ScriptConfigIni.ps1 +++ b/Modules/ScriptConfig/Helpers/ConvertFrom-ScriptConfigIni.ps1 @@ -1,19 +1,19 @@ <# .SYNOPSIS - Convert the INI file content to a hashtable containing the configuration. + Convert the INI file content to a hashtable containing the + configuration. .EXAMPLE - PS C:\> Get-Content -Path 'config.ini' | ConvertFrom-ScriptConfigIni - Use the pipeline input to parse the INI file content. + PS C:\> Get-Content -Path 'config.ini' | ConvertFrom-ScriptConfigIni + Use the pipeline input to parse the INI file content. .NOTES - Author : Claudio Spizzi - License : MIT License + Author : Claudio Spizzi + License : MIT License .LINK - https://github.com/claudiospizzi/ScriptConfig + https://github.com/claudiospizzi/ScriptConfig #> - function ConvertFrom-ScriptConfigIni { [CmdletBinding()] @@ -26,7 +26,9 @@ function ConvertFrom-ScriptConfigIni $Content ) - $config = @{} + $config = @{ + PSTypeName = 'ScriptConfig.Configuration' + } try { @@ -99,7 +101,7 @@ function ConvertFrom-ScriptConfigIni } } - Write-Output $config + [PSCustomObject] $config } catch { diff --git a/Modules/ScriptConfig/Helpers/ConvertFrom-ScriptConfigJson.ps1 b/Modules/ScriptConfig/Helpers/ConvertFrom-ScriptConfigJson.ps1 index c48a309..ee655b0 100644 --- a/Modules/ScriptConfig/Helpers/ConvertFrom-ScriptConfigJson.ps1 +++ b/Modules/ScriptConfig/Helpers/ConvertFrom-ScriptConfigJson.ps1 @@ -1,19 +1,19 @@ <# .SYNOPSIS - Convert the JSON file content to a hashtable containing the configuration. + Convert the JSON file content to a hashtable containing the + configuration. .EXAMPLE - PS C:\> Get-Content -Path 'config.json' | ConvertFrom-ScriptConfigJson - Use the pipeline input to parse the JSON file content. + PS C:\> Get-Content -Path 'config.json' | ConvertFrom-ScriptConfigJson + Use the pipeline input to parse the JSON file content. .NOTES - Author : Claudio Spizzi - License : MIT License + Author : Claudio Spizzi + License : MIT License .LINK - https://github.com/claudiospizzi/ScriptConfig + https://github.com/claudiospizzi/ScriptConfig #> - function ConvertFrom-ScriptConfigJson { [CmdletBinding()] diff --git a/Modules/ScriptConfig/Helpers/ConvertFrom-ScriptConfigXml.ps1 b/Modules/ScriptConfig/Helpers/ConvertFrom-ScriptConfigXml.ps1 index 67f03d6..4b65193 100644 --- a/Modules/ScriptConfig/Helpers/ConvertFrom-ScriptConfigXml.ps1 +++ b/Modules/ScriptConfig/Helpers/ConvertFrom-ScriptConfigXml.ps1 @@ -1,19 +1,19 @@ <# .SYNOPSIS - Convert the XML file content to a hashtable containing the configuration. + Convert the XML file content to a hashtable containing the + configuration. .EXAMPLE - PS C:\> Get-Content -Path 'config.xml' | ConvertFrom-ScriptConfigXml - Use the pipeline input to parse the XML file content. + PS C:\> Get-Content -Path 'config.xml' | ConvertFrom-ScriptConfigXml + Use the pipeline input to parse the XML file content. .NOTES - Author : Claudio Spizzi - License : MIT License + Author : Claudio Spizzi + License : MIT License .LINK - https://github.com/claudiospizzi/ScriptConfig + https://github.com/claudiospizzi/ScriptConfig #> - function ConvertFrom-ScriptConfigXml { [CmdletBinding()] diff --git a/Modules/ScriptConfig/Tests/Integration/IniConfigDemo.ps1 b/Modules/ScriptConfig/Tests/Integration/IniConfigDemo.ps1 index eff27ca..c66d7a7 100644 --- a/Modules/ScriptConfig/Tests/Integration/IniConfigDemo.ps1 +++ b/Modules/ScriptConfig/Tests/Integration/IniConfigDemo.ps1 @@ -1,16 +1,16 @@ # Load the default configuration file in INI format -$Config = Get-ScriptConfig -Format INI +$Config = Get-ScriptConfig -Format 'INI' # Access the configuration settings -Write-Host "String :" $Config.MyString -Write-Host "Integer Positive :" $Config.MyIntegerPositive -Write-Host "Integer Negative :" $Config.MyIntegerNegative -Write-Host "Boolean True :" $Config.MyBooleanTrue -Write-Host "Boolean False :" $Config.MyBooleanFalse -Write-Host "Array :" $Config.MyArray -Write-Host "Array Item :" $Config.MyArray[0] -Write-Host "Hashtable :" $Config.MyHashtable -Write-Host "Hashtable Item :" $Config.MyHashtable['Hello'] -Write-Host "Hashtable Keys :" $Config.MyHashtable.Keys -Write-Host "Hashtable Values :" $Config.MyHashtable.Values +Write-Verbose "String :" $Config.MyString +Write-Verbose "Integer Positive :" $Config.MyIntegerPositive +Write-Verbose "Integer Negative :" $Config.MyIntegerNegative +Write-Verbose "Boolean True :" $Config.MyBooleanTrue +Write-Verbose "Boolean False :" $Config.MyBooleanFalse +Write-Verbose "Array :" $Config.MyArray +Write-Verbose "Array Item :" $Config.MyArray[0] +Write-Verbose "Hashtable :" $Config.MyHashtable +Write-Verbose "Hashtable Item :" $Config.MyHashtable['Hello'] +Write-Verbose "Hashtable Keys :" $Config.MyHashtable.Keys +Write-Verbose "Hashtable Values :" $Config.MyHashtable.Values diff --git a/Modules/ScriptConfig/Tests/Integration/JsonConfigDemo.ps1 b/Modules/ScriptConfig/Tests/Integration/JsonConfigDemo.ps1 index e83a49c..dff35de 100644 --- a/Modules/ScriptConfig/Tests/Integration/JsonConfigDemo.ps1 +++ b/Modules/ScriptConfig/Tests/Integration/JsonConfigDemo.ps1 @@ -1,16 +1,16 @@ # Load the default configuration file in JSON format -$Config = Get-ScriptConfig -Format JSON +$Config = Get-ScriptConfig -Format 'JSON' # Access the configuration settings -Write-Host "String :" $Config.MyString -Write-Host "Integer Positive :" $Config.MyIntegerPositive -Write-Host "Integer Negative :" $Config.MyIntegerNegative -Write-Host "Boolean True :" $Config.MyBooleanTrue -Write-Host "Boolean False :" $Config.MyBooleanFalse -Write-Host "Array :" $Config.MyArray -Write-Host "Array Item :" $Config.MyArray[0] -Write-Host "Hashtable :" $Config.MyHashtable -Write-Host "Hashtable Item :" $Config.MyHashtable['Hello'] -Write-Host "Hashtable Keys :" $Config.MyHashtable.Keys -Write-Host "Hashtable Values :" $Config.MyHashtable.Values +Write-Verbose "String :" $Config.MyString +Write-Verbose "Integer Positive :" $Config.MyIntegerPositive +Write-Verbose "Integer Negative :" $Config.MyIntegerNegative +Write-Verbose "Boolean True :" $Config.MyBooleanTrue +Write-Verbose "Boolean False :" $Config.MyBooleanFalse +Write-Verbose "Array :" $Config.MyArray +Write-Verbose "Array Item :" $Config.MyArray[0] +Write-Verbose "Hashtable :" $Config.MyHashtable +Write-Verbose "Hashtable Item :" $Config.MyHashtable['Hello'] +Write-Verbose "Hashtable Keys :" $Config.MyHashtable.Keys +Write-Verbose "Hashtable Values :" $Config.MyHashtable.Values diff --git a/Modules/ScriptConfig/Tests/Integration/XmlConfigDemo.ps1 b/Modules/ScriptConfig/Tests/Integration/XmlConfigDemo.ps1 index 7a4ef75..4a39011 100644 --- a/Modules/ScriptConfig/Tests/Integration/XmlConfigDemo.ps1 +++ b/Modules/ScriptConfig/Tests/Integration/XmlConfigDemo.ps1 @@ -1,16 +1,16 @@ # Load the default configuration file in XML format -$Config = Get-ScriptConfig -Format XML +$Config = Get-ScriptConfig -Format 'XML' # Access the configuration settings -Write-Host "String :" $Config.MyString -Write-Host "Integer Positive :" $Config.MyIntegerPositive -Write-Host "Integer Negative :" $Config.MyIntegerNegative -Write-Host "Boolean True :" $Config.MyBooleanTrue -Write-Host "Boolean False :" $Config.MyBooleanFalse -Write-Host "Array :" $Config.MyArray -Write-Host "Array Item :" $Config.MyArray[0] -Write-Host "Hashtable :" $Config.MyHashtable -Write-Host "Hashtable Item :" $Config.MyHashtable['Hello'] -Write-Host "Hashtable Keys :" $Config.MyHashtable.Keys -Write-Host "Hashtable Values :" $Config.MyHashtable.Values +Write-Verbose "String :" $Config.MyString +Write-Verbose "Integer Positive :" $Config.MyIntegerPositive +Write-Verbose "Integer Negative :" $Config.MyIntegerNegative +Write-Verbose "Boolean True :" $Config.MyBooleanTrue +Write-Verbose "Boolean False :" $Config.MyBooleanFalse +Write-Verbose "Array :" $Config.MyArray +Write-Verbose "Array Item :" $Config.MyArray[0] +Write-Verbose "Hashtable :" $Config.MyHashtable +Write-Verbose "Hashtable Item :" $Config.MyHashtable['Hello'] +Write-Verbose "Hashtable Keys :" $Config.MyHashtable.Keys +Write-Verbose "Hashtable Values :" $Config.MyHashtable.Values diff --git a/Modules/ScriptConfig/Tests/Unit/ConvertFrom-ScriptConfigIni.Tests.ps1 b/Modules/ScriptConfig/Tests/Unit/ConvertFrom-ScriptConfigIni.Tests.ps1 index 7682db9..6a3f732 100644 --- a/Modules/ScriptConfig/Tests/Unit/ConvertFrom-ScriptConfigIni.Tests.ps1 +++ b/Modules/ScriptConfig/Tests/Unit/ConvertFrom-ScriptConfigIni.Tests.ps1 @@ -1,78 +1,98 @@ -$ModulePath = Resolve-Path -Path "$PSScriptRoot\..\..\Modules" | ForEach-Object Path -$ModuleName = Get-ChildItem -Path $ModulePath | Select-Object -First 1 -ExpandProperty BaseName +$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 +Remove-Module -Name $moduleName -Force -ErrorAction SilentlyContinue +Import-Module -Name "$modulePath\$moduleName" -Force InModuleScope ScriptConfig { Describe 'ConvertFrom-ScriptConfigIni' { - $ResultString = 'This is a test INI config file!' - $ResultIntegerPositive = 42 - $ResultIntegerNegative = -153 - $ResultBooleanTrue = $true - $ResultBooleanFalse = $false - $ResultArray = @( 'Lorem', 'Ipsum' ) - $ResultHashtable = @{ Foo = 'Bar'; Hello = 'World' } - - $Content = Get-Content -Path "$PSScriptRoot\TestData\config.ini" - It 'should be able to convert the example config file' { - $Config = ConvertFrom-ScriptConfigIni -Content $Content + # Arrange + $content = Get-Content -Path "$PSScriptRoot\TestData\config.ini" + + # Act + $config = ConvertFrom-ScriptConfigIni -Content $content - $Config | Should Not BeNullOrEmpty + # Assert + $config | Should -Not -BeNullOrEmpty } It 'shloud be able to parse a string' { - $Config = ConvertFrom-ScriptConfigIni -Content $Content + # Arrange + $content = Get-Content -Path "$PSScriptRoot\TestData\config.ini" - $Config.MyString | Should Be $ResultString - $Config.MyString.GetType() | Should Be ([System.String]) + # Act + $config = ConvertFrom-ScriptConfigIni -Content $content + + # Assert + $config.MyString | Should -Be 'This is a test INI config file!' + $config.MyString.GetType() | Should -Be ([System.String]) } It 'shloud be able to parse an integer' { - $Config = ConvertFrom-ScriptConfigIni -Content $Content + # Arrange + $content = Get-Content -Path "$PSScriptRoot\TestData\config.ini" - $Config.MyIntegerPositive | Should Be $ResultIntegerPositive - $Config.MyIntegerPositive.GetType() | Should Be ([System.Int32]) + # Act + $config = ConvertFrom-ScriptConfigIni -Content $content - $Config.MyIntegerNegative | Should Be $ResultIntegerNegative - $Config.MyIntegerNegative.GetType() | Should Be ([System.Int32]) + # Assert + $config.MyIntegerPositive | Should -Be 42 + $config.MyIntegerPositive.GetType() | Should -Be ([System.Int32]) + $config.MyIntegerNegative | Should -Be -153 + $config.MyIntegerNegative.GetType() | Should -Be ([System.Int32]) } It 'shloud be able to parse an boolean' { - $Config = ConvertFrom-ScriptConfigIni -Content $Content + # Arrange + $content = Get-Content -Path "$PSScriptRoot\TestData\config.ini" - $Config.MyBooleanTrue | Should Be $ResultBooleanTrue - $Config.MyBooleanTrue.GetType() | Should Be ([System.Boolean]) + # Act + $config = ConvertFrom-ScriptConfigIni -Content $content - $Config.MyBooleanFalse | Should Be $ResultBooleanFalse - $Config.MyBooleanFalse.GetType() | Should Be ([System.Boolean]) + # Assert + $config.MyBooleanTrue | Should -BeTrue + $config.MyBooleanTrue.GetType() | Should -Be ([System.Boolean]) + $config.MyBooleanFalse | Should -BeFalse + $config.MyBooleanFalse.GetType() | Should -Be ([System.Boolean]) } It 'shloud be able to parse an array' { - $Config = ConvertFrom-ScriptConfigIni -Content $Content + # Arrange + $content = Get-Content -Path "$PSScriptRoot\TestData\config.ini" + $expectedArray = @( 'Lorem', 'Ipsum' ) + + # Act + $config = ConvertFrom-ScriptConfigIni -Content $content - $Config.MyArray | Should Not BeNullOrEmpty - $Config.MyArray | Should Be $ResultArray - $Config.MyArray.GetType() | Should Be ([System.Object[]]) + # Assert + $config.MyArray | Should -Not -BeNullOrEmpty + $config.MyArray | Should -Be $expectedArray + $config.MyArray.GetType() | Should -Be ([System.Object[]]) } It 'shloud be able to parse an hashtable' { - $Config = ConvertFrom-ScriptConfigIni -Content $Content + # Arrange + $content = Get-Content -Path "$PSScriptRoot\TestData\config.ini" + $expectedHashtable = @{ Foo = 'Bar'; Hello = 'World' } + + # Act + $config = ConvertFrom-ScriptConfigIni -Content $content - $Config.MyHashtable | Should Not BeNullOrEmpty - $Config.MyHashtable.Keys | Should Be $ResultHashtable.Keys - $Config.MyHashtable.Values | Should Be $ResultHashtable.Values - $Config.MyHashtable.GetType() | Should Be ([System.Collections.Hashtable]) + # Assert + $config.MyHashtable | Should -Not -BeNullOrEmpty + $config.MyHashtable.Keys -as [string[]] | Should -Be ($expectedHashtable.Keys -as [string[]]) + $config.MyHashtable.Values -as [string[]] | Should -Be ($expectedHashtable.Values -as [string[]]) + $config.MyHashtable.GetType() | Should -Be ([System.Collections.Hashtable]) } } } diff --git a/Modules/ScriptConfig/Tests/Unit/ConvertFrom-ScriptConfigJson.Tests.ps1 b/Modules/ScriptConfig/Tests/Unit/ConvertFrom-ScriptConfigJson.Tests.ps1 index 1347879..a65250e 100644 --- a/Modules/ScriptConfig/Tests/Unit/ConvertFrom-ScriptConfigJson.Tests.ps1 +++ b/Modules/ScriptConfig/Tests/Unit/ConvertFrom-ScriptConfigJson.Tests.ps1 @@ -1,78 +1,98 @@ -$ModulePath = Resolve-Path -Path "$PSScriptRoot\..\..\Modules" | ForEach-Object Path -$ModuleName = Get-ChildItem -Path $ModulePath | Select-Object -First 1 -ExpandProperty BaseName +$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 +Remove-Module -Name $moduleName -Force -ErrorAction SilentlyContinue +Import-Module -Name "$modulePath\$moduleName" -Force InModuleScope ScriptConfig { Describe 'ConvertFrom-ScriptConfigJson' { - $ResultString = 'This is a test JSON config file!' - $ResultIntegerPositive = 42 - $ResultIntegerNegative = -153 - $ResultBooleanTrue = $true - $ResultBooleanFalse = $false - $ResultArray = @( 'Lorem', 'Ipsum' ) - $ResultHashtable = @{ Foo = 'Bar'; Hello = 'World' } - - $Content = Get-Content -Path "$PSScriptRoot\TestData\config.json" - It 'should be able to convert the example config file' { - $Config = ConvertFrom-ScriptConfigJson -Content $Content + # Arrange + $content = Get-Content -Path "$PSScriptRoot\TestData\config.json" + + # Act + $config = ConvertFrom-ScriptConfigJson -Content $content - $Config | Should Not BeNullOrEmpty + # Assert + $config | Should -Not -BeNullOrEmpty } It 'shloud be able to parse a string' { - $Config = ConvertFrom-ScriptConfigJson -Content $Content + # Arrange + $content = Get-Content -Path "$PSScriptRoot\TestData\config.json" - $Config.MyString | Should Be $ResultString - $Config.MyString.GetType() | Should Be ([System.String]) + # Act + $config = ConvertFrom-ScriptConfigJson -Content $content + + # Assert + $config.MyString | Should -Be 'This is a test JSON config file!' + $config.MyString.GetType() | Should -Be ([System.String]) } It 'shloud be able to parse an integer' { - $Config = ConvertFrom-ScriptConfigJson -Content $Content + # Arrange + $content = Get-Content -Path "$PSScriptRoot\TestData\config.json" - $Config.MyIntegerPositive | Should Be $ResultIntegerPositive - $Config.MyIntegerPositive.GetType() | Should Be ([System.Int32]) + # Act + $config = ConvertFrom-ScriptConfigJson -Content $content - $Config.MyIntegerNegative | Should Be $ResultIntegerNegative - $Config.MyIntegerNegative.GetType() | Should Be ([System.Int32]) + # Assert + $config.MyIntegerPositive | Should -Be 42 + $config.MyIntegerPositive.GetType() | Should -Be ([System.Int32]) + $config.MyIntegerNegative | Should -Be -153 + $config.MyIntegerNegative.GetType() | Should -Be ([System.Int32]) } It 'shloud be able to parse an boolean' { - $Config = ConvertFrom-ScriptConfigJson -Content $Content + # Arrange + $content = Get-Content -Path "$PSScriptRoot\TestData\config.json" - $Config.MyBooleanTrue | Should Be $ResultBooleanTrue - $Config.MyBooleanTrue.GetType() | Should Be ([System.Boolean]) + # Act + $config = ConvertFrom-ScriptConfigJson -Content $content - $Config.MyBooleanFalse | Should Be $ResultBooleanFalse - $Config.MyBooleanFalse.GetType() | Should Be ([System.Boolean]) + # Assert + $config.MyBooleanTrue | Should -BeTrue + $config.MyBooleanTrue.GetType() | Should -Be ([System.Boolean]) + $config.MyBooleanFalse | Should -BeFalse + $config.MyBooleanFalse.GetType() | Should -Be ([System.Boolean]) } It 'shloud be able to parse an array' { - $Config = ConvertFrom-ScriptConfigJson -Content $Content + # Arrange + $content = Get-Content -Path "$PSScriptRoot\TestData\config.json" + $expectedArray = @( 'Lorem', 'Ipsum' ) + + # Act + $config = ConvertFrom-ScriptConfigJson -Content $content - $Config.MyArray | Should Not BeNullOrEmpty - $Config.MyArray | Should Be $ResultArray - $Config.MyArray.GetType() | Should Be ([System.Object[]]) + # Assert + $config.MyArray | Should -Not -BeNullOrEmpty + $config.MyArray | Should -Be $expectedArray + $config.MyArray.GetType() | Should -Be ([System.Object[]]) } It 'shloud be able to parse an hashtable' { - $Config = ConvertFrom-ScriptConfigJson -Content $Content + # Arrange + $content = Get-Content -Path "$PSScriptRoot\TestData\config.json" + $expectedHashtable = @{ Foo = 'Bar'; Hello = 'World' } + + # Act + $config = ConvertFrom-ScriptConfigJson -Content $content - $Config.MyHashtable | Should Not BeNullOrEmpty - $Config.MyHashtable.Keys | Should Be $ResultHashtable.Keys - $Config.MyHashtable.Values | Should Be $ResultHashtable.Values - $Config.MyHashtable.GetType() | Should Be ([System.Collections.Hashtable]) + # Assert + $config.MyHashtable | Should -Not -BeNullOrEmpty + $config.MyHashtable.Keys -as [string[]] | Should -Be ($expectedHashtable.Keys -as [string[]]) + $config.MyHashtable.Values -as [string[]] | Should -Be ($expectedHashtable.Values -as [string[]]) + $config.MyHashtable.GetType() | Should -Be ([System.Collections.Hashtable]) } } } diff --git a/Modules/ScriptConfig/Tests/Unit/ConvertFrom-ScriptConfigXml.Tests.ps1 b/Modules/ScriptConfig/Tests/Unit/ConvertFrom-ScriptConfigXml.Tests.ps1 index 8e5e586..bf8cb80 100644 --- a/Modules/ScriptConfig/Tests/Unit/ConvertFrom-ScriptConfigXml.Tests.ps1 +++ b/Modules/ScriptConfig/Tests/Unit/ConvertFrom-ScriptConfigXml.Tests.ps1 @@ -1,78 +1,98 @@ -$ModulePath = Resolve-Path -Path "$PSScriptRoot\..\..\Modules" | ForEach-Object Path -$ModuleName = Get-ChildItem -Path $ModulePath | Select-Object -First 1 -ExpandProperty BaseName +$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 +Remove-Module -Name $moduleName -Force -ErrorAction SilentlyContinue +Import-Module -Name "$modulePath\$moduleName" -Force InModuleScope ScriptConfig { Describe 'ConvertFrom-ScriptConfigXml' { - $ResultString = 'This is a test XML config file!' - $ResultIntegerPositive = 42 - $ResultIntegerNegative = -153 - $ResultBooleanTrue = $true - $ResultBooleanFalse = $false - $ResultArray = @( 'Lorem', 'Ipsum' ) - $ResultHashtable = @{ Foo = 'Bar'; Hello = 'World' } - - $Content = Get-Content -Path "$PSScriptRoot\TestData\config.xml" - It 'should be able to convert the example config file' { - $Config = ConvertFrom-ScriptConfigXml -Content $Content + # Arrange + $content = Get-Content -Path "$PSScriptRoot\TestData\config.xml" + + # Act + $config = ConvertFrom-ScriptConfigXml -Content $content - $Config | Should Not BeNullOrEmpty + # Assert + $config | Should -Not -BeNullOrEmpty } It 'shloud be able to parse a string' { - $Config = ConvertFrom-ScriptConfigXml -Content $Content + # Arrange + $content = Get-Content -Path "$PSScriptRoot\TestData\config.xml" - $Config.MyString | Should Be $ResultString - $Config.MyString.GetType() | Should Be ([System.String]) + # Act + $config = ConvertFrom-ScriptConfigXml -Content $content + + # Assert + $config.MyString | Should -Be 'This is a test XML config file!' + $config.MyString.GetType() | Should -Be ([System.String]) } It 'shloud be able to parse an integer' { - $Config = ConvertFrom-ScriptConfigXml -Content $Content + # Arrange + $content = Get-Content -Path "$PSScriptRoot\TestData\config.xml" - $Config.MyIntegerPositive | Should Be $ResultIntegerPositive - $Config.MyIntegerPositive.GetType() | Should Be ([System.Int32]) + # Act + $config = ConvertFrom-ScriptConfigXml -Content $content - $Config.MyIntegerNegative | Should Be $ResultIntegerNegative - $Config.MyIntegerNegative.GetType() | Should Be ([System.Int32]) + # Assert + $config.MyIntegerPositive | Should -Be 42 + $config.MyIntegerPositive.GetType() | Should -Be ([System.Int32]) + $config.MyIntegerNegative | Should -Be -153 + $config.MyIntegerNegative.GetType() | Should -Be ([System.Int32]) } It 'shloud be able to parse an boolean' { - $Config = ConvertFrom-ScriptConfigXml -Content $Content + # Arrange + $content = Get-Content -Path "$PSScriptRoot\TestData\config.xml" - $Config.MyBooleanTrue | Should Be $ResultBooleanTrue - $Config.MyBooleanTrue.GetType() | Should Be ([System.Boolean]) + # Act + $config = ConvertFrom-ScriptConfigXml -Content $content - $Config.MyBooleanFalse | Should Be $ResultBooleanFalse - $Config.MyBooleanFalse.GetType() | Should Be ([System.Boolean]) + # Assert + $config.MyBooleanTrue | Should -BeTrue + $config.MyBooleanTrue.GetType() | Should -Be ([System.Boolean]) + $config.MyBooleanFalse | Should -BeFalse + $config.MyBooleanFalse.GetType() | Should -Be ([System.Boolean]) } It 'shloud be able to parse an array' { - $Config = ConvertFrom-ScriptConfigXml -Content $Content + # Arrange + $content = Get-Content -Path "$PSScriptRoot\TestData\config.xml" + $expectedArray = @( 'Lorem', 'Ipsum' ) + + # Act + $config = ConvertFrom-ScriptConfigXml -Content $content - $Config.MyArray | Should Not BeNullOrEmpty - $Config.MyArray | Should Be $ResultArray - $Config.MyArray.GetType() | Should Be ([System.Object[]]) + # Assert + $config.MyArray | Should -Not -BeNullOrEmpty + $config.MyArray | Should -Be $expectedArray + $config.MyArray.GetType() | Should -Be ([System.Object[]]) } It 'shloud be able to parse an hashtable' { - $Config = ConvertFrom-ScriptConfigXml -Content $Content + # Arrange + $content = Get-Content -Path "$PSScriptRoot\TestData\config.xml" + $expectedHashtable = @{ Foo = 'Bar'; Hello = 'World' } + + # Act + $config = ConvertFrom-ScriptConfigXml -Content $content - $Config.MyHashtable | Should Not BeNullOrEmpty - $Config.MyHashtable.Keys | Should Be $ResultHashtable.Keys - $Config.MyHashtable.Values | Should Be $ResultHashtable.Values - $Config.MyHashtable.GetType() | Should Be ([System.Collections.Hashtable]) + # Assert + $config.MyHashtable | Should -Not -BeNullOrEmpty + $config.MyHashtable.Keys -as [string[]] | Should -Be ($expectedHashtable.Keys -as [string[]]) + $config.MyHashtable.Values -as [string[]] | Should -Be ($expectedHashtable.Values -as [string[]]) + $config.MyHashtable.GetType() | Should -Be ([System.Collections.Hashtable]) } } } diff --git a/Modules/ScriptConfig/Tests/Unit/Get-ScriptConfig.Tests.ps1 b/Modules/ScriptConfig/Tests/Unit/Get-ScriptConfig.Tests.ps1 index ffdd54c..a7bdd47 100644 --- a/Modules/ScriptConfig/Tests/Unit/Get-ScriptConfig.Tests.ps1 +++ b/Modules/ScriptConfig/Tests/Unit/Get-ScriptConfig.Tests.ps1 @@ -1,126 +1,136 @@ -$ModulePath = Resolve-Path -Path "$PSScriptRoot\..\..\Modules" | ForEach-Object Path -$ModuleName = Get-ChildItem -Path $ModulePath | Select-Object -First 1 -ExpandProperty BaseName +$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 +Remove-Module -Name $moduleName -Force -ErrorAction SilentlyContinue +Import-Module -Name "$modulePath\$moduleName" -Force Describe 'Get-ScriptConfig' { - Context 'Result' { - - $ResultStringIni = 'This is a test INI config file!' - $ResultStringJson = 'This is a test JSON config file!' - $ResultStringXml = 'This is a test XML config file!' - $ResultIntegerPositive = 42 - $ResultIntegerNegative = -153 - $ResultBooleanTrue = $true - $ResultBooleanFalse = $false - $ResultArray = @( 'Lorem', 'Ipsum' ) - $ResultHashtable = @{ Foo = 'Bar'; Hello = 'World' } + Context 'Test Data' { It 'shloud be able to load a valid INI configuration file' { - $Config = Get-ScriptConfig -Path "$PSScriptRoot\TestData\config.ini" -Format INI - - $Config | Should Not BeNullOrEmpty - - $Config.MyString | Should Be $ResultStringIni - $Config.MyIntegerPositive | Should Be $ResultIntegerPositive - $Config.MyIntegerNegative | Should Be $ResultIntegerNegative - $Config.MyBooleanTrue | Should Be $ResultBooleanTrue - $Config.MyBooleanFalse | Should Be $ResultBooleanFalse - $Config.MyArray | Should Be $ResultArray - $Config.MyHashtable.Keys | Should Be $ResultHashtable.Keys - $Config.MyHashtable.Values | Should Be $ResultHashtable.Values + # 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' { - $Config = Get-ScriptConfig -Path "$PSScriptRoot\TestData\config.json" -Format JSON - - $Config | Should Not BeNullOrEmpty - - $Config.MyString | Should Be $ResultStringJson - $Config.MyIntegerPositive | Should Be $ResultIntegerPositive - $Config.MyIntegerNegative | Should Be $ResultIntegerNegative - $Config.MyBooleanTrue | Should Be $ResultBooleanTrue - $Config.MyBooleanFalse | Should Be $ResultBooleanFalse - $Config.MyArray | Should Be $ResultArray - $Config.MyHashtable.Keys | Should Be $ResultHashtable.Keys - $Config.MyHashtable.Values | Should Be $ResultHashtable.Values + # 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' { - $Config = Get-ScriptConfig -Path "$PSScriptRoot\TestData\config.xml" -Format XML - - $Config | Should Not BeNullOrEmpty - - $Config.MyString | Should Be $ResultStringXml - $Config.MyIntegerPositive | Should Be $ResultIntegerPositive - $Config.MyIntegerNegative | Should Be $ResultIntegerNegative - $Config.MyBooleanTrue | Should Be $ResultBooleanTrue - $Config.MyBooleanFalse | Should Be $ResultBooleanFalse - $Config.MyArray | Should Be $ResultArray - $Config.MyHashtable.Keys | Should Be $ResultHashtable.Keys - $Config.MyHashtable.Values | Should Be $ResultHashtable.Values + # 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 'Format Detection' { + Context 'Mocked Convert Script' { - Mock ConvertFrom-ScriptConfigIni { return @{} } -ModuleName ScriptConfig - Mock ConvertFrom-ScriptConfigJson { return @{} } -ModuleName ScriptConfig - Mock ConvertFrom-ScriptConfigXml { return @{} } -ModuleName ScriptConfig + 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' { - $Config = Get-ScriptConfig -Path "$PSScriptRoot\TestData\config.ini" + # Act + Get-ScriptConfig -Path "$PSScriptRoot\TestData\config.ini" | Out-Null - Assert-MockCalled 'ConvertFrom-ScriptConfigIni' -ModuleName ScriptConfig -Times 1 -Exactly + # Assert + Assert-MockCalled 'ConvertFrom-ScriptConfigIni' -ModuleName 'ScriptConfig' -Scope 'It' -Times 1 -Exactly } It 'should call the JSON function if a .json file is specified' { - $Config = Get-ScriptConfig -Path "$PSScriptRoot\TestData\config.json" + # Act + Get-ScriptConfig -Path "$PSScriptRoot\TestData\config.json" | Out-Null - Assert-MockCalled 'ConvertFrom-ScriptConfigJson' -ModuleName ScriptConfig -Times 1 -Exactly + # Assert + Assert-MockCalled 'ConvertFrom-ScriptConfigJson' -ModuleName 'ScriptConfig' -Scope 'It' -Times 1 -Exactly } It 'should call the XML function if a .xml file is specified' { - $Config = Get-ScriptConfig -Path "$PSScriptRoot\TestData\config.xml" + # Act + Get-ScriptConfig -Path "$PSScriptRoot\TestData\config.xml" | Out-Null - Assert-MockCalled 'ConvertFrom-ScriptConfigXml' -ModuleName ScriptConfig -Times 1 -Exactly + # Assert + Assert-MockCalled 'ConvertFrom-ScriptConfigXml' -ModuleName 'ScriptConfig' -Scope 'It' -Times 1 -Exactly } - } - - Context 'Format Parameter' { - - Mock ConvertFrom-ScriptConfigIni { return @{} } -ModuleName ScriptConfig - Mock ConvertFrom-ScriptConfigJson { return @{} } -ModuleName ScriptConfig - Mock ConvertFrom-ScriptConfigXml { return @{} } -ModuleName ScriptConfig It 'should call the INI function if INI format is specified' { - $Config = Get-ScriptConfig -Path "$PSScriptRoot\TestData\config.ini" -Format INI + # Act + Get-ScriptConfig -Path "$PSScriptRoot\TestData\config.ini" -Format 'INI' | Out-Null - Assert-MockCalled 'ConvertFrom-ScriptConfigIni' -ModuleName ScriptConfig -Times 1 -Exactly + # Assert + Assert-MockCalled 'ConvertFrom-ScriptConfigIni' -ModuleName 'ScriptConfig' -Scope 'It' -Times 1 -Exactly } It 'should call the JSON function if JSON format is specified' { - $Config = Get-ScriptConfig -Path "$PSScriptRoot\TestData\config.json" -Format JSON + # Act + Get-ScriptConfig -Path "$PSScriptRoot\TestData\config.json" -Format 'JSON' | Out-Null - Assert-MockCalled 'ConvertFrom-ScriptConfigJson' -ModuleName ScriptConfig -Times 1 -Exactly + # Assert + Assert-MockCalled 'ConvertFrom-ScriptConfigJson' -ModuleName 'ScriptConfig' -Scope 'It' -Times 1 -Exactly } It 'should call the XML function if XML format is specified' { - $Config = Get-ScriptConfig -Path "$PSScriptRoot\TestData\config.xml" -Format XML + # Act + Get-ScriptConfig -Path "$PSScriptRoot\TestData\config.xml" -Format 'XML' | Out-Null - Assert-MockCalled 'ConvertFrom-ScriptConfigXml' -ModuleName ScriptConfig -Times 1 -Exactly + # Assert + Assert-MockCalled 'ConvertFrom-ScriptConfigXml' -ModuleName 'ScriptConfig' -Scope 'It' -Times 1 -Exactly } } }