From c7b6b089dbd241d7fa8a9489a4b6d57db6c71664 Mon Sep 17 00:00:00 2001 From: Oliver Lipkau Date: Wed, 16 Jan 2019 17:37:37 +0100 Subject: [PATCH] Fixed backwards compatibility Values should only be of type [array] when necessary. [string] otherwise as mentioned by @EliaSaSe in https://github.com/lipkau/PsIni/pull/43#issuecomment-454057704 --- PSIni/Functions/Get-IniContent.ps1 | 14 +++++++++++--- PSIni/PsIni.psd1 | 2 +- Tests/PsIni.Tests.ps1 | 17 +++++++++++++++-- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/PSIni/Functions/Get-IniContent.ps1 b/PSIni/Functions/Get-IniContent.ps1 index e321840..c2268c7 100644 --- a/PSIni/Functions/Get-IniContent.ps1 +++ b/PSIni/Functions/Get-IniContent.ps1 @@ -142,9 +142,17 @@ Function Get-IniContent { $name, $value = $matches[1..2] Write-Verbose "$($MyInvocation.MyCommand.Name):: Adding key $name with value: $value" if (-not $ini[$section][$name]) { - $ini[$section][$name] = @($value) - } else { - $ini[$section][$name] += $value + $ini[$section][$name] = $value + } + else { + if ($ini[$section][$name] -is [string]) { + $ini[$section][$name] = [System.Collections.ArrayList]::new() + $ini[$section][$name].Add($ini[$section][$name]) | Out-Null + $ini[$section][$name].Add($value) | Out-Null + } + else { + $ini[$section][$name].Add($value) | Out-Null + } } continue } diff --git a/PSIni/PsIni.psd1 b/PSIni/PsIni.psd1 index 0ec3a7e..5c74dc5 100644 --- a/PSIni/PsIni.psd1 +++ b/PSIni/PsIni.psd1 @@ -36,7 +36,7 @@ Description = 'Convert hashtable to INI file and back. @ http://lipkau.github.io/PsIni/' # Minimum version of the Windows PowerShell engine required by this module - PowerShellVersion = '2.0' + PowerShellVersion = '3.0' # Name of the Windows PowerShell host required by this module PowerShellHostName = '' diff --git a/Tests/PsIni.Tests.ps1 b/Tests/PsIni.Tests.ps1 index 0853d3c..bea0aef 100644 --- a/Tests/PsIni.Tests.ps1 +++ b/Tests/PsIni.Tests.ps1 @@ -101,9 +101,11 @@ Describe "PsIni functionality" { Context "Reading INI" { - # act + #arrange Out-IniFile -InputObject $dictIn -FilePath $iniFile - $dictOut = Get-IniContent -FilePath $iniFile + + # act + $global:dictOut = Get-IniContent -FilePath $iniFile # assert It "creates a OrderedDictionary from an INI file" { @@ -115,6 +117,17 @@ Describe "PsIni functionality" { Compare-Object $dictIn $dictOut } + #assert + It "reads sames keys into an [array]" { + $dictOut["Category2"]["Key3"].gettype().FullName | Should -Be "System.Collections.ArrayList" + } + + It "keeps non repeating keys as [string]" { + $dictOut["Category1"]["Key1"] | Should -BeOfType [String] + $dictOut["Category1"]["Key2"] | Should -BeOfType [String] + $dictOut["Category2"]["Key4"] | Should -BeOfType [String] + } + } Context "Updating INI Content" {