# To use tox, see https://tox.readthedocs.io
# Simply pip or conda install tox
# If you use conda, you may also want to install tox-conda
# then run `tox` or `tox -- {pytest args}`
# To run in parallel using `tox -p` (this does not appear to work for this repo)

# To rebuild the tox environment, for example when dependencies change, use
# `tox -r`

# Note: if the following error is encountered: `ImportError while loading conftest`
# then then deleting compiled files has been found to fix it: `find . -name \*.pyc -delete`

[tox]
envlist = py{312,313}-sphinx7

[testenv]
usedevelop=true
passenv = TERM  # To make terminal coloring / other variables pass through

[testenv:py{312,313}-pre-commit]
extras = code_style
commands = pre-commit run {posargs}

[testenv:compile]
deps =
    sphinx-theme-builder[cli]
commands =
    stb compile

[testenv:docs-{update,clean}]
extras =
    doc
whitelist_externals = rm
commands =
    clean: rm -rf docs/_build
    sphinx-build -n --keep-going -b {posargs:html} docs/ docs/_build/{posargs:html}

[testenv:docs-live]
extras =
    doc
deps =
    sphinx-theme-builder[cli]
commands =
    stb serve docs --open-browser

[testenv:py{312,313}-sphinx{7,8}]
extras = test
deps =
    sphinx7: sphinx>=7,<8
    sphinx8: sphinx>=8,<9
commands = pytest {posargs}

[testenv:visual]
description = Run visual regression tests with Playwright
passenv =
    TERM
    HOME
    CI
    PATH
setenv =
    SNAPSHOT_DIR = macos
deps =
    jupyter-book==1.0.4post1
    sphinx-exercise
    sphinx-proof
    sphinxcontrib-youtube
    sphinx-togglebutton
    sphinxext-rediraffe
    sphinx-reredirects
allowlist_externals =
    npm
    npx
    git
    bash
commands =
    # Clone the test lecture site if not present
    bash -c "[ -d lecture-python-programming.myst ] || git clone --branch quantecon-book-theme https://github.com/QuantEcon/lecture-python-programming.myst"
    # Build the lecture site if not already built
    bash -c "[ -d lecture-python-programming.myst/lectures/_build/html ] || (cd lecture-python-programming.myst && jb build lectures)"
    # Install npm dependencies including Playwright
    npm ci
    npx playwright install chromium
    # Run visual tests
    npm run test:visual {posargs}

[testenv:visual-update]
description = Update visual regression test baselines
passenv =
    TERM
    HOME
    PATH
setenv =
    SNAPSHOT_DIR = macos
deps =
    jupyter-book==1.0.4post1
    sphinx-exercise
    sphinx-proof
    sphinxcontrib-youtube
    sphinx-togglebutton
    sphinxext-rediraffe
    sphinx-reredirects
allowlist_externals =
    npm
    npx
    git
    bash
commands =
    bash -c "[ -d lecture-python-programming.myst ] || git clone --branch quantecon-book-theme https://github.com/QuantEcon/lecture-python-programming.myst"
    bash -c "[ -d lecture-python-programming.myst/lectures/_build/html ] || (cd lecture-python-programming.myst && jb build lectures)"
    npm ci
    npx playwright install chromium
    npm run test:visual:update {posargs}
