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

Update noxfile #126

Merged
merged 6 commits into from
Jan 15, 2021
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
3 changes: 2 additions & 1 deletion .github/workflows/constraints.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pip==20.3.3
nox==2020.8.22
poetry==1.1.2
poetry==1.0.10
virtualenv==20.1.0
nox-poetry==0.7.1
63 changes: 52 additions & 11 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
# mypy and typguard tests have been disabled temporarly as typing hint need rework
matrix:
include:
- { python-version: 3.8, os: ubuntu-latest, session: "pre-commit" }
- { python-version: 3.8, os: ubuntu-latest, session: "safety" }
- { python-version: 3.9, os: ubuntu-latest, session: "pre-commit" }
- { python-version: 3.9, os: ubuntu-latest, session: "safety" }
# - { python-version: 3.9, os: ubuntu-latest, session: "mypy" }
# - { python-version: 3.8, os: ubuntu-latest, session: "mypy" }
# - { python-version: 3.7, os: ubuntu-latest, session: "mypy" }
- { python-version: 3.9, os: ubuntu-latest, session: "tests" }
- { python-version: 3.8, os: ubuntu-latest, session: "tests" }
- { python-version: 3.7, os: ubuntu-latest, session: "tests" }
- { python-version: 3.8, os: windows-latest, session: "tests" }
- { python-version: 3.8, os: macos-latest, session: "tests" }
# - { python-version: 3.8, os: ubuntu-latest, session: "typeguard" }
- { python-version: 3.9, os: windows-latest, session: "tests" }
- { python-version: 3.9, os: macos-latest, session: "tests" }
# - { python-version: 3.9, os: ubuntu-latest, session: "typeguard" }
# - { python-version: 3.9, os: ubuntu-latest, session: "xdoctest" }
- { python-version: 3.8, os: ubuntu-latest, session: "docs-build" }

env:
Expand All @@ -52,7 +52,7 @@ jobs:

- name: Install Nox
run: |
pip install --constraint=.github/workflows/constraints.txt nox
pip install --constraint=.github/workflows/constraints.txt nox nox-poetry
nox --version

- name: Compute pre-commit cache key
Expand All @@ -71,7 +71,7 @@ jobs:
print("::set-output name=result::{}".format(result))

- name: Restore pre-commit cache
uses: actions/cache@v2
uses: actions/cache@v2.1.3
if: matrix.session == 'pre-commit'
with:
path: ~/.cache/pre-commit
Expand All @@ -83,18 +83,59 @@ jobs:
run: |
nox --force-color --python=${{ matrix.python-version }}

- name: Upload coverage data
if: always() && matrix.session == 'tests'
uses: "actions/upload-artifact@v2.2.1"
with:
name: coverage-data
path: ".coverage.*"

- name: Upload documentation
if: matrix.session == 'docs-build'
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v2.2.1
with:
name: docs
path: docs/_build

coverage:
runs-on: ubuntu-latest
needs: tests
steps:
- name: Check out the repository
uses: actions/checkout@v2.3.4

- name: Set up Python 3.9
uses: actions/setup-python@v2.2.1
with:
python-version: 3.9

- name: Upgrade pip
run: |
pip install --constraint=.github/workflows/constraints.txt pip
pip --version

- name: Install Poetry
run: |
pip install --constraint=.github/workflows/constraints.txt poetry
poetry --version

- name: Install Nox
run: |
pip install --constraint=.github/workflows/constraints.txt nox nox-poetry
nox --version

- name: Download coverage data
uses: actions/download-artifact@v2.0.6
with:
name: coverage-data

- name: Combine coverage data and display human readable report
run: |
nox --force-color --session=coverage

- name: Create coverage report
if: always() && matrix.session == 'tests'
run: |
nox --force-color --session=coverage -- xml

- name: Upload coverage report
if: always() && matrix.session == 'tests'
uses: codecov/codecov-action@v1.1.1
146 changes: 21 additions & 125 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Nox sessions."""
import hashlib
import shutil
import sys
from pathlib import Path
from textwrap import dedent

import nox
import nox_poetry.patch
from nox.sessions import Session


Expand All @@ -22,110 +22,6 @@
)


class Poetry:
"""Helper class for invoking Poetry inside a Nox session.

Attributes:
session: The Session object.
"""

def __init__(self, session: Session) -> None:
"""Constructor."""
self.session = session

def export(self, path: Path, *, dev: bool) -> None:
"""Export the lock file to requirements format.

Args:
path: The destination path.
dev: If True, include development dependencies.
"""
options = ["--dev"] if dev else []
self.session.run(
"poetry",
"export",
"--format=requirements.txt",
f"--output={path}",
*options,
external=True,
)

def build(self, *args: str) -> str:
"""Build the package.

Args:
args: Command-line arguments for ``poetry build``.

Returns:
The basename of the wheel built by Poetry.
"""
output = self.session.run(
"poetry", "build", *args, external=True, silent=True, stderr=None
)
assert isinstance(output, str) # noqa: S101
return output.split()[-1]


def export_requirements(session: Session, *, dev: bool) -> Path:
"""Export the lock file to requirements format.

Args:
session: The Session object.
dev: If True, include development dependencies.

