|
| 1 | +# Copyright 2016 Alethea Katherine Flowers |
| 2 | +# Source - https://github.com/wntrblm/nox/blob/main/noxfile.py |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import functools |
| 20 | +import os |
| 21 | +import platform |
| 22 | +import shutil |
| 23 | + |
| 24 | +import nox |
| 25 | + |
| 26 | +ON_WINDOWS_CI = "CI" in os.environ and platform.system() == "Windows" |
| 27 | + |
| 28 | +# Skip 'conda_tests' if user doesn't have conda installed |
| 29 | +nox.options.sessions = ["tests", "cover", "lint", "docs"] |
| 30 | +if shutil.which("conda"): |
| 31 | + nox.options.sessions.append("conda_tests") |
| 32 | + |
| 33 | + |
| 34 | +@nox.session(python=["3.7", "3.8", "3.9", "3.10"]) |
| 35 | +def tests(session): |
| 36 | + """Run test suite with pytest.""" |
| 37 | + session.create_tmp() |
| 38 | + session.install("-r", "requirements-test.txt") |
| 39 | + session.install("-e", ".[tox_to_nox]") |
| 40 | + tests = session.posargs or ["tests/"] |
| 41 | + session.run( |
| 42 | + "pytest", |
| 43 | + "--cov=nox", |
| 44 | + "--cov-config", |
| 45 | + "pyproject.toml", |
| 46 | + "--cov-report=", |
| 47 | + *tests, |
| 48 | + env={"COVERAGE_FILE": f".coverage.{session.python}"}, |
| 49 | + ) |
| 50 | + session.notify("cover") |
| 51 | + |
| 52 | + |
| 53 | +@nox.session(python=["3.7", "3.8", "3.9", "3.10"], venv_backend="conda") |
| 54 | +def conda_tests(session): |
| 55 | + """Run test suite with pytest.""" |
| 56 | + session.create_tmp() |
| 57 | + session.conda_install( |
| 58 | + "--file", "requirements-conda-test.txt", "--channel", "conda-forge" |
| 59 | + ) |
| 60 | + session.install("-e", ".", "--no-deps") |
| 61 | + tests = session.posargs or ["tests/"] |
| 62 | + session.run("pytest", *tests) |
| 63 | + |
| 64 | + |
| 65 | +@nox.session |
| 66 | +def cover(session): |
| 67 | + """Coverage analysis.""" |
| 68 | + if ON_WINDOWS_CI: |
| 69 | + return |
| 70 | + |
| 71 | + session.install("coverage[toml]") |
| 72 | + session.run("coverage", "combine") |
| 73 | + session.run("coverage", "report", "--fail-under=100", "--show-missing") |
| 74 | + session.run("coverage", "erase") |
| 75 | + |
| 76 | + |
| 77 | +@nox.session(python="3.9") |
| 78 | +def lint(session): |
| 79 | + """Run pre-commit linting.""" |
| 80 | + session.install("pre-commit") |
| 81 | + # See https://github.com/theacodes/nox/issues/545 |
| 82 | + # and https://github.com/pre-commit/pre-commit/issues/2178#issuecomment-1002163763 |
| 83 | + session.run( |
| 84 | + "pre-commit", |
| 85 | + "run", |
| 86 | + "--all-files", |
| 87 | + "--show-diff-on-failure", |
| 88 | + "--hook-stage=manual", |
| 89 | + env={"SETUPTOOLS_USE_DISTUTILS": "stdlib"}, |
| 90 | + *session.posargs, |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +@nox.session |
| 95 | +def docs(session): |
| 96 | + """Build the documentation.""" |
| 97 | + output_dir = os.path.join(session.create_tmp(), "output") |
| 98 | + doctrees, html = map( |
| 99 | + functools.partial(os.path.join, output_dir), ["doctrees", "html"] |
| 100 | + ) |
| 101 | + shutil.rmtree(output_dir, ignore_errors=True) |
| 102 | + session.install("-r", "requirements-test.txt") |
| 103 | + session.install(".") |
| 104 | + session.cd("docs") |
| 105 | + sphinx_args = ["-b", "html", "-W", "-d", doctrees, ".", html] |
| 106 | + |
| 107 | + if not session.interactive: |
| 108 | + sphinx_cmd = "sphinx-build" |
| 109 | + else: |
| 110 | + sphinx_cmd = "sphinx-autobuild" |
| 111 | + sphinx_args.insert(0, "--open-browser") |
| 112 | + |
| 113 | + session.run(sphinx_cmd, *sphinx_args) |
0 commit comments