GLAM-Workbench / glam-workbench.github.io

https://glam-workbench.github.io/
25 stars 6 forks source link

Add config & tools for testing/linting/formatting #51

Closed wragge closed 10 months ago

wragge commented 2 years ago

Run the QA on a single file without committing:

pre-commit run --files Composite-thumbnails.ipynb

This should run automatically when committed.

Runs:

Alternatively, to run separately:

nbqa isort

Test a single file:

pytest --nbval-lax Analysing_OCR_corrections.ipynb

Add skip-nbval tag to any cell you don't want to run.

Set up

Create requirements-dev.in with the following:

pytest nbval nbqa black[jupyter] isort flake8 pre-commit

Install

Create .pre-commit-config.yaml:

repos:
- repo: https://github.com/nbQA-dev/nbQA
rev: 1.2.3
hooks:
- id: nbqa-isort
- id: nbqa-black
- id: nbqa-flake8

Run

pre-commit install to set up hooks

Create config

pyproject.toml:

Modify as necessary to exclude files

[tool.nbqa.addopts]
flake8 = [
    "--ignore=E501,W503"
]
[tool.pytest.ini_options]
addopts = "--ignore-glob=Untitled* --ignore=snippets.ipynb --ignore=newspaper-explorer.ipynb --ignore-glob=App* --ignore-glob=draft* --ignore=appmode-checking.ipynb"

Handy test script

test_and_lint.sh:

#!/bin/bash

pytest --nbval-lax $1

if nbqa isort $1; then

echo "ISort: $(tput setab 2)$(tput setaf 7)PASSED$(tput sgr0)"

else

echo "ISort: $(tput setab 1)$(tput setaf 7)FAILED$(tput sgr0)"

fi

if nbqa black $1; then

echo "Black: $(tput setab 2)$(tput setaf 7)PASSED$(tput sgr0)"

else

echo "Black: $(tput setab 1)$(tput setaf 7)FAILED$(tput sgr0)"

fi

if nbqa flake8 $1; then

echo "Flake8: $(tput setab 2)$(tput setaf 7)PASSED$(tput sgr0)"

else

echo "Flake8: $(tput setab 1)$(tput setaf 7)FAILED$(tput sgr0)"

fi
wragge commented 1 year ago

Also include checks for broken links using pytest-check-links: https://github.com/jupyterlab/pytest-check-links