Skip to content

Commit c97291f

Browse files
mbaludaCopilot
andcommitted
Fix matrix tooling for reused fixtures and suite selection
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 5b4dd36 commit c97291f

8 files changed

Lines changed: 133 additions & 33 deletions

File tree

‎.github/workflows/tooling-unit-tests.yml‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,10 @@ jobs:
116116

117117
- name: Run PyTest
118118
run: |
119-
pytest scripts/release/update_release_assets_test.py
119+
pytest scripts/release/update_release_assets_test.py \
120+
scripts/release/create_supported_rules_list_test.py \
121+
scripts/verify_rule_package_consistency_test.py
122+
123+
- name: Run matrix tooling regression tests
124+
shell: pwsh
125+
run: ./scripts/matrix_testing/Get-TestFile.Tests.ps1
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
1-
$AVAILABLE_SUITES = @("CERT-C++", "AUTOSAR", "MISRA-C-2025", "MISRA-C-2012", "CERT-C", "MISRA-C++-2023")
1+
$SUITE_DEFINITIONS = [ordered]@{
2+
"CERT-C++" = @{}
3+
"AUTOSAR" = @{}
4+
"MISRA-C-2025" = @{
5+
IncludeSuites = @("MISRA-C-2012")
6+
ExcludeTags = @("external/misra/legacy")
7+
}
8+
"MISRA-C-2012" = @{}
9+
"CERT-C" = @{}
10+
"MISRA-C++-2023" = @{}
11+
}
12+
$AVAILABLE_SUITES = @($SUITE_DEFINITIONS.Keys)
213
$AVAILABLE_LANGUAGES = @("c", "cpp")

‎scripts/PSCodingStandards/Get-RuleForPath.ps1‎

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@
2222
# cpp/common/test/rules/informationleakageacrossboundaries/multilayer.cpp
2323
# cpp/common/test/rules/informationleakageacrossboundaries/test.cpp
2424

25-
# And produces one or more rules for it. It does this by loading every rule
26-
# and computing the test directory for it. This test directory is then
27-
# used to see if a) it is a substring of the supplied path or if b) it
28-
# is a substring of the path once the substitution `/src/` -> `/test/` is
29-
# applied
25+
# Matches original rule directories and resolved fixture directories, including
26+
# shared tests and compiler-specific variants.
3027

