# Path: servi/command.py
# def process_and_run_command_line(command_line=None):
#         servi_parser, sub_parsers = setup_parsers()
# 
#         commands = load_plugins(sub_parsers)
# 
#         args, extra_args = parse_args(commands, servi_parser, command_line)
# 
#         log_level = arg_to_log_level(args.verbose)
#         retval = False
#         with use_log_level(log_level):
#             if args.template_dir:
#                 c.TMPL_DIR_SITE = args.template_dir
#                 warn('*** Just set TMPL_DIR_SITE to |{0}|'.format(
#                     c.TMPL_DIR_SITE))
# 
#             if not args.command:
#                 error('\n***** Error ******\nNo command on command line.\n')
#                 servi_parser.print_help()
#                 raise ServiError('No command line')
# 
#             if args.command not in commands:
#                 raise ServiError('Unknown command: {0}'.format(args.command))
# 
#             special = getattr(commands[args.command], "special", {})
# 
#             if not special.get("skip_init", False):
#                 master_dir = c.find_master_dir(os.getcwd())
#                 set_master_dir(master_dir)
#                 load_user_config()
# 
#             try:
#                 info('Servi - Running: {0}\n'.format(args.command))
#                 if c.MASTER_DIR:
#                     debug('Master Directory: {0}'
#                           .format(os.path.abspath(c.MASTER_DIR)))
#                 debug('Template Directory: {0}'
#                       .format(os.path.abspath(c.TMPL_DIR_SITE)))
# 
#                 retval = args.command_func(args, extra_args)
#             except (ForceError, ServiError):
#                 raise
# 
# 
#         return retval
# 
# Path: servi/commands/buildbox.py
# def get_all_boxes():
#     # Returns a SORTED list of all boxes in c.BOX_DIR
#     # Sorted by semantic version
#     # It is a tuple of [filename, semanticVersion, ...]
# 
#     boxes = []
#     try:
#         existing_boxes = os.listdir(c.BOX_DIR)
#     except FileNotFoundError:
#         return []
# 
#     if existing_boxes:
#         boxes = []
#         for f in existing_boxes:
#             match = re.match('servi_box_([0-9]+_[0-9]+_[0-9]+)\.box', f)
#             if match:
#                 ver = SemanticVersion(match.group(1).replace('_', '.'))
#                 boxes.append((f, ver))
# 
#         boxes.sort(key=lambda tup: tup[1], reverse=True)
# 
#     return boxes
# 
# Path: servi/exceptions.py
# class ServiError(Exception):
#     # Expected errrors. Handle gracefully (ie: no stack dump)
#     def __init__(self, msg=None):
#         self.msg = msg
# 
#     def __str__(self):
#         return (
#             '\n***** ERROR *****\n'
#             '{0}\n'
#             '\n**Servi Aborting**'.format(self.msg))
# 
# class ForceError(Exception):
#     def __init__(self, msg=None):
#         self.msg = msg
# 
#     def __str__(self):
#         return (
#             '\n***** ERROR *****\n'
#             '{0}\n'
#             'Use -f / --force to override.\n'
#             '\n**Servi Aborting**'.format(self.msg))


import subprocess
import servi.commands.buildbox as buildbox
from tests.fixtures import *
from servi.utils import *
from servi.command import process_and_run_command_line as servi_run
from servi.commands.buildbox import get_all_boxes
from servi.exceptions import ServiError, ForceError
        'servi_box_1_1_1.box',
        'bogusfile',
    ]

    for f in boxlist:
        with open(os.path.join(c.BOX_DIR, f), 'w') as fp:
            fp.write('stuff')

    assert get_all_boxes()[0][0] == 'servi_box_3_4_11.box'
    assert len(get_all_boxes()) == 3

    os.remove(os.path.join(c.BOX_DIR, boxlist[0]))
    assert get_all_boxes()[0][0] == 'servi_box_1_1_12.box'

    os.remove(os.path.join(c.BOX_DIR, boxlist[1]))
    assert get_all_boxes()[0][0] == 'servi_box_1_1_1.box'

    os.remove(os.path.join(c.BOX_DIR, boxlist[2]))
    assert get_all_boxes() == []


def test_build_box(clean_master, mock_template_dir, servi_init, clean_boxes):
    assert servi_run('buildbox --mock')

    # Now that it already exists
    assert servi_run('buildbox --mock') == buildbox.SKIPPED


def test_use_box(clean_master, mock_template_dir, servi_init, clean_boxes):
    with pytest.raises(ServiError):

