$here = (Split-Path -Parent $MyInvocation.MyCommand.Path) . "$here.ps1" Describe "Process" { Mock -CommandName Get-EnvironmentVariable -MockWith { throw 'Asked for non-Process paths' } Mock -CommandName Set-EnvironmentVariable -MockWith { throw 'Should not set non-Process paths' } Context "Append 1 Remove 1" { Mock -CommandName Get-EnvironmentVariable -ParameterFilter { $Name -eq 'Path' -and $StoreName -eq 'Process' } -MockWith { return "path1;path2" } Mock -CommandName Set-EnvironmentVariable -ParameterFilter { $Name -eq 'Path' -and $StoreName -eq 'Process' } -MockWith { $Value | Should -Be "path2;path3" } It 'Does not throw' { { Set-PathVariable -StoreName 'Process' -Append 'path3' -Remove 'path1' } | Should -Not -Throw } It 'Called the Set-EnvironmentVariable correctly' { Assert-MockCalled -CommandName Set-EnvironmentVariable -ParameterFilter { $Name -eq 'Path' -and $StoreName -eq 'User' } -Times 0 } It 'Called the Set-EnvironmentVariable correctly' { Assert-MockCalled -CommandName Set-EnvironmentVariable -ParameterFilter { $Name -eq 'Path' -and $StoreName -eq 'Machine' } -Times 0 } It 'Called the Set-EnvironmentVariable correctly' { Assert-MockCalled -CommandName Set-EnvironmentVariable -ParameterFilter { $Name -eq 'Path' -and $StoreName -eq 'Process' } -Times 1 } } Context "Prepend 1 Remove 1" { Mock -CommandName Get-EnvironmentVariable -ParameterFilter { $Name -eq 'Path' -and $StoreName -eq 'Process' } -MockWith { return "path1;path2" } Mock -CommandName Set-EnvironmentVariable -ParameterFilter { $Name -eq 'Path' -and $StoreName -eq 'Process' } -MockWith { $Value | Should -Be "path3;path2" } It 'Does not throw' { { Set-PathVariable -StoreName 'Process' -Prepend 'path3' -Remove 'path1' } | Should -Not -Throw } It 'Called the Set-EnvironmentVariable correctly' { Assert-MockCalled -CommandName Set-EnvironmentVariable -ParameterFilter { $Name -eq 'Path' -and $StoreName -eq 'User' } -Times 0 } It 'Called the Set-EnvironmentVariable correctly' { Assert-MockCalled -CommandName Set-EnvironmentVariable -ParameterFilter { $Name -eq 'Path' -and $StoreName -eq 'Machine' } -Times 0 } It 'Called the Set-EnvironmentVariable correctly' { Assert-MockCalled -CommandName Set-EnvironmentVariable -ParameterFilter { $Name -eq 'Path' -and $StoreName -eq 'Process' } -Times 1 } } Context "Append 5 Remove 5 with strings that don't exist in the first path" { Mock -CommandName Get-EnvironmentVariable -ParameterFilter { $Name -eq 'Path' -and $StoreName -eq 'Process' } -MockWith { return "path1;path2" } Mock -CommandName Set-EnvironmentVariable -ParameterFilter { $Name -eq 'Path' -and $StoreName -eq 'Process' } -MockWith { # remove then add, so any adds should be there even if the remove says to remove them $Value | Should -Be "path10;path11;path12;path13;path14" } It 'Does not throw' { { Set-PathVariable -StoreName 'Process' -Append 'path10','path11','path12','path13','path14' -Remove 'path1','path2','path10','path11','path14' } | Should -Not -Throw } It 'Called the Set-EnvironmentVariable correctly' { Assert-MockCalled -CommandName Set-EnvironmentVariable -ParameterFilter { $Name -eq 'Path' -and $StoreName -eq 'User' } -Times 0 } It 'Called the Set-EnvironmentVariable correctly' { Assert-MockCalled -CommandName Set-EnvironmentVariable -ParameterFilter { $Name -eq 'Path' -and $StoreName -eq 'Machine' } -Times 0 } It 'Called the Set-EnvironmentVariable correctly' { Assert-MockCalled -CommandName Set-EnvironmentVariable -ParameterFilter { $Name -eq 'Path' -and $StoreName -eq 'Process' } -Times 1 } } Context "Append 3 Remove 5 with a weird collection" { Mock -CommandName Get-EnvironmentVariable -ParameterFilter { $Name -eq 'Path' -and $StoreName -eq 'Process' } -MockWith { return "path14;path13;path10;path11;path12;path1;path2" } Mock -CommandName Set-EnvironmentVariable -ParameterFilter { $Name -eq 'Path' -and $StoreName -eq 'Process' } -MockWith { # remove then add, so any adds should be there even if the remove says to remove them $Value | Should -Be "path13;path12;path10;path11;path14" } It 'Does not throw' { { Set-PathVariable -StoreName 'Process' -Append 'path10','path11','path14' -Remove 'path1','path2','path10','path11','path14' } | Should -Not -Throw } It 'Called the Set-EnvironmentVariable correctly' { Assert-MockCalled -CommandName Set-EnvironmentVariable -ParameterFilter { $Name -eq 'Path' -and $StoreName -eq 'User' } -Times 0 } It 'Called the Set-EnvironmentVariable correctly' { Assert-MockCalled -CommandName Set-EnvironmentVariable -ParameterFilter { $Name -eq 'Path' -and $StoreName -eq 'Machine' } -Times 0 } It 'Called the Set-EnvironmentVariable correctly' { Assert-MockCalled -CommandName Set-EnvironmentVariable -ParameterFilter { $Name -eq 'Path' -and $StoreName -eq 'Process' } -Times 1 } } Context 'It is gonna throw!' { It 'Throws for User!' { { Set-PathVariable -StoreName 'User' -Append 'path10','path11','path14' -Remove 'path1','path2','path10','path11','path14' } | Should -Throw } It 'Throws for Machine!' { { Set-PathVariable -StoreName 'Machine' -Append 'path10','path11','path14' -Remove 'path1','path2','path10','path11','path14' } | Should -Throw } } }