Skip to content

Tests - Report Query() calls whose value goes to the output - #10798

Open
andreasjordan wants to merge 2 commits into
developmentfrom
bare-query-compliance-check
Open

andreasjordan wants to merge 2 commits into
developmentfrom
bare-query-compliance-check

Conversation

@andreasjordan

@andreasjordan andreasjordan commented Oct 18, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #10794: the check that keeps new bare Query() calls out, now that the fixes are in. #10791, #10795, #10796 and #10797 are merged, and development is merged into this branch (1431760). The scan finds exactly the 19 listed sites, and dbatools.Tests.ps1 passes in the lab (87 passed, 6 skipped, 0 failed, Windows PowerShell 5.1).

What it checks

The Query() script methods of the SMO Server and Database objects return $null for a batch without a result set, and a call whose value nothing takes writes that $null into the output of the command. The new test in the "command structure" Describe (Compliance tag, no SQL Server needed) reports every .Query(...) call in public/ and private/functions/ whose value reaches its function.

Get-DbaBareQueryFinding in tests/dbatools.CommandStructure.ps1 follows the value up the syntax tree, the way PowerShell passes it on:

The value goes on through The value is taken by
statement blocks, $( ), @( ), ( ), array literals an assignment, a cast, a redirection to a file or $null
the bodies of if, switch, loops, try, catch, finally, trap a further command in a pipeline (| Out-Null, | Select-Object)
the last command of a pipeline return, throw, an argument, an operand, the object of a member
a scriptblock, to the command, method, & or . that runs it the condition of an if, a loop or a switch

So $items | ForEach-Object { $server.Query("DROP ...") } is reported, while $rows = if ($x) { $server.Query($sql) } and foreach ($row in @($db.Query($sql))) are not. The last one is the false positive the scan in the issue had (Get-EncryptedObjectImageValue).

The exceptions

19 calls return the rows of a SELECT as their output on purpose (Get-DbaConnection, Get-DbaPermission, Read-DbaTransactionLog, the helpers of Get-DbaDatabase, ...). They are listed next to the rule 1 exceptions with file, function, exact number of sites and reason, and the comparison is shared: a new site in a listed function, or a listed one that is gone, fails the check until the entry is updated. The rule 1 messages are unchanged.

Fixtures in the "command structure analyzer" Describe cover both columns of the table above, a helper function inside a command, and the method name in any spelling.

tests/CLAUDE.md

"Array Operations" recommended $results.Status.Count for counting. That skips a $null in the output, and it is how these $null went unnoticed: it now says to count the output itself with @($results).Count, or to assert that it holds no $null. The section on the structural checks describes the new one.

Tests

PowerShell 5.1 and 7.6, the two "command structure" Describes:

Merge order

After #10791, #10795, #10796 and #10797. If a bare Query() comes into development before that, this check reports it as well.

🤖 Generated with Claude Code

Query() returns $null for a batch without a result set, and a call whose value nothing takes writes
that $null into the output of the command (#10794). #10791, #10795, #10796 and #10797 fix the 86
such sites on development; this check keeps new ones out.

Get-DbaBareQueryFinding follows the value of every .Query(...) call up the tree, the way PowerShell
passes it on, and the "command structure" Describe reports every call whose value reaches its
function. The 19 calls that return the rows of a SELECT as their output are listed with the exact
number of sites, like the rule 1 exceptions, which share the comparison with them now. Fixtures in
the analyzer Describe cover what passes the value on and what takes it.

tests/CLAUDE.md no longer recommends counting with $results.Status.Count, which skips a $null, and
documents the check.

Until the four PRs are merged, the new test fails on purpose, with the 86 sites they fix.

(do dbatools)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@andreasjordan
andreasjordan marked this pull request as ready for review October 23, 2026 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant