Skip to content

Commit

Permalink
test for mac is removed as it's not possible to start the docker cont…
Browse files Browse the repository at this point in the history
…ainer on mac like this
  • Loading branch information
Aviksaikat committed Jan 6, 2024
1 parent 5613bec commit c0c7c64
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 131 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ jobs:

strategy:
matrix:
os: [ubuntu-latest, macos-latest]
os: [ubuntu-latest, windows-latest]
python-version: ["3.9", "3.10", "3.11", "3.12"]

steps:
Expand Down
196 changes: 66 additions & 130 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,131 +1,67 @@
"""Nox sessions."""
import os
import shlex
import shutil
# import shlex
# import shutil
import sys
from pathlib import Path
from textwrap import dedent

# from textwrap import dedent
import nox
from nox import Session, session

"""
Helps for future developer to implement tests using Nox
- https://medium.com/analytics-vidhya/nox-the-shining-python-test-automation-tool-3e189e343b57
- https://sethmlarson.dev/nox-pyenv-all-python-versions#:~:text=Usually%20this%20is%20done%20for,other%20things%20like%20dependency%20versions).&text=This%20is%20awesome!,target%20a%20specific%20Python%20version.
""" # noqa: 501


package = "bee_py"
python_versions = ["3.12", "3.11", "3.10", "3.9"]
nox.needs_version = ">= 2023.4.22"
nox.options.sessions = (
"pre-commit",
"safety",
# "pre-commit",
# "safety",
"mypy",
"tests",
"typeguard",
"xdoctest",
"docs-build",
# "typeguard",
# "xdoctest",
# "docs-build",
)


def activate_virtualenv_in_precommit_hooks(session: Session) -> None:
"""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.
"""
assert session.bin is not None # nosec # noqa: S101

# Only patch hooks containing a reference to this session's bindir. Support
# quoting rules for Python and bash, but strip the outermost quotes so we
# can detect paths within the bindir, like <bindir>/python.
bindirs = [
bindir[1:-1] if bindir[0] in "'\"" else bindir for bindir in (repr(session.bin), shlex.quote(session.bin))
]

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

headers = {
# pre-commit < 2.16.0
"python": f"""\
import os
os.environ["VIRTUAL_ENV"] = {virtualenv!r}
os.environ["PATH"] = os.pathsep.join((
{session.bin!r},
os.environ.get("PATH", ""),
))
""",
# pre-commit >= 2.16.0
"bash": f"""\
VIRTUAL_ENV={shlex.quote(virtualenv)}
PATH={shlex.quote(session.bin)}"{os.pathsep}$PATH"
""",
# pre-commit >= 2.17.0 on Windows forces sh shebang
"/bin/sh": f"""\
VIRTUAL_ENV={shlex.quote(virtualenv)}
PATH={shlex.quote(session.bin)}"{os.pathsep}$PATH"
""",
}

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

if not hook.read_bytes().startswith(b"#!"):
continue

text = hook.read_text()

if not any(Path("A") == Path("a") and bindir.lower() in text.lower() or bindir in text for bindir in bindirs):
continue

lines = text.splitlines()

for executable, header in headers.items():
if executable in lines[0].lower():
lines.insert(1, dedent(header))
hook.write_text("\n".join(lines))
break


@session(name="pre-commit", python=python_versions[0])
def precommit(session: Session) -> None:
"""Lint using pre-commit."""
args = session.posargs or [
"run",
"--all-files",
"--hook-stage=manual",
"--show-diff-on-failure",
]
session.install(
"bandit",
"black",
"darglint",
"flake8",
"flake8-bugbear",
"flake8-docstrings",
"flake8-rst-docstrings",
"isort",
"pep8-naming",
"pre-commit",
"pre-commit-hooks",
"pyupgrade",
)
session.run("pre-commit", *args)
if args and args[0] == "install":
activate_virtualenv_in_precommit_hooks(session)
# @session(name="pre-commit", python=python_versions[0])
# def precommit(session: Session) -> None:
# """Lint using pre-commit."""
# args = session.posargs or [
# "run",
# "--all-files",
# "--hook-stage=manual",
# "--show-diff-on-failure",
# ]
# session.install(
# "bandit",
# "black",
# "darglint",
# "flake8",
# "flake8-bugbear",
# "flake8-docstrings",
# "flake8-rst-docstrings",
# "isort",
# "pep8-naming",
# "pre-commit",
# "pre-commit-hooks",
# "pyupgrade",
# )
# session.run("pre-commit", *args)
# if args and args[0] == "install":
# activate_virtualenv_in_precommit_hooks(session)


@session(python=python_versions)
def mypy(session: Session) -> None:
"""Type-check using mypy."""
args = session.posargs or ["src", "tests", "docs/conf.py"]
session.install(".")
session.install(".[lint]")
session.install("mypy", "pytest")
session.run("mypy", *args)
if not session.posargs:
Expand All @@ -135,7 +71,7 @@ def mypy(session: Session) -> None:
@session(python=python_versions)
def tests(session: Session) -> None:
"""Run the test suite."""
session.install(".")
session.install(".[test]")
session.install("coverage[toml]", "pytest", "pygments")
try:
session.run("coverage", "run", "--parallel", "-m", "pytest", *session.posargs)
Expand All @@ -160,37 +96,37 @@ def coverage(session: Session) -> None:
@session(python=python_versions[0])
def typeguard(session: Session) -> None:
"""Runtime type checking using Typeguard."""
session.install(".")
session.install(".[dev]")
session.install("pytest", "typeguard", "pygments")
session.run("pytest", f"--typeguard-packages={package}", *session.posargs)


@session(name="docs-build", python=python_versions[0])
def docs_build(session: Session) -> None:
"""Build the documentation."""
args = session.posargs or ["docs", "docs/_build"]
if not session.posargs and "FORCE_COLOR" in os.environ:
args.insert(0, "--color")
# @session(name="docs-build", python=python_versions[0])
# def docs_build(session: Session) -> None:
# """Build the documentation."""
# args = session.posargs or ["docs", "docs/_build"]
# if not session.posargs and "FORCE_COLOR" in os.environ:
# args.insert(0, "--color")

session.install(".")
session.install("sphinx", "sphinx-click", "furo", "myst-parser")
# session.install(".")
# session.install("sphinx", "sphinx-click", "furo", "myst-parser")

build_dir = Path("docs", "_build")
if build_dir.exists():
shutil.rmtree(build_dir)
# build_dir = Path("docs", "_build")
# if build_dir.exists():
# shutil.rmtree(build_dir)

session.run("sphinx-build", *args)
# session.run("sphinx-build", *args)


@session(python=python_versions[0])
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"]
session.install(".")
session.install("sphinx", "sphinx-autobuild", "sphinx-click", "furo", "myst-parser")
# @session(python=python_versions[0])
# 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"]
# session.install(".")
# session.install("sphinx", "sphinx-autobuild", "sphinx-click", "furo", "myst-parser")

build_dir = Path("docs", "_build")
if build_dir.exists():
shutil.rmtree(build_dir)
# build_dir = Path("docs", "_build")
# if build_dir.exists():
# shutil.rmtree(build_dir)

session.run("sphinx-autobuild", *args)
# session.run("sphinx-autobuild", *args)

0 comments on commit c0c7c64

Please sign in to comment.