# Path: tests/klltest.py
# def kll_run(args):
#     '''
#     Run kll compiler
# 
#     @return: Exit code
#     '''
#     with pytest.raises(SystemExit) as pytest_wrapped_e:
#         kll.main(args)
#     assert pytest_wrapped_e.type == SystemExit
#     return pytest_wrapped_e.value.code
# 
# def header_test(name, args):
#     '''
#     Prints out the name of the test and the arguments passed
# 
#     @param name: Name of the test
#     @param args: List of arguments
#     '''
#     print('\n---- {} ---- {}'.format(name, args))
# 
# @pytest.fixture(scope="session")
# def kiibohd_controller_repo():
#     '''
#     Downloads a cached copy of the kiibohd controller repo
#     '''
#     tmp_dir = os.path.join(tempfile.gettempdir(), 'kll_controller_test')
#     kll_dir = os.path.join(tmp_dir, 'kll')
# 
#     try:
#         if not os.path.isdir(tmp_dir):
#             # Clone if not available
#             Repo.clone_from('https://github.com/kiibohd/controller.git', tmp_dir)
#         else:
#             # Update otherwise
#             repo = Repo(tmp_dir)
#             repo.remotes.origin.fetch('+refs/heads/*:refs/remotes/origin/*')
#             repo.remotes.origin.pull()
#     except exc.GitCommandError:
#         # TODO Timeout loop, wait for repo to initialize
#         repo = Repo(tmp_dir)
#         pass
# 
#     try:
#         # Check for kll compiler as well (not used during testing, but required for controller tests)
#         if not os.path.isdir(kll_dir):
#             # Clone if not available
#             Repo.clone_from('https://github.com/kiibohd/kll.git', kll_dir)
#         else:
#             # Update otherwise
#             repo_kll = Repo(kll_dir)
#             repo_kll.remotes.origin.pull()
#     except exc.GitCommandError:
#         # TODO Timeout loop, wait for repo to initialize
#         repo = Repo(tmp_dir)
#         pass
# 
#     return tmp_dir


import os
import pytest
import tempfile
from tests.klltest import kll_run, header_test, kiibohd_controller_repo
    '''
    Runs a simple kiibohd test
    '''
    # Prepare tmp directory
    tmp_dir = os.path.join(tempfile.gettempdir(), 'kll_pytest')
    os.makedirs(tmp_dir, exist_ok=True)
    target_dir = tempfile.mkdtemp(suffix='-simple', prefix='kiibohd-', dir=tmp_dir)

    # Controller git repo
    controller = kiibohd_controller_repo

    # Run test
    args = [
        '--config',
        os.path.join(controller, 'Scan/Devices/MatrixARMPeriodic/capabilities.kll'),
        os.path.join(controller, 'Macro/PartialMap/capabilities.kll'),
        os.path.join(controller, 'Output/USB/capabilities.kll'),
        '--base',
        os.path.join(controller, 'Scan/Infinity_60/scancode_map.kll'),
        'kll/examples/nonetest.kll',
        '--default',
        'kll/layouts/stdFuncMap.kll',
        '--emitter', 'kiibohd',
        '--def-output', os.path.join(target_dir, 'kll_defs.h'),
        '--map-output', os.path.join(target_dir, 'generatedKeymap.h'),
        '--hid-output', os.path.join(target_dir, 'usb_hid.h'),
        '--pixel-output', os.path.join(target_dir, 'generatedPixelmap.c'),
        '--json-output', os.path.join(target_dir, 'kll.json'),
    ]
    header_test('{}'.format(target_dir), args)

# Path: tests/klltest.py
# def kll_run(args):
#     '''
#     Run kll compiler
# 
#     @return: Exit code
#     '''
#     with pytest.raises(SystemExit) as pytest_wrapped_e:
#         kll.main(args)
#     assert pytest_wrapped_e.type == SystemExit
#     return pytest_wrapped_e.value.code
# 
# def header_test(name, args):
#     '''
#     Prints out the name of the test and the arguments passed
# 
#     @param name: Name of the test
#     @param args: List of arguments
#     '''
#     print('\n---- {} ---- {}'.format(name, args))
# 
# @pytest.fixture(scope="session")
# def kiibohd_controller_repo():
#     '''
#     Downloads a cached copy of the kiibohd controller repo
#     '''
#     tmp_dir = os.path.join(tempfile.gettempdir(), 'kll_controller_test')
#     kll_dir = os.path.join(tmp_dir, 'kll')
# 
#     try:
#         if not os.path.isdir(tmp_dir):
#             # Clone if not available
#             Repo.clone_from('https://github.com/kiibohd/controller.git', tmp_dir)
#         else:
#             # Update otherwise
#             repo = Repo(tmp_dir)
#             repo.remotes.origin.fetch('+refs/heads/*:refs/remotes/origin/*')
#             repo.remotes.origin.pull()
#     except exc.GitCommandError:
#         # TODO Timeout loop, wait for repo to initialize
#         repo = Repo(tmp_dir)
#         pass
# 
#     try:
#         # Check for kll compiler as well (not used during testing, but required for controller tests)
#         if not os.path.isdir(kll_dir):
#             # Clone if not available
#             Repo.clone_from('https://github.com/kiibohd/kll.git', kll_dir)
#         else:
#             # Update otherwise
#             repo_kll = Repo(kll_dir)
#             repo_kll.remotes.origin.pull()
#     except exc.GitCommandError:
#         # TODO Timeout loop, wait for repo to initialize
#         repo = Repo(tmp_dir)
#         pass
# 
#     return tmp_dir


# Path: tests/test_kiibohd.py
import os
import pytest
import tempfile
from tests.klltest import kll_run, header_test, kiibohd_controller_repo

    '''
    Runs a simple kiibohd test
    '''
    # Prepare tmp directory
    tmp_dir = os.path.join(tempfile.gettempdir(), 'kll_pytest')
    os.makedirs(tmp_dir, exist_ok=True)
    target_dir = tempfile.mkdtemp(suffix='-simple', prefix='kiibohd-', dir=tmp_dir)

    # Controller git repo
    controller = kiibohd_controller_repo

    # Run test
    args = [
        '--config',
        os.path.join(controller, 'Scan/Devices/MatrixARMPeriodic/capabilities.kll'),
        os.path.join(controller, 'Macro/PartialMap/capabilities.kll'),
        os.path.join(controller, 'Output/USB/capabilities.kll'),
        '--base',
        os.path.join(controller, 'Scan/Infinity_60/scancode_map.kll'),
        'kll/examples/nonetest.kll',
        '--default',
        'kll/layouts/stdFuncMap.kll',
        '--emitter', 'kiibohd',
        '--def-output', os.path.join(target_dir, 'kll_defs.h'),
        '--map-output', os.path.join(target_dir, 'generatedKeymap.h'),
        '--hid-output', os.path.join(target_dir, 'usb_hid.h'),
        '--pixel-output', os.path.join(target_dir, 'generatedPixelmap.c'),
        '--json-output', os.path.join(target_dir, 'kll.json'),
    ]
    header_test('{}'.format(target_dir), args)

    ret = kll_run(args)