From 1c3411d54ba0ab65ae850206abb486af1a77fb7a Mon Sep 17 00:00:00 2001 From: Rene Hernandez Date: Sun, 24 Feb 2019 13:28:50 -0500 Subject: [PATCH 1/2] Check if function exists before removing it --- Functions/Mock.ps1 | 4 +++- Functions/OuterMock.Tests.ps1 | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 Functions/OuterMock.Tests.ps1 diff --git a/Functions/Mock.ps1 b/Functions/Mock.ps1 index 6e6555ee9..c61fa824d 100644 --- a/Functions/Mock.ps1 +++ b/Functions/Mock.ps1 @@ -748,7 +748,9 @@ function Exit-MockScope { [string[]] $Aliases ) - $ExecutionContext.InvokeProvider.Item.Remove("Function:\$CommandName", $false, $true, $true) + if ($ExecutionContext.InvokeProvider.Item.Exists("Function:\$CommandName", $true, $true)) { + $ExecutionContext.InvokeProvider.Item.Remove("Function:\$CommandName", $false, $true, $true) + } foreach ($alias in $Aliases) { if ($ExecutionContext.InvokeProvider.Item.Exists("Alias:$alias", $true, $true)) { diff --git a/Functions/OuterMock.Tests.ps1 b/Functions/OuterMock.Tests.ps1 new file mode 100644 index 000000000..f20bfddd9 --- /dev/null +++ b/Functions/OuterMock.Tests.ps1 @@ -0,0 +1,10 @@ +Set-StrictMode -Version Latest + +function f () {} + +mock f {} + +Describe 'Mock at script scope' { + It 't' { + } +} From 066a356bc1bd324b0d21a3316c6a8719fdb694da Mon Sep 17 00:00:00 2001 From: Rene Hernandez Date: Sun, 24 Feb 2019 13:38:57 -0500 Subject: [PATCH 2/2] Add it defintion to make test pass --- Functions/OuterMock.Tests.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Functions/OuterMock.Tests.ps1 b/Functions/OuterMock.Tests.ps1 index f20bfddd9..4560424a2 100644 --- a/Functions/OuterMock.Tests.ps1 +++ b/Functions/OuterMock.Tests.ps1 @@ -2,9 +2,10 @@ Set-StrictMode -Version Latest function f () {} -mock f {} +Mock f {} Describe 'Mock at script scope' { It 't' { + 1 | Should -Be 1 } }