Skip to content

[test] Add tests for config.formatErrorContext, formatValidationErrorRecursive, formatSchemaError#1435

Merged
lpcox merged 1 commit intomainfrom
test/coverage-validation-schema-error-format-8e1971fac13ec579
Feb 28, 2026
Merged

[test] Add tests for config.formatErrorContext, formatValidationErrorRecursive, formatSchemaError#1435
lpcox merged 1 commit intomainfrom
test/coverage-validation-schema-error-format-8e1971fac13ec579

Conversation

@github-actions
Copy link
Contributor

Test Coverage Improvement: Schema Error Formatting Functions

Functions Analyzed

  • Package: internal/config
  • Functions:
    • formatErrorContext — provides human-friendly diagnostic context for JSON schema validation errors based on message content (7 if-branches)
    • formatValidationErrorRecursive — recursively formats schema errors with depth-based indentation
    • formatSchemaError — entry point that dispatches to the above and appends version + documentation footer
  • Previous Coverage: ~0% direct coverage (only reached indirectly via TestEnhancedErrorMessages which requires network-fetched schemas)
  • Complexity: High — formatErrorContext has 7 independent if-branches plus a keyword-location check; formatValidationErrorRecursive is recursive with depth tracking

Why These Functions?

formatErrorContext has the highest branch density in validation_schema.go with 7 independent conditional blocks, none directly tested. The existing TestEnhancedErrorMessages calls validateJSONSchema which fetches a schema from GitHub over the network, making those tests environment-dependent and leaving the formatting functions without reliable direct coverage.

Tests Added

  • TestFormatErrorContext (20 table-driven cases):

    • All 7 branch conditions: additionalProperties, type mismatch (expected+but got/type), enum (value must be one of / must be), missing required (missing properties / required), pattern (does not match pattern / pattern), min/max (must be >= / must be <= / minimum / maximum), oneOf (doesn't validate with any of / oneOf)
    • Keyword location shown when different from instance location
    • Keyword location suppressed when same as instance location or empty
    • Prefix string prepended correctly to all output lines
    • Unrecognized messages return empty string
  • TestFormatValidationErrorRecursive (7 sub-tests):

    • Root-level error formatting with location and message
    • Empty InstanceLocation falls back to (root) placeholder
    • Depth 0 appends trailing newline; depth > 0 does not
    • Recursive processing of Causes slice
    • Nested causes get 2× indentation per depth level
    • Multiple root-level causes are all included
    • Context details from formatErrorContext are included in output
  • TestFormatSchemaError (6 sub-tests):

    • nil input returns nil
    • *jsonschema.ValidationError gets full formatted message with version, location, and documentation footer
    • Non-schema errors.New gets compact one-line format with version
    • fmt.Errorf wrapped errors get compact format
    • additionalProperties errors include context details in full path
    • Recursive causes are included in output

Test Execution

Tests are designed to pass in CI where Go module dependencies are available. They use jsonschema.ValidationError struct directly (no network required), version.Set() for deterministic version assertions, and follow the project's table-driven test conventions with testify/assert.


Generated by Test Coverage Improver

Generated by Test Coverage Improver

Warning

⚠️ Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • proxy.golang.org

…ve, formatSchemaError

These three functions in internal/config/validation_schema.go handle
user-friendly formatting of JSON Schema validation errors. They were
only exercised indirectly through TestEnhancedErrorMessages which
requires a network-fetched schema, leaving branches untested.

New test file adds comprehensive coverage:
- TestFormatErrorContext: 20 table-driven cases covering all 7 branch
  conditions (additionalProperties, type mismatch, enum, missing
  required, pattern, min/max, oneOf), keyword location display logic,
  prefix prepending, and unrecognized messages
- TestFormatValidationErrorRecursive: 7 sub-tests covering root-level
  errors, empty location fallback (<root>), depth-based indentation,
  recursive cause traversal, and context detail inclusion
- TestFormatSchemaError: 6 sub-tests covering nil input, jsonschema
  ValidationError formatting with version/location/footer, non-schema
  errors, fmt.Errorf wrapped errors, and recursive cause inclusion

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review February 28, 2026 04:48
Copilot AI review requested due to automatic review settings February 28, 2026 04:48
@lpcox lpcox merged commit f9b6274 into main Feb 28, 2026
2 checks passed
@lpcox lpcox deleted the test/coverage-validation-schema-error-format-8e1971fac13ec579 branch February 28, 2026 04:48
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds focused, offline unit tests for JSON Schema validation error formatting helpers in internal/config, improving coverage without relying on network-fetched schemas.

Changes:

  • Introduces table-driven tests for formatErrorContext message-to-context formatting.
  • Adds recursion/indentation tests for formatValidationErrorRecursive (including nested Causes).
  • Adds entry-point tests for formatSchemaError, including footer/version presence and non-schema error behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +362 to +366
t.Run("jsonschema.ValidationError includes version and location info", func(t *testing.T) {
version.Set("v1.0.0-test")

ve := &jsonschema.ValidationError{
InstanceLocation: "mcpServers.github",
Copy link

Copilot AI Feb 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TestFormatSchemaError repeatedly calls version.Set(...) but never restores the prior value. Since internal/version uses a package-level mutable variable, this can leak state to other tests in the config package and make the suite order-dependent. Consider capturing prev := version.Get() and restoring it with t.Cleanup(...) (or avoid setting entirely and assert against version.Get()).

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants