Ejemplo n.º 1
0
def test_passes_template_context_to_render_method():
    engine = templates.Engine()
    p_template = Mock()
    engine.env.get_template = Mock(return_value=p_template)

    templates.Engine().render_file('fake.jinja2', {'fake': 'context'})

    p_template.render.assert_called_once_with({'fake': 'context'})
Ejemplo n.º 2
0
def test_passes_template_context_to_render_method():
    engine = templates.Engine()
    p_template = Mock()
    engine.env.from_string = Mock(return_value=p_template)

    templates.Engine().render('fake template string', {'fake': 'context'})

    p_template.render.assert_called_once_with({'fake': 'context'})
Ejemplo n.º 3
0
def test_uses_env_get_template():
    """
    When calling Engine.render_template()
    then Engine.env.get_template is called to load the template.
    """
    # Setup
    engine = templates.Engine()
    engine.env.from_string = Mock()

    templates.Engine().render('fake template string')

    engine.env.from_string.assert_called_once_with('fake template string')
Ejemplo n.º 4
0
def test_uses_env_get_template():
    """
    When calling Engine.render_template()
    then Engine.env.get_template is called to load the template.
    """
    # Setup
    engine = templates.Engine()
    engine.env.get_template = Mock()

    templates.Engine().render_file('fake.jinja2')

    engine.env.get_template.assert_called_once_with('fake.jinja2')
Ejemplo n.º 5
0
def run_script(script: Script, options: CliOptions) -> None:
    """ Run the script with the given (command line) options. """
    template_ctx = build_template_context(script, options)
    verbose = RunContext().get('verbose')
    pretend = RunContext().get('pretend')

    if verbose >= 3:
        log.info('Compiling script <35>{name}\n{script}'.format(
            name=script.name, script=shell.highlight(script.command, 'jinja')))
        yaml_str = yaml.dump(template_ctx, default_flow_style=False)
        log.info('with context:\n{}\n'.format(shell.highlight(
            yaml_str, 'yaml')))

    # Command is either specified directly in pelconf.yaml or lives in a
    # separate file.
    command = script.command
    if script.command_file:
        with open(conf.proj_path(script.command_file)) as fp:
            command = fp.read()

    if not command:
        raise ValueError(
            "Scripts must have 'command' or 'command_file' specified.")

    cmd = templates.Engine().render(command, template_ctx)
    retcode = exec_script_command(cmd, pretend)

    if verbose:
        log.info("Script exited with code: <33>{}", retcode)

    if retcode not in script.success_exit_codes:
        sys.exit(retcode)
Ejemplo n.º 6
0
def init(quick: bool, blank: bool, force: bool):
    """ Create an empty pelconf.yaml from template """
    config_file = 'pelconf.yaml'
    prompt = "-- <35>{} <32>already exists. Wipe it?<0>".format(config_file)

    if not force and exists(config_file) and not click.confirm(
            shell.fmt(prompt)):
        log.info("Canceled")
        return

    ctx = dict(blank=blank)

    if not blank:
        form = InitForm().run(quick=quick)
        ctx.update(form.values)

    config_content = templates.Engine().render_file('pelconf.yaml', ctx)

    log.info('Writing <35>{}'.format(config_file))
    fs.write_file(config_file, config_content)

    if context.get('verbose') > 0:
        print(
            f"{'- ' * 40}\n{shell.highlight(config_content, 'yaml')}{'- ' * 40}"
        )
Ejemplo n.º 7
0
def test_calls_make_env_when_created_for_the_first_time(p_make_env: Mock):
    # Make sure we don't have any lingering instance from other tests.
    util.Singleton.instances = {}

    templates.Engine()

    p_make_env.assert_called_once()

    # Clean-up afterwards (Engine is a singleton so changes will be
    # preserved after the test finishes - Engine.env is a Mock).
    util.Singleton.instances = {}
Ejemplo n.º 8
0
def test_is_singleton():
    assert templates.Engine() is templates.Engine()
Ejemplo n.º 9
0
def test_uses_double_curly_brace_for_expressions():
    assert templates.Engine().env.variable_start_string == '{{'
    assert templates.Engine().env.variable_end_string == '}}'
Ejemplo n.º 10
0
def test_has_all_filters(filter_name: str):
    # Make sure we don't have any lingering instance from other tests.
    assert filter_name in templates.Engine().env.filters