3128
function Get-RuleForPath {
3229
param([Parameter(Mandatory)]
@@ -47,31 +44,31 @@ function Get-RuleForPath {
4744
}
4845

4946

50-
$modifiedPathWithReplacement = Join-Path (Resolve-Path . -Relative) $Path
47+
$repositoryRoot = Get-RepositoryRoot
48+
$modifiedPath = [IO.Path]::GetRelativePath(
49+
$repositoryRoot, [IO.Path]::GetFullPath($Path, $repositoryRoot))
5150
# replace "src" with "test" to make it match up
5251
$sep = [IO.Path]::DirectorySeparatorChar
53-
$modifiedPathWithReplacement = $modifiedPathWithReplacement.Replace( ($sep + "src" + $sep + "rules"), ($sep + "test" + $sep + "rules"))
54-
$modifiedPath = Join-Path (Resolve-Path . -Relative) $Path
52+
$modifiedPathWithReplacement = $modifiedPath.Replace(
53+
($sep + "src" + $sep + "rules"), ($sep + "test" + $sep + "rules"))
5554

5655

5756
$matchingRules = @()
5857

5958
# for each query, create the test directory
6059
foreach($q in $allQueries){
61-
# get test directory
62-
$testDirs = (Get-ATestDirectory -RuleObject $q -Language $Language)
60+
# Keep the wrapper's own directory even when its fixture is elsewhere.
61+
$standard = ($q.__memberof_suite -split "-")[0].ToLowerInvariant()
62+
$testDirs = @(Join-Path $Language "$standard/test/rules/$($q.__memberof_rule)")
63+
$testDirs += Get-ATestDirectory -RuleObject $q -Language $Language
6364
foreach($testDirectory in $testDirs){
64-
# resolve path to be compatible
65-
$testPath = (Join-Path (Resolve-Path . -Relative) $testDirectory)
65+
$testPath = [IO.Path]::GetRelativePath(
66+
$repositoryRoot, [IO.Path]::GetFullPath($testDirectory, $repositoryRoot))
6667

67-
if((Split-Path $modifiedPath -Parent) -eq $testPath){
68+
if ((Split-Path $modifiedPath -Parent) -eq $testPath -or
69+
(Split-Path $modifiedPathWithReplacement -Parent) -eq $testPath) {
6870
$matchingRules += $q
69-
continue
70-
}
71-
72-
if((Split-Path $modifiedPathWithReplacement -Parent) -eq $testPath){
73-
$matchingRules += $q
74-
continue
71+
break
7572
}
7673
}
7774
}

‎scripts/PSCodingStandards/Get-RulesInPackageAndSuite.ps1‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ function Get-RulesInPackageAndSuite {
3535
$queries | Add-Member -NotePropertyName __memberof_package -NotePropertyValue $Package.BaseName
3636
$queries | Add-Member -NotePropertyName __memberof_rule -NotePropertyValue $n.Name
3737
$queries | Add-Member -NotePropertyName __memberof_language -NotePropertyValue $Language
38+
$queries | Add-Member -NotePropertyName __applicable_suites -NotePropertyValue @($Suite)
39+
foreach ($targetSuite in $SUITE_DEFINITIONS.Keys) {
40+
$definition = $SUITE_DEFINITIONS[$targetSuite]
41+
if ($definition.IncludeSuites -contains $Suite) {
42+
$queries | Where-Object {
43+
-not ($_.tags | Where-Object { $definition.ExcludeTags -contains $_ })
44+
} | ForEach-Object { $_.__applicable_suites += $targetSuite }
45+
}
46+
}
3847

3948
$rulesInPackage += $queries
4049
}

‎scripts/PSCodingStandards/Get-TestFile.ps1‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ function Get-TestFile {
3737
}
3838

3939
$testDirectory = Join-Path (Join-Path (Join-Path (Join-Path $Language $standardString) "test") "rules") $ruleDir
40+
$testFile = Join-Path $testDirectory "$queryName$extension"
4041
$referenceFile = Join-Path $testDirectory "$queryName.testref"
41-
if (Test-Path -LiteralPath $referenceFile -PathType Leaf) {
42+
if (-not (Test-Path -LiteralPath $testFile -PathType Leaf) -and
43+
(Test-Path -LiteralPath $referenceFile -PathType Leaf)) {
4244
$reference = Get-Content -LiteralPath $referenceFile -Raw -ErrorAction Stop
4345
if ([string]::IsNullOrWhiteSpace($reference)) {
4446
throw "Empty test reference: $referenceFile"
@@ -47,7 +49,8 @@ function Get-TestFile {
4749
throw "Test reference must be repository-relative: $referenceFile"
4850
}
4951

50-
$testFile = Join-Path (Get-RepositoryRoot) $reference.Trim()
52+
$repositoryRoot = Get-RepositoryRoot
53+
$testFile = Join-Path $repositoryRoot $reference.Trim()
5154
if ([IO.Path]::GetExtension($testFile) -notin @(".ql", ".qlref")) {
5255
throw "Test reference must name a .ql or .qlref file: $referenceFile"
5356
}
@@ -58,8 +61,8 @@ function Get-TestFile {
5861
if (-not (Test-Path -LiteralPath $expectedFile -PathType Leaf)) {
5962
throw "Referenced test has no expected results: $expectedFile"
6063
}
61-
return (Resolve-Path -LiteralPath $testFile).Path
64+
return [IO.Path]::GetRelativePath($repositoryRoot, (Resolve-Path -LiteralPath $testFile).Path)
6265
}
6366

64-
return Join-Path $testDirectory "$queryName$extension"
67+
return $testFile
6568
}

‎scripts/PSCodingStandards/README.md‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,16 @@ with matching `.expected` results. A new query variant needs its own runnable te
5656
but can reuse an existing C/C++ fixture. The matrix runner uses the referenced
5757
file's directory, query name, and expected results.
5858

59-
Run the test-reference regression checks with:
59+
Direct runnable tests take precedence over `.testref` files. Returned test files
60+
and their base fixture directories are repository-relative paths, so wrappers
61+
sharing a fixture use the same matrix lock.
62+
63+
`__applicable_suites` controls matrix suite selection without changing
64+
`__memberof_suite` ownership. Add suites to `$SUITE_DEFINITIONS` in `Config.ps1`;
65+
`IncludeSuites` lists additional owning suites, and `ExcludeTags` filters their
66+
queries. MISRA C:2025 uses this configuration to include non-legacy MISRA C:2012-owned queries.
67+
68+
Run the matrix tooling regression checks with:
6069

6170
```
6271
pwsh -NoLogo -NoProfile -File scripts/matrix_testing/Get-TestFile.Tests.ps1

‎scripts/matrix_testing/CreateMatrixTestReport.ps1‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ foreach ($s in $AVAILABLE_SUITES) {
232232
# filter the set down based on the selections
233233

234234
if ($Suite) {
235-
$queriesToCheck = $allQueries | Where-Object { $_.__memberof_suite -eq $SuiteName }
235+
$queriesToCheck = $allQueries | Where-Object { $_.__applicable_suites -contains $SuiteName }
236236
}
237237
elseif ($Package) {
238238
$queriesToCheck = $allQueries | Where-Object { $_.__memberof_package -eq $PackageName }

‎scripts/matrix_testing/Get-TestFile.Tests.ps1‎

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,21 @@ $repositoryRoot = (Resolve-Path "$PSScriptRoot/../..").Path
33
Push-Location $repositoryRoot
44
try {
55
Import-Module "$repositoryRoot/scripts/PSCodingStandards/CodingStandards"
6-
$queries = Get-RulesInSuite -Language c -Suite "MISRA-C-2025"
6+
$queries = @(foreach ($suite in $AVAILABLE_SUITES) {
7+
Get-RulesInSuite -Language c -Suite $suite
8+
})
79
$expectedFiles = @{
8-
"LanguageExtensionsShouldBeMinimized" = "c/misra/test/rules/RULE-1-2/LanguageExtensionsShouldBeMinimized.qlref"
10+
"LanguageExtensionsShouldBeMinimized" =
11+
"c/misra/test/rules/RULE-1-2/LanguageExtensionsShouldBeMinimized.qlref"
12+
"LanguageExtensionsShouldNotBeUsed" =
13+
"c/misra/test/rules/RULE-1-2/LanguageExtensionsShouldNotBeUsed.qlref"
914
"ReservedMacroName" = "c/misra/test/rules/RULE-21-1/ReservedMacroName.qlref"
15+
"DefineAndUndefUsedOnReservedIdentifierOrMacroName" =
16+
"c/misra/test/rules/RULE-21-1/DefineAndUndefUsedOnReservedIdentifierOrMacroName.qlref"
1017
"SinglePointOfExit" = "c/misra/test/rules/RULE-15-5/SinglePointOfExit.qlref"
18+
"DoNotCallSystem" = "c/cert/test/rules/ENV33-C/DoNotCallSystem.qlref"
19+
"ConversionBetweenFunctionPointerAndOtherType" =
20+
"c/misra/test/rules/RULE-11-1/ConversionBetweenFunctionPointerAndOtherType.qlref"
1121
}
1222

1323
foreach ($name in $expectedFiles.Keys) {
@@ -16,27 +26,82 @@ try {
1626
throw "Expected exactly one query named $name"
1727
}
1828
$testFile = Get-TestFile -RuleObject $query[0] -Language c
19-
$expected = [IO.Path]::GetFullPath((Join-Path $repositoryRoot $expectedFiles[$name]))
20-
if ([IO.Path]::GetFullPath($testFile) -ne $expected) {
29+
$expected = [IO.Path]::GetRelativePath(
30+
$repositoryRoot, (Join-Path $repositoryRoot $expectedFiles[$name]))
31+
if ($testFile -ne $expected) {
2132
throw "Unexpected test file for ${name}: $testFile"
2233
}
2334
$directory = Get-TestDirectory -RuleObject $query[0] -Language c
24-
if ([IO.Path]::GetFullPath($directory) -ne (Split-Path $expected -Parent)) {
35+
if ($directory -ne (Split-Path $expected -Parent)) {
2536
throw "Unexpected fixture directory for ${name}: $directory"
2637
}
2738
if (-not (Test-Path ([IO.Path]::ChangeExtension($testFile, ".expected")))) {
2839
throw "Missing expected results for $name"
2940
}
3041
}
3142

43+
$sourceFiles = @{
44+
"c/misra/src/rules/RULE-20-15/ReservedMacroName.ql" = "ReservedMacroName"
45+
"c/misra/src/rules/DIR-1-2/LanguageExtensionsShouldBeMinimized.ql" =
46+
"LanguageExtensionsShouldBeMinimized"
47+
}
48+
foreach ($source in $sourceFiles.Keys) {
49+
foreach ($path in @($source, (Join-Path $repositoryRoot $source))) {
50+
$rules = @(Get-RuleForPath -Language c -Path $path)
51+
if ($rules.Count -ne 1 -or $rules[0].short_name -ne $sourceFiles[$source]) {
52+
throw "Unexpected rules for source path: $path"
53+
}
54+
}
55+
}
56+
$rules = @(Get-RuleForPath -Language c -Path $expectedFiles["ReservedMacroName"])
57+
if ($rules.Count -ne 2 -or $rules.short_name -notcontains "ReservedMacroName" -or
58+
$rules.short_name -notcontains "DefineAndUndefUsedOnReservedIdentifierOrMacroName") {
59+
throw "Expected both wrappers for the shared reserved-macro fixture."
60+
}
61+
62+
$currentQueries = @($queries | Where-Object {
63+
$_.__applicable_suites -contains "MISRA-C-2025"
64+
})
65+
foreach ($name in @(
66+
"FunctionDeclaredImplicitly",
67+
"ArrayFunctionArgumentNumberOfElements",
68+
"TerminationFunctionsOfStdlibhUsed"
69+
)) {
70+
$query = @($currentQueries | Where-Object { $_.short_name -eq $name })
71+
if ($query.Count -ne 1 -or $query[0].__memberof_suite -ne "MISRA-C-2012") {
72+
throw "Expected the older-owned query in MISRA-C-2025: $name"
73+
}
74+
}
75+
if ($currentQueries | Where-Object { $_.tags -contains "external/misra/legacy" }) {
76+
throw "Legacy wrappers must not be included in MISRA-C-2025."
77+
}
78+
79+
& (Get-Module CodingStandards) {
80+
$SUITE_DEFINITIONS["Future-Suite"] = @{
81+
IncludeSuites = @("CERT-C")
82+
ExcludeTags = @("external/cert/severity/low")
83+
}
84+
try {
85+
$futureQueries = @(Get-RulesInSuite -Language c -Suite "CERT-C" |
86+
Where-Object { $_.__applicable_suites -contains "Future-Suite" })
87+
if (-not $futureQueries -or
88+
$futureQueries.tags -contains "external/cert/severity/low") {
89+
throw "Configured suite inclusion and tag exclusions were not applied."
90+
}
91+
}
92+
finally {
93+
$SUITE_DEFINITIONS.Remove("Future-Suite")
94+
}
95+
}
96+
3297
$shared = [pscustomobject]@{
3398
shared_implementation_short_name = "SharedQuery"
3499
}
35100
$sharedFile = Get-TestFile -RuleObject $shared -Language cpp
36101
if ($sharedFile.Replace("\", "/") -ne "cpp/common/test/rules/sharedquery/SharedQuery.ql") {
37102
throw "Unexpected shared test file: $sharedFile"
38103
}
39-
Write-Host "Get-TestFile regression tests passed."
104+
Write-Host "Matrix tooling regression tests passed."
40105
}
41106
finally {
42107
Pop-Location

0 commit comments

Comments
 (0)