Example #1
0
def test_formatter_pipes_a_json_string_to_stdout():
    env = Environment('falconf.yaml')
    flyer = Flyer(mode='test', env=env)
    formatter = Formatter(flyer)
    out = capture_stdout(lambda : formatter.pipe_to_stdout(flyer))
    assert isinstance(json.loads(out), dict)
Example #2
0
def format_results(flyer, debug, output='json'):
    """
    Use debug and output to decide how to show results of the run.

    Args:
        flyer (Flyer)
        debug (boolean)
        output (string): One of 'json', 'formatted', 'clean', 'return'. Defaults to 'json'.

    Returns:
        None: if output != 'return'
        dict: if output == 'return'
    """
    formatter = Formatter(flyer, elapsed_time=ELAPSED_TIME)
    formatter.parse_steps(flyer)

    if debug:
        # print a pretty version of the results JSON
        formatter.pipe_debug_to_stdout(flyer)
        return None
    elif output == 'return':
        # just return the results dict
        return formatter.return_results(flyer)
    elif output == 'formatted':
        """
        print results as:

        ------------------
        Executing `command` ...
        stderr
        stdout
        ------------------
        """
        results = formatter.return_results(flyer)
        steps = results['steps']

        for s in range(len(steps)):
            if steps[s]['command'] is not 'noop':
                print('Executing `{}`...'.format(steps[s]['command']))
                stderr = steps[s]['err']
                if stderr != '':
                    eprint(stderr)
                stdout = steps[s]['out']
                if stdout != '':
                    print(stdout)
                if s < len(steps) - 1:
                    print('\n------------------')
        return None
    elif output == 'clean':
        """
        print results as a sequence of:

        stderr
        stdout
        """
        results = formatter.return_results(flyer)
        steps = results['steps']

        for s in range(len(steps)):
            if steps[s]['command'] is not 'noop':
                err = steps[s]['err']
                if err != '':
                    eprint(err)
                out = steps[s]['out']
                if out != '':
                    print(out)
    else:
        # print the results JSON
        formatter.pipe_to_stdout(flyer)
        return None