#!/usr/bin/env bash

# Exit build script on first failure.
set -e

# Echo commands to stdout.
set -x

# Exit on unset variable.
set -u

# Location of app source files.
readonly SOURCE_DIR=beancount_chase

# Location of virtualenv.
readonly VIRTUALENV_DIR=.venv

# Delete pyc files from previous builds.
find . \
  -name "*.pyc" \
  -type f \
  -not -path "./${VIRTUALENV_DIR}/*" \
  -delete

# Run unit tests and calculate code coverage.
pytest --cov="${SOURCE_DIR}" "${SOURCE_DIR}"

# Check that source has correct formatting.
yapf --diff --recursive ./

# Check correct sorting for imports.
isort \
  . \
  --force-single-line-imports \
  --diff \
  --check-only \
  --skip-glob="${VIRTUALENV_DIR}/*"

# Run static analysis for Python bugs/cruft.
flake8 "${SOURCE_DIR}/"

# Check for other style violations.
PYTHONPATH="${SOURCE_DIR}" pylint "${SOURCE_DIR}"
