Skip to content

Commit 2d122fd

Browse files
Copilotbergmeister
andauthored
Reject background pipelines as DSC resources
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
1 parent 001a9c9 commit 2d122fd

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

‎Rules/AvoidAlias.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ private ScriptBlockExpressionAst GetDscResourceBody(CommandAst cmdAst)
318318
if (!(cmdAst.Parent is PipelineAst commandPipeline)
319319
|| commandPipeline.PipelineElements.Count != 1
320320
|| commandPipeline.PipelineElements[0] != cmdAst
321+
|| commandPipeline.Background
321322
|| !(commandPipeline.Parent is NamedBlockAst || commandPipeline.Parent is StatementBlockAst))
322323
{
323324
return null;
@@ -346,6 +347,7 @@ private ScriptBlockExpressionAst GetDscResourceBody(CommandAst cmdAst)
346347
if (index >= 0 && index + 1 < statements.Count
347348
&& statements[index + 1] is PipelineAst bodyPipeline
348349
&& bodyPipeline.PipelineElements.Count == 1
350+
&& !bodyPipeline.Background
349351
&& bodyPipeline.PipelineElements[0] is CommandExpressionAst bodyExpression)
350352
{
351353
if (HasStatementSeparator(cmdAst, bodyPipeline)

‎Tests/Rules/AvoidUsingAlias.tests.ps1‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ Configuration PackageTest {
208208
Write-Output $(Verb Argument {})
209209
@(Verb Argument {})
210210
Verb Argument {} > $null
211+
Verb Argument {} &
211212
if (Verb Argument {}) {}
212213
return Verb Argument {}
213214
if ($true) {
@@ -227,7 +228,7 @@ Configuration PackageTest {
227228

228229
$violations = @($rule.AnalyzeScript($configuration, $null))
229230

230-
$violations.Count | Should -Be 5
231+
$violations.Count | Should -Be 6
231232
$violations | ForEach-Object {
232233
$_.SuggestedCorrections[0].Text | Should -Be 'Get-Verb'
233234
}
@@ -241,6 +242,8 @@ Configuration PackageTest {
241242
gci .; {}
242243
gci . &
243244
{}
245+
gci .
246+
{} &
244247
}
245248
'@
246249
$ast = [System.Management.Automation.Language.Parser]::ParseInput($target, [ref]$null, [ref]$null)
@@ -252,7 +255,7 @@ Configuration PackageTest {
252255

253256
$violations = @($rule.AnalyzeScript($configuration, $null))
254257

255-
$violations.Count | Should -Be 3
258+
$violations.Count | Should -Be 4
256259
$violations | ForEach-Object {
257260
$_.Extent.Text | Should -Be 'gci'
258261
$_.SuggestedCorrections[0].Text | Should -Be 'Get-ChildItem'

0 commit comments

Comments
 (0)