# 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


import subprocess as sp
import pytest
from servi.command import process_and_run_command_line as servi_run
from servi.exceptions import *
def test_servi_shellscript(tmpdir):
    tempdir = str(tmpdir.mkdir('serviplate'))
    os.chdir(tempdir)
    os.makedirs(os.path.join(tempdir, 'path1/path1.1/path1.1.1'))
    print('cwd: {0}'.format(os.getcwd()))
    assert servi_run('init --skip_servifile_globals .')

    shell_cmd = 'servi'

    assert not sp.call(
        [shell_cmd, 'update', '--help'],
        cwd=os.path.join(tempdir, 'path1/path1.1/path1.1.1'))
    assert not sp.call(
        [shell_cmd, 'update', '--help'],
        cwd=os.path.join(tempdir, 'path1/path1.1'))
    assert not sp.call(
        [shell_cmd, 'update', '--help'],
        cwd=os.path.join(tempdir, 'path1'))
    assert not sp.call(
        [shell_cmd, 'update', '--help'],
        cwd=os.path.join(tempdir, '.'))

    assert not sp.call(
        [shell_cmd, 'update', '--help'],
        cwd=os.path.join(tempdir, '..'))
    assert not sp.call(
        [shell_cmd, 'update', '--help'],
        cwd=tempdir)

    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: tests/test_servi_shellscript.py
import subprocess as sp
import pytest
from servi.command import process_and_run_command_line as servi_run
from servi.exceptions import *

def test_servi_shellscript(tmpdir):
    tempdir = str(tmpdir.mkdir('serviplate'))
    os.chdir(tempdir)
    os.makedirs(os.path.join(tempdir, 'path1/path1.1/path1.1.1'))
    print('cwd: {0}'.format(os.getcwd()))
    assert servi_run('init --skip_servifile_globals .')

    shell_cmd = 'servi'

    assert not sp.call(
        [shell_cmd, 'update', '--help'],
        cwd=os.path.join(tempdir, 'path1/path1.1/path1.1.1'))
    assert not sp.call(
        [shell_cmd, 'update', '--help'],
        cwd=os.path.join(tempdir, 'path1/path1.1'))
    assert not sp.call(
        [shell_cmd, 'update', '--help'],
        cwd=os.path.join(tempdir, 'path1'))
    assert not sp.call(
        [shell_cmd, 'update', '--help'],
        cwd=os.path.join(tempdir, '.'))

    assert not sp.call(
        [shell_cmd, 'update', '--help'],
        cwd=os.path.join(tempdir, '..'))
    assert not sp.call(
        [shell_cmd, 'update', '--help'],
        cwd=tempdir)

    with pytest.raises(ServiError):

        servi_run(' ')