Exemplo n.º 1
0
def test_config_unknown(tmpdir):
    cfg1 = tmpdir.join("config1.whatisthis")
    cfg1.write("z: 3\nw: 10\n")

    with pytest.raises(SystemExit) as exc:
        run_cli(stout, (3, ), argv=f"@{cfg1.strpath}".split(), expand="@")
    assert exc.value.code == 2
Exemplo n.º 2
0
def test_leftovers():
    assert run_cli(leftovers, (), argv=["hello", "there"]) == ["there"]
    assert run_cli(leftovers, (), argv="book".split()) == []
    assert run_cli(leftovers, (), argv="my --pear --orange".split()) == [
        "--pear",
        "--orange",
    ]
Exemplo n.º 3
0
def test_unknown_argument():
    with pytest.raises(SystemExit) as exc:
        run_cli(stout, (3, ), argv="--x=4".split())
    assert exc.value.code == 2

    with pytest.raises(SystemExit) as exc:
        run_cli(stout, (3, ), tag=tag.Boption, argv="--z=3 --w=10".split())
    assert exc.value.code == 2
Exemplo n.º 4
0
def test_config_dict():
    assert run_cli(stout, (3, ), argv=[{"z": 3, "w": 10}]) == (16, 8)

    assert run_cli(patriotism, (), argv=[{
        "flag": True,
        "-n": 2
    }]) == "wavewave"

    assert run_cli(patriotism, (), argv=[{"flag": False}]) == "don't wave"
Exemplo n.º 5
0
def main():
    commands = {}
    for entry_point in pkg_resources.iter_entry_points("paperoni.command"):
        commands[entry_point.name] = entry_point.load()
    try:
        run_cli(commands, expand="@")
    except PaperoniError as err:
        print(f"{type(err).__name__}:", err.args[0], file=sys.stderr)
        sys.exit(1)
Exemplo n.º 6
0
def entry_sy_config():
    commands = {}
    for name, value in globals().items():
        parts = name.split("_")
        if parts[0] == "config" and len(parts) > 1:
            curr = commands
            for part in parts[1:-1]:
                curr = curr.setdefault(part, {})
            curr[parts[-1]] = value
    run_cli(commands)
Exemplo n.º 7
0
def test_nargs():
    assert run_cli(ice_cream, (), argv="--duo 1 2 --tang".split()) == (
        [1, 2],
        [],
    )
    assert run_cli(ice_cream, (), argv="--tang --duo 1 2".split()) == (
        [1, 2],
        [],
    )
    assert run_cli(ice_cream, (), argv="--duo 1 2 --tang 3 4 5".split()) == (
        [1, 2],
        [3, 4, 5],
    )
Exemplo n.º 8
0
def test_append():
    assert run_cli(accum, (), argv=["--junk", "x", "--junk", "y"]) == (
        ["x", "y"],
        [],
    )
    with pytest.raises(SystemExit):
        run_cli(accum, (), argv=["--junk", "x", "y"])

    assert run_cli(accum, (), argv=["--clusters", "x", "y", "--clusters",
                                    "z"]) == ([], [["x", "y"], ["z"]])
    assert run_cli(accum, (),
                   argv=["--clusters", "x", "--junk", "y", "--clusters",
                         "z"]) == (["y"], [["x"], ["z"]])
Exemplo n.º 9
0
def test_config_cfg(tmpdir):
    cfg1 = tmpdir.join("config1.cfg")
    cfg1.write("[default]\nz = 3\nw = 10\n")

    assert run_cli(stout, (3, ),
                   argv=[],
                   expand=ArgsExpander("@", default_file=cfg1)) == (16, 8)

    cfg2 = tmpdir.join("config2.cfg")
    cfg2.write("[ohno]\nz = 3\nw = 10\n")

    with pytest.raises(SystemExit) as exc:
        run_cli(stout, (3, ), argv=f"@{cfg2.strpath}".split(), expand="@")
    assert exc.value.code == 2
Exemplo n.º 10
0
def test_positional():
    assert run_cli(groot, (), argv=["Marcel"]) == "Grootings, Marcel!"

    with pytest.raises(SystemExit):
        run_cli(groot, (), argv=[])

    assert run_cli(translate, (), argv=["4", "7"]) == [4, 7]

    with pytest.raises(SystemExit):
        run_cli(translate, (), argv=["4"])

    with pytest.raises(SystemExit):
        run_cli(translate, (), argv=["4", "7", "8"])

    assert run_cli(reverso, (), argv=list("goomba")) == list("abmoog")
    assert run_cli(reverso, (), argv=[]) == []
