Skip to content

Commit

Permalink
Merge pull request #132 from kiudee/130_nox
Browse files Browse the repository at this point in the history
Migrate to Github Actions & Nox
  • Loading branch information
kiudee committed Jun 26, 2021
2 parents 8ebae49 + a18dca3 commit 2165035
Show file tree
Hide file tree
Showing 13 changed files with 249 additions and 119 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ jobs:
with:
python-version: '3.9'
architecture: x64
- run: pip install tox==3.23.1
- run: pip install nox==2021.6.12
- run: pip install nox-poetry==0.8.6
- run: pip install poetry==1.1.6
- run: tox
- run: nox
- run: poetry build
- run: poetry publish --username=__token__ --password=${{ secrets.PYPI_TOKEN }}
52 changes: 52 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Tests
on:
- push
- pull_request
jobs:
tests:
runs-on: "${{ matrix.os }}"
strategy:
fail-fast: false
matrix:
include:
- { python-version: 3.8, os: ubuntu-latest, session: "pre-commit" }
- { python-version: 3.8, os: ubuntu-latest, session: "tests" }
- { python-version: 3.9, os: ubuntu-latest, session: "tests" }
- { python-version: 3.8, os: macos-latest, session: "tests" }
- { python-version: 3.9, os: macos-latest, session: "tests" }

name: "${{ matrix.session }} ${{ matrix.python-version }} / ${{ matrix.os }}"
env:
NOXSESSION: "${{ matrix.session }}"
steps:
- uses: actions/checkout@v2.3.4
- uses: actions/setup-python@v2.2.1
with:
python-version: ${{ matrix.python-version }}
architecture: x64
- run: pip install nox==2021.6.12
- run: pip install nox-poetry==0.8.6
- run: pip install poetry==1.1.4
- name: Compute pre-commit cache key
if: matrix.session == 'pre-commit'
id: pre-commit-cache
shell: python
run: |
import hashlib
import sys
python = "py{}.{}".format(*sys.version_info[:2])
payload = sys.version.encode() + sys.executable.encode()
digest = hashlib.sha256(payload).hexdigest()
result = "${{ runner.os }}-{}-{}-pre-commit".format(python, digest[:8])
print("::set-output name=result::{}".format(result))
- name: Restore pre-commit cache
uses: actions/cache@v2.1.4
if: matrix.session == 'pre-commit'
with:
path: ~/.cache/pre-commit
key: "${{ steps.pre-commit-cache.outputs.result }}-${{ hashFiles('.pre-commit-config.yaml') }}"
restore-keys: |
"${{ steps.pre-commit-cache.outputs.result }}-"
- name: Run Nox
run: |
nox --force-color --python=${{ matrix.python-version }}
21 changes: 0 additions & 21 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

sys.path.insert(0, os.path.abspath(".."))

import tune
import tune # noqa

# -- General configuration ---------------------------------------------

Expand Down
86 changes: 86 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
"""Nox sessions."""
from pathlib import Path
from textwrap import dedent

import nox
from nox_poetry import session

locations = "tune", "noxfile.py"
nox.options.sessions = ("pre-commit", "tests")
python_versions = ["3.8", "3.9"]


def activate_virtualenv_in_precommit_hooks(session):
"""Activate virtualenv in hooks installed by pre-commit.
This function patches git hooks installed by pre-commit to activate the
session's virtual environment. This allows pre-commit to locate hooks in
that environment when invoked from git.
Args:
session: The Session object.
"""
if session.bin is None:
return

virtualenv = session.env.get("VIRTUAL_ENV")
if virtualenv is None:
return

hookdir = Path(".git") / "hooks"
if not hookdir.is_dir():
return

for hook in hookdir.iterdir():
if hook.name.endswith(".sample") or not hook.is_file():
continue

text = hook.read_text()
bindir = repr(session.bin)[1:-1] # strip quotes
if not (
Path("A") == Path("a") and bindir.lower() in text.lower() or bindir in text
):
continue

lines = text.splitlines()
if not (lines[0].startswith("#!") and "python" in lines[0].lower()):
continue

header = dedent(
f"""\
import os
os.environ["VIRTUAL_ENV"] = {virtualenv!r}
os.environ["PATH"] = os.pathsep.join((
{session.bin!r},
os.environ.get("PATH", ""),
))
"""
)

lines.insert(1, header)
hook.write_text("\n".join(lines))


@session(python=python_versions)
def tests(session):
"""Run the test suite."""
session.install(".[data]")
session.install("pytest", "nox", "nox-poetry")
session.run("pytest", *session.posargs)


@session(python="3.8")
def black(session):
"""Run black code formatter."""
args = session.posargs or locations
session.install("black")
session.run("black", *args)


@session(name="pre-commit", python="3.8")
def precommit(session):
args = session.posargs or ["run", "--all-files", "--show-diff-on-failure"]
session.install(
"pre-commit", "black", "flake8", "doc8", "zimports",
)
session.run("pre-commit", *args)
if args and args[0] == "install":
activate_virtualenv_in_precommit_hooks(session)
126 changes: 86 additions & 40 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2165035

Please sign in to comment.