Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -IgnoreEmptySection parameter to Get-IniContent #69

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion PSIni/Functions/Get-IniContent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@

# Remove lines determined to be comments from the resulting dictionary.
[Switch]
$IgnoreComments
$IgnoreComments,

# Remove sections without any key
[Switch]
$IgnoreEmptySections
)

Begin {
Expand Down Expand Up @@ -161,6 +165,18 @@
}
}
Write-Verbose "$($MyInvocation.MyCommand.Name):: Finished Processing file: $FilePath"
if($IgnoreEmptySections){
$ToRemove = [System.Collections.ArrayList]@()
foreach($Section in $ini.Keys){
if(($ini[$Section]).Count -eq 0){
$null = $ToRemove.Add($Section)
}
}
foreach($Section in $ToRemove){
Write-Verbose "$($MyInvocation.MyCommand.Name):: Removing empty section $Section"
$null = $ini.Remove($Section)
}
}
Write-Output $ini
}

Expand Down
28 changes: 28 additions & 0 deletions Tests/PsIni.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Describe "PsIni functionality" {
$dictIn["Category2"] = New-Object System.Collections.Specialized.OrderedDictionary([System.StringComparer]::OrdinalIgnoreCase)
$dictIn["Category2"]["Key3"] = @("Value3.1", "Value3.2", "Value3.3")
$dictIn["Category2"]["Key4"] = "Value4"
$dictIn["Category3"] = New-Object System.Collections.Specialized.OrderedDictionary([System.StringComparer]::OrdinalIgnoreCase)

Context "Load Module" {

Expand Down Expand Up @@ -130,6 +131,33 @@ Describe "PsIni functionality" {

}

Context "Reading INI Ignoring empty sections" {

#arrange
Out-IniFile -InputObject $dictIn -FilePath $iniFile

# act
$global:dictOut = Get-IniContent -FilePath $iniFile -IgnoreEmptySections

# assert
It "creates a OrderedDictionary from an INI file" {
($dictOut.GetType()) | Should Be System.Collections.Specialized.OrderedDictionary
}

#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]
$dictOut["Category3"] | Should -BeNullOrEmpty
}

}

Context "Updating INI Content" {

# act
Expand Down