From c9f60a3661836c077464cb57f95bf468d6824fee Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Sat, 16 Sep 2023 14:33:24 -0400 Subject: [PATCH] Initial setup --- .coveragerc | 6 ++++ .drone.yml | 23 ++++++++++++ .gitignore | 62 +++++++++++++++++++++++++++++++++ .pre-commit-config.yaml | 39 +++++++++++++++++++++ LICENSE | 10 ++++++ README.md | 26 ++++++++++++++ dev-requirements.in | 9 +++++ dev-requirements.txt | 73 +++++++++++++++++++++++++++++++++++++++ htmx_contact/__init__.py | 0 htmx_contact/app.py | 14 ++++++++ pyproject.toml | 37 ++++++++++++++++++++ requirements.in | 2 ++ requirements.txt | 28 +++++++++++++++ scripts/_common.sh | 10 ++++++ scripts/bootstrap.sh | 38 ++++++++++++++++++++ scripts/run_unit_tests.sh | 12 +++++++ tasks.py | 20 +++++++++++ tests/__init__.py | 0 tests/conftest.py | 13 +++++++ tests/test_routes.py | 10 ++++++ 20 files changed, 432 insertions(+) create mode 100644 .coveragerc create mode 100644 .drone.yml create mode 100644 .gitignore create mode 100644 .pre-commit-config.yaml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 dev-requirements.in create mode 100644 dev-requirements.txt create mode 100644 htmx_contact/__init__.py create mode 100644 htmx_contact/app.py create mode 100644 pyproject.toml create mode 100644 requirements.in create mode 100644 requirements.txt create mode 100644 scripts/_common.sh create mode 100755 scripts/bootstrap.sh create mode 100755 scripts/run_unit_tests.sh create mode 100644 tasks.py create mode 100644 tests/__init__.py create mode 100644 tests/conftest.py create mode 100644 tests/test_routes.py diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..debdf50 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,6 @@ +[run] +source = htmx_contact +omit = test* + +[report] +show_missing = True diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..27b9bb5 --- /dev/null +++ b/.drone.yml @@ -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.11-bullseye + commands: + - bash -c './scripts/run_unit_tests.sh' + +trigger: + event: + - pull_request + - push + +# Secrets used to pull private images +image_pull_secrets: + - dockerconfigjson diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e8afa11 --- /dev/null +++ b/.gitignore @@ -0,0 +1,62 @@ +# ---> Python +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*,cover + +# Translations +*.mo +*.pot + +# Django stuff: +*.log + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Pycharm +.idea diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..0e859e5 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,39 @@ +default_stages: [commit, push] +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.2.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files + - id: debug-statements + - repo: https://github.com/shellcheck-py/shellcheck-py + rev: v0.9.0.5 + hooks: + - id: shellcheck + - repo: https://github.com/psf/black + rev: 22.10.0 + hooks: + - id: black + args: ["htmx_contact", "./tests/"] + types: [ python ] + - repo: local + hooks: + - id: isort + name: isort + entry: isort + language: system + types: [python] + require_serial: true + - id: ruff + name: ruff + entry: ruff + language: system + types: [python] + args: [ + # '-rn', # Only display messages + # '-sn', # Don't display the score + # '--disable=C,R,W0511', # Disable C and R type messages, and TODO fixme warning + ] + require_serial: true diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..79a30b0 --- /dev/null +++ b/LICENSE @@ -0,0 +1,10 @@ + +MIT License + +Copyright (c) 2023, Drew Bednar + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..57043f0 --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# htmx_contact + +Learning HTMX by building a the htmx contact.app + +## First time setup + +Create a virtual environment and activate it. Now from the root project directory run `./scripts/bootstrap`. This will install `pip-tools` and sync any dependencies for the first time. + +## Dependency management + +Dependencies are managed via [pip-tools]. + +### Adding a dependency + +To add a dependency, edit `requirements.in` (or `dev-requirements.in` +for dev dependencies) and add your dependency then run `pip-compile +requirements.in`. + +### Syncing dependencies + +Run `pip-sync requirements.txt dev_requirements.txt`. + +## Testing + +Run the tests by invoking `py.test` in the project root. Make sure you +run any pending migrations beforehand. diff --git a/dev-requirements.in b/dev-requirements.in new file mode 100644 index 0000000..1e77b75 --- /dev/null +++ b/dev-requirements.in @@ -0,0 +1,9 @@ +black +invoke +isort +pip-tools +pre-commit +pytest +pytest-cov +shellcheck-py==0.9.0.5 +ruff diff --git a/dev-requirements.txt b/dev-requirements.txt new file mode 100644 index 0000000..d5cead1 --- /dev/null +++ b/dev-requirements.txt @@ -0,0 +1,73 @@ +# +# This file is autogenerated by pip-compile with Python 3.11 +# by the following command: +# +# pip-compile dev-requirements.in +# +black==23.9.1 + # via -r dev-requirements.in +build==1.0.3 + # via pip-tools +cfgv==3.4.0 + # via pre-commit +click==8.1.7 + # via + # black + # pip-tools +coverage[toml]==7.3.1 + # via pytest-cov +distlib==0.3.7 + # via virtualenv +filelock==3.12.4 + # via virtualenv +identify==2.5.29 + # via pre-commit +iniconfig==2.0.0 + # via pytest +invoke==2.2.0 + # via -r dev-requirements.in +isort==5.12.0 + # via -r dev-requirements.in +mypy-extensions==1.0.0 + # via black +nodeenv==1.8.0 + # via pre-commit +packaging==23.1 + # via + # black + # build + # pytest +pathspec==0.11.2 + # via black +pip-tools==7.3.0 + # via -r dev-requirements.in +platformdirs==3.10.0 + # via + # black + # virtualenv +pluggy==1.3.0 + # via pytest +pre-commit==3.4.0 + # via -r dev-requirements.in +pyproject-hooks==1.0.0 + # via build +pytest==7.4.2 + # via + # -r dev-requirements.in + # pytest-cov +pytest-cov==4.1.0 + # via -r dev-requirements.in +pyyaml==6.0.1 + # via pre-commit +ruff==0.0.290 + # via -r dev-requirements.in +shellcheck-py==0.9.0.5 + # via -r dev-requirements.in +virtualenv==20.24.5 + # via pre-commit +wheel==0.41.2 + # via pip-tools + +# The following packages are considered to be unsafe in a requirements file: +# pip +# setuptools diff --git a/htmx_contact/__init__.py b/htmx_contact/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/htmx_contact/app.py b/htmx_contact/app.py new file mode 100644 index 0000000..10588e0 --- /dev/null +++ b/htmx_contact/app.py @@ -0,0 +1,14 @@ +from flask import Flask +from flask import redirect + +app = Flask("htmx_contact") + + +@app.route("/", methods=["GET"]) +def index(): + return redirect("/contacts") + + +@app.route("/contacts", methods=["GET"]) +def contacts(): + return "Hello HTMX" diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..caf0352 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,37 @@ +[tool.isort] +profile = "black" +line_length = 120 +force_single_line = true +filter_files = true + +[tool.black] +line-length = 120 +skip-string-normalization = true +exclude = "(^/\\.git|^/env/|^/venv/|^/node_modules/)" + +[tool.ruff] +line-length = 120 +# Exclude a variety of commonly ignored directories. +exclude = [ + ".bzr", + ".direnv", + ".eggs", + ".git", + ".git-rewrite", + ".hg", + ".mypy_cache", + ".nox", + ".pants.d", + ".pytype", + ".ruff_cache", + ".svn", + ".tox", + ".venv", + "__pypackages__", + "_build", + "buck-out", + "build", + "dist", + "node_modules", + "venv", +] diff --git a/requirements.in b/requirements.in new file mode 100644 index 0000000..e39cea1 --- /dev/null +++ b/requirements.in @@ -0,0 +1,2 @@ +flask +sqlalchemy diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..3fa448e --- /dev/null +++ b/requirements.txt @@ -0,0 +1,28 @@ +# +# This file is autogenerated by pip-compile with Python 3.11 +# by the following command: +# +# pip-compile requirements.in +# +blinker==1.6.2 + # via flask +click==8.1.7 + # via flask +flask==2.3.3 + # via -r requirements.in +greenlet==2.0.2 + # via sqlalchemy +itsdangerous==2.1.2 + # via flask +jinja2==3.1.2 + # via flask +markupsafe==2.1.3 + # via + # jinja2 + # werkzeug +sqlalchemy==2.0.20 + # via -r requirements.in +typing-extensions==4.7.1 + # via sqlalchemy +werkzeug==2.3.7 + # via flask diff --git a/scripts/_common.sh b/scripts/_common.sh new file mode 100644 index 0000000..b10e732 --- /dev/null +++ b/scripts/_common.sh @@ -0,0 +1,10 @@ +#!/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/scripts/bootstrap.sh b/scripts/bootstrap.sh new file mode 100755 index 0000000..8a0f4b6 --- /dev/null +++ b/scripts/bootstrap.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +# setting -e to exit immediately on a command failure. +# 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' + +# shellcheck source=/dev/null +source "$(dirname "$0")/_common.sh" + +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 +} + +function install_precommit_hooks() { + if [ ! -d ".git" ]; then + git init + fi + + if [ -z "$VIRTUAL_ENV" ]; then + echo "warning: you are not in a virtualenv" + exit 1 + fi + + pre-commit install +} + +project_bootstrap +install_precommit_hooks diff --git a/scripts/run_unit_tests.sh b/scripts/run_unit_tests.sh new file mode 100755 index 0000000..7ca34c3 --- /dev/null +++ b/scripts/run_unit_tests.sh @@ -0,0 +1,12 @@ +#! /usr/bin/env bash +set -ex + +# shellcheck source=/dev/null +source "$(dirname "$0")/_common.sh" + +if [ "${DRONE}" == "true" ]; then + _setup_env + pip install -r requirements.txt -r dev-requirements.txt +fi + +exec "${VIRTUAL_ENV}/bin/python" -m pytest -vv --cov "$@" diff --git a/tasks.py b/tasks.py new file mode 100644 index 0000000..9cd39f3 --- /dev/null +++ b/tasks.py @@ -0,0 +1,20 @@ +from invoke import task + + +@task +def serve_dev(c, debugger=True, reload=True, threads=True, port=8888, host="0.0.0.0"): + """Serves the htmx_contact.app locally""" + cmd = "flask --app ./htmx_contact/app.py run" + cmd += " --debug" if debugger else "" + cmd += " --reload" if reload else "" + cmd += " --with-threads" if threads else "" + cmd += f" --host {host}" if host else "" + cmd += f" --port {port}" if port else "" + print(cmd) + c.run(cmd, pty=True) + + +@task +def test(c): + """executes test suite""" + c.run("pytest -vvv tests/", pty=True) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..de9d3c5 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,13 @@ +import pytest + +from htmx_contact.app import app + + +@pytest.fixture() +def client(): + return app.test_client() + + +@pytest.fixture() +def runner(app): + return app.test_cli_runner() diff --git a/tests/test_routes.py b/tests/test_routes.py new file mode 100644 index 0000000..42446b2 --- /dev/null +++ b/tests/test_routes.py @@ -0,0 +1,10 @@ +def test_index_redirect_to_contacts(client): + response = client.get("/") + assert response.status_code == 302 + assert response.headers.get("Location") == "/contacts" + + +def test_get_contacts(client): + response = client.get("/contacts") + assert response.status_code == 200 + assert "htmx" in response.text.lower()