Import('env')
Import('program')

import os
import shlex
import shutil
import subprocess

from SCons.Errors import BuildError


def run_tests(target=None, source=None, env=None):
    pytest = shutil.which('pytest')
    if not pytest:
        raise BuildError('Unable to find pytest in PATH')

    test_dir = os.environ.get('RM_TS_DIR', '/tmp/rmlint-tests')
    os.makedirs(test_dir, exist_ok=True)

    test_env = dict(os.environ, RM_TS_DIR=test_dir)
    command = (pytest, *shlex.split(env['PYTEST_ARGS']))

    if status := subprocess.call(command, env=test_env):
        Exit(status)

env.Alias('test',
    env.Depends(
        env.Command('run_tests', None, Action(run_tests, "Running tests")),
        program
    )
)
