Skip to content

Commit

Permalink
Add unit tests and test pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
satterly committed Nov 17, 2020
1 parent b8f552f commit 693553d
Show file tree
Hide file tree
Showing 15 changed files with 655 additions and 170 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PROJECT=zabbix_alerta
4 changes: 4 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[flake8]
ignore = F403, E501, W503
exclude = *.wsgi
max-line-length = 140
45 changes: 45 additions & 0 deletions .github/workflows/github-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: CI Tests

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
python-version: [3.7, 3.8]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
id: install-deps
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
pip install -r requirements.txt
pip install -r requirements-dev.txt
pip install .
- name: Pre-commit hooks
id: hooks
run: |
pre-commit run -a --show-diff-on-failure
- name: Lint with flake8
id: lint
run: |
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
flake8 . --count --exit-zero --max-complexity=50 --max-line-length=127 --statistics
- name: Test with pytest
id: test
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/alerta
run: |
pytest --cov=zabbix-alerta test_*.py
2 changes: 2 additions & 0 deletions .isort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[settings]
known_third_party = alertaclient,protobix,pyzabbix,setuptools
44 changes: 44 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
repos:
- repo: https://github.com/pre-commit/mirrors-autopep8
rev: v1.5.1
hooks:
- id: autopep8
- repo: https://github.com/pre-commit/pre-commit-hooks.git
rev: v2.5.0
hooks:
- id: check-added-large-files
- id: check-ast
- id: check-byte-order-marker
- id: check-case-conflict
- id: check-docstring-first
- id: check-json
- id: check-merge-conflict
- id: check-yaml
- id: debug-statements
- id: double-quote-string-fixer
- id: end-of-file-fixer
- id: fix-encoding-pragma
args: ['--remove']
- id: pretty-format-json
args: ['--autofix']
- id: name-tests-test
args: ['--django']
- id: requirements-txt-fixer
- id: trailing-whitespace
- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.7
hooks:
- id: flake8
- repo: https://github.com/asottile/pyupgrade
rev: v1.27.0
hooks:
- id: pyupgrade
args: ['--py3-plus']
- repo: https://github.com/asottile/seed-isort-config
rev: v1.9.4
hooks:
- id: seed-isort-config
- repo: https://github.com/pre-commit/mirrors-isort
rev: v4.3.21
hooks:
- id: isort
94 changes: 94 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!make

VENV=venv
PYTHON=$(VENV)/bin/python
PIP=$(VENV)/bin/pip --disable-pip-version-check
PYLINT=$(VENV)/bin/pylint
MYPY=$(VENV)/bin/mypy
BLACK=$(VENV)/bin/black
TOX=$(VENV)/bin/tox
PYTEST=$(VENV)/bin/pytest
DOCKER_COMPOSE=docker-compose
PRE_COMMIT=$(VENV)/bin/pre-commit
WHEEL=$(VENV)/bin/wheel
TWINE=$(VENV)/bin/twine
GIT=git

.DEFAULT_GOAL:=help

-include .env .env.local .env.*.local

ifndef PROJECT
$(error PROJECT is not set)
endif

VERSION=$(shell cut -d "'" -f 2 $(PROJECT)/version.py)

PKG_SDIST=dist/*-$(VERSION).tar.gz
PKG_WHEEL=dist/*-$(VERSION)-*.whl

all: help

$(PIP):
python3 -m venv $(VENV)

$(PYLINT): $(PIP)
$(PIP) install pylint

$(MYPY): $(PIP)
$(PIP) install mypy

$(BLACK): $(PIP)
$(PIP) install black

$(TOX): $(PIP)
$(PIP) install tox

$(PYTEST): $(PIP)
$(PIP) install pytest

$(PRE_COMMIT): $(PIP)
$(PIP) install pre-commit

$(WHEEL): $(PIP)
$(PIP) install wheel

$(TWINE): $(PIP)
$(PIP) install twine

ifdef TOXENV
toxparams?=-e $(TOXENV)
endif

## format - Code formatter.
format: $(BLACK)
$(BLACK) -l120 -S -v $(PROJECT)

## hooks - Run pre-commit hooks.
hooks: $(PRE_COMMIT)
$(PRE_COMMIT) run --all-files --show-diff-on-failure

## lint - Lint and type checking.
lint: $(PYLINT) $(BLACK) $(MYPY)
$(PYLINT) --rcfile pylintrc $(PROJECT).py
$(BLACK) -l120 -S --check -v $(PROJECT).py || true
$(MYPY) $(PROJECT).py

## test - Run unit tests.
test: $(PYTEST)
$(PYTEST)

## help - Show this help.
help: Makefile
@echo ''
@echo 'Usage:'
@echo ' make [TARGET]'
@echo ''
@echo 'Targets:'
@sed -n 's/^##//p' $<
@echo ''

@echo 'Add project-specific env variables to .env file:'
@echo 'PROJECT=$(PROJECT)'

.PHONY: help lint test build sdist wheel clean all
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,3 @@ services:
#networks:
# zbxnet:
# driver: bridge

2 changes: 2 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[mypy]
ignore_missing_imports = True
Loading

0 comments on commit 693553d

Please sign in to comment.