Finalize structured diagnostic aggregation
This commit is contained in:
@@ -259,7 +259,7 @@ type rejectedFile struct {
|
||||
}
|
||||
|
||||
type warningsFile struct {
|
||||
Warnings []contracts.Warning `json:"warnings"`
|
||||
Groups []contracts.DiagnosticGroup `json:"groups"`
|
||||
}
|
||||
|
||||
func logicalFiles(req contracts.OutputRequest, options Options) ([]contracts.OutputFile, error) {
|
||||
@@ -332,7 +332,7 @@ func logicalFiles(req contracts.OutputRequest, options Options) ([]contracts.Out
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
warningsOutput, err := jsonFile("warnings.json", warningsFile{Warnings: cloneWarnings(req.Warnings)})
|
||||
warningsOutput, err := jsonFile("warnings.json", warningsFile{Groups: warningGroups(req.Diagnostics)})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -487,11 +487,14 @@ func cloneRejected(rejected []contracts.RejectedOutput) []contracts.RejectedOutp
|
||||
return append([]contracts.RejectedOutput(nil), rejected...)
|
||||
}
|
||||
|
||||
func cloneWarnings(warnings []contracts.Warning) []contracts.Warning {
|
||||
if len(warnings) == 0 {
|
||||
return []contracts.Warning{}
|
||||
func warningGroups(collection contracts.DiagnosticCollection) []contracts.DiagnosticGroup {
|
||||
groups := make([]contracts.DiagnosticGroup, 0)
|
||||
for _, group := range collection.Groups {
|
||||
if group.Disposition == contracts.DiagnosticDispositionWarning {
|
||||
groups = append(groups, group)
|
||||
}
|
||||
}
|
||||
return append([]contracts.Warning(nil), warnings...)
|
||||
return contracts.CloneDiagnosticCollection(contracts.DiagnosticCollection{Groups: groups}).Groups
|
||||
}
|
||||
|
||||
func encoderErrorf(format string, args ...any) error {
|
||||
|
||||
@@ -146,7 +146,7 @@ func TestEncodeReturnsLogicalFilesForNormalizedOutputs(t *testing.T) {
|
||||
ValidatorName: "validator",
|
||||
},
|
||||
},
|
||||
Warnings: []contracts.Warning{{ReasonCode: "warning", Message: "check source"}},
|
||||
Diagnostics: warningDiagnostics("warning", "check source"),
|
||||
}
|
||||
|
||||
result, err := New().Encode(context.Background(), req)
|
||||
@@ -211,7 +211,7 @@ func TestEncodeIncludesRejectedAndWarningsWhenEmpty(t *testing.T) {
|
||||
t.Fatalf("rejected = %#v, want empty array", got)
|
||||
}
|
||||
warnings := decodeObject(t, fileBytes(t, result.Files, "warnings.json"))
|
||||
if got := warnings["warnings"].([]any); len(got) != 0 {
|
||||
if got := warnings["groups"].([]any); len(got) != 0 {
|
||||
t.Fatalf("warnings = %#v, want empty array", got)
|
||||
}
|
||||
}
|
||||
@@ -665,7 +665,7 @@ func TestEncodeDoesNotMutateInputs(t *testing.T) {
|
||||
Rejected: []contracts.RejectedOutput{
|
||||
{Stage: "extract", LaneID: "spells", Message: "not accepted"},
|
||||
},
|
||||
Warnings: []contracts.Warning{{ReasonCode: "warning", Message: "message"}},
|
||||
Diagnostics: warningDiagnostics("warning", "message"),
|
||||
}
|
||||
before := mustMarshal(t, req)
|
||||
|
||||
@@ -681,14 +681,14 @@ func TestEncodeDoesNotMutateInputs(t *testing.T) {
|
||||
req.NormalizeOutputs[0].Artifact.Content[0] = '['
|
||||
req.NormalizeOutputs[0].Artifact.Metadata["name"] = "changed"
|
||||
req.Rejected[0].Message = "changed"
|
||||
req.Warnings[0].Message = "changed"
|
||||
req.Diagnostics.Groups[0].Samples[0].Message = "changed"
|
||||
|
||||
if !stdjson.Valid(fileBytes(t, result.Files, "lanes/spells.json")) {
|
||||
t.Fatal("output changed after request mutation")
|
||||
}
|
||||
warnings := decodeObject(t, fileBytes(t, result.Files, "warnings.json"))
|
||||
gotWarnings := warnings["warnings"].([]any)
|
||||
if gotWarnings[0].(map[string]any)["message"] != "message" {
|
||||
gotWarnings := warnings["groups"].([]any)
|
||||
if gotWarnings[0].(map[string]any)["samples"].([]any)[0].(map[string]any)["message"] != "message" {
|
||||
t.Fatalf("warnings output changed after request mutation: %#v", gotWarnings)
|
||||
}
|
||||
}
|
||||
@@ -696,9 +696,7 @@ func TestEncodeDoesNotMutateInputs(t *testing.T) {
|
||||
func TestOutputFilesDoNotContainWarnings(t *testing.T) {
|
||||
result, err := New().Encode(context.Background(), contracts.OutputRequest{
|
||||
NormalizeOutputs: []contracts.SerializedOutput{normalizeOutput("spells", `{"spell":"Shield"}`)},
|
||||
Warnings: []contracts.Warning{
|
||||
{ReasonCode: "pipeline-warning", Message: "warning"},
|
||||
},
|
||||
Diagnostics: warningDiagnostics("pipeline-warning", "warning"),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Encode() error = %v, want nil", err)
|
||||
@@ -710,6 +708,17 @@ func TestOutputFilesDoNotContainWarnings(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func warningDiagnostics(reasonCode, message string) contracts.DiagnosticCollection {
|
||||
return contracts.DiagnosticCollection{Groups: []contracts.DiagnosticGroup{{
|
||||
Disposition: contracts.DiagnosticDispositionWarning,
|
||||
Category: contracts.DiagnosticCategoryDegradation,
|
||||
ReasonCode: reasonCode,
|
||||
Origin: contracts.DiagnosticOrigin{Stage: contracts.DiagnosticOriginStageChunk},
|
||||
OccurrenceCount: 1,
|
||||
Samples: []contracts.DiagnosticSample{{Scope: "test", Message: message}},
|
||||
}}}
|
||||
}
|
||||
|
||||
func normalizeOutput(laneID string, content string) contracts.SerializedOutput {
|
||||
return contracts.SerializedOutput{
|
||||
LaneID: laneID,
|
||||
|
||||
Reference in New Issue
Block a user