#!/usr/bin/env python3
"""Run everything that needs to pass for CI to be green.

This is executed by GitHub actions against Python 3.6, 3.7, 3.8, and 3.9 on both Windows and Ubuntu.

PYTHON_ARGCOMPLETE_OK
"""
import os
from pathlib import Path
from shutil import rmtree
from subprocess import CalledProcessError, run

os.environ['MILC_APP_NAME'] = 'ci_tests'
os.environ['MILC_APP_AUTHOR'] = 'MILC'

from milc import cli


@cli.entrypoint('Run CI Tests...')
def main(cli):
    build_ok = True

    if Path('build').exists():
        rmtree('build')

    cli.log.info('Running nose2 tests...')
    cmd = ['nose2']
    result = run(cmd)
    if result.returncode != 0:
        build_ok = False

    cli.log.info('Running flake8...')
    cmd = ['flake8']
    result = run(cmd)
    if result.returncode != 0:
        build_ok = False

    cli.log.info('Running yapf...')
    cmd = ['yapf', '-q', '-r', '.']
    result = run(cmd)
    if result.returncode != 0:
        build_ok = False
        cli.log.error('Improperly formatted code. Please run this: yapf -i -r .')

    if build_ok:
        cli.log.info('{fg_green}All tests passed!')
        return True

    cli.log.error('Tests are not passing! Please fix them before opening a PR.')
    return False


if __name__ == '__main__':
    cli()
