From d74777631c5155ea349a429b342abce87d37c8be Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Fri, 4 Jan 2019 23:14:15 +0000 Subject: [PATCH 01/10] Use ForEach operator instead of Foreach-Object Avoid unecessary use of the pipeline. --- .../Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 index 198cccf5964..84b4a4c9953 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 @@ -26,7 +26,7 @@ Describe 'ConvertTo-Json' -tags "CI" { $null = $ps.AddScript({ $obj = [PSCustomObject]@{P1 = ''; P2 = ''; P3 = ''; P4 = ''; P5 = ''; P6 = ''} $obj.P1 = $obj.P2 = $obj.P3 = $obj.P4 = $obj.P5 = $obj.P6 = $obj - 1..100 | Foreach-Object { $obj } | ConvertTo-Json -Depth 10 -Verbose + (1..100).ForEach{ ConvertTo-Json -InputObject $obj -Depth 10 -Verbose } # the conversion is expected to take some time, this throw is in case it doesn't throw "Should not have thrown exception" }) From d496b5509669483e2f7a4bc9cacde005a049a7e1 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Fri, 4 Jan 2019 23:35:53 +0000 Subject: [PATCH 02/10] Wait for running state rather than a verbose message ConvertTo-Json never writes to the verbose stream. --- .../Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 index 84b4a4c9953..c328cb3fb00 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 @@ -26,13 +26,13 @@ Describe 'ConvertTo-Json' -tags "CI" { $null = $ps.AddScript({ $obj = [PSCustomObject]@{P1 = ''; P2 = ''; P3 = ''; P4 = ''; P5 = ''; P6 = ''} $obj.P1 = $obj.P2 = $obj.P3 = $obj.P4 = $obj.P5 = $obj.P6 = $obj - (1..100).ForEach{ ConvertTo-Json -InputObject $obj -Depth 10 -Verbose } + (1..100).ForEach{ ConvertTo-Json -InputObject $obj -Depth 10 } # the conversion is expected to take some time, this throw is in case it doesn't throw "Should not have thrown exception" }) $null = $ps.BeginInvoke() - # wait for verbose message from ConvertTo-Json to ensure cmdlet is processing - Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } + # Wait for invocation to start running. + Wait-UntilTrue { $ps.InvocationStateInfo.State -eq "Running" } $null = $ps.BeginStop($null, $null) # wait a bit to ensure state has changed, not using synchronous Stop() to avoid blocking Pester Start-Sleep -Milliseconds 100 From bbf3dcb3dc2a51d2e9a8f140d6e2ecc6bedf4472 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Fri, 4 Jan 2019 23:42:27 +0000 Subject: [PATCH 03/10] Wait for invocation to stop. Use a timeout and interval to execute quicker. --- .../Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 index c328cb3fb00..b141eb54d46 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 @@ -33,9 +33,10 @@ Describe 'ConvertTo-Json' -tags "CI" { $null = $ps.BeginInvoke() # Wait for invocation to start running. Wait-UntilTrue { $ps.InvocationStateInfo.State -eq "Running" } + # Not using synchronous Stop() to avoid blocking Pester. $null = $ps.BeginStop($null, $null) - # wait a bit to ensure state has changed, not using synchronous Stop() to avoid blocking Pester - Start-Sleep -Milliseconds 100 + # Wait for invocation to stop. + Wait-UntilTrue { $ps.InvocationStateInfo.State -eq "Stopped" } -TimeoutInMilliseconds 1000 -IntervalInMilliseconds 10 $ps.InvocationStateInfo.State | Should -BeExactly "Stopped" $ps.Dispose() } From cf700cccc09a688d6b8db60dbe20369f861d2f27 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Sat, 5 Jan 2019 03:01:35 +0000 Subject: [PATCH 04/10] [Feature] Refactor and reformat --- .../ConvertTo-Json.Tests.ps1 | 93 +++++++++++-------- 1 file changed, 52 insertions(+), 41 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 index b141eb54d46..eacdcfa89a1 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 @@ -1,65 +1,76 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -Describe 'ConvertTo-Json' -tags "CI" { - BeforeAll { - $newline = [System.Environment]::NewLine - } +using namespace System.Environment +using namespace System.Management.Automation +using namespace Newtonsoft.Json.Linq + +Describe 'ConvertTo-Json' -tags 'CI' { It 'Newtonsoft.Json.Linq.Jproperty should be converted to Json properly' { - $EgJObject = New-Object -TypeName Newtonsoft.Json.Linq.JObject - $EgJObject.Add("TestValue1", "123456") - $EgJObject.Add("TestValue2", "78910") - $EgJObject.Add("TestValue3", "99999") - $dict = @{} - $dict.Add('JObject', $EgJObject) - $dict.Add('StrObject', 'This is a string Object') - $properties = @{'DictObject' = $dict; 'RandomString' = 'A quick brown fox jumped over the lazy dog'} - $object = New-Object -TypeName psobject -Property $properties + $object = [pscustomobject]@{ + HashTable = [hashtable]@{ + JObject = [JObject]::FromObject( @{ + TestValue1 = 123456; + TestValue2 = 78910; + TestValue3 = 99999 + } ); + StrObject = 'This is a string Object' + }; + RandomString = 'A quick brown fox jumped over the lazy dog' + } + $jsonFormat = ConvertTo-Json -InputObject $object $jsonFormat | Should -Match '"TestValue1": 123456' $jsonFormat | Should -Match '"TestValue2": 78910' $jsonFormat | Should -Match '"TestValue3": 99999' } - It "StopProcessing should succeed" { - $ps = [PowerShell]::Create() - $null = $ps.AddScript({ - $obj = [PSCustomObject]@{P1 = ''; P2 = ''; P3 = ''; P4 = ''; P5 = ''; P6 = ''} - $obj.P1 = $obj.P2 = $obj.P3 = $obj.P4 = $obj.P5 = $obj.P6 = $obj - (1..100).ForEach{ ConvertTo-Json -InputObject $obj -Depth 10 } - # the conversion is expected to take some time, this throw is in case it doesn't - throw "Should not have thrown exception" - }) - $null = $ps.BeginInvoke() - # Wait for invocation to start running. - Wait-UntilTrue { $ps.InvocationStateInfo.State -eq "Running" } + It 'StopProcessing should succeed' { + $ps = [powershell]::Create() + [void]$ps.AddScript( { + $obj = [pscustomobject]@{P1 = ''; P2 = ''; P3 = ''; P4 = ''; P5 = ''; P6 = ''} + $obj.P1 = $obj.P2 = $obj.P3 = $obj.P4 = $obj.P5 = $obj.P6 = $obj + (1..100).ForEach{ + ConvertTo-Json -InputObject $obj -Depth 10 + } + throw 'ConvertTo-Json finished processing before it could be stopped.' + } ) + + [void]$ps.BeginInvoke() + + # Wait for instance to start. + if (-not (Wait-UntilTrue { $ps.InvocationStateInfo.State -eq [PSInvocationState]::Running } -TimeoutInMilliseconds 1000 -IntervalInMilliseconds 10)) { + throw 'PowerShell instance did not start.' + } + # Not using synchronous Stop() to avoid blocking Pester. - $null = $ps.BeginStop($null, $null) - # Wait for invocation to stop. - Wait-UntilTrue { $ps.InvocationStateInfo.State -eq "Stopped" } -TimeoutInMilliseconds 1000 -IntervalInMilliseconds 10 - $ps.InvocationStateInfo.State | Should -BeExactly "Stopped" + [void]$ps.BeginStop($null, $null) + + # Instance should stop. + Wait-UntilTrue { $ps.InvocationStateInfo.State -eq [PSInvocationState]::Stopped } -TimeoutInMilliseconds 1000 -IntervalInMilliseconds 10 | Should -BeTrue $ps.Dispose() } - It "The result string is packed in an array symbols when AsArray parameter is used." { - $output = 1 | ConvertTo-Json -AsArray - $output | Should -BeLike "``[*1*]" + It 'The result string is packed in an array symbols when AsArray parameter is used.' { + $output = ConvertTo-Json -InputObject 1 -AsArray + $output | Should -BeLike '`[*1*]' - $output = 1,2 | ConvertTo-Json -AsArray - $output | Should -BeLike "``[*1*2*]" + $output = ConvertTo-Json -InputObject (1, 2) -AsArray + $output | Should -BeLike '`[*1*2*]' } - It "The result string is not packed in the array symbols when there is only one input object and AsArray parameter is not used." { - $output = 1 | ConvertTo-Json + It 'The result string is not packed in the array symbols when there is only one input object and AsArray parameter is not used.' { + $output = ConvertTo-Json -InputObject 1 $output | Should -BeExactly '1' } - It "The result string should ." -TestCases @( - @{name = "be not escaped by default."; params = @{}; expected = "{$newline ""abc"": ""'def'""$newline}" } - @{name = "be not escaped with '-EscapeHandling Default'."; params = @{EscapeHandling = 'Default'}; expected = "{$newline ""abc"": ""'def'""$newline}" } - @{name = "be escaped with '-EscapeHandling EscapeHtml'."; params = @{EscapeHandling = 'EscapeHtml'}; expected = "{$newline ""abc"": ""\u0027def\u0027""$newline}" } + It 'The result string should .' -TestCases @( + $nl = [Environment]::NewLine + @{name = 'be not escaped by default.'; params = @{}; expected = "{$nl ""abc"": ""'def'""$nl}" } + @{name = 'be not escaped with "-EscapeHandling Default".'; params = @{EscapeHandling = 'Default'}; expected = "{$nl ""abc"": ""'def'""$nl}" } + @{name = 'be escaped with "-EscapeHandling EscapeHtml".'; params = @{EscapeHandling = 'EscapeHtml'}; expected = "{$nl ""abc"": ""\u0027def\u0027""$nl}" } ) { - param ($name, $params ,$expected) + param ($name, $params, $expected) @{ 'abc' = "'def'" } | ConvertTo-Json @params | Should -BeExactly $expected } From 57c8f0dc8d2460b7816a9e020de6ff421977745d Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Sat, 5 Jan 2019 06:55:41 +0000 Subject: [PATCH 05/10] Write to the verbose stream and wait for that instead --- .../Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 index eacdcfa89a1..cf5dbd2f098 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 @@ -31,6 +31,7 @@ Describe 'ConvertTo-Json' -tags 'CI' { $obj = [pscustomobject]@{P1 = ''; P2 = ''; P3 = ''; P4 = ''; P5 = ''; P6 = ''} $obj.P1 = $obj.P2 = $obj.P3 = $obj.P4 = $obj.P5 = $obj.P6 = $obj (1..100).ForEach{ + Write-Verbose 'Ready' -Verbose ConvertTo-Json -InputObject $obj -Depth 10 } throw 'ConvertTo-Json finished processing before it could be stopped.' @@ -38,10 +39,7 @@ Describe 'ConvertTo-Json' -tags 'CI' { [void]$ps.BeginInvoke() - # Wait for instance to start. - if (-not (Wait-UntilTrue { $ps.InvocationStateInfo.State -eq [PSInvocationState]::Running } -TimeoutInMilliseconds 1000 -IntervalInMilliseconds 10)) { - throw 'PowerShell instance did not start.' - } + Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -TimeoutInMilliseconds 1000 -IntervalInMilliseconds 10 # Not using synchronous Stop() to avoid blocking Pester. [void]$ps.BeginStop($null, $null) From a302c2c4006a583139181208a04e3db70afd79ac Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Sat, 5 Jan 2019 10:01:11 +0000 Subject: [PATCH 06/10] [Feature] Add comment --- .../Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 index cf5dbd2f098..4380f1286a1 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 @@ -31,7 +31,7 @@ Describe 'ConvertTo-Json' -tags 'CI' { $obj = [pscustomobject]@{P1 = ''; P2 = ''; P3 = ''; P4 = ''; P5 = ''; P6 = ''} $obj.P1 = $obj.P2 = $obj.P3 = $obj.P4 = $obj.P5 = $obj.P6 = $obj (1..100).ForEach{ - Write-Verbose 'Ready' -Verbose + Write-Verbose -Message 'Ready' -Verbose ConvertTo-Json -InputObject $obj -Depth 10 } throw 'ConvertTo-Json finished processing before it could be stopped.' @@ -39,6 +39,7 @@ Describe 'ConvertTo-Json' -tags 'CI' { [void]$ps.BeginInvoke() + # Wait until there is output in the verbose stream. Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -TimeoutInMilliseconds 1000 -IntervalInMilliseconds 10 # Not using synchronous Stop() to avoid blocking Pester. From 5d703befaeb9e22d5c120178bb8d9dcc320e2b30 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Thu, 10 Jan 2019 02:06:37 +0000 Subject: [PATCH 07/10] Wait to ensure ConvertTo-Json has started processing --- .../Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 index 4380f1286a1..625393d1c37 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 @@ -42,6 +42,9 @@ Describe 'ConvertTo-Json' -tags 'CI' { # Wait until there is output in the verbose stream. Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -TimeoutInMilliseconds 1000 -IntervalInMilliseconds 10 + # Wait to ensure ConvertTo-Json has started processing. + Start-Sleep -Milliseconds 200 + # Not using synchronous Stop() to avoid blocking Pester. [void]$ps.BeginStop($null, $null) From c3ec12cde076f7fd405138e90176726f983c4d8b Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Fri, 11 Jan 2019 03:47:51 +0000 Subject: [PATCH 08/10] Move ForEach out of script --- .../ConvertTo-Json.Tests.ps1 | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 index 625393d1c37..bb0373661be 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 @@ -26,17 +26,19 @@ Describe 'ConvertTo-Json' -tags 'CI' { } It 'StopProcessing should succeed' { + $obj = (1..100).ForEach{ + $_ = [pscustomobject]@{P1 = ''; P2 = ''; P3 = ''; P4 = ''; P5 = ''; P6 = ''} + $_.P1 = $_.P2 = $_.P3 = $_.P4 = $_.P5 = $_.P6 = $_ + Write-Output $_ + } + $ps = [powershell]::Create() [void]$ps.AddScript( { - $obj = [pscustomobject]@{P1 = ''; P2 = ''; P3 = ''; P4 = ''; P5 = ''; P6 = ''} - $obj.P1 = $obj.P2 = $obj.P3 = $obj.P4 = $obj.P5 = $obj.P6 = $obj - (1..100).ForEach{ - Write-Verbose -Message 'Ready' -Verbose - ConvertTo-Json -InputObject $obj -Depth 10 - } - throw 'ConvertTo-Json finished processing before it could be stopped.' - } ) - + param ($obj) + Write-Verbose -Message 'Ready' -Verbose + ConvertTo-Json -InputObject $obj -Depth 10 + throw 'ConvertTo-Json finished processing before it could be stopped.' + } ).AddArgument($obj) [void]$ps.BeginInvoke() # Wait until there is output in the verbose stream. From dbb33af013e4de9e89f35f100256d9180313dc22 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Fri, 11 Jan 2019 04:32:25 +0000 Subject: [PATCH 09/10] Use default timeout interval for Wait-UntilTrue and adjust sleep time --- .../Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 index bb0373661be..b67dfe8defe 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 @@ -42,16 +42,16 @@ Describe 'ConvertTo-Json' -tags 'CI' { [void]$ps.BeginInvoke() # Wait until there is output in the verbose stream. - Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -TimeoutInMilliseconds 1000 -IntervalInMilliseconds 10 + Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -IntervalInMilliseconds 10 # Wait to ensure ConvertTo-Json has started processing. - Start-Sleep -Milliseconds 200 + Start-Sleep -Milliseconds 1000 # Not using synchronous Stop() to avoid blocking Pester. [void]$ps.BeginStop($null, $null) # Instance should stop. - Wait-UntilTrue { $ps.InvocationStateInfo.State -eq [PSInvocationState]::Stopped } -TimeoutInMilliseconds 1000 -IntervalInMilliseconds 10 | Should -BeTrue + Wait-UntilTrue { $ps.InvocationStateInfo.State -eq [PSInvocationState]::Stopped } -IntervalInMilliseconds 10 | Should -BeTrue $ps.Dispose() } From a3ce06225588b018b5804dda9a2ffceb59acdb84 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Sat, 12 Jan 2019 16:43:31 +0000 Subject: [PATCH 10/10] [Feature] Empty commit for CI