From 7ac43519c7f6e4a205c4f37185e081dfe744ad34 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Sat, 19 Aug 2023 23:18:13 -0400 Subject: [PATCH 1/2] saving work --- {{cookiecutter.project_slug}}/.drone.yaml | 23 +++++++++++++++++++ {{cookiecutter.project_slug}}/pyproject.toml | 2 +- .../scripts/_common.sh | 9 ++++++++ .../scripts/{bootstrap => bootstrap.sh} | 14 ++++++++++- .../scripts/run_unit_tests.sh | 9 ++++++++ 5 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 {{cookiecutter.project_slug}}/.drone.yaml create mode 100644 {{cookiecutter.project_slug}}/scripts/_common.sh rename {{cookiecutter.project_slug}}/scripts/{bootstrap => bootstrap.sh} (69%) create mode 100644 {{cookiecutter.project_slug}}/scripts/run_unit_tests.sh diff --git a/{{cookiecutter.project_slug}}/.drone.yaml b/{{cookiecutter.project_slug}}/.drone.yaml new file mode 100644 index 0000000..9c4c699 --- /dev/null +++ b/{{cookiecutter.project_slug}}/.drone.yaml @@ -0,0 +1,23 @@ +kind: pipeline +type: docker +name: CI Test Pipeline + +steps: +- name: greeting + image: alpine + commands: + - echo "Welcome to drone\n" + +- name: Unit Tests + image: python:3.8-buster + commands: + - bash -c './ci_scripts/run_unit_tests.sh' + +trigger: + event: + exclude: + - pull_request + +# Secrets used to pull private images +image_pull_secrets: + - dockerconfigjson \ No newline at end of file diff --git a/{{cookiecutter.project_slug}}/pyproject.toml b/{{cookiecutter.project_slug}}/pyproject.toml index b2335dc..45f344e 100644 --- a/{{cookiecutter.project_slug}}/pyproject.toml +++ b/{{cookiecutter.project_slug}}/pyproject.toml @@ -7,4 +7,4 @@ filter_files = true [tool.black] line-length = 120 skip-string-normalization = true -exclude = "(^/\\.git|^/env/|^/venv/)" +exclude = "(^/\\.git|^/env/|^/venv/|^/node_modules/)" diff --git a/{{cookiecutter.project_slug}}/scripts/_common.sh b/{{cookiecutter.project_slug}}/scripts/_common.sh new file mode 100644 index 0000000..4173faf --- /dev/null +++ b/{{cookiecutter.project_slug}}/scripts/_common.sh @@ -0,0 +1,9 @@ +#! /usr/bin/env bash + +set -ex + +source "$(dirname "$0")/_common.sh" + +_setup_env + +${VIRTUAL_ENV}/bin/python -m pytest -vv --cov "$@" \ No newline at end of file diff --git a/{{cookiecutter.project_slug}}/scripts/bootstrap b/{{cookiecutter.project_slug}}/scripts/bootstrap.sh similarity index 69% rename from {{cookiecutter.project_slug}}/scripts/bootstrap rename to {{cookiecutter.project_slug}}/scripts/bootstrap.sh index b396ae1..8a84291 100755 --- a/{{cookiecutter.project_slug}}/scripts/bootstrap +++ b/{{cookiecutter.project_slug}}/scripts/bootstrap.sh @@ -4,7 +4,19 @@ # setting -o pipefail sets the exit code of a pipeline to that of the rightmost command to exit with a non-zero status, or to zero if all commands of the pipeline exit successfully. set -eo pipefail -if [ -z "$VIRTUAL_ENV" ]; then +function _setup_env() { + # build a virtual env and install + if [ -z $VIRTUAL_ENV ]; then + python3 -m venv env + source env/bin/activate + fi + +if [ "${DRONE}" == "true" ]; then + _setup_env + +fi + +if [ -z "${VIRTUAL_ENV}" ]; then echo "warning: you are not in a virtualenv" exit 1 fi diff --git a/{{cookiecutter.project_slug}}/scripts/run_unit_tests.sh b/{{cookiecutter.project_slug}}/scripts/run_unit_tests.sh new file mode 100644 index 0000000..4173faf --- /dev/null +++ b/{{cookiecutter.project_slug}}/scripts/run_unit_tests.sh @@ -0,0 +1,9 @@ +#! /usr/bin/env bash + +set -ex + +source "$(dirname "$0")/_common.sh" + +_setup_env + +${VIRTUAL_ENV}/bin/python -m pytest -vv --cov "$@" \ No newline at end of file From 8feef6906d68362bbb7be27600163bac2ce7d063 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Sun, 20 Aug 2023 09:36:08 -0400 Subject: [PATCH 2/2] Working setup --- {{cookiecutter.project_slug}}/.drone.yaml | 6 +-- .../.pre-commit-config.yaml | 2 +- .../dev-requirements.in | 1 + .../scripts/_common.sh | 19 ++++---- .../scripts/bootstrap.sh | 44 +++++++++++-------- .../scripts/run_unit_tests.sh | 9 ++-- .../tests/test_example.py | 1 - 7 files changed, 46 insertions(+), 36 deletions(-) mode change 100644 => 100755 {{cookiecutter.project_slug}}/scripts/run_unit_tests.sh diff --git a/{{cookiecutter.project_slug}}/.drone.yaml b/{{cookiecutter.project_slug}}/.drone.yaml index 9c4c699..e4e3377 100644 --- a/{{cookiecutter.project_slug}}/.drone.yaml +++ b/{{cookiecutter.project_slug}}/.drone.yaml @@ -9,15 +9,15 @@ steps: - echo "Welcome to drone\n" - name: Unit Tests - image: python:3.8-buster + image: python:3.11-bullseye commands: - bash -c './ci_scripts/run_unit_tests.sh' trigger: event: - exclude: - pull_request + - push # Secrets used to pull private images image_pull_secrets: - - dockerconfigjson \ No newline at end of file + - dockerconfigjson diff --git a/{{cookiecutter.project_slug}}/.pre-commit-config.yaml b/{{cookiecutter.project_slug}}/.pre-commit-config.yaml index 47e03ee..6535f3a 100644 --- a/{{cookiecutter.project_slug}}/.pre-commit-config.yaml +++ b/{{cookiecutter.project_slug}}/.pre-commit-config.yaml @@ -9,7 +9,7 @@ repos: - id: check-added-large-files - id: debug-statements - repo: https://github.com/shellcheck-py/shellcheck-py - rev: v0.8.0.4 + rev: v0.9.0.5 hooks: - id: shellcheck - repo: https://github.com/psf/black diff --git a/{{cookiecutter.project_slug}}/dev-requirements.in b/{{cookiecutter.project_slug}}/dev-requirements.in index e0ee54e..1e77b75 100644 --- a/{{cookiecutter.project_slug}}/dev-requirements.in +++ b/{{cookiecutter.project_slug}}/dev-requirements.in @@ -5,4 +5,5 @@ pip-tools pre-commit pytest pytest-cov +shellcheck-py==0.9.0.5 ruff diff --git a/{{cookiecutter.project_slug}}/scripts/_common.sh b/{{cookiecutter.project_slug}}/scripts/_common.sh index 4173faf..b10e732 100644 --- a/{{cookiecutter.project_slug}}/scripts/_common.sh +++ b/{{cookiecutter.project_slug}}/scripts/_common.sh @@ -1,9 +1,10 @@ -#! /usr/bin/env bash - -set -ex - -source "$(dirname "$0")/_common.sh" - -_setup_env - -${VIRTUAL_ENV}/bin/python -m pytest -vv --cov "$@" \ No newline at end of file +#!/usr/bin/env bash + +function _setup_env() { + # build a virtual env + if [ -z "${VIRTUAL_ENV}" ]; then + python3 -m venv env + # shellcheck source=/dev/null + source env/bin/activate + fi +} diff --git a/{{cookiecutter.project_slug}}/scripts/bootstrap.sh b/{{cookiecutter.project_slug}}/scripts/bootstrap.sh index 8a84291..8a0f4b6 100755 --- a/{{cookiecutter.project_slug}}/scripts/bootstrap.sh +++ b/{{cookiecutter.project_slug}}/scripts/bootstrap.sh @@ -1,32 +1,38 @@ #!/usr/bin/env bash # setting -e to exit immediately on a command failure. -# setting -o pipefail sets the exit code of a pipeline to that of the rightmost command to exit with a non-zero status, or to zero if all commands of the pipeline exit successfully. +# set -o pipefail sets the exit code of a pipeline to +# that of the rightmost command to exit with a non-zero status, +# or to zero if all commands of the pipeline exit successfully. +# set -u affects variables. When set, a reference to any variable +# you haven't previously defined - with the exceptions of $* and $@ - +# is an error, and causes the program to immediately exit set -eo pipefail +IFS=$'\n\t' -function _setup_env() { - # build a virtual env and install - if [ -z $VIRTUAL_ENV ]; then - python3 -m venv env - source env/bin/activate - fi +# shellcheck source=/dev/null +source "$(dirname "$0")/_common.sh" -if [ "${DRONE}" == "true" ]; then +function project_bootstrap() { _setup_env + pip install -U pip pip-tools=='7.3.0' + pip-compile requirements.in + pip-compile dev-requirements.in + pip-sync requirements.txt dev-requirements.txt +} -fi +function install_precommit_hooks() { + if [ ! -d ".git" ]; then + git init + fi -if [ -z "${VIRTUAL_ENV}" ]; then + if [ -z "$VIRTUAL_ENV" ]; then echo "warning: you are not in a virtualenv" exit 1 -fi + fi -pip install -U pip pip-tools -pip-compile requirements.in -pip-compile dev-requirements.in -pip-sync requirements.txt dev-requirements.txt - -if [ ! -d ".git" ]; then - git init pre-commit install -fi +} + +project_bootstrap +install_precommit_hooks diff --git a/{{cookiecutter.project_slug}}/scripts/run_unit_tests.sh b/{{cookiecutter.project_slug}}/scripts/run_unit_tests.sh old mode 100644 new mode 100755 index 4173faf..7ca34c3 --- a/{{cookiecutter.project_slug}}/scripts/run_unit_tests.sh +++ b/{{cookiecutter.project_slug}}/scripts/run_unit_tests.sh @@ -1,9 +1,12 @@ #! /usr/bin/env bash - set -ex +# shellcheck source=/dev/null source "$(dirname "$0")/_common.sh" -_setup_env +if [ "${DRONE}" == "true" ]; then + _setup_env + pip install -r requirements.txt -r dev-requirements.txt +fi -${VIRTUAL_ENV}/bin/python -m pytest -vv --cov "$@" \ No newline at end of file +exec "${VIRTUAL_ENV}/bin/python" -m pytest -vv --cov "$@" diff --git a/{{cookiecutter.project_slug}}/tests/test_example.py b/{{cookiecutter.project_slug}}/tests/test_example.py index ca64ecf..68ea5ce 100644 --- a/{{cookiecutter.project_slug}}/tests/test_example.py +++ b/{{cookiecutter.project_slug}}/tests/test_example.py @@ -4,4 +4,3 @@ from {{cookiecutter.project_slug}}.example import Example def test_example(): my_example = Example(name="dirp") assert my_example.name == "dirp" -