#!/bin/bash

set -e
set -x  # Enable debug output


# # run linting of python
if [ "$SKIP_LINT" -ne 1 ]; then
    echo 'Running linting...'
    ruff check reporting/ tests/ dev.py
else
    echo 'Skipping linting...'
fi

# set flags for pytest-cov
COVERAGE_ARGS=""
if [ "$COV_REPORT" != "off" ]; then
    COVERAGE_ARGS="--cov-report $COV_REPORT --cov=reporting"
fi

# Ensure reports directory exists
mkdir -p reports

# run pytest with junitxml output for Jenkins
echo 'Running tests...'

if [ -n "$TESTS" ]; then
    # Run a specific test suite (e.g. integration tests) without coverage reporting
    PYTHONDONTWRITEBYTECODE=1 python -m pytest -v $TESTS ${TEST_ARGS}
else
    # Run unit tests with coverage
    PYTHONDONTWRITEBYTECODE=1 COVERAGE_FILE=reports/.coverage python -m pytest -v --ignore=tests/integration tests/ \
            -p no:ddtrace \
            -p no:cacheprovider \
            --cov reporting \
            --cov-report xml:reports/coverage.xml \
            --cov-report term \
            --junitxml=reports/pyunit.xml

    # Check if the report was generated and print a message
    if [ -f reports/pyunit.xml ]; then
        echo "JUnit XML report generated successfully."
    else
        echo "ERROR: JUnit XML report was NOT generated!"
        exit 2
    fi
fi

echo 'Done!'
