Skip to content
This repository has been archived by the owner on Feb 17, 2021. It is now read-only.

nox #28

Merged
merged 3 commits into from
May 12, 2020
Merged

nox #28

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
2 changes: 1 addition & 1 deletion .github/workflows/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Dependabot

on:
pull_request:
branches: [ master ]
branches: [master]

jobs:
automerge:
Expand Down
22 changes: 0 additions & 22 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,3 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-added-large-files
- repo: https://github.com/prettier/prettier
rev: 2.0.5
hooks:
- id: prettier
- repo: local
hooks:
- id: black
name: black
entry: poetry run black
language: system
types: [python]
- id: flake8
name: flake8
entry: poetry run flake8
language: system
types: [python]
- id: mypy
name: mypy
entry: poetry run mypy
language: system
types: [python]
require_serial: true
181 changes: 113 additions & 68 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,106 +1,151 @@
"""Nox sessions."""
import contextlib
import tempfile
from typing import Any
from pathlib import Path
from typing import cast
from typing import Iterator

import nox
from nox.sessions import Session


package = "toml_validator"
nox.options.sessions = "lint", "safety", "mypy", "tests"
python_versions = ["3.8"]
nox.options.sessions = "pre-commit", "safety", "mypy", "tests"
locations = "src", "tests", "noxfile.py"


def install_with_constraints(session: Session, *args: str, **kwargs: Any) -> None:
"""Install packages constrained by Poetry's lock file.
This function is a wrapper for nox.sessions.Session.install. It
invokes pip to install packages inside of the session's virtualenv.
Additionally, pip is passed a constraints file generated from
Poetry's lock file, to ensure that the packages are pinned to the
versions specified in poetry.lock. This allows you to manage the
packages as Poetry development dependencies.
Arguments:
class Poetry:
"""Helper class for invoking Poetry inside a Nox session.

Attributes:
session: The Session object.
args: Command-line arguments for pip.
kwargs: Additional keyword arguments for Session.install.
"""
with tempfile.NamedTemporaryFile() as requirements:
session.run(
"poetry",
"export",
"--dev",
"--format=requirements.txt",
f"--output={requirements.name}",
external=True,

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

@contextlib.contextmanager
def export(self, *args: str) -> Iterator[Path]:
"""Export the lock file to requirements format.

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

Yields:
The path to the requirements file.
"""
with tempfile.TemporaryDirectory() as directory:
requirements = Path(directory) / "requirements.txt"
self.session.run(
"poetry",
"export",
*args,
"--format=requirements.txt",
f"--output={requirements}",
external=True,
)
yield requirements

def version(self) -> str:
"""Retrieve the package version.

Returns:
The package version.
"""
output = self.session.run(
"poetry", "version", external=True, silent=True, stderr=None
)
session.install(f"--constraint={requirements.name}", *args, **kwargs)
return cast(str, output).split()[1]

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

@nox.session(python="3.8")
def black(session: Session) -> None:
"""Run black code formatter."""
args = session.posargs or locations
install_with_constraints(session, "black")
session.run("black", *args)
Args:
args: Command-line arguments for ``poetry build``.
"""
self.session.run("poetry", "build", *args, external=True)


@nox.session(python=["3.8", "3.7"])
def lint(session: Session) -> None:
"""Lint using flake8."""
args = session.posargs or locations
install_with_constraints(
session, "flake8", "flake8-bandit", "flake8-black", "flake8-bugbear",
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)

with poetry.export() as requirements:
session.install(f"--requirement={requirements}")

poetry.build("--format=wheel")

version = poetry.version()
session.install(
"--no-deps", "--force-reinstall", f"dist/{package}-{version}-py3-none-any.whl"
)
session.run("flake8", *args)


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``.
"""
poetry = Poetry(session)
with poetry.export("--dev") as requirements:
session.install(f"--constraint={requirements}", *args)


@nox.session(name="pre-commit", python="3.8")
def precommit(session: Session) -> None:
"""Lint using pre-commit."""
args = session.posargs or ["run", "--all-files"] # , "--show-diff-on-failure"]
install(session, "pre-commit")
session.run("pre-commit", *args)


@nox.session(python="3.8")
def safety(session: Session) -> None:
"""Scan dependencies for insecure packages."""
with tempfile.NamedTemporaryFile() as requirements:
session.run(
"poetry",
"export",
"--dev",
"--format=requirements.txt",
"--without-hashes",
f"--output={requirements.name}",
external=True,
)
install_with_constraints(session, "safety")
session.run("safety", "check", f"--file={requirements.name}", "--full-report")
poetry = Poetry(session)
with poetry.export("--dev", "--without-hashes") as requirements:
install(session, "safety")
session.run("safety", "check", f"--file={requirements}", "--bare")


@nox.session(python=["3.8", "3.7"])
@nox.session(python=python_versions)
def mypy(session: Session) -> None:
"""Type-check using mypy."""
args = session.posargs or locations
install_with_constraints(session, "mypy")
install(session, "mypy")
session.run("mypy", *args)


@nox.session(python=["3.8", "3.7"])
@nox.session(python=python_versions)
def tests(session: Session) -> None:
"""Run the test suite."""
args = session.posargs or ["--cov", "-m", "not e2e"]
session.run("poetry", "install", "--no-dev", external=True)
install_with_constraints(
session, "coverage[toml]", "pytest", "pytest-cov", "pytest-mock"
)
args = session.posargs or ["--cov"]
install_package(session)
install(session, "coverage[toml]", "pytest", "pytest-cov", "pytest_mock")
session.run("pytest", *args)


@nox.session(python=["3.8", "3.7"])
@nox.session(python=python_versions)
def typeguard(session: Session) -> None:
"""Runtime type checking using Typeguard."""
args = session.posargs or ["-m", "not e2e"]
session.run("poetry", "install", "--no-dev", external=True)
install_with_constraints(session, "pytest", "pytest-mock", "typeguard")
session.run("pytest", f"--typeguard-packages={package}", *args)


@nox.session(python="3.8")
def coverage(session: Session) -> None:
"""Upload coverage data."""
install_with_constraints(session, "coverage[toml]", "codecov")
session.run("coverage", "xml", "--fail-under=0")
session.run("codecov", *session.posargs)
install_package(session)
install(session, "pytest", "typeguard")
session.run("pytest", f"--typeguard-packages={package}", *session.posargs)
Loading