Back to Repositories

Validating PyProject Schema Configuration in Black

This test suite validates the schema configuration for Black’s pyproject.toml integration, focusing on entry point registration and schema validation for the Black code formatter.

Test Coverage Overview

The test coverage focuses on validating Black’s schema entrypoint registration and configuration validation.

Key areas tested include:
  • Entry point discovery for ‘validate_pyproject.tool_schema’
  • Schema loading functionality
  • Verification of schema properties like line-length configuration
  • Version-specific entry point handling for Python 3.10+

Implementation Analysis

The testing approach implements version-specific logic to handle different importlib.metadata APIs across Python versions.

Technical implementation details:
  • Conditional logic for Python version compatibility
  • Entry point discovery and loading verification
  • Schema property validation
  • Direct schema object comparison

Technical Details

Testing tools and configuration:
  • importlib.metadata for entry point handling
  • sys module for version detection
  • pytest for test execution
  • validate_pyproject integration for schema validation

Best Practices Demonstrated

The test implementation showcases several testing best practices for configuration validation.

Notable practices include:
  • Version-aware implementation
  • Direct schema validation
  • Explicit property verification
  • Clean separation of concerns between discovery and validation

psf/black

tests/test_schema.py

            
import importlib.metadata
import sys


def test_schema_entrypoint() -> None:
    if sys.version_info < (3, 10):
        eps = importlib.metadata.entry_points()["validate_pyproject.tool_schema"]
        (black_ep,) = [ep for ep in eps if ep.name == "black"]
    else:
        (black_ep,) = importlib.metadata.entry_points(
            group="validate_pyproject.tool_schema", name="black"
        )

    black_fn = black_ep.load()
    schema = black_fn()
    assert schema == black_fn("black")
    assert schema["properties"]["line-length"]["type"] == "integer"