#!/usr/bin/env python3
import os, sys
import subprocess
from colorama import Fore, init


init(autoreset=True)


bin_dir = os.path.abspath(os.path.join(sys.executable, os.path.pardir))
EXIT_CODE = 0


def check_if_exit_code():
    if EXIT_CODE != 0:
        print("\n{}Tests Failed. {}Please check messages above and then re-run the command.".format(Fore.RED, Fore.YELLOW))
        print("{}Exit Code: {}".format(Fore.RED, EXIT_CODE))
        exit(1)

print("{}Linting...".format(Fore.YELLOW))


FLAKE8_IGNORE = '--ignore=E128,E501,E401,E402'
try:
    subprocess.check_output([os.path.join(bin_dir, 'flake8'), 'theme', FLAKE8_IGNORE])
    subprocess.check_output([os.path.join(bin_dir, 'flake8'), 'scripts', FLAKE8_IGNORE])
    subprocess.check_output([os.path.join(bin_dir, 'flake8'), 'pelicanconf.py', FLAKE8_IGNORE])
    subprocess.check_output([os.path.join(bin_dir, 'flake8'), sys.argv[0], FLAKE8_IGNORE])  # Test this file
except subprocess.CalledProcessError as e:
    print(Fore.RED, e.output.decode())
    EXIT_CODE = 1

check_if_exit_code()
print("{}All Python tests passed! {}Testing Static Files...".format(Fore.GREEN, Fore.YELLOW))


EXIT_CODE = os.system('npm test')
check_if_exit_code()

print("{}All Tests Passed!".format(Fore.GREEN))