FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Dynamically count expected rules in GetScriptAnalyzerRule test by andyleejordan · Pull Request #2167 · PowerShell/PSScriptAnalyzer · GitHub

Dynamically count expected rules in GetScriptAnalyzerRule test - #2167

Merged
Andy Jordan (andyleejordan) merged 1 commit into
mainfrom
fix-dynamic-rule-count
Mar 18, 2026
Merged

Dynamically count expected rules in GetScriptAnalyzerRule test#2167
Andy Jordan (andyleejordan) merged 1 commit into
mainfrom
fix-dynamic-rule-count

Conversation

Copy link
Copy Markdown
Member

Had Copilot do this:

Replace hardcoded rule count with dynamic counting of [Export(typeof(I...Rule))] attributes in C# source files. This prevents the test from breaking every time a new rule is added.

Seems like it works. I noticed every time a PR that added a rule was merged, all PRs after it failed because this test had a hardcoded assumption.

Replace hardcoded rule count with dynamic counting of [Export(typeof(I...Rule))]
attributes in C# source files. This prevents the test from breaking every time a
new rule is added.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Pull request overview

Updates the Get-ScriptAnalyzerRule Pester test to avoid hardcoding the expected number of built-in rules by deriving the expected count dynamically from the repository’s C# rule sources.

Changes:

  • Replaces the fixed expected rule count (72) with a dynamic count based on scanning Rules/**/*.cs for Export(typeof(I*Rule)) occurrences.
  • Adds comments explaining the dynamic rule-counting approach.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +66 to +71
# Dynamically count the expected number of rules from source files
# by finding all C# files with [Export(typeof(I...Rule))] attributes
$rulesRoot = Resolve-Path "$PSScriptRoot/../../Rules"
$expectedNumRules = (Get-ChildItem -Path $rulesRoot -Filter '*.cs' -Recurse |
Select-String -Pattern 'Export\(typeof\s*\(I\w+Rule\)\)' |
Select-Object -ExpandProperty Path -Unique).Count

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Wow bad Copilot, that suggestion would mean that we don't end up testing anything at all.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

(Claude did the actual PR...lol)

Andy Jordan (andyleejordan) merged commit f3e9134 into main Mar 18, 2026
11 checks passed
Andy Jordan (andyleejordan) deleted the fix-dynamic-rule-count branch March 18, 2026 17:04

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Nice addition but it can still be of value to keep a hard=coded check (in addition to this), how about we make it less troublesome by just asserting greater than instead of equals so this test doesn't need updating for every new rule?

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

4 participants


Back | FazBrowse Home | New Git URL