Пример #1
0
def test_choice_retries_on_failure(mock_input):
    """
    Tests the function will continue to retry until a valid option
    has been entered
    """
    answer = choice('Choose: ', choices=CHOICES)
    assert answer == 'Major'
Пример #2
0
def main():

    # Parse command line arguments
    args = parse_args(sys.argv[1:])

    # Get directory from where script was called
    cwd = os.getcwd()

    # Validate current environment before proceeding
    runchecks(cwd)

    # Acquire current git tag
    tag = get_tag(cwd)

    if tag is None:
        if confirm('No tags found, create initial tag: "0.1.0"'):
            create_tag(cwd, '0.1.0', args.message)
        sys.exit()

    # Strip prefix from tag, "v1.2.3" is not a semantic version
    # But it's a common way to indiciate a version number in version control
    prefix, tag = strip_prefix(tag)

    if args.bump is None:
        question = 'Choose: [M]ajor/[m]inor/[p]atch: '
        choices = ('Major', 'minor', 'patch')
        args.bump = choice(question, choices, allow_prefix=True)

    current_tag = Semver(tag)
    next_tag = str(current_tag.bump(args.bump))

    # Print version diff preview
    if args.preview:
        old, new = ('{}{}\n'.format(prefix or '', v) for v in (tag, next_tag))
        diff = ''.join(color_diff(ndiff([old], [new])))
        print(crayons.magenta('\nVersion Diff:'), diff, sep='\n')

    # If previous tag had a prefix, rejoin the prefix after bumping
    if prefix is not None:
        next_tag = prefix + next_tag

    if args.files:
        # Strip prefix from files
        _, new_tag = strip_prefix(next_tag)
        # Replace tag in each file
        diffs = [
            find_and_replace(f, tag, new_tag, args.preview) for f in args.files
        ]
        # Get names of all the files
        file_names = [f.name for f in args.files]
        # Print diffs (if any exist)
        if any(diffs):
            for fname, diff in zip(file_names, diffs):
                print('\n'.join(color_diff(diff.split('\n'))))

        # Log & commit changes if not in preview mode
        if not args.preview:
            fnames = ', '.join(file_names)
            print(crayons.red("\n  modified: {} ").format(fnames))
            if confirm('\nCommit changes?'):
                run(['git', 'add'] + file_names)
                run(['git', 'commit', '-m', '"bump version number"'])

    # Exit without creating tag if --no-tag positional argument is passed
    if args.no_tag or args.preview:
        sys.exit()

    # Create the tag
    result = create_tag(cwd, next_tag, args.message)
    if result.returncode == 0:
        success_msg = '\n  Created new tag: {}'.format(next_tag)
        print(crayons.green(success_msg))
Пример #3
0
def test_choice_with_lower_disabled(mock_input):
    """Tests case sensitivity is respected when lower kwarg is False"""
    answer = choice('Choice:', choices=CHOICES, lower=False)
    assert answer == 'patch'
Пример #4
0
def test_choice_case_insensitive_by_default(mock_input):
    answer = choice('Choose: ', choices=CHOICES)
    assert answer == 'Major'
    answer = choice('Choose:', choices=CHOICES)
    assert answer == 'Major'
Пример #5
0
def test_choice_lowers_case():
    answer = choice('Choose: ', choices=CHOICES)
    assert answer == 'Major'
Пример #6
0
def test_choice_accepts_prefix():
    answer = choice('Choose: ', choices=CHOICES, allow_prefix=True)
    assert answer == 'Major'