#!/bin/bash

set -eu

# defaults if not set
: "${SKIP_LINT:=0}"
: "${SKIP_TYPE_CHECKS:=0}"
: "${SKIP_TESTS:=0}"
: "${TEST_ARGS:=}"
: "${SKIP_COV:=0}"

# run linting
if [ "$SKIP_LINT" -ne 1 ]; then
    echo 'Running linting...'
    poetry check --lock
    shellcheck lint-and-test.sh
    hadolint Dockerfile
    yamllint -s .hadolint.yaml .yamllint.yaml docker-compose.yaml software-catalog.yaml
    ruff check src tests
    ruff format --check src tests
else
    echo 'Skipping linting...'
fi

# run type checks
if [ "$SKIP_TYPE_CHECKS" -ne 1 ]; then
    echo 'Running type checks...'
    mypy src tests
else
    echo 'Skipping type checks...'
fi

# run tests
if [ "$SKIP_TESTS" -ne 1 ]; then
    echo 'Running tests...'
    if [ "$SKIP_COV" -ne 1 ]; then
        COV_ARGS="--cov=src --cov-report=term --cov-report=xml:coverage.xml"
    else
        COV_ARGS="--no-cov"
    fi
    # intentional word splitting for multi-arg variables
    # shellcheck disable=SC2086
    pytest tests --ignore=tests/integration/ $COV_ARGS $TEST_ARGS
else
    echo 'Skipping tests...'
fi

echo 'Done!'
