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

Replace tests/unit/test_doc.py with a pre-commit hook. #56727

Merged
merged 3 commits into from
Apr 24, 2020
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
57 changes: 56 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,48 @@ repos:
- --py-version=3.9
- --platform=linux

# ----- Invoke ---------------------------------------------------------------------------------------------------->
- id: pip-tools-compile
alias: compile-py3.5-invoke-requirements
name: Linux Py3.5 Invoke Requirements
files: ^requirements/static/invoke\.in$
args:
- -v
- --py-version=3.5

- id: pip-tools-compile
alias: compile-py3.6-invoke-requirements
name: Linux Py3.6 Invoke Requirements
files: ^requirements/static/invoke\.in$
args:
- -v
- --py-version=3.6

- id: pip-tools-compile
alias: compile-py3.7-invoke-requirements
name: Linux Py3.7 Invoke Requirements
files: ^requirements/static/invoke\.in$
args:
- -v
- --py-version=3.7

- id: pip-tools-compile
alias: compile-py3.8-invoke-requirements
name: Linux Py3.8 Invoke Requirements
files: ^requirements/static/invoke\.in$
args:
- -v
- --py-version=3.8

- id: pip-tools-compile
alias: compile-py3.9-invoke-requirements
name: Linux Py3.9 Invoke Requirements
files: ^requirements/static/invoke\.in$
args:
- -v
- --py-version=3.9
# <---- Invoke -----------------------------------------------------------------------------------------------------

- repo: https://github.com/timothycrosley/isort
rev: "1e78a9acf3110e1f9721feb591f89a451fc9876a"
hooks:
Expand Down Expand Up @@ -539,7 +581,7 @@ repos:
- id: nox-py2
alias: lint-salt
name: Lint Salt
files: ^((setup|noxfile)|salt/.*)\.py$
files: ^((setup|noxfile)|(salt|tasks)/.*)\.py$
args:
- -e
- lint-salt-pre-commit
Expand All @@ -556,3 +598,16 @@ repos:
- -e
- lint-tests-pre-commit
- --

- repo: https://github.com/saltstack/salt-nox-pre-commit
rev: master
hooks:
- id: nox-py2
alias: check-docs
name: Check Docs
files: ^(salt/.*\.py|doc/ref/.*\.rst)$
args:
- -e
- invoke-pre-commit
- --
- docs.check
71 changes: 70 additions & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@ def lint_salt(session):
if session.posargs:
paths = session.posargs
else:
paths = ["setup.py", "noxfile.py", "salt/"]
paths = ["setup.py", "noxfile.py", "salt/", "tasks/"]
_lint(session, ".pylintrc", flags, paths)


Expand Down Expand Up @@ -1133,3 +1133,72 @@ def docs_man(session, compress, update):
if compress:
session.run("tar", "-cJvf", "man-archive.tar.xz", "_build/man", external=True)
os.chdir("..")


def _invoke(session):
"""
Run invoke tasks
"""
requirements_file = "requirements/static/invoke.in"
distro_constraints = [
"requirements/static/{}/invoke.txt".format(_get_pydir(session))
]
install_command = ["--progress-bar=off", "-r", requirements_file]
for distro_constraint in distro_constraints:
install_command.extend(["--constraint", distro_constraint])
session.install(*install_command, silent=PIP_INSTALL_SILENT)
cmd = ["inv"]
files = []

# Unfortunately, invoke doesn't support the nargs functionality like argpase does.
# Let's make it behave properly
for idx, posarg in enumerate(session.posargs):
if idx == 0:
cmd.append(posarg)
continue
if posarg.startswith("--"):
cmd.append(posarg)
continue
files.append(posarg)
if files:
cmd.append("--files={}".format(" ".join(files)))
session.run(*cmd)


@nox.session(name="invoke", python="3")
def invoke(session):
_invoke(session)


@nox.session(name="invoke-pre-commit", python="3")
def invoke_pre_commit(session):
if "VIRTUAL_ENV" not in os.environ:
session.error(
"This should be running from within a virtualenv and "
"'VIRTUAL_ENV' was not found as an environment variable."
)
if "pre-commit" not in os.environ["VIRTUAL_ENV"]:
session.error(
"This should be running from within a pre-commit virtualenv and "
"'VIRTUAL_ENV'({}) does not appear to be a pre-commit virtualenv.".format(
os.environ["VIRTUAL_ENV"]
)
)
from nox.virtualenv import VirtualEnv

# Let's patch nox to make it run inside the pre-commit virtualenv
try:
session._runner.venv = VirtualEnv( # pylint: disable=unexpected-keyword-arg
os.environ["VIRTUAL_ENV"],
interpreter=session._runner.func.python,
reuse_existing=True,
venv=True,
)
except TypeError:
# This is still nox-py2
session._runner.venv = VirtualEnv(
os.environ["VIRTUAL_ENV"],
interpreter=session._runner.func.python,
reuse_existing=True,
)
_invoke(session)
2 changes: 2 additions & 0 deletions requirements/static/invoke.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoke
blessings
9 changes: 9 additions & 0 deletions requirements/static/py3.5/invoke.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile -o requirements/static/py3.5/invoke.txt -v requirements/static/invoke.in
#
blessings==1.7
invoke==1.4.1
six==1.14.0 # via blessings
9 changes: 9 additions & 0 deletions requirements/static/py3.6/invoke.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile -o requirements/static/py3.6/invoke.txt -v requirements/static/invoke.in
#
blessings==1.7
invoke==1.4.1
six==1.14.0 # via blessings
9 changes: 9 additions & 0 deletions requirements/static/py3.7/invoke.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile -o requirements/static/py3.7/invoke.txt -v requirements/static/invoke.in
#
blessings==1.7
invoke==1.4.1
six==1.14.0 # via blessings
9 changes: 9 additions & 0 deletions requirements/static/py3.8/invoke.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile -o requirements/static/py3.8/invoke.txt -v requirements/static/invoke.in
#
blessings==1.7
invoke==1.4.1
six==1.14.0 # via blessings
9 changes: 9 additions & 0 deletions requirements/static/py3.9/invoke.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile -o requirements/static/py3.9/invoke.txt -v requirements/static/invoke.in
#
blessings==1.7
invoke==1.4.1
six==1.14.0 # via blessings
28 changes: 28 additions & 0 deletions tasks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# What is this directory?

This directory contains python scripts which should be called by [invoke](https://pypi.org/project/invoke).

Instead of having several multi-purpose python scripts scatered through multiple paths in the salt code base,
we will now concentrate them under an invoke task.

## Calling Invoke

Invoke can be called in the following ways.

### Installed system-wide

If invoke is installed system-wide, be sure you also have `blessings` installed if you want coloured output, although
it's not a hard requirement.

```
inv docs.check
```

### Using Nox

Since salt already uses nox, and nox manages virtual environments and respective requirements, calling invoke is as
simple as:

```
nox -e invoke -- docs.check
```
9 changes: 9 additions & 0 deletions tasks/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- coding: utf-8 -*-

from invoke import Collection # pylint: disable=3rd-party-module-not-gated

from . import docs, loader

ns = Collection()
ns.add_collection(Collection.from_module(docs, name="docs"), name="docs")
ns.add_collection(Collection.from_module(loader, name="loader"), name="loader")
Loading