Skip to content

Commit

Permalink
feat: add schema and validate-pyproject support (#44)
Browse files Browse the repository at this point in the history
* feat: add schema and validate-pyproject support

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* tests: add some tests for validate-pyproject

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii committed Apr 30, 2024
1 parent 5b7f0e6 commit 6819405
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 8 deletions.
16 changes: 15 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ repos:
files: src|tests
args: []
additional_dependencies:
["tomli", "pathspec", "importlib-resources", "pytest"]
[
"tomli",
"pathspec",
"importlib-resources",
"pytest",
"validate-pyproject",
]

- repo: https://github.com/henryiii/check-sdist
rev: "v0.1.3"
Expand All @@ -74,3 +80,11 @@ repos:
language: pygrep
entry: PyBind|Numpy|Cmake|CCache|Github|PyTest
exclude: .pre-commit-config.yaml

- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.28.2
hooks:
- id: check-dependabot
- id: check-github-workflows
- id: check-metaschema
files: "^src/check_sdist/resources/check-sdist.schema.json$"
15 changes: 15 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from pathlib import Path

import nox

nox.needs_version = ">=2024.4.15"
Expand Down Expand Up @@ -53,3 +55,16 @@ def build(session: nox.Session) -> None:

session.install("build")
session.run("python", "-m", "build")


@nox.session
def generate_schema(session: nox.Session) -> None:
"""
Generate a schema file.
"""

deps = nox.project.load_toml("scripts/generate_schema.py")["dependencies"]
session.install(*deps)
out = session.run("python", "scripts/generate_schema.py", silent=True)
path = Path("src/check_sdist/resources/check-sdist.schema.json")
path.write_text(out)
12 changes: 5 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ test = [
"pytest >=6",
"pytest-cov >=3",
"pyproject-hooks !=1.1.0",
]
dev = [
"check-sdist[test]",
"validate-pyproject >=0.16",
]
uv = [
"uv",
Expand All @@ -55,12 +53,12 @@ Changelog = "https://github.com/henryiii/check-sdist/releases"
[project.scripts]
check-sdist = "check_sdist.__main__:main"

[project.entry-points."validate_pyproject.tool_schema"]
check-sdist = "check_sdist.schema:get_schema"

[tool.hatch]
version.path = "src/check_sdist/__init__.py"
envs.default.dependencies = [
"pytest",
"pytest-cov",
]
envs.default.features = ["test"]


[tool.pytest.ini_options]
Expand Down
40 changes: 40 additions & 0 deletions scripts/generate_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python3

# /// script
# dependencies = ["pyyaml"]
# ///
from __future__ import annotations

import json

import yaml

starter = """
$schema: http://json-schema.org/draft-07/schema#
$id: https://json.schemastore.org/partial-check-sdist.json
additionalProperties: false
description: check-sdist's settings.
type: object
properties:
sdist-only:
description: Files that only are in the SDist. Gitignore style lines.
type: array
items:
type: string
git-only:
description: Files that are only in Git. Gitignore style lines.
type: array
items:
type: string
default-ignore:
description: Ignore some common files
default: true
type: boolean
recurse-submodules:
description: Look in all submodules too.
default: true
type: boolean
"""

schema = yaml.safe_load(starter)
print(json.dumps(schema, indent=2))
33 changes: 33 additions & 0 deletions src/check_sdist/resources/check-sdist.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://json.schemastore.org/partial-check-sdist.json",
"additionalProperties": false,
"description": "check-sdist's settings.",
"type": "object",
"properties": {
"sdist-only": {
"description": "Files that only are in the SDist. Gitignore style lines.",
"type": "array",
"items": {
"type": "string"
}
},
"git-only": {
"description": "Files that are only in Git. Gitignore style lines.",
"type": "array",
"items": {
"type": "string"
}
},
"default-ignore": {
"description": "Ignore some common files",
"default": true,
"type": "boolean"
},
"recurse-submodules": {
"description": "Look in all submodules too.",
"default": true,
"type": "boolean"
}
}
}
14 changes: 14 additions & 0 deletions src/check_sdist/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from __future__ import annotations

import json
from typing import Any

from .resources import resources


def get_schema(tool_name: str = "check-sdist") -> dict[str, Any]:
"Get the stored complete schema for check-sdist settings."
assert tool_name == "check-sdist", "Only check-sdist is supported."

with resources.joinpath("check-sdist.schema.json").open(encoding="utf-8") as f:
return json.load(f) # type: ignore[no-any-return]
45 changes: 45 additions & 0 deletions tests/test_validate_pyproject.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from __future__ import annotations

import pytest
import validate_pyproject.api


def test_validate_pyproject_defaults():
example = {
"tool": {
"check-sdist": {
"sdist-only": [],
"git-only": [],
"default-ignore": True,
"recurse-submodules": True,
}
}
}
validator = validate_pyproject.api.Validator()
assert validator(example) is not None


def test_validate_pyproject_extra_key():
example: dict[str, object] = {
"tool": {
"check-sdist": {
"sdists-only": [],
}
}
}
validator = validate_pyproject.api.Validator()
with pytest.raises(validate_pyproject.error_reporting.ValidationError):
validator(example)


def test_validate_pyproject_invalid_value():
example = {
"tool": {
"check-sdist": {
"sdist-only": [1],
}
}
}
validator = validate_pyproject.api.Validator()
with pytest.raises(validate_pyproject.error_reporting.ValidationError):
validator(example)

0 comments on commit 6819405

Please sign in to comment.