def prune_completions(prefix, all_test_names):
    """Filter returning only items that will complete the current prefix."""

    completions = set()
    for test_name in all_test_names:
        if test_name.startswith(prefix):
            next_break = test_name.find('.', len(prefix) + 1)
            if next_break >= 0:
                # Add only enough to complete this level; don't drown
                # the user with all the possible completions from
                # here.
                completions.add(test_name[:next_break])
            else:
                # If there are no more levels, then add the full name
                # of the leaf.
                completions.add(test_name)
    return completions