# Path: servi/command.py
# def process_and_run_command_line(command_line=None):
#         servi_parser, sub_parsers = setup_parsers()
# 
#         commands = load_plugins(sub_parsers)
# 
#         args, extra_args = parse_args(commands, servi_parser, command_line)
# 
#         log_level = arg_to_log_level(args.verbose)
#         retval = False
#         with use_log_level(log_level):
#             if args.template_dir:
#                 c.TMPL_DIR_SITE = args.template_dir
#                 warn('*** Just set TMPL_DIR_SITE to |{0}|'.format(
#                     c.TMPL_DIR_SITE))
# 
#             if not args.command:
#                 error('\n***** Error ******\nNo command on command line.\n')
#                 servi_parser.print_help()
#                 raise ServiError('No command line')
# 
#             if args.command not in commands:
#                 raise ServiError('Unknown command: {0}'.format(args.command))
# 
#             special = getattr(commands[args.command], "special", {})
# 
#             if not special.get("skip_init", False):
#                 master_dir = c.find_master_dir(os.getcwd())
#                 set_master_dir(master_dir)
#                 load_user_config()
# 
#             try:
#                 info('Servi - Running: {0}\n'.format(args.command))
#                 if c.MASTER_DIR:
#                     debug('Master Directory: {0}'
#                           .format(os.path.abspath(c.MASTER_DIR)))
#                 debug('Template Directory: {0}'
#                       .format(os.path.abspath(c.TMPL_DIR_SITE)))
# 
#                 retval = args.command_func(args, extra_args)
#             except (ForceError, ServiError):
#                 raise
# 
# 
#         return retval
# 
# Path: servi/commands/buildbox.py
# def get_all_boxes():
#     # Returns a SORTED list of all boxes in c.BOX_DIR
#     # Sorted by semantic version
#     # It is a tuple of [filename, semanticVersion, ...]
# 
#     boxes = []
#     try:
#         existing_boxes = os.listdir(c.BOX_DIR)
#     except FileNotFoundError:
#         return []
# 
#     if existing_boxes:
#         boxes = []
#         for f in existing_boxes:
#             match = re.match('servi_box_([0-9]+_[0-9]+_[0-9]+)\.box', f)
#             if match:
#                 ver = SemanticVersion(match.group(1).replace('_', '.'))
#                 boxes.append((f, ver))
# 
#         boxes.sort(key=lambda tup: tup[1], reverse=True)
# 
#     return boxes
# 
# Path: servi/exceptions.py
# class ServiError(Exception):
#     # Expected errrors. Handle gracefully (ie: no stack dump)
#     def __init__(self, msg=None):
#         self.msg = msg
# 
#     def __str__(self):
#         return (
#             '\n***** ERROR *****\n'
#             '{0}\n'
#             '\n**Servi Aborting**'.format(self.msg))
# 
# class ForceError(Exception):
#     def __init__(self, msg=None):
#         self.msg = msg
# 
#     def __str__(self):
#         return (
#             '\n***** ERROR *****\n'
#             '{0}\n'
#             'Use -f / --force to override.\n'
#             '\n**Servi Aborting**'.format(self.msg))


# Path: tests/test_box.py
import subprocess
import servi.commands.buildbox as buildbox
from tests.fixtures import *
from servi.utils import *
from servi.command import process_and_run_command_line as servi_run
from servi.commands.buildbox import get_all_boxes
from servi.exceptions import ServiError, ForceError

        'servi_box_1_1_1.box',
        'bogusfile',
    ]

    for f in boxlist:
        with open(os.path.join(c.BOX_DIR, f), 'w') as fp:
            fp.write('stuff')

    assert get_all_boxes()[0][0] == 'servi_box_3_4_11.box'
    assert len(get_all_boxes()) == 3

    os.remove(os.path.join(c.BOX_DIR, boxlist[0]))
    assert get_all_boxes()[0][0] == 'servi_box_1_1_12.box'

    os.remove(os.path.join(c.BOX_DIR, boxlist[1]))
    assert get_all_boxes()[0][0] == 'servi_box_1_1_1.box'

    os.remove(os.path.join(c.BOX_DIR, boxlist[2]))
    assert get_all_boxes() == []


def test_build_box(clean_master, mock_template_dir, servi_init, clean_boxes):
    assert servi_run('buildbox --mock')

    # Now that it already exists
    assert servi_run('buildbox --mock') == buildbox.SKIPPED


def test_use_box(clean_master, mock_template_dir, servi_init, clean_boxes):
    with pytest.raises(ServiError):

        assert servi_run('usebox --mock')