示例#1
0
def test_command_error_captures_output() -> None:
    script = "\n".join([
        "import sys",
        "print('stdout message')",
        "print('stderr message', file=sys.stderr)",
        "sys.exit(42)",
    ])
    with pytest.raises(CommandError) as err:
        Cmd("python3")("-c", script).out()

    traceback.print_exception(err.type, err.value, err.tb)
    exc = err.value
    assert exc.status.code == 42
    assert exc.command.startswith("python3 -c '")
    assert exc.stdout == "stdout message\n"
    assert exc.stderr == "stderr message\n"
示例#2
0
def test_run_path_like() -> None:
    Cmd("echo")(Path(".")).run()
示例#3
0
def test_run() -> None:
    Cmd("true").run()
示例#4
0
def test_run_raises() -> None:
    with pytest.raises(CommandError):
        Cmd("false").run()
示例#5
0
def test_output_bytes() -> None:
    out = Cmd("echo", "12345").output_bytes()
    assert out == b"12345\n"
示例#6
0
def test_exec() -> None:
    script = "\n".join(
        ["import cmdlib", "cmdlib.Cmd('echo', 'arg1', 'arg2').exec()"])
    output = Cmd("python3")("-c", script).out()
    assert output == "arg1 arg2\n"
示例#7
0
def test_json() -> None:
    out = Cmd("echo", '{"a":1,"b":2,"c":null}').json()
    assert out == dict(a=1, b=2, c=None)
示例#8
0
def test_output() -> None:
    out = Cmd("echo", "some output").output()
    assert out == "some output\n"
示例#9
0
def test_cmd_args() -> None:
    ls = Cmd("ls", "--recursive", "--size")
    assert ls.args == ["ls", "--recursive", "--size"]
示例#10
0
def test_cmd_kwargs() -> None:
    ls = Cmd("ls", "dir1", color="never")
    assert ls.args == ["ls", "dir1", "--color=never"]
示例#11
0
def test_kwargs_bool_to_option_flag() -> None:
    cmd = Cmd("cp")(verbose=True)
    assert cmd.args == ["cp", "--verbose"]
示例#12
0
def test_kwargs_options() -> None:
    cmd = Cmd("cp")(target_directory="..")
    assert cmd.args == ["cp", "--target-directory=.."]
示例#13
0
def test_args_chaining_does_not_mutate() -> None:
    cp = Cmd("cp")
    cp_verbose = cp("--verbose")  # This should not mutate `cp`.
    assert cp.args == ["cp"]
    assert cp_verbose.args == ["cp", "--verbose"]
示例#14
0
def test_command_str_path_like() -> None:
    cmd = Cmd("ls")(Path("."))
    assert str(cmd) == "ls ."
示例#15
0
def test_command_str_quoted() -> None:
    cmd = Cmd("echo")("some arg")
    assert str(cmd) == r"echo 'some arg'"