From 4c1e05cf51ef5a31dc0a375c7b434e9254c1f92e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olav=20R=C3=B8nnestad=20Birkeland?= Date: Mon, 27 May 2024 18:27:17 +0200 Subject: [PATCH 1/4] Started writing new test --- ...UsingScopeModifierInNewRunspaces.tests.ps1 | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/Tests/Rules/UseUsingScopeModifierInNewRunspaces.tests.ps1 b/Tests/Rules/UseUsingScopeModifierInNewRunspaces.tests.ps1 index 65ba1fdec..c2678f927 100644 --- a/Tests/Rules/UseUsingScopeModifierInNewRunspaces.tests.ps1 +++ b/Tests/Rules/UseUsingScopeModifierInNewRunspaces.tests.ps1 @@ -278,20 +278,27 @@ Describe "UseUsingScopeModifierInNewRunspaces" { # Issue 1492: https://github.com/PowerShell/PSScriptAnalyzer/issues/1492 @{ Description = 'Does not throw when the same variable name is used in two different sessions' - ScriptBlock = @' -function Get-One{ - Invoke-Command -Session $sourceRemoteSession { - $a = $sccmModule - foo $a - } -} -function Get-Two{ - Invoke-Command -Session $sourceRemoteSession { - $a = $sccmModule - foo $a - } -} -'@ + ScriptBlock = '{ + function Get-One { + Invoke-Command -Session -ScriptBlock $sourceRemoteSession { + $a = $sccmModule + foo $a + } + } + function Get-Two { + Invoke-Command -Session -ScriptBlock $sourceRemoteSession { + $a = $sccmModule + foo $a + } + } + }' + } + # Script block with variables in params(), issue #1504: https://github.com/PowerShell/PSScriptAnalyzer/issues/1504 + @{ + Description = 'Does not throw when variable is defined inside params()' + ScriptBlock = '{ + + }' } ) } From 4e0086b5a479dfd4bc7967f3ba2e99d713932a3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olav=20R=C3=B8nnestad=20Birkeland?= Date: Mon, 27 May 2024 18:39:31 +0200 Subject: [PATCH 2/4] Wrote tests for both Start-Job and Start-ThreadJob --- ...UsingScopeModifierInNewRunspaces.tests.ps1 | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/Tests/Rules/UseUsingScopeModifierInNewRunspaces.tests.ps1 b/Tests/Rules/UseUsingScopeModifierInNewRunspaces.tests.ps1 index c2678f927..6a785c3f1 100644 --- a/Tests/Rules/UseUsingScopeModifierInNewRunspaces.tests.ps1 +++ b/Tests/Rules/UseUsingScopeModifierInNewRunspaces.tests.ps1 @@ -146,7 +146,6 @@ Describe "UseUsingScopeModifierInNewRunspaces" { } Context "Should not detect anything" { - It "should not emit anything for: " { [System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition $ScriptBlock -Settings $settings $warnings.Count | Should -Be 0 @@ -293,11 +292,25 @@ Describe "UseUsingScopeModifierInNewRunspaces" { } }' } - # Script block with variables in params(), issue #1504: https://github.com/PowerShell/PSScriptAnalyzer/issues/1504 + # ScriptBlock with variables in params(), issue #1504: https://github.com/PowerShell/PSScriptAnalyzer/issues/1504 + ## Microsoft.PowerShell.Core \ Start-Job @{ - Description = 'Does not throw when variable is defined inside params()' + Description = 'Does not throw when variable is defined inside params() - Start-Job' ScriptBlock = '{ - + Start-Job -ScriptBlock { + Param($Foo) + $Foo + } -ArgumentList "Bar" | Receive-Job -Wait -AutoRemoveJob + }' + } + ## Microsoft.PowerShell.ThreadJob \ Start-ThreadJob + @{ + Description = 'Does not throw when variable is defined inside params() - Start-Job' + ScriptBlock = '{ + Start-ThreadJob -ScriptBlock { + Param($Foo) + $Foo + } -ArgumentList "Bar" | Receive-Job -Wait -AutoRemoveJob }' } ) From 9d5d58f05e0095bd0e54733e0e1a748f551979a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olav=20R=C3=B8nnestad=20Birkeland?= Date: Mon, 27 May 2024 18:47:59 +0200 Subject: [PATCH 3/4] Changed wording in comment --- Tests/Rules/UseUsingScopeModifierInNewRunspaces.tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/Rules/UseUsingScopeModifierInNewRunspaces.tests.ps1 b/Tests/Rules/UseUsingScopeModifierInNewRunspaces.tests.ps1 index 6a785c3f1..f115bebaa 100644 --- a/Tests/Rules/UseUsingScopeModifierInNewRunspaces.tests.ps1 +++ b/Tests/Rules/UseUsingScopeModifierInNewRunspaces.tests.ps1 @@ -295,7 +295,7 @@ Describe "UseUsingScopeModifierInNewRunspaces" { # ScriptBlock with variables in params(), issue #1504: https://github.com/PowerShell/PSScriptAnalyzer/issues/1504 ## Microsoft.PowerShell.Core \ Start-Job @{ - Description = 'Does not throw when variable is defined inside params() - Start-Job' + Description = 'Does not warn when variable is defined inside params() - Start-Job' ScriptBlock = '{ Start-Job -ScriptBlock { Param($Foo) @@ -305,7 +305,7 @@ Describe "UseUsingScopeModifierInNewRunspaces" { } ## Microsoft.PowerShell.ThreadJob \ Start-ThreadJob @{ - Description = 'Does not throw when variable is defined inside params() - Start-Job' + Description = 'Does not warn when variable is defined inside params() - Start-Job' ScriptBlock = '{ Start-ThreadJob -ScriptBlock { Param($Foo) From 991b5736dcd448d0f442a41d7770423ee1e43ecb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olav=20R=C3=B8nnestad=20Birkeland?= Date: Mon, 27 May 2024 18:49:23 +0200 Subject: [PATCH 4/4] Corrected Params() to Param() --- Tests/Rules/UseUsingScopeModifierInNewRunspaces.tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/Rules/UseUsingScopeModifierInNewRunspaces.tests.ps1 b/Tests/Rules/UseUsingScopeModifierInNewRunspaces.tests.ps1 index f115bebaa..687df807a 100644 --- a/Tests/Rules/UseUsingScopeModifierInNewRunspaces.tests.ps1 +++ b/Tests/Rules/UseUsingScopeModifierInNewRunspaces.tests.ps1 @@ -295,7 +295,7 @@ Describe "UseUsingScopeModifierInNewRunspaces" { # ScriptBlock with variables in params(), issue #1504: https://github.com/PowerShell/PSScriptAnalyzer/issues/1504 ## Microsoft.PowerShell.Core \ Start-Job @{ - Description = 'Does not warn when variable is defined inside params() - Start-Job' + Description = 'Does not warn when variable is defined inside param() - Start-Job' ScriptBlock = '{ Start-Job -ScriptBlock { Param($Foo) @@ -305,7 +305,7 @@ Describe "UseUsingScopeModifierInNewRunspaces" { } ## Microsoft.PowerShell.ThreadJob \ Start-ThreadJob @{ - Description = 'Does not warn when variable is defined inside params() - Start-Job' + Description = 'Does not warn when variable is defined inside param() - Start-Job' ScriptBlock = '{ Start-ThreadJob -ScriptBlock { Param($Foo)