Returns:
The path to the requirements file.
"""
tmpdir = Path(session.create_tmp())
name = "dev-requirements.txt" if dev else "requirements.txt"
path = tmpdir / name
hashfile = tmpdir / f"{name}.hash"

lockdata = Path("poetry.lock").read_bytes()
digest = hashlib.blake2b(lockdata).hexdigest()

if not hashfile.is_file() or hashfile.read_text() != digest:
Poetry(session).export(path, dev=dev)
hashfile.write_text(digest)

return path


def install_package(session: Session) -> None:
"""Build and install the package.

Build a wheel from the package, and install it into the virtual environment
of the specified Nox session.

The package requirements are installed using the versions specified in
Poetry's lock file.

Args:
session: The Session object.
"""
poetry = Poetry(session)
wheel = poetry.build("--format=wheel")
requirements = export_requirements(session, dev=False)

session.install(f"--requirement={requirements}")
session.install("--no-deps", "--force-reinstall", f"dist/{wheel}")


def install(session: Session, *args: str) -> None:
"""Install development dependencies into the session's virtual environment.

This function is a wrapper for nox.sessions.Session.install.

The packages must be managed as development dependencies in Poetry.

Args:
session: The Session object.
args: Command-line arguments for ``pip install``.
"""
requirements = export_requirements(session, dev=True)
session.install(f"--constraint={requirements}", *args)


def activate_virtualenv_in_precommit_hooks(session: Session) -> None:
"""Activate virtualenv in hooks installed by pre-commit.

Expand Down Expand Up @@ -177,12 +73,11 @@ def activate_virtualenv_in_precommit_hooks(session: Session) -> None:
hook.write_text("\n".join(lines))


@nox.session(name="pre-commit", python="3.8")
@nox.session(name="pre-commit", python="3.9")
def precommit(session: Session) -> None:
"""Lint using pre-commit."""
args = session.posargs or ["run", "--all-files", "--show-diff-on-failure"]
install(
session,
session.install(
"black",
"darglint",
"flake8",
Expand All @@ -200,20 +95,20 @@ def precommit(session: Session) -> None:
activate_virtualenv_in_precommit_hooks(session)


@nox.session(python="3.8")
@nox.session(python="3.9")
def safety(session: Session) -> None:
"""Scan dependencies for insecure packages."""
install(session, "safety")
requirements = export_requirements(session, dev=True)
requirements = nox_poetry.export_requirements(session)
session.install("safety")
session.run("safety", "check", f"--file={requirements}", "--bare")


@nox.session(python=python_versions)
def mypy(session: Session) -> None:
"""Type-check using mypy."""
args = session.posargs or ["src", "tests", "docs/conf.py"]
install_package(session)
install(session, "mypy")
session.install(".")
session.install("mypy", "pytest")
session.run("mypy", *args)
if not session.posargs:
session.run("mypy", f"--python-executable={sys.executable}", "noxfile.py")
Expand All @@ -222,12 +117,13 @@ def mypy(session: Session) -> None:
@nox.session(python=python_versions)
def tests(session: Session) -> None:
"""Run the test suite."""
install_package(session)
install(session, "coverage[toml]", "pytest")
session.install(".")
session.install("coverage[toml]", "pytest", "pygments")
try:
session.run("coverage", "run", "--parallel", "-m", "pytest", *session.posargs)
finally:
session.notify("coverage")
if session.interactive:
session.notify("coverage")


@nox.session
Expand All @@ -237,7 +133,7 @@ def coverage(session: Session) -> None:
has_args = session.posargs and len(session._runner.manifest) == 1
args = session.posargs if has_args else ["report"]

install(session, "coverage[toml]")
session.install("coverage[toml]")

if not has_args and any(Path().glob(".coverage.*")):
session.run("coverage", "combine")
Expand All @@ -248,26 +144,26 @@ def coverage(session: Session) -> None:
@nox.session(python=python_versions)
def typeguard(session: Session) -> None:
"""Runtime type checking using Typeguard."""
install_package(session)
install(session, "pytest", "typeguard")
session.install(".")
session.install("pytest", "typeguard", "pygments")
session.run("pytest", f"--typeguard-packages={package}", *session.posargs)


@nox.session(python=python_versions)
def xdoctest(session: Session) -> None:
"""Run examples with xdoctest."""
args = session.posargs or ["all"]
install_package(session)
install(session, "xdoctest")
session.install(".")
session.install("xdoctest[colors]")
session.run("python", "-m", "xdoctest", package, *args)


@nox.session(name="docs-build", python="3.8")
def docs_build(session: Session) -> None:
"""Build the documentation."""
args = session.posargs or ["docs", "docs/_build"]
install_package(session)
install(session, "sphinx")
session.install(".")
session.install("sphinx", "sphinx-click", "sphinx-rtd-theme")

build_dir = Path("docs", "_build")
if build_dir.exists():
Expand All @@ -280,8 +176,8 @@ def docs_build(session: Session) -> None:
def docs(session: Session) -> None:
"""Build and serve the documentation with live reloading on file changes."""
args = session.posargs or ["--open-browser", "docs", "docs/_build"]
install_package(session)
install(session, "sphinx", "sphinx-autobuild")
session.install(".")
session.install("sphinx", "sphinx-autobuild", "sphinx-rtd-theme")

build_dir = Path("docs", "_build")
if build_dir.exists():
Expand Down
Loading