1+ BeforeDiscovery { Import-Module " $PSScriptRoot /../../../src/common/Environment.psm1" }
2+
3+ Describe ' Invoke-Teardown Tests' {
4+ BeforeAll {
5+ # Save original values
6+ $Script :OriginalPSDefaultParameterValues = $Global :PSDefaultParameterValues.Clone ()
7+ }
8+
9+ AfterAll {
10+ # Restore original values
11+ $Global :PSDefaultParameterValues = $Script :OriginalPSDefaultParameterValues
12+ }
13+
14+ BeforeEach {
15+ # Set up test values before each test
16+ $Global :PSDefaultParameterValues [' *:ErrorAction' ] = ' Stop'
17+ $Global :PSDefaultParameterValues [' *:WarningAction' ] = ' Continue'
18+ $Global :PSDefaultParameterValues [' *:InformationAction' ] = ' Continue'
19+ $Global :PSDefaultParameterValues [' *:Verbose' ] = $true
20+ $Global :PSDefaultParameterValues [' *:Debug' ] = $true
21+ $Global :PSDefaultParameterValues [' *-Module:Verbose' ] = $true
22+ }
23+
24+ Context ' Parameter Value Cleanup' {
25+ It ' Should remove ErrorAction from PSDefaultParameterValues' {
26+ $Global :PSDefaultParameterValues.ContainsKey (' *:ErrorAction' ) | Should - Be $true
27+
28+ InModuleScope Environment {
29+ Invoke-Teardown
30+ }
31+
32+ $Global :PSDefaultParameterValues.ContainsKey (' *:ErrorAction' ) | Should - Be $false
33+ }
34+
35+ It ' Should remove WarningAction from PSDefaultParameterValues' {
36+ $Global :PSDefaultParameterValues.ContainsKey (' *:WarningAction' ) | Should - Be $true
37+
38+ InModuleScope Environment {
39+ Invoke-Teardown
40+ }
41+
42+ $Global :PSDefaultParameterValues.ContainsKey (' *:WarningAction' ) | Should - Be $false
43+ }
44+
45+ It ' Should remove InformationAction from PSDefaultParameterValues' {
46+ $Global :PSDefaultParameterValues.ContainsKey (' *:InformationAction' ) | Should - Be $true
47+
48+ InModuleScope Environment {
49+ Invoke-Teardown
50+ }
51+
52+ $Global :PSDefaultParameterValues.ContainsKey (' *:InformationAction' ) | Should - Be $false
53+ }
54+
55+ It ' Should remove Verbose from PSDefaultParameterValues' {
56+ $Global :PSDefaultParameterValues.ContainsKey (' *:Verbose' ) | Should - Be $true
57+
58+ InModuleScope Environment {
59+ Invoke-Teardown
60+ }
61+
62+ $Global :PSDefaultParameterValues.ContainsKey (' *:Verbose' ) | Should - Be $false
63+ }
64+
65+ It ' Should remove Debug from PSDefaultParameterValues' {
66+ $Global :PSDefaultParameterValues.ContainsKey (' *:Debug' ) | Should - Be $true
67+
68+ InModuleScope Environment {
69+ Invoke-Teardown
70+ }
71+
72+ $Global :PSDefaultParameterValues.ContainsKey (' *:Debug' ) | Should - Be $false
73+ }
74+
75+ It ' Should remove Module Verbose from PSDefaultParameterValues' {
76+ $Global :PSDefaultParameterValues.ContainsKey (' *-Module:Verbose' ) | Should - Be $true
77+
78+ InModuleScope Environment {
79+ Invoke-Teardown
80+ }
81+
82+ $Global :PSDefaultParameterValues.ContainsKey (' *-Module:Verbose' ) | Should - Be $false
83+ }
84+ }
85+
86+ Context ' Multiple Teardown Calls' {
87+ It ' Should handle being called multiple times without error' {
88+ InModuleScope Environment {
89+ { Invoke-Teardown } | Should -Not - Throw
90+ { Invoke-Teardown } | Should -Not - Throw
91+ { Invoke-Teardown } | Should -Not - Throw
92+ }
93+ }
94+
95+ It ' Should not throw when keys do not exist' {
96+ # Remove some keys manually first
97+ $Global :PSDefaultParameterValues.Remove (' *:ErrorAction' )
98+ $Global :PSDefaultParameterValues.Remove (' *:Verbose' )
99+
100+ InModuleScope Environment {
101+ { Invoke-Teardown } | Should -Not - Throw
102+ }
103+ }
104+ }
105+
106+ Context ' Integration with Invoke-Setup' {
107+ It ' Should clean up all values set by Invoke-Setup' {
108+ # First set up
109+ InModuleScope Environment {
110+ Invoke-Setup
111+ }
112+
113+ # Verify setup worked
114+ $Global :PSDefaultParameterValues.ContainsKey (' *:ErrorAction' ) | Should - Be $true
115+ $Global :PSDefaultParameterValues.ContainsKey (' *:WarningAction' ) | Should - Be $true
116+ $Global :PSDefaultParameterValues.ContainsKey (' *:InformationAction' ) | Should - Be $true
117+
118+ # Then tear down
119+ InModuleScope Environment {
120+ Invoke-Teardown
121+ }
122+
123+ # Verify teardown worked
124+ $Global :PSDefaultParameterValues.ContainsKey (' *:ErrorAction' ) | Should - Be $false
125+ $Global :PSDefaultParameterValues.ContainsKey (' *:WarningAction' ) | Should - Be $false
126+ $Global :PSDefaultParameterValues.ContainsKey (' *:InformationAction' ) | Should - Be $false
127+ $Global :PSDefaultParameterValues.ContainsKey (' *:Verbose' ) | Should - Be $false
128+ $Global :PSDefaultParameterValues.ContainsKey (' *:Debug' ) | Should - Be $false
129+ $Global :PSDefaultParameterValues.ContainsKey (' *-Module:Verbose' ) | Should - Be $false
130+ }
131+ }
132+
133+ Context ' Selective Removal' {
134+ It ' Should only remove specific keys and leave others intact' {
135+ # Add some unrelated keys
136+ $Global :PSDefaultParameterValues [' Get-Process:Name' ] = ' powershell'
137+ $Global :PSDefaultParameterValues [' Test-Custom:Param' ] = ' value'
138+
139+ InModuleScope Environment {
140+ Invoke-Teardown
141+ }
142+
143+ # Verify environment-specific keys are removed
144+ $Global :PSDefaultParameterValues.ContainsKey (' *:ErrorAction' ) | Should - Be $false
145+ $Global :PSDefaultParameterValues.ContainsKey (' *:WarningAction' ) | Should - Be $false
146+
147+ # Verify other keys are preserved
148+ $Global :PSDefaultParameterValues.ContainsKey (' Get-Process:Name' ) | Should - Be $true
149+ $Global :PSDefaultParameterValues.ContainsKey (' Test-Custom:Param' ) | Should - Be $true
150+
151+ # Clean up test keys
152+ $Global :PSDefaultParameterValues.Remove (' Get-Process:Name' )
153+ $Global :PSDefaultParameterValues.Remove (' Test-Custom:Param' )
154+ }
155+ }
156+ }
0 commit comments