Exemplo n.º 11
0
def test_config_yaml(tmpdir):
    cfg1 = tmpdir.join("config1.yaml")
    cfg1.write("z: 3\nw: 10\n")

    assert run_cli(stout, (3, ),
                   argv=[],
                   expand=ArgsExpander("@", default_file=cfg1)) == (16, 8)
Exemplo n.º 12
0
def test_extras():
    fns = [append_number, append_bool]
    assert run_cli(spaghetti, (fns, ),
                   extras=fns,
                   argv="--num 37 --boo".split()) == [37, True]

    assert run_cli(spaghetti, (fns, ),
                   extras=fns,
                   argv="--boo --num 37".split()) == [37, True]

    assert run_cli(spaghetti, (fns, ), extras=fns,
                   argv="--no-boo".split()) == [False]

    assert run_cli(fettucini, (fns, ), argv="--boo --num 37".split()) == [
        37,
        True,
    ]
Exemplo n.º 13
0
def test_negate():
    assert run_cli(boo, (), argv=["--clap"]) == (False, True, None)
    with pytest.raises(SystemExit):
        run_cli(boo, (), argv=["--jeer"])

    assert run_cli(boo, (), argv=["--no-good"]) == (True, False, None)
    with pytest.raises(SystemExit):
        run_cli(boo, (), argv=["--good"])

    assert run_cli(boo, (), argv=["--potato"]) == (True, True, True)
    assert run_cli(boo, (), argv=["--famine"]) == (True, True, False)
Exemplo n.º 14
0
def test_config_subcommands(tmpdir):
    cfg1 = tmpdir.join("config1.json")
    cfg1.write(json.dumps({"flag": True}))

    assert (run_cli(
        {
            "thingy": thingy,
            "patriotism": patriotism
        },
        (),
        argv=f"patriotism @{cfg1.strpath}".split(),
        expand="@",
    ) == "wave")

    cfg2 = tmpdir.join("config2.json")
    with pytest.raises(SystemExit) as exc:
        run_cli(
            {
                "thingy": thingy,
                "patriotism": patriotism
            },
            (),
            argv=f"patriotism @{cfg2.strpath}".split(),
            expand="@",
        )
    assert exc.value.code == 2

    cfg3 = tmpdir.join("config1.json")
    cfg3.write(json.dumps({"#command": "patriotism", "flag": True}))
    assert (run_cli(
        {
            "thingy": thingy,
            "patriotism": patriotism
        },
        (),
        argv=f"@{cfg3.strpath}".split(),
        expand="@",
    ) == "wave")
Exemplo n.º 15
0
def test_cli():
    assert (run_cli(lager, ("a", "b"),
                    argv="--z=:foo".split(),
                    eval_env={"foo": "c"}) == "abc")
    assert run_cli(lager, (3, 2), argv="--z=:math:cos(0)".split()) == 6
    assert run_cli(stout, (3, ), argv="--z=3".split()) == (7, 8)
    assert run_cli(stout, (3, ), argv="--z=3 --w=10".split()) == (16, 8)
    assert run_cli(stout, (3, ), tag=tag.Boption, argv="--z=3".split()) == (
        7,
        8,
    )

    assert run_cli(thingy, (), argv=["--arg", "1"]) == "1"
    assert run_cli(thingy, (), argv=["--arg", "xyz"]) == "xyz"
    assert (run_cli(thingy, (),
                    eval_env={"foo": "bar"},
                    argv=["--arg", ":foo"]) == "bar")
    assert run_cli(thingy, (),
                   eval_env={"foo": [1, 2, 3]},
                   argv=["--arg", ":foo"]) == [1, 2, 3]

    assert (run_cli(thing, (),
                    eval_env={"foo": [1, 2, 3]},
                    argv=["--arg", ":foo"]) == ":foo")
Exemplo n.º 16
0
def main():
    mod, run, sess, thread = run_cli(cli)

    try:
        if run is not None:
            if thread:
                threads.run_in_thread(run, session=sess)
            else:
                run()
    except Exception as exc:
        if sess is not None:
            sess.blt["$$exc_info"] = sys.exc_info()
            sess.queue_result(exc, type="exception")
        else:
            raise
    finally:
        Evaluator(mod, vars(mod) if mod else {}, None, sess).loop()
