Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include wc validator in the pytest suite for the exported html #83

Merged
merged 3 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
- name: init playwright nbconvert-a11y
run: |
playwright install --with-deps chromium
npm install vnu-jar
pip install -e.
doit copy
- name: test with pytest
Expand Down
2 changes: 1 addition & 1 deletion test-environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ dependencies:
- openjdk
- pip
- pip:
- html5validator
- pytest-playwright
- nbval
- accessible-pygments
- requests-cache
- markdown-it-py[plugins,linkify]
- python-slugify
- html5lib
- nodejs
- playwright
- nbconvert
- pytest
Expand Down
92 changes: 92 additions & 0 deletions tests/test_w3c.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# requires node
# requires jvm

import itertools, operator, functools, collections, exceptiongroup, re
import pathlib, json, subprocess, shlex

EXCLUDE = re.compile(
"""or with a “role” attribute whose value is “table”, “grid”, or “treegrid”.$"""
# https://github.com/validator/validator/issues/1125
)


@functools.lru_cache(1)
def vnu_jar():
VNU_JAR = (
pathlib.Path(subprocess.check_output(shlex.split("npm root vnu-jar")).strip().decode())
/ "vnu-jar/build/dist/vnu.jar"
)
assert VNU_JAR.exists()
return VNU_JAR


def validate_html(*files: pathlib.Path) -> dict:
return json.loads(
subprocess.check_output(
shlex.split(f"java -jar {vnu_jar()} --stdout --format json --exit-zero-always")
+ list(files)
).decode()
)


def organize_validator_results(results):
collect = collections.defaultdict(functools.partial(collections.defaultdict, list))
for (error, msg), group in itertools.groupby(
results["messages"], key=operator.itemgetter("type", "message")
):
for item in group:
collect[error][msg].append(item)
return collect

def raise_if_errors(results, exclude=EXCLUDE):
collect = organize_validator_results(results)
exceptions = []
for msg in collect["error"]:
if not exclude or not exclude.search(msg):
exceptions.append(exceptiongroup.ExceptionGroup(msg, [Exception(x["extract"]) for x in collect["error"][msg]]))
if exceptions:
raise exceptiongroup.ExceptionGroup("nu validator errors", exceptions)


import dataclasses
from json import dumps, loads
from logging import getLogger
from pathlib import Path

import exceptiongroup
from test_nbconvert_html5 import exporter


from pytest import fixture, mark

HERE = Path(__file__).parent
NOTEBOOKS = HERE / "notebooks"
EXPORTS = HERE / "exports"
HTML = EXPORTS / "html"
LOGGER = getLogger(__name__)
VALIDATOR = EXPORTS / "validator"

# it would be possible to test loaded baseline documents with playwright.
# export the resting state document and pass them to the validator.
# this would be better validate widgets.

@mark.parametrize(
"notebook",
list(
x
for x in NOTEBOOKS.glob("*.ipynb")
if x.name not in {"Imaging_Sky_Background_Estimation.ipynb"}
),
)
def test_baseline_w3c(page, exporter, notebook):
target = HTML / notebook.with_suffix(".html").name
target.parent.mkdir(exist_ok=True, parents=True)
target.write_text(exporter.from_filename(notebook)[0])

result = validate_html(target)
VALIDATOR.mkdir(parents=True, exist_ok=True)
audit = VALIDATOR / notebook.with_suffix(".json").name
LOGGER.info(f"""writing {audit} with {len(result.get("violations", ""))} violations""")
audit.write_text(dumps(result))

raise_if_errors(result)
Loading