Skip to content

Commit fe40fbf

Browse files
iSazonovAndrew
authored andcommitted
Unload test modules (#11061)
1 parent 8fcd772 commit fe40fbf

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

test/powershell/Modules/Microsoft.PowerShell.Core/RemoteImportModule.Tests.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Describe "Remote import-module tests" -Tags 'Feature','RequireAdminOnWindows' {
2424
if ($IsWindows) {
2525
$pssession | Remove-PSSession -ErrorAction SilentlyContinue
2626
}
27+
28+
Remove-Module TestImport -Force -ErrorAction SilentlyContinue
2729
}
2830

2931
BeforeEach {

test/powershell/Modules/Microsoft.PowerShell.Security/ConstrainedLanguageModules.Tests.ps1

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ try
236236
"@{ ModuleVersion='1.0'; RootModule='$moduleFilePathI' }" | Out-File $moduleManifestPathI
237237

238238
# Module with using directive
239-
$sriptModuleNameJ = "ModuleWithUsing_System32"
240-
$moduleFilePathJ = Join-Path $TestModulePath ($sriptModuleNameJ + ".psm1")
239+
$scriptModuleNameJ = "ModuleWithUsing_System32"
240+
$moduleFilePathJ = Join-Path $TestModulePath ($scriptModuleNameJ + ".psm1")
241241
@'
242242
using module {0}
243243
function PublicUsingFn {{ [Class1]::GetMessage() }}
@@ -247,6 +247,19 @@ try
247247
Write-Verbose "Test module files created"
248248
}
249249

250+
AfterAll {
251+
Remove-Module $scriptModuleNameA -Force -ErrorAction SilentlyContinue
252+
Remove-Module $scriptModuleNameB -Force -ErrorAction SilentlyContinue
253+
Remove-Module $scriptModuleNameC -Force -ErrorAction SilentlyContinue
254+
Remove-Module $scriptModuleNameD -Force -ErrorAction SilentlyContinue
255+
Remove-Module $scriptModuleNameE -Force -ErrorAction SilentlyContinue
256+
Remove-Module $scriptModuleNameF -Force -ErrorAction SilentlyContinue
257+
Remove-Module $scriptModuleNameG -Force -ErrorAction SilentlyContinue
258+
Remove-Module $scriptModuleNameH -Force -ErrorAction SilentlyContinue
259+
Remove-Module $scriptModuleNameI -Force -ErrorAction SilentlyContinue
260+
Remove-Module $scriptModuleNameJ -Force -ErrorAction SilentlyContinue
261+
}
262+
250263
It "Verifies that importing trusted module in system lockdown which dot sources a ps1 file while exporting all functions with wildcard throws expected error" {
251264

252265
try
@@ -457,6 +470,10 @@ try
457470
"@{ ModuleVersion = '1.0'; RootModule = '$moduleFileName'; FunctionsToExport = 'PublicFn' }" > $manifestFileName
458471
}
459472

473+
AfterAll {
474+
Remove-Module ImportTrustedManifestWithCallOperator_System32 -Force -ErrorAction SilentlyContinue
475+
}
476+
460477
It "Verifies expected error when call operator attempts to access trusted module scope function" {
461478

462479
try
@@ -638,6 +655,7 @@ try
638655
finally
639656
{
640657
Invoke-LanguageModeTestingSupportCmdlet -RevertLockdownMode -EnableFullLanguageMode
658+
Remove-Module ImportUnTrustedManifestWithFnExport_System32 -Force -ErrorAction SilentlyContinue
641659
}
642660

643661
$expectedError.FullyQualifiedErrorId | Should -BeExactly "Modules_MismatchedLanguageModes,Microsoft.PowerShell.Commands.ImportModuleCommand"
@@ -807,6 +825,7 @@ try
807825
finally
808826
{
809827
Invoke-LanguageModeTestingSupportCmdlet -RevertLockdownMode -EnableFullLanguageMode
828+
Remove-Module ImportTrustedModuleWithNoFnExport_System32 -Force -ErrorAction SilentlyContinue
810829
}
811830

812831
$module.ExportedCommands.Count | Should -Be 0
@@ -837,6 +856,7 @@ try
837856
finally
838857
{
839858
Invoke-LanguageModeTestingSupportCmdlet -RevertLockdownMode -EnableFullLanguageMode
859+
Remove-Module ImportTrustedModuleWithFnExport_System32 -Force -ErrorAction SilentlyContinue
840860
}
841861

842862
$module.ExportedCommands.Count | Should -Be 1
@@ -868,6 +888,7 @@ try
868888
finally
869889
{
870890
Invoke-LanguageModeTestingSupportCmdlet -RevertLockdownMode -EnableFullLanguageMode
891+
Remove-Module ImportTrustedModuleWithWildcardFnExport_System32 -Force -ErrorAction SilentlyContinue
871892
}
872893

873894
$module.ExportedCommands.Count | Should -Be 2
@@ -927,6 +948,7 @@ try
927948
finally
928949
{
929950
Invoke-LanguageModeTestingSupportCmdlet -RevertLockdownMode -EnableFullLanguageMode
951+
Remove-Module ImportTrustedManifestWithNoFnExport_System32 -Force -ErrorAction SilentlyContinue
930952
}
931953

932954
$module.ExportedCommands.Count | Should -Be 0
@@ -957,6 +979,7 @@ try
957979
finally
958980
{
959981
Invoke-LanguageModeTestingSupportCmdlet -RevertLockdownMode -EnableFullLanguageMode
982+
Remove-Module ImportTrustedManifestWithWildcardFnExport1_System32 -Force -ErrorAction SilentlyContinue
960983
}
961984

962985
$module.ExportedCommands.Count | Should -Be 0
@@ -987,6 +1010,7 @@ try
9871010
finally
9881011
{
9891012
Invoke-LanguageModeTestingSupportCmdlet -RevertLockdownMode -EnableFullLanguageMode
1013+
Remove-Module ImportTrustedManifestWithWildcardNameFnExport_System32 -Force -ErrorAction SilentlyContinue
9901014
}
9911015

9921016
$module.ExportedCommands.Count | Should -Be 0
@@ -1019,6 +1043,7 @@ try
10191043
finally
10201044
{
10211045
Invoke-LanguageModeTestingSupportCmdlet -RevertLockdownMode -EnableFullLanguageMode
1046+
Remove-Module ImportTrustedManifestWithWildcardModFnExport_System32 -Force -ErrorAction SilentlyContinue
10221047
}
10231048

10241049
$module.ExportedCommands.Count | Should -Be 1
@@ -1050,6 +1075,7 @@ try
10501075
finally
10511076
{
10521077
Invoke-LanguageModeTestingSupportCmdlet -RevertLockdownMode -EnableFullLanguageMode
1078+
Remove-Module ImportTrustedManifestWithCmdletExport_System32 -Force -ErrorAction SilentlyContinue
10531079
}
10541080

10551081
$module.ExportedCommands.Count | Should -Be 0
@@ -1092,6 +1118,7 @@ try
10921118
finally
10931119
{
10941120
Invoke-LanguageModeTestingSupportCmdlet -RevertLockdownMode -EnableFullLanguageMode
1121+
Remove-Module ImportTrustedManifestWithTwoMods_System32 -Force -ErrorAction SilentlyContinue
10951122
}
10961123

10971124
$module.ExportedCommands.Count | Should -Be 1
@@ -1124,6 +1151,7 @@ try
11241151
finally
11251152
{
11261153
Invoke-LanguageModeTestingSupportCmdlet -RevertLockdownMode -EnableFullLanguageMode
1154+
Remove-Module ImportTrustedManifestWithExportFn_System32 -Force -ErrorAction SilentlyContinue
11271155
}
11281156

11291157
$module.ExportedCommands.Count | Should -Be 1
@@ -1155,6 +1183,7 @@ try
11551183
finally
11561184
{
11571185
Invoke-LanguageModeTestingSupportCmdlet -RevertLockdownMode -EnableFullLanguageMode
1186+
Remove-Module ImportTrustedManifestWithNestedModsAndFnExport_System32 -Force -ErrorAction SilentlyContinue
11581187
}
11591188

11601189
$module.ExportedCommands.Count | Should -Be 1
@@ -1186,6 +1215,7 @@ try
11861215
finally
11871216
{
11881217
Invoke-LanguageModeTestingSupportCmdlet -RevertLockdownMode -EnableFullLanguageMode
1218+
Remove-Module ImportTrustedManifestWithNestedModsAndNoFnExport_System32 -Force -ErrorAction SilentlyContinue
11891219
}
11901220

11911221
$module.ExportedCommands.Count | Should -Be 0
@@ -1216,6 +1246,7 @@ try
12161246
finally
12171247
{
12181248
Invoke-LanguageModeTestingSupportCmdlet -RevertLockdownMode -EnableFullLanguageMode
1249+
Remove-Module ImportTrustedManifestWithNestedModsAndNoExtNoFnExport_System32 -Force -ErrorAction SilentlyContinue
12191250
}
12201251

12211252
$module.ExportedCommands.Count | Should -Be 1
@@ -1260,6 +1291,7 @@ try
12601291
finally
12611292
{
12621293
Invoke-LanguageModeTestingSupportCmdlet -RevertLockdownMode -EnableFullLanguageMode
1294+
Remove-Module ImportTrustedManifestWithDotSourceModAndFnExport_System32 -Force -ErrorAction SilentlyContinue
12631295
}
12641296

12651297
$module.ExportedCommands.Count | Should -Be 1
@@ -1305,6 +1337,7 @@ try
13051337
finally
13061338
{
13071339
Invoke-LanguageModeTestingSupportCmdlet -RevertLockdownMode -EnableFullLanguageMode
1340+
Remove-Module ImportUntrustedManifestWithNoFnExport -Force -ErrorAction SilentlyContinue
13081341
}
13091342

13101343
$module.ExportedCommands.Count | Should -Be 2
@@ -1335,6 +1368,7 @@ try
13351368
finally
13361369
{
13371370
Invoke-LanguageModeTestingSupportCmdlet -RevertLockdownMode -EnableFullLanguageMode
1371+
Remove-Module ImportUntrustedManifestWithFnExport -Force -ErrorAction SilentlyContinue
13381372
}
13391373

13401374
$module.ExportedCommands.Count | Should -Be 1
@@ -1365,6 +1399,7 @@ try
13651399
finally
13661400
{
13671401
Invoke-LanguageModeTestingSupportCmdlet -RevertLockdownMode -EnableFullLanguageMode
1402+
Remove-Module ImportUnTrustedModuleWithNoFnExport -Force -ErrorAction SilentlyContinue
13681403
}
13691404

13701405
$module.ExportedCommands.Count | Should -Be 2
@@ -1395,6 +1430,7 @@ try
13951430
finally
13961431
{
13971432
Invoke-LanguageModeTestingSupportCmdlet -RevertLockdownMode -EnableFullLanguageMode
1433+
Remove-Module ImportUnTrustedModuleWithFnExport -Force -ErrorAction SilentlyContinue
13981434
}
13991435

14001436
$module.ExportedCommands.Count | Should -Be 1
@@ -1518,6 +1554,7 @@ try
15181554
finally
15191555
{
15201556
Invoke-LanguageModeTestingSupportCmdlet -RevertLockdownMode -EnableFullLanguageMode
1557+
Remove-Module UntrustedScriptFoo -Force -ErrorAction SilentlyContinue
15211558
}
15221559

15231560
$result | Should -BeExactly "ConstrainedLanguage"

test/powershell/Modules/Microsoft.PowerShell.Security/ConstrainedLanguageRestriction.Tests.ps1

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ try
131131
$template -f $configFilePath > $moduleFilePath
132132
}
133133

134+
AfterAll {
135+
Remove-Module $scriptModuleName -Force -ErrorAction SilentlyContinue
136+
}
137+
134138
It "Verifies that a NoLanguage runspace pool throws the expected 'script not allowed' error" {
135139

136140
try
@@ -405,6 +409,7 @@ try
405409
finally
406410
{
407411
Invoke-LanguageModeTestingSupportCmdlet -EnableFullLanguageMode
412+
Remove-Module PSDiagnostics -Force -ErrorAction SilentlyContinue
408413
}
409414

410415
$expectedError.FullyQualifiedErrorId | Should -BeExactly "CantInvokeCallOperatorAcrossLanguageBoundaries"
@@ -889,17 +894,17 @@ try
889894
$PSDefaultParameterValues["it:skip"] = $true
890895
}
891896
}
892-
897+
893898
AfterAll {
894-
899+
895900
if ($skipTest) {
896901
$global:PSDefaultParameterValues = $originalDefaultParameterValues
897902
}
898903
}
899904

