def test_shell_command_extra_input_var_substitution(): # repeat the test with variable substitution in the command string n = node.node_shell_command('testvarsub', 'echo', True, ctx={'testvar': 'test val'}) assert 'test val' in n.execute( api.MatchResult.from_input('testvarsub {{testvar}}'), None)
def test_shell_command(): n = node.node_shell_command('testls', 'ls', True) assert os.path.pardir in n.execute(api.MatchResult.from_input('-a'), None)
def test_shell_command_with_failure(): n = node.node_shell_command('testfailure', 'false', True) n.on_failure(node.node_shell_command('onfailure', 'echo failed', True)) assert 'failed' in n.execute(api.MatchResult.from_input('ls -a'), None)
def node_factory_command(key, val, ctx={}, usage=None): """ handles "#/definitions/type_command" :param key: :param val: :param ctx: :return: """ # command can be specified by a simple string if isinstance(val, str): root = node.CmdNode(key, context=ctx, usage=usage, method_evaluate=evaluators.require_all_children) n = node.node_shell_command(key + "_cmdstr", val, ctx=ctx, return_output=False) n.match = matchers.match_always_consume_no_input root.add_child(n) return root # command can be a list of commands (nesting allowed) elif isinstance(val, list): root = node.CmdNode(key, context=ctx, usage=usage, method_evaluate=evaluators.require_all_children) # add child cmds for i, c in enumerate(val): cn = node_factory_command(key + '_' + str(i + 1), c, ctx=ctx) root.add_child(cn) # swallow completions cn.match = matchers.match_always_consume_no_input return root # command can be a dict with keys {do,help,env} elif isinstance(val, dict): root = node.CmdNode(key, context=ctx, method_evaluate=evaluators.require_all_children) newctx = ctx.copy() if 'vars' in val: newctx.update(val['vars']) try: cn = node_factory_command(key + '_do_dict', val['do'], ctx=newctx) root.add_child(cn) # swallow completions cn.match = matchers.match_always_consume_no_input # cn.evaluate = evaluators.require_all_children except Exception as e: # replace the root node with an error message echo root = node.node_display_message(key, str(e)) if 'on_failure' in val: print('adding failure node exe wrapper. cmd = {}'.format( val['on_failure'])) root.on_failure( node_factory_command(key + '_on_failure', val['on_failure'], ctx=newctx)) return root else: raise ValueError( "value of command {} must be a string, list, or dict. type is {}". format(key, type(val)))