Refactor invoke tasks

Favor importing and running other tasks over running
dependent tasks via shell.

Add docstrings to tasks that were missing them.

Move build and install to 'package' submodule.

Update documentation.
This commit is contained in:
Jon Grace-Cox
2022-08-24 13:25:58 -07:00
parent 1c6cc031c2
commit 16392e13fe
6 changed files with 64 additions and 41 deletions

View File

@@ -129,14 +129,18 @@ For example:
> inv --list > inv --list
Available tasks: Available tasks:
build Build the package.
clean Clean up the project area.
examples Generate examples markdown. examples Generate examples markdown.
server.docker-build colors.update Generate colors Enum from Mozilla color keywords.
server.docker-run housekeeping.clean Clean up the project area.
server.run package.build Build the package and write wheel to 'dist/' directory.
package.install Install the locally built version from 'dist/'.
server.docker-build Build docker image for anybadge server.
server.docker-run Run containerised anybadge server.
server.run Run local anybadge server.
test.cli Run CLI tests against currently installed version.
test.docker Run dockerised tests. test.docker Run dockerised tests.
test.local Run local tests. test.local Run local tests.
test.pypi Run tests against Pypi version.
``` ```
You can get help for a command using `inv --help <command>`. You can get help for a command using `inv --help <command>`.
@@ -177,13 +181,13 @@ release.
To test the latest available PyPi package, run: To test the latest available PyPi package, run:
```bash ```bash
inv test.pypi> inv test.pypi
``` ```
To test a specific version of a PyPi package, run: To test a specific version of a PyPi package, run:
```bash ```bash
inv test.pypi --version=\<VERSION> inv test.pypi --version=\<VERSION\>
``` ```
When the tests run they will output test files into a `\<VERSION>_\<DATETIME>` directory under `test_files/`. When the tests run they will output test files into a `\<VERSION>_\<DATETIME>` directory under `test_files/`.
@@ -207,7 +211,7 @@ If you would like to build, install and run the cli tests against a local instal
CLI code), you can use: CLI code), you can use:
```bash ```bash
inv build && inv install && inv test.cli inv package.build && inv package.install && inv test.cli
``` ```
Note that this will force install the built wheel from the project `dist/` directory over any existing local install. Note that this will force install the built wheel from the project `dist/` directory over any existing local install.

View File

@@ -5,33 +5,13 @@ import subprocess
from pathlib import Path from pathlib import Path
from invoke import task, Collection from invoke import task, Collection
from tasks import test, server, housekeeping, colors from tasks import test, server, housekeeping, colors, package
PROJECT_DIR = Path(__file__).parent.parent PROJECT_DIR = Path(__file__).parent.parent
os.chdir(PROJECT_DIR) os.chdir(PROJECT_DIR)
@task
def build(c):
"""Build the package."""
print("Building package...")
subprocess.run(["python", "setup.py", "bdist_wheel"])
@task
def install(c):
"""Install the locally built version from dist."""
print("Installing package...")
file_list = list((Path(PROJECT_DIR) / Path("dist")).glob("anybadge-*.whl"))
if len(file_list) > 1:
print("Not sure which dist package to install. Clean dist directory first.")
return
dist_file = file_list[0]
print(f"Installing: {dist_file}")
subprocess.run(["pip", "install", "--force-reinstall", dist_file])
@task @task
def examples(c): def examples(c):
"""Generate examples markdown.""" """Generate examples markdown."""
@@ -41,6 +21,6 @@ def examples(c):
main() main()
namespace = Collection(test, server, housekeeping, colors) namespace = Collection(test, server, housekeeping, colors, package)
for fn in [build, examples, install]: for fn in [examples]:
namespace.add_task(fn) namespace.add_task(fn)

View File

@@ -9,6 +9,7 @@ from anybadge.colors import Color
@task @task
def update(c): def update(c):
"""Generate colors Enum from Mozilla color keywords."""
url = "https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/color_keywords" url = "https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/color_keywords"
response = requests.get(url) response = requests.get(url)

View File

