diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFrom-StringData.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFrom-StringData.cs index b8c7d6dbc6e..f058d649275 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFrom-StringData.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFrom-StringData.cs @@ -2,8 +2,8 @@ // Licensed under the MIT License. using System; -using System.Management.Automation; using System.Collections; +using System.Management.Automation; using System.Text.RegularExpressions; namespace Microsoft.PowerShell.Commands @@ -30,19 +30,26 @@ public string StringData { return _stringData; } + set { _stringData = value; } } + /// + /// Gets or sets the delimiter. + /// + [Parameter(Position = 1)] + public char Delimiter { get; set; } = '='; + /// /// protected override void ProcessRecord() { Hashtable result = new Hashtable(StringComparer.OrdinalIgnoreCase); - if (String.IsNullOrEmpty(_stringData)) + if (string.IsNullOrEmpty(_stringData)) { WriteObject(result); return; @@ -54,10 +61,10 @@ protected override void ProcessRecord() { string s = line.Trim(); - if (String.IsNullOrEmpty(s) || s[0] == '#') + if (string.IsNullOrEmpty(s) || s[0] == '#') continue; - int index = s.IndexOf('='); + int index = s.IndexOf(Delimiter); if (index <= 0) { throw PSTraceSource.NewInvalidOperationException( diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-StringData.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-StringData.Tests.ps1 index 00eb504fc04..3b774181685 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-StringData.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-StringData.Tests.ps1 @@ -65,3 +65,38 @@ bazz = 2 $(ConvertFrom-StringData -StringData $sampleData).Values | Should -BeIn @("0","1","2") } } + +Describe "Delimiter parameter tests" -Tags "CI" { + BeforeAll { + $TestCases = @( + @{ Delimiter = ':'; StringData = 'value:10'; ExpectedResult = @{ Values = 10 } } + @{ Delimiter = '-'; StringData = 'a-b' ; ExpectedResult = @{ Values = 'b' } } + @{ Delimiter = ','; StringData = 'c,d' ; ExpectedResult = @{ Values = 'd' } } + ) + } + + It "Default delimiter '=' works" { + $actualValue = ConvertFrom-StringData -StringData 'a=b' + + $actualValue.Values | Should -BeExactly "b" + $actualValue.Keys | Should -BeExactly "a" + } + + It "Should not throw on given delimiter" { + $sampleData = @" +a:b +"@ + { $sampleData | ConvertFrom-StringData -Delimiter ':' } | Should -Not -Throw + } + + It 'is able to parse with delimiter ""' -TestCases $TestCases { + param($Delimiter, $StringData, $ExpectedResult) + + $Result = ConvertFrom-StringData -StringData $StringData -Delimiter $Delimiter + + foreach ($Key in $ExpectedResult.Keys) { + $Key | Should -BeIn $ExpectedResult.Keys + $Result.$Key | Should -Be $ExpectedResult.$Key + } + } +}