#!/bin/bash

SOURCE_DIRS=('*/src/*' 'swim_test_project/test/*.py' 'swim_test_errors/test/*.py', 'swim_test_errors/snapshots/*')

function maybe_typos {(
    if command -v typos &> /dev/null; then
        typos
    fi
)}

function check_untracked_srcs {(
    untracked=$(git ls-files --others --exclude-standard -- ${SOURCE_DIRS[@]})
    if [[ "$untracked" != "" ]]; then
        echo -e "[\033[0;43mwarn\033[0m]Untracked files:\n\033[0;33m$untracked\033[0m"
    fi
)}

# Parens are there to start a new shell where we can exit early on failure
function run_tests {(
    cargo +stable fmt -- --check && \
    maybe_typos && \
    cargo test && \
    sh -c "cd output_test && make" && \
    sh -c "cd swim_tests && swim test" && \
    sh -c "cd swim_test_errors && python3 snapshot.py"  && \
    # Remember to add && \ when adding more tests
    true
)}

if ! run_tests ; then
    echo "----------------------------------------------------"
    echo -e "[\033[0;41merror\033[0m] Local tests failed. Force a commit using --no-verify"
    exit 1
fi

check_untracked_srcs
