Harden chunk map export and retire completed plans
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
stdjson "encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime"
|
||||
"regexp"
|
||||
"sort"
|
||||
@@ -247,10 +249,16 @@ func serializedOutputFile(name string, artifact contracts.SerializedArtifact) (c
|
||||
if !isJSONMediaType(mediaType) {
|
||||
return contracts.OutputFile{}, encoderErrorf("normalized output %q has unsupported media type %q", name, mediaType)
|
||||
}
|
||||
decoder := stdjson.NewDecoder(bytes.NewReader(content))
|
||||
decoder.UseNumber()
|
||||
var decoded any
|
||||
if err := stdjson.Unmarshal(content, &decoded); err != nil {
|
||||
if err := decoder.Decode(&decoded); err != nil {
|
||||
return contracts.OutputFile{}, encoderErrorf("normalized output %q contains invalid JSON: %w", name, err)
|
||||
}
|
||||
var trailing any
|
||||
if err := decoder.Decode(&trailing); err != io.EOF {
|
||||
return contracts.OutputFile{}, encoderErrorf("normalized output %q contains multiple JSON values", name)
|
||||
}
|
||||
pretty, err := marshalPretty(decoded)
|
||||
if err != nil {
|
||||
return contracts.OutputFile{}, err
|
||||
|
||||
@@ -234,6 +234,23 @@ func TestEncodeIncludesValidatedChunkMap(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodePreservesChunkMapAnnotationNumbers(t *testing.T) {
|
||||
artifact := acceptedChunkMapArtifactWithPlanAnnotation(t, stdjson.RawMessage(`{"decimal":1.0,"large":9007199254740993}`))
|
||||
result, err := NewWithOptions(Options{IncludeChunkMap: true}).Encode(context.Background(), contracts.OutputRequest{
|
||||
ChunkMap: &artifact,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Encode() error = %v", err)
|
||||
}
|
||||
value, err := chunkmap.New().Decode(fileBytes(t, result.Files, chunkMapFileName))
|
||||
if err != nil {
|
||||
t.Fatalf("Decode(emitted chunk map) error = %v", err)
|
||||
}
|
||||
if got, want := string(value.PlanAnnotations["test/numbers"]), `{"decimal":1.0,"large":9007199254740993}`; got != want {
|
||||
t.Fatalf("numeric annotation = %s, want %s", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodeOmitsChunkMapWithoutAcceptedArtifact(t *testing.T) {
|
||||
result, err := NewWithOptions(Options{IncludeChunkMap: true}).Encode(context.Background(), contracts.OutputRequest{
|
||||
Manifest: artifacts.RunManifest{RunID: "run-1"},
|
||||
@@ -544,6 +561,10 @@ func normalizeOutput(laneID string, content string) contracts.SerializedOutput {
|
||||
}
|
||||
|
||||
func acceptedChunkMapArtifact(t *testing.T) contracts.SerializedArtifact {
|
||||
return acceptedChunkMapArtifactWithPlanAnnotation(t, nil)
|
||||
}
|
||||
|
||||
func acceptedChunkMapArtifactWithPlanAnnotation(t *testing.T, annotation stdjson.RawMessage) contracts.SerializedArtifact {
|
||||
t.Helper()
|
||||
document := &source.SourceDocument{
|
||||
ID: "source-1",
|
||||
@@ -562,6 +583,9 @@ func acceptedChunkMapArtifact(t *testing.T) contracts.SerializedArtifact {
|
||||
}
|
||||
document.Digest = digest
|
||||
plan := source.ChunkPlan{SourceDigest: digest, Ranges: []source.ChunkRange{{StartUnitID: 1, EndUnitID: 1}}}
|
||||
if annotation != nil {
|
||||
plan.Annotations = source.ChunkAnnotations{"test/numbers": append(stdjson.RawMessage(nil), annotation...)}
|
||||
}
|
||||
chunks, err := source.MaterializeChunkPlan(document, plan)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
Reference in New Issue
Block a user