From b8c9578e5bed0094c3dc55b5e96ec8cf76939c74 Mon Sep 17 00:00:00 2001 From: "Jim Truher (MSFT)" Date: Mon, 23 May 2016 15:04:13 -0700 Subject: [PATCH] modify updateformatdata tests to use runspace to isolate format changes. This should be the pattern for any test which mainupulates current state to protect from unintended side effects. This was done by default in ttest because each test would have its own runspace --- test/powershell/Update-FormatData.Tests.ps1 | 43 +++++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/test/powershell/Update-FormatData.Tests.ps1 b/test/powershell/Update-FormatData.Tests.ps1 index c52214830..b2153ba75 100644 --- a/test/powershell/Update-FormatData.Tests.ps1 +++ b/test/powershell/Update-FormatData.Tests.ps1 @@ -1,5 +1,17 @@ Describe "Update-FormatData" { + BeforeAll { + $path = Join-Path -Path $TestDrive -ChildPath "outputfile.ps1xml" + $ps = [powershell]::Create() + $iss = [system.management.automation.runspaces.initialsessionstate]::CreateDefault2() + $rs = [system.management.automation.runspaces.runspacefactory]::CreateRunspace($iss) + $rs.Open() + $ps.Runspace = $rs + } + AfterAll { + $rs.Close() + $ps.Dispose() + } Context "Validate Update-FormatData update correctly" { It "Should not throw upon reloading previous formatting file" { @@ -7,19 +19,19 @@ Describe "Update-FormatData" { } It "Should validly load formatting data" { - $path = Join-Path -Path $TestDrive -ChildPath "outputfile.ps1xml" Get-FormatData -typename System.Diagnostics.Process | Export-FormatData -Path $path - { Update-FormatData -prependPath $path } | Should Not throw - Remove-Item $path -ErrorAction SilentlyContinue + $null = $ps.AddScript("Update-FormatData -prependPath $path") + $ps.Invoke() + $ps.HadErrors | Should be $false } } } Describe "Update-FormatData basic functionality" -Tags DRT{ - $testfilename = "testfile.ps1xml" - $testfile = Join-Path -Path $TestDrive -ChildPath $testfilename - - It "Update-FormatData with WhatIf should work"{ + BeforeAll { + $testfilename = "testfile.ps1xml" + $testfile = Join-Path -Path $TestDrive -ChildPath $testfilename + $xmlContent=@" @@ -37,14 +49,11 @@ Describe "Update-FormatData basic functionality" -Tags DRT{ "@ $xmlContent > $testfile - try - { - { Update-FormatData -Append $testfile -WhatIf } | Should Not Throw - { Update-FormatData -Prepend $testfile -WhatIf } | Should Not Throw - } - finally - { - Remove-Item $testfile -ErrorAction SilentlyContinue - } + } + + It "Update-FormatData with WhatIf should work"{ + + { Update-FormatData -Append $testfile -WhatIf } | Should Not Throw + { Update-FormatData -Prepend $testfile -WhatIf } | Should Not Throw } -} \ No newline at end of file +}