@@ -9,7 +9,7 @@ def delete_files(files: str):
subprocess.run(["rm", "-rf", file]) subprocess.run(["rm", "-rf", file])
@task() @task
def clean(c): def clean(c):
"""Clean up the project area.""" """Clean up the project area."""
print("Cleaning the project directory...") print("Cleaning the project directory...")

30
tasks/package.py Normal file
View File

@@ -0,0 +1,30 @@
import subprocess
from pathlib import Path
from invoke import task
PROJECT_DIR = Path(__file__).parent.parent
def run_build():
subprocess.run(["python", "setup.py", "bdist_wheel"])
@task
def build(c):
"""Build the package and write wheel to 'dist/' directory."""
print("Building package...")
run_build()
@task
def install(c):
"""Install the locally built version from 'dist/'."""
print("Installing package...")
file_list = list((Path(PROJECT_DIR) / Path("dist")).glob("anybadge-*.whl"))
if len(file_list) > 1:
print("Not sure which dist package to install. Clean dist directory first.")
return
dist_file = file_list[0]
print(f"Installing: {dist_file}")
subprocess.run(["pip", "install", "--force-reinstall", dist_file])

View File

@@ -1,11 +1,11 @@
import subprocess import subprocess
from pathlib import Path from pathlib import Path
from time import sleep
from invoke import task from invoke import task
from tasks.housekeeping import clean
PROJECT_DIR = Path(__file__).parent.parent PROJECT_DIR = Path(__file__).parent.parent
DOCKER_TAG = "test-anybadge:latest"
@task @task
@@ -19,9 +19,8 @@ def local(c):
def build_test_docker_image(): def build_test_docker_image():
subprocess.run( print("Building test docker image... ")
f"(cd docker/test && docker build . -t test-anybadge:latest)", shell=True subprocess.run(f"(cd docker/test && docker build . -t {DOCKER_TAG})", shell=True)
)
@task @task
@@ -29,11 +28,14 @@ def docker(c):
"""Run dockerised tests.""" """Run dockerised tests."""
print("Running containerised tests...") print("Running containerised tests...")
subprocess.run("invoke clean", shell=True) from tasks.housekeeping import clean
subprocess.run("invoke build", shell=True) from tasks.package import build
clean(c)
build(c)
build_test_docker_image() build_test_docker_image()
subprocess.run( subprocess.run(
f"docker run -v {PROJECT_DIR}:/app test-anybadge:latest /work/run_docker_tests.sh", f"docker run --rm -v {PROJECT_DIR}:/app {DOCKER_TAG} /work/run_docker_tests.sh",
shell=True, shell=True,
) )
@@ -43,13 +45,17 @@ def pypi(c, version="latest"):
"""Run tests against Pypi version.""" """Run tests against Pypi version."""
print("Running tests against pypi version...") print("Running tests against pypi version...")
from tasks.housekeeping import clean
clean(c) clean(c)
test_files = PROJECT_DIR / Path("test_files") test_files = PROJECT_DIR / Path("test_files")
test_files.mkdir(exist_ok=True) test_files.mkdir(exist_ok=True)
build_test_docker_image() build_test_docker_image()
print("Running tests in docker image... ")
subprocess.run( subprocess.run(
f"docker run -e VERSION={version} -v {test_files.absolute()}:/test_files test-anybadge:latest /work/run_pypi_tests.sh", f"docker run --rm -e VERSION={version} -v {test_files.absolute()}:/test_files {DOCKER_TAG} /work/run_pypi_tests.sh",
shell=True, shell=True,
) )
@@ -59,6 +65,8 @@ def cli(c, version="latest"):
"""Run CLI tests against currently installed version.""" """Run CLI tests against currently installed version."""
print("Running tests against currently installed version...") print("Running tests against currently installed version...")
from tasks.housekeeping import clean
clean(c) clean(c)
test_files = PROJECT_DIR / Path("test_files") test_files = PROJECT_DIR / Path("test_files")
test_files.mkdir(exist_ok=True) test_files.mkdir(exist_ok=True)