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
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", ]
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
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"
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)
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)
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], )
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"]])
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
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=[]) == []
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)
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, ]
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)
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")
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")
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()
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)
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
def entry_sy(): run_cli(main)
def test_no_env(): with pytest.raises(Exception): run_cli(lager, ("a", "b"), argv="--z=:foo".split())
def test_conflict(): with pytest.raises(ConflictError): run_cli(stout, (3, ), argv="--z=3 --q=10".split())
def test_required_argument(): with pytest.raises(SystemExit): run_cli(lager, (3, 4), argv=[])
def test_missing_global(): def wish(): return love with pytest.raises(NameError): run_cli(wish, (), argv=[])
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
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"])
def test_multiple_positional(): assert run_cli(multipos, (), argv=["hello", "there"]) == ("there", "hello")
def test_bad_entry(): with pytest.raises(TypeError): run_cli([patriotism], (), argv="--flag")
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"