Skip to content

Commit db56f00

Browse files
authored
[TypeSpecValidation] Add suppression mechanism specific to TSV-All (#30324)
- Also adds "DryRun" param for debugging
1 parent 9af6991 commit db56f00

File tree

3 files changed

+83
-14
lines changed

3 files changed

+83
-14
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<#
2+
.DESCRIPTION
3+
Returns the suppressions for a tool applicable to a path. Walks up the directory tree to find all files named
4+
"suppressions.yaml", parses and validates the contents, and returns the suppressions matching the tool and path.
5+
Suppressions are ordered by file (closest to path is first), then within the file (closest to top is first).
6+
7+
.PARAMETER Tool
8+
Name of tool. Matched against property "tool" in suppressions.yaml.
9+
10+
.PARAMETER Path
11+
Path to file or directory under analysis.
12+
13+
.OUTPUTS
14+
Hashtable[]
15+
Array of suppressions matching tool and path (may be empty). See the "get-suppressions" tool for the definition
16+
of the suppression object.
17+
#>
18+
function Get-Suppressions {
19+
param (
20+
[string]$Tool,
21+
[string]$Path
22+
)
23+
24+
# -NoEnumerate to prevent single-element arrays from being collapsed to a single object
25+
# -AsHashtable is closer to raw JSON than PSCustomObject
26+
$suppressions = npm exec --no -- get-suppressions $Tool $Path | ConvertFrom-Json -NoEnumerate -AsHashtable
27+
28+
if ($LASTEXITCODE -ne 0) {
29+
throw "Failure running 'npm exec get-suppressions'"
30+
}
31+
32+
return $suppressions;
33+
}
34+
35+
<#
36+
.DESCRIPTION
37+
Returns the first suppression for a tool applicable to a path. Walks up the directory tree to find all files named
38+
"suppressions.yaml", parses and validates the contents, and returns the first suppression matching the tool and path.
39+
Suppressions are ordered by file (closest to path is first), then within the file (closest to top is first).
40+
41+
.PARAMETER Tool
42+
Name of tool. Matched against property "tool" in suppressions.yaml.
43+
44+
.PARAMETER Path
45+
Path to file or directory under analysis.
46+
47+
.OUTPUTS
48+
Hashtable
49+
First suppressions matching tool and path (may be null). See the "get-suppressions" tool for the definition
50+
of the suppression object.
51+
#>
52+
function Get-Suppression {
53+
param (
54+
[string]$Tool,
55+
[string]$Path
56+
)
57+
58+
$suppressions = @(Get-Suppressions $Tool $Path)
59+
return $suppressions ? $suppressions[0] : $null;
60+
}

eng/scripts/TypeSpec-Requirement.ps1

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,14 @@ Set-StrictMode -Version 3
1616

1717
. $PSScriptRoot/ChangedFiles-Functions.ps1
1818
. $PSScriptRoot/Logging-Functions.ps1
19+
. $PSScriptRoot/Suppressions-Functions.ps1
1920

20-
function Get-Suppression {
21+
function Get-ValidatedSuppression {
2122
param (
2223
[string]$fileInSpecFolder
2324
)
2425

25-
# -NoEnumerate to prevent single-element arrays from being collapsed to a single object
26-
# -AsHashtable is closer to raw JSON than PSCustomObject
27-
$suppressions = npm exec --no -- get-suppressions TypeSpecRequirement $fileInSpecFolder | ConvertFrom-Json -NoEnumerate -AsHashtable
28-
29-
if ($LASTEXITCODE -ne 0) {
30-
LogError "Failure running 'npm exec get-suppressions'"
31-
LogJobFailure
32-
exit 1
33-
}
34-
35-
# For now, we just use the first matching suppression returned by "get-suppressions" (#29003)
36-
$suppression = $suppressions ? $suppressions[0] : $null
26+
$suppression = Get-Suppression "TypeSpecRequirement" $fileInSpecFolder
3727

3828
if ($suppression) {
3929
# Each path must specify a single version (without wildcards) under "preview|stable"
@@ -90,7 +80,7 @@ else {
9080

9181
$fullPath = (Join-Path $repoPath $file)
9282

93-
$suppression = Get-Suppression $fullPath
83+
$suppression = Get-ValidatedSuppression $fullPath
9484
if ($suppression) {
9585
$reason = $suppression["reason"] ?? "<no reason specified>"
9686

eng/scripts/TypeSpec-Validation.ps1

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
param (
33
[switch]$CheckAll = $false,
44
[switch]$GitClean = $false,
5+
[switch]$DryRun = $false,
56
[string]$BaseCommitish = "HEAD^",
67
[string]$TargetCommitish = "HEAD"
78
)
89

910
. $PSScriptRoot/Logging-Functions.ps1
11+
. $PSScriptRoot/Suppressions-Functions.ps1
1012

1113
$typespecFolders = &"$PSScriptRoot/Get-TypeSpec-Folders.ps1" -BaseCommitish:$BaseCommitish -TargetCommitish:$TargetCommitish -CheckAll:$CheckAll
1214

@@ -15,7 +17,24 @@ if ($typespecFolders) {
1517
$typespecFolders = $typespecFolders.Split('',[System.StringSplitOptions]::RemoveEmptyEntries)
1618
foreach ($typespecFolder in $typespecFolders) {
1719
LogGroupStart "Validating $typespecFolder"
20+
21+
if ($CheckAll) {
22+
$suppression = Get-Suppression "TypeSpecValidationAll" $typespecFolder
23+
if ($suppression) {
24+
$reason = $suppression["reason"] ?? "<no reason specified>"
25+
LogInfo "Suppressed: $reason"
26+
LogGroupEnd
27+
continue
28+
}
29+
}
30+
1831
LogInfo "npm exec --no -- tsv $typespecFolder"
32+
33+
if ($DryRun) {
34+
LogGroupEnd
35+
continue
36+
}
37+
1938
npm exec --no -- tsv $typespecFolder 2>&1 | Write-Host
2039
if ($LASTEXITCODE) {
2140
$typespecFoldersWithFailures += $typespecFolder

0 commit comments

Comments
 (0)