From 2fa7c9e0779951346d13d46b6de3444a732defca Mon Sep 17 00:00:00 2001 From: Stephen Valdinger Date: Tue, 1 Oct 2019 12:09:44 -0400 Subject: [PATCH 01/10] First attempt at -Delimiter on ConvertFrom-StringData --- .../utility/ConvertFrom-StringData.cs | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) 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..e09ba315a72 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,34 @@ public string StringData { return _stringData; } + set { _stringData = value; } } + private string _Delimiter; + + /// + /// The delimiter to index from to create the object + /// + /// + [Parameter(Position = 1)] + public string 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 +69,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( From 0faca8063787093e7bf2a10625f8b8bdc8532a31 Mon Sep 17 00:00:00 2001 From: Stephen Valdinger Date: Tue, 1 Oct 2019 16:03:24 -0400 Subject: [PATCH 02/10] Add fixes as per @iSazonov --- .../commands/utility/ConvertFrom-StringData.cs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) 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 e09ba315a72..de07825a846 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFrom-StringData.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFrom-StringData.cs @@ -37,19 +37,12 @@ public string StringData } } - private string _Delimiter; /// - /// The delimiter to index from to create the object + /// Gets or sets the delimiter. /// - /// [Parameter(Position = 1)] - public string Delimiter - { - - get{} - set{} - } + public char Delimiter { get; set; } = '='; /// /// From 317060e5edde2346ecfadfa6b62da1a03a917f30 Mon Sep 17 00:00:00 2001 From: Stephen Valdinger Date: Tue, 1 Oct 2019 16:26:17 -0400 Subject: [PATCH 03/10] removes errant white space --- .../commands/utility/ConvertFrom-StringData.cs | 1 - 1 file changed, 1 deletion(-) 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 de07825a846..f058d649275 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFrom-StringData.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFrom-StringData.cs @@ -37,7 +37,6 @@ public string StringData } } - /// /// Gets or sets the delimiter. /// From 0222d7e7e13facdb319c2d37dda844e8b6e6af57 Mon Sep 17 00:00:00 2001 From: Stephen Valdinger Date: Tue, 1 Oct 2019 19:19:10 -0400 Subject: [PATCH 04/10] Adds additional tests to validate new parameter --- .../ConvertFrom-StringData.Tests.ps1 | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) 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..9f9c6add830 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" { + + $sampleData = @" + +a:b + +"@ + + + It "Should return the data on the left side in the key" { + $actualValue = ConvertFrom-StringData -StringData 'a:b' -Delimiter ':' + + $actualValue.Keys | Should -BeExactly "a" + } + + It "Should return the data on the right side in the value" { + $actualValue = ConvertFrom-StringData -StringData 'a=b' + + $actualValue.Values | Should -BeExactly "b" + } + + It "Should not throw on given delimiter" { + {$sampleData | ConvertFrom-StringData -Delimiter ':' }| Should -Not -Throw + } + + It "Should handle multiple delimiter types"{ + {"A:B" | ConvertFrom-StringData -Delimiter ':' } | Should -Not -Throw + {"A-B" | ConvertFrom-StringData -Delimiter "-" } | Should -Not -Throw + {"A,B" | ConvertFrom-StringData -Delimiter "," } | Should -Not -Throw + } + +} + + From 3c82aa4f6f8bb589b02a80cea77216ac46141d9b Mon Sep 17 00:00:00 2001 From: Stephen Valdinger Date: Tue, 1 Oct 2019 19:22:31 -0400 Subject: [PATCH 05/10] Whoopsy, add "CI" tag to new Describe block --- .../ConvertFrom-StringData.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 9f9c6add830..10fccef382a 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-StringData.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-StringData.Tests.ps1 @@ -66,7 +66,7 @@ bazz = 2 } } -Describe "Delimiter parameter tests" { +Describe "Delimiter parameter tests" -Tags "CI" { $sampleData = @" From 5f9ad7722898d8cf72fadd24a459ca2c6e7e4ed4 Mon Sep 17 00:00:00 2001 From: Stephen Valdinger Date: Tue, 1 Oct 2019 19:50:43 -0400 Subject: [PATCH 06/10] Update tests to use Test Cases. Thanks @vexx32 for the suggestion! --- .../ConvertFrom-StringData.Tests.ps1 | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) 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 10fccef382a..adee9fcf8b3 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-StringData.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-StringData.Tests.ps1 @@ -91,12 +91,20 @@ a:b {$sampleData | ConvertFrom-StringData -Delimiter ':' }| Should -Not -Throw } - It "Should handle multiple delimiter types"{ - {"A:B" | ConvertFrom-StringData -Delimiter ':' } | Should -Not -Throw - {"A-B" | ConvertFrom-StringData -Delimiter "-" } | Should -Not -Throw - {"A,B" | ConvertFrom-StringData -Delimiter "," } | Should -Not -Throw + + $TestCases = @( @{ Delimiter = ':'; StringData = 'value:10'; ExpectedResult = @{ Values = 10 } }, + @{ Delimiter = '-'; StringData = 'a-b' ; ExpectedResult = @{ Values = 'b' } }, + @{ Delimiter = ','; StringData = 'c,d' ; ExpectedResult = @{ Values = 'd' } } + ) + + It 'is able to parse with delimiter ""' -TestCases $TestCases { + param($Delimiter, $StringData, $ExpectedResult) + + $(ConvertFrom-StringData -StringData $StringData -Delimiter $Delimiter).Values | Should -Be $ExpectedResult.Values + } + } From 329509dc68fc2bd6af4f91c0918b801c11b570f4 Mon Sep 17 00:00:00 2001 From: Stephen Valdinger Date: Tue, 1 Oct 2019 20:14:08 -0400 Subject: [PATCH 07/10] Reformat test cases to validate both name as value --- .../ConvertFrom-StringData.Tests.ps1 | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) 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 adee9fcf8b3..e8012e0486b 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-StringData.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-StringData.Tests.ps1 @@ -92,15 +92,19 @@ a:b } - $TestCases = @( @{ Delimiter = ':'; StringData = 'value:10'; ExpectedResult = @{ Values = 10 } }, - @{ Delimiter = '-'; StringData = 'a-b' ; ExpectedResult = @{ Values = 'b' } }, - @{ Delimiter = ','; StringData = 'c,d' ; ExpectedResult = @{ Values = 'd' } } + $TestCases = @( + @{ Delimiter = ':'; StringData = 'value:10'; ExpectedResult = @{ Values = 10 } } + @{ Delimiter = '-'; StringData = 'a-b' ; ExpectedResult = @{ Values = 'b' } } + @{ Delimiter = ','; StringData = 'c,d' ; ExpectedResult = @{ Values = 'd' } } ) It 'is able to parse with delimiter ""' -TestCases $TestCases { param($Delimiter, $StringData, $ExpectedResult) - $(ConvertFrom-StringData -StringData $StringData -Delimiter $Delimiter).Values | Should -Be $ExpectedResult.Values + $Result = ConvertFrom-StringData -StringData $StringData -Delimiter $Delimiter + foreach ($Name in $ExpectedResult.Keys) { + $Result.$Name | Should -Be $ExpectedResult.$Name + } } From 673671ce48226322c0800afc5ac2cfe71353c613 Mon Sep 17 00:00:00 2001 From: Stephen Valdinger Date: Wed, 2 Oct 2019 10:28:12 -0400 Subject: [PATCH 08/10] Address pester changes from review comments. --- .../ConvertFrom-StringData.Tests.ps1 | 37 ++++++------------- 1 file changed, 12 insertions(+), 25 deletions(-) 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 e8012e0486b..c0f6c7a1ea6 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-StringData.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-StringData.Tests.ps1 @@ -67,37 +67,27 @@ bazz = 2 } Describe "Delimiter parameter tests" -Tags "CI" { - - $sampleData = @" - -a:b - -"@ - - - It "Should return the data on the left side in the key" { - $actualValue = ConvertFrom-StringData -StringData 'a:b' -Delimiter ':' - - $actualValue.Keys | Should -BeExactly "a" + BeforeAll { + $TestCases = @( + @{ Delimiter = ':'; StringData = 'value:10'; ExpectedResult = @{ Values = 10 } } + @{ Delimiter = '-'; StringData = 'a-b' ; ExpectedResult = @{ Values = 'b' } } + @{ Delimiter = ','; StringData = 'c,d' ; ExpectedResult = @{ Values = 'd' } } + ) } - - It "Should return the data on the right side in the value" { + 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 | ConvertFrom-StringData -Delimiter ':' }| Should -Not -Throw + $sampleData = @" +a:b +"@ + { $sampleData | ConvertFrom-StringData -Delimiter ':' }| Should -Not -Throw } - - $TestCases = @( - @{ Delimiter = ':'; StringData = 'value:10'; ExpectedResult = @{ Values = 10 } } - @{ Delimiter = '-'; StringData = 'a-b' ; ExpectedResult = @{ Values = 'b' } } - @{ Delimiter = ','; StringData = 'c,d' ; ExpectedResult = @{ Values = 'd' } } - ) - It 'is able to parse with delimiter ""' -TestCases $TestCases { param($Delimiter, $StringData, $ExpectedResult) @@ -108,7 +98,4 @@ a:b } - } - - From 3914c0eb5735079f859e58447bd8293a91ffcbaf Mon Sep 17 00:00:00 2001 From: Stephen Valdinger Date: Tue, 8 Oct 2019 16:25:07 -0400 Subject: [PATCH 09/10] Update tests per latest PR Review comments --- .../ConvertFrom-StringData.Tests.ps1 | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) 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 c0f6c7a1ea6..402ce83e866 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-StringData.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-StringData.Tests.ps1 @@ -74,6 +74,7 @@ Describe "Delimiter parameter tests" -Tags "CI" { @{ Delimiter = ','; StringData = 'c,d' ; ExpectedResult = @{ Values = 'd' } } ) } + It "Default delimiter '=' works" { $actualValue = ConvertFrom-StringData -StringData 'a=b' @@ -85,17 +86,18 @@ Describe "Delimiter parameter tests" -Tags "CI" { $sampleData = @" a:b "@ - { $sampleData | ConvertFrom-StringData -Delimiter ':' }| Should -Not -Throw + { $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 ($Name in $ExpectedResult.Keys) { - $Result.$Name | Should -Be $ExpectedResult.$Name - } + foreach ($Key in $ExpectedResult.Keys) { + $Key | Should -BeIn $ExpectedResult.Keys + $Result.$Key | Should -Be $ExpectedResult.$Key + } } } From 25b571ec3ac9e5e87a43c068e999aeb590150158 Mon Sep 17 00:00:00 2001 From: Andrew Menagarishvili Date: Fri, 11 Oct 2019 12:12:31 -0700 Subject: [PATCH 10/10] formatting - fixed indent and removed empty line --- .../ConvertFrom-StringData.Tests.ps1 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 402ce83e866..3b774181685 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-StringData.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-StringData.Tests.ps1 @@ -86,7 +86,7 @@ Describe "Delimiter parameter tests" -Tags "CI" { $sampleData = @" a:b "@ - { $sampleData | ConvertFrom-StringData -Delimiter ':' } | Should -Not -Throw + { $sampleData | ConvertFrom-StringData -Delimiter ':' } | Should -Not -Throw } It 'is able to parse with delimiter ""' -TestCases $TestCases { @@ -99,5 +99,4 @@ a:b $Result.$Key | Should -Be $ExpectedResult.$Key } } - }