Ejemplo n.º 1
0
def show(context, log, results_file, verbose, item):
    """
    Print test results info from provided results json file.

    If no results file is supplied echo results from most recent
    test in history if it exists.

    If verbose option selected, echo all test cases.

    If log option selected echo test log.
    """
    history_log = context.obj['history_log']
    no_color = context.obj['no_color']
    if not results_file:
        # Find results/log file from history
        # Default -1 is most recent test run
        try:
            with open(history_log, 'r') as f:
                lines = f.readlines()
            history = lines[len(lines) - item]
        except IndexError:
            echo_style('History result at index %s does not exist.' % item,
                       no_color,
                       fg='red')
            sys.exit(1)
        except Exception:
            echo_style(
                'Unable to retrieve results history, '
                'provide results file or re-run test.',
                no_color,
                fg='red')
            sys.exit(1)

        log_file = get_log_file_from_item(history)
        if log:
            echo_log(log_file, no_color)
        else:
            echo_results_file(
                log_file.rsplit('.', 1)[0] + '.results', no_color, verbose)

    elif log:
        # Log file provided
        log_file = results_file.rsplit('.', 1)[0] + '.log'
        echo_log(log_file, no_color)
    else:
        # Results file provided
        echo_results_file(results_file, no_color, verbose)
Ejemplo n.º 2
0
def delete(context, item):
    """
    Delete the specified history item from the history log.
    """
    history_log = context.obj['history_log']
    no_color = context.obj['no_color']
    try:
        with open(history_log, 'r+') as f:
            lines = f.readlines()
            history = lines.pop(len(lines) - item)
            f.seek(0)
            f.write(''.join(lines))
            f.flush()
            f.truncate()
    except IndexError:
        echo_style('History result at index %s does not exist.' % item,
                   no_color,
                   fg='red')
        sys.exit(1)
    except Exception as error:
        echo_style('Unable to delete result item {0}. {1}'.format(item, error),
                   no_color,
                   fg='red')
        sys.exit(1)

    log_file = get_log_file_from_item(history)
    try:
        os.remove(log_file)
    except Exception:
        echo_style('Unable to delete results file for item {0}.'.format(item),
                   no_color,
                   fg='red')

    try:
        os.remove(log_file.rsplit('.', 1)[0] + '.results')
    except Exception:
        echo_style('Unable to delete log file for item {0}.'.format(item),
                   no_color,
                   fg='red')