900905
It 'Foreach-Object -Parallel must run in ConstrainedLanguage mode under system lock down' {
901906

902-
try
907+
try
903908
{
904909
$ExecutionContext.SessionState.LanguageMode = "ConstrainedLanguage"
905910
Invoke-LanguageModeTestingSupportCmdlet -SetLockdownMode
@@ -940,6 +945,10 @@ try
940945
'@ -f $scriptFilePath | Out-File -FilePath $scriptModulePath
941946
}
942947

948+
AfterAll {
949+
Remove-Module $scriptModuleName -Force -ErrorAction SilentlyContinue
950+
}
951+
943952
It "Verifies a scriptblock from a trusted script file does not run as trusted" {
944953

945954
$result = $null
@@ -985,6 +994,11 @@ try
985994
'@ -f $importModulePath | Out-File -FilePath $scriptModulePath
986995
}
987996

997+
AfterAll {
998+
Remove-Module $importModuleName -Force -ErrorAction SilentlyContinue
999+
Remove-Module $scriptModuleName -Force -ErrorAction SilentlyContinue
1000+
}
1001+
9881002
It "Verifies that trusted module functions run in FullLanguage" {
9891003

9901004
$result1 = $null
@@ -1040,6 +1054,7 @@ try
10401054
AfterAll {
10411055

10421056
Remove-Module -Name T1ScriptClass_System32 -Force -ErrorAction Ignore
1057+
Remove-Module -Name T1ScriptClass -Force -ErrorAction Ignore
10431058
}
10441059

10451060
It "Verifies that classes cannot be created in script running under constrained language" {

0 commit comments

Comments
 (0)