Skip to content

chore: Extract schema validation test helper to reduce boilerplate #143

Description

@jflowers

Summary

Extract the repeated schema compilation and validation boilerplate into a reusable test helper function.

Related: #118, #140, #141, #142

Problem

The schema compilation and validation pattern is repeated 7 times across internal/report/report_test.go (lines 215-241, 354-378, 416-441, 448-460, 465-527, 533-597, 604-667). Each instance has the same boilerplate:

sch, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON))
// error check
compiler := jsonschema.NewCompiler()
err = compiler.AddResource("schema.json", sch)
// error check
compiled, err := compiler.Compile("schema.json")
// error check
err = compiled.Validate(document)
// error check

With #140 (QualitySchema exposure), #141 (CRAPSchema), and #142 (round-trip tests), this count will grow to 12-15+ instances.

Proposed Change

Extract a test helper:

func compileSchema(t *testing.T, schemaJSON string) *jsonschema.Schema {
    t.Helper()
    sch, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON))
    if err != nil {
        t.Fatalf("failed to parse schema JSON: %v", err)
    }
    compiler := jsonschema.NewCompiler()
    if err := compiler.AddResource("schema.json", sch); err != nil {
        t.Fatalf("failed to add schema resource: %v", err)
    }
    compiled, err := compiler.Compile("schema.json")
    if err != nil {
        t.Fatalf("failed to compile schema: %v", err)
    }
    return compiled
}

Uses standard library testing package only — no third-party assertion libraries.

Scope

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions