Exemplo n.º 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"
Exemplo n.º 2
0
def test_run_path_like() -> None:
    Cmd("echo")(Path(".")).run()
Exemplo n.º 3
0
def test_run() -> None:
    Cmd("true").run()
Exemplo n.º 4
0
def test_run_raises() -> None:
    with pytest.raises(CommandError):
        Cmd("false").run()
Exemplo n.º 5
0
def test_output_bytes() -> None:
    out = Cmd("echo", "12345").output_bytes()
    assert out == b"12345\n"
Exemplo 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"
Exemplo 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)
Exemplo n.º 8
0
def test_output() -> None:
    out = Cmd("echo", "some output").output()
    assert out == "some output\n"
Exemplo n.º 9
0
def test_cmd_args() -> None:
    ls = Cmd("ls", "--recursive", "--size")
    assert ls.args == ["ls", "--recursive", "--size"]
Exemplo n.º 10
0
def test_cmd_kwargs() -> None:
    ls = Cmd("ls", "dir1", color="never")
    assert ls.args == ["ls", "dir1", "--color=never"]
Exemplo n.º 11
0
def test_kwargs_bool_to_option_flag() -> None:
    cmd = Cmd("cp")(verbose=True)
    assert cmd.args == ["cp", "--verbose"]
Exemplo n.º 12
0
def test_kwargs_options() -> None:
    cmd = Cmd("cp")(target_directory="..")
    assert cmd.args == ["cp", "--target-directory=.."]
Exemplo n.º 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"]
Exemplo n.º 14
0
def test_command_str_path_like() -> None:
    cmd = Cmd("ls")(Path("."))
    assert str(cmd) == "ls ."
Exemplo n.º 15
0
def test_command_str_quoted() -> None:
    cmd = Cmd("echo")("some arg")
    assert str(cmd) == r"echo 'some arg'"