Skip to content

Commit 1c551ab

Browse files
committed
small enhancement to make rules that don't match not appear
1 parent dc310b9 commit 1c551ab

File tree

2 files changed

+67
-5
lines changed

2 files changed

+67
-5
lines changed

scripts/PSCodingStandards/Get-RuleForPath.ps1

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
# is a substring of the path once the substitution `/src/` -> `/test/` is
2929
# applied
3030

31+
$global:ruleCacheC = $null;
32+
$global:ruleCacheCPP = $null;
33+
$global:enableRuleCache = $false
3134
function Get-RuleForPath {
3235
param([Parameter(Mandatory)]
3336
[string]
@@ -41,9 +44,30 @@ function Get-RuleForPath {
4144
$allQueries = @()
4245
$queriesToCheck = @()
4346

44-
# load all the queries
45-
foreach ($s in $AVAILABLE_SUITES) {
46-
$allQueries += Get-RulesInSuite -Suite $s -Language $Language
47+
if($global:enableRuleCache){
48+
# load all the queries
49+
if($Language -eq 'cpp'){
50+
$ruleCache = $global:ruleCacheCPP
51+
}else{
52+
$ruleCache = $global:ruleCacheC
53+
}
54+
}
55+
56+
if(-not $ruleCache){
57+
58+
foreach ($s in $AVAILABLE_SUITES) {
59+
$allQueries += Get-RulesInSuite -Suite $s -Language $Language
60+
}
61+
62+
if($global:enableRuleCache){
63+
if($Language -eq 'cpp'){
64+
$global:ruleCacheCPP = $allQueries
65+
}else{
66+
$global:ruleCacheC = $allQueries
67+
}
68+
}
69+
}else{
70+
$allQueries = $ruleCache
4771
}
4872

4973
$modifiedPathWithReplacement = Join-Path (Resolve-Path . -Relative) $Path
@@ -57,12 +81,11 @@ function Get-RuleForPath {
5781

5882
# for each query, create the test directory
5983
foreach($q in $allQueries){
60-
6184
# get test directory
6285
$testDirs = (Get-ATestDirectory -RuleObject $q -Language $Language)
6386
foreach($testDirectory in $testDirs){
6487
# resolve path to be compatible
65-
$testPath = Join-Path (Resolve-Path . -Relative) $testDirectory
88+
$testPath = (Join-Path (Resolve-Path . -Relative) $testDirectory) + [IO.Path]::DirectorySeparatorChar
6689

6790
# see if the TEST directory is a substring of the full path
6891
if($modifiedPath.StartsWith($testPath)){
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function Test-GetRuleForPath {
2+
param(
3+
[Parameter(Mandatory)]
4+
[string]
5+
$PR
6+
)
7+
8+
$prData = (gh pr view -R github/codeql-coding-standards $PR --json headRefOid,headRepository,author,isCrossRepository,headRepositoryOwner,headRefName,files) | ConvertFrom-Json
9+
10+
foreach($f in $prData.files){
11+
try {
12+
Write-Host "[C] Scanning file for relationship $($f.path)..."
13+
$rulesToTest = Get-RuleForPath -Language c -Path "$($f.path)"
14+
15+
Write-Host "[C] Got $($rulesToTest.Count) potential C rules..."
16+
17+
foreach($r in $rulesToTest){
18+
$ruleNames += $r.__memberof_rule
19+
Write-Host "[C] Found rule $r "
20+
}
21+
}catch{
22+
Write-Host "No $Language rules found for path: $($f.path)"
23+
}
24+
25+
26+
try {
27+
Write-Host "[CPP] Scanning file for relationship $($f.path)..."
28+
$rulesToTest = Get-RuleForPath -Language cpp -Path "$($f.path)"
29+
30+
Write-Host "[CPP] Got $($rulesToTest.Count) potential CPP rules..."
31+
32+
foreach($r in $rulesToTest){
33+
Write-Host "[CPP] Found rule $r "
34+
}
35+
}catch{
36+
Write-Host "No CPP rules found for path: $($f.path)"
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)