Exemplo n.º 17
0
def test_config_file(tmpdir):
    cfg1 = tmpdir.join("config1.json")
    cfg1.write(json.dumps({"z": 3, "w": 10}))

    assert run_cli(stout, (3, ),
                   argv=[],
                   expand=ArgsExpander("@", default_file=cfg1)) == (16, 8)

    assert run_cli(
        stout,
        (3, ),
        argv=[f"@{cfg1.strpath}"],
        expand="@",
    ) == (
        16,
        8,
    )

    assert run_cli(
        stout,
        (3, ),
        argv=[f"&{cfg1.strpath}"],
        expand="@&",
    ) == (
        16,
        8,
    )

    cfg2 = tmpdir.join("config2.json")
    with pytest.raises(SystemExit) as exc:
        run_cli(stout, (3, ), argv=f"@{cfg2.strpath}".split(), expand="@")
    assert exc.value.code == 2

    cfg3 = tmpdir.join("config3.json")
    cfg3.write(json.dumps({"#include": cfg1.strpath, "w": 10}))
    assert run_cli(stout, (3, ),
                   argv=[],
                   expand=ArgsExpander("@", default_file=cfg3)) == (16, 8)

    assert run_cli(stout, (3, ), argv=[{"#include": cfg1.strpath}]) == (16, 8)
Exemplo n.º 18
0
def test_subcommands():
    assert (run_cli(
        {
            "thingy": thingy,
            "patriotism": patriotism
        },
        (),
        argv="thingy --arg xyz".split(),
    ) == "xyz")

    assert (run_cli(
        {
            "thingy": thingy,
            "patriotism": patriotism
        },
        (),
        argv="patriotism --flag".split(),
    ) == "wave")

    assert (run_cli(
        {
            "thingy": thingy,
            "patri": {
                "otism": patriotism
            }
        },
        (),
        argv="patri otism --flag".split(),
    ) == "wave")

    assert (run_cli(
        {
            "thingy": thingy,
            "patri": {
                "__doc__": "Vaccines certainly don't cause",
                "otism": patriotism,
            },
        },
        (),
        argv="patri otism --flag".split(),
    ) == "wave")

    with pytest.raises(SystemExit) as exc:
        run_cli(
            {
                "thingy": thingy,
                "patriotism": patriotism
            },
            (),
            argv="thingy --flag".split(),
        )
    assert exc.value.code == 2

    assert (run_cli(
        {
            "thingy": thingy,
            "patriotism": patriotism
        },
        (),
        argv=["patriotism", {
            "flag": True
        }],
    ) == "wave")

    # Test with no arguments
    with pytest.raises(SystemExit) as exc:
        assert (run_cli({
            "thingy": thingy,
            "patriotism": patriotism
        }, (),
                        argv="") == "xyz")
    assert exc.value.code == 1
Exemplo n.º 19
0
def entry_sy():
    run_cli(main)
Exemplo n.º 20
0
def test_no_env():
    with pytest.raises(Exception):
        run_cli(lager, ("a", "b"), argv="--z=:foo".split())
Exemplo n.º 21
0
def test_conflict():
    with pytest.raises(ConflictError):
        run_cli(stout, (3, ), argv="--z=3 --q=10".split())
Exemplo n.º 22
0
def test_required_argument():
    with pytest.raises(SystemExit):
        run_cli(lager, (3, 4), argv=[])
Exemplo n.º 23
0
def test_missing_global():
    def wish():
        return love

    with pytest.raises(NameError):
        run_cli(wish, (), argv=[])
Exemplo n.º 24
0
def test_types():
    assert run_cli(patriotism, (), argv=[]) == "wave"
    assert run_cli(patriotism, (), argv="-f".split()) == "wave"
    assert run_cli(patriotism, (), argv="--flag".split()) == "wave"
    assert run_cli(patriotism, (), argv="--no-flag".split()) == "don't wave"
    assert run_cli(patriotism, (),
                   argv="--flag -n 3".split()) == "wavewavewave"
    with pytest.raises(SystemExit) as exc:
        run_cli(patriotism, (), argv="--flag=1".split())
    assert exc.value.code == 2

    with pytest.raises(SystemExit) as exc:
        run_cli(patriotism, (), argv="--flag --times=3".split())
    assert exc.value.code == 2

    with pytest.raises(SystemExit) as exc:
        run_cli(patriotism, (), argv="-n ohno".split())
    assert exc.value.code == 2
Exemplo n.º 25
0
def test_multiple_positional_bad():
    with pytest.raises(Exception):
        run_cli(scattered_multipos, (), argv=["hello", "there"])
    with pytest.raises(Exception):
        run_cli(scattered_multipos2, (), argv=["hello", "there"])
Exemplo n.º 26
0
def test_multiple_positional():
    assert run_cli(multipos, (), argv=["hello", "there"]) == ("there", "hello")
Exemplo n.º 27
0
def test_bad_entry():
    with pytest.raises(TypeError):
        run_cli([patriotism], (), argv="--flag")
Exemplo n.º 28
0
def test_subcommands_as_class():
    assert (run_cli(
        farm,
        argv="build --material wood".split()) == "built farm out of wood")

    assert run_cli(farm, argv="duck honk --repeat 3".split()) == "honkhonkhonk"