Exemplo n.º 1
0
def test_add_duty_to_multiple_collections():
    """Check what happens when adding the same duty to multiple collections."""
    collection1 = Collection()
    collection2 = Collection()

    duty = decorate(none, name="duty")

    collection1.add(duty)
    collection2.add(duty)

    duty1 = collection1.get("duty")
    duty2 = collection2.get("duty")

    assert duty1 is not duty2
    assert duty1.collection is collection1
    assert duty2.collection is collection2
Exemplo n.º 2
0
def test_clear_collection():
    """Check that duties and their aliases are correctly cleared from a collection."""
    collection = Collection()
    collection.add(decorate(none, name="duty_1"))
    collection.clear()
    with pytest.raises(KeyError):
        collection.get("duty-1")
Exemplo n.º 3
0
def test_register_aliases():
    """Register a duty and its aliases."""
    duty = decorate(none,
                    name="hello",
                    aliases=["HELLO", "_hello_", ".hello."])
    collection = Collection()
    collection.add(duty)
    assert collection.get("hello")
    assert collection.get("HELLO")
    assert collection.get("_hello_")
    assert collection.get(".hello.")
Exemplo n.º 4
0
Arquivo: cli.py Projeto: pawamoy/duty
def main(
    args: Optional[List[str]] = None
) -> int:  # noqa: WPS212 (return statements)
    """
    Run the main program.

    This function is executed when you type `duty` or `python -m duty`.

    Arguments:
        args: Arguments passed from the command line.

    Returns:
        An exit code.
    """
    parser = get_parser()
    opts = parser.parse_args(args=args)
    remainder = opts.remainder

    collection = Collection(opts.duties_file)
    collection.load()

    if opts.help is not None:
        print_help(parser, opts, collection)
        return 0

    if opts.list:
        print(textwrap.indent(collection.format_help(), prefix="  "))
        return 0

    try:
        arg_lists = split_args(remainder, collection.names())
    except ValueError as error:
        print(error, file=sys.stderr)
        return 1

    if not arg_lists:
        print("> Please choose at least one duty", file=sys.stderr)
        return 1

    global_opts = specified_options(
        opts, exclude={"duties_file", "list", "help", "remainder"})
    try:
        commands = parse_commands(arg_lists, global_opts, collection)
    except TypeError as error:  # noqa: WPS440 (variable overlap)
        print(f"> {error}", file=sys.stderr)
        return 1

    for duty, posargs, kwargs in commands:
        try:
            duty.run(*posargs, **kwargs)
        except DutyFailure as failure:
            return failure.code

    return 0
Exemplo n.º 5
0
def test_run_pre_post_duties_refs():
    """Run pre- and post- duties as duties references."""
    pre_calls = []
    post_calls = []

    collection = Collection()
    collection.add(decorate(lambda ctx: pre_calls.append(True), name="pre"))
    collection.add(decorate(lambda ctx: post_calls.append(True), name="post"))

    duty = Duty("name", "description", lambda ctx: None, collection=collection, pre=["pre"], post=["post"])
    duty.run()

    assert pre_calls[0] is True
    assert post_calls[0] is True
Exemplo n.º 6
0
def test_replace_name_and_set_alias():
    """Replace underscores by dashes in duties names."""
    collection = Collection()
    collection.add(decorate(none, name="snake_case"))
    assert collection.get("snake_case") is collection.get("snake-case")
Exemplo n.º 7
0
def test_dont_get_duty():
    """Don't find a duty."""
    collection = Collection()
    with pytest.raises(KeyError):
        collection.get("hello")