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