Esempio n. 1
0
def test_module_baking():
    sh = cmdy(n=True)
    assert id(sh) != id(cmdy)
    assert id(sh.Cmdy) != id(cmdy.Cmdy)
    assert id(sh._CMDY_EVENT) != id(_CMDY_EVENT)
    assert id(sh._CMDY_BAKED_ARGS) != id(cmdy._CMDY_BAKED_ARGS)
    strcmd = sh.Cmdy('echo')(_=123).strcmd
    assert strcmd == 'echo -n 123'
    strcmd = cmdy.echo(123).strcmd
    assert strcmd == 'echo 123'

    # test event blocking
    cmdy.echo().p()
    assert cmdy._CMDY_EVENT.is_set()
    assert not sh._CMDY_EVENT.is_set()
    c = cmdy.echo()  # suppose to run but locked
    assert isinstance(c, CmdyHolding)
    c = sh.echo()  # ran
    assert isinstance(c, sh.CmdyResult)

    sh2 = sh(n=True, e=True)
    c = sh2.echo(_=1).h()
    assert c.strcmd == 'echo -n -e 1'

    assert (sh.CmdyExecNotFoundError == sh2.CmdyExecNotFoundError ==
            CmdyExecNotFoundError)

    with pytest.raises(CmdyExecNotFoundError):
        sh2.nonexisting()
Esempio n. 2
0
def _plotTimes(times, opts):
    rcode = _compose_rcode(times, opts)
    logger.info("Plotting results ...")
    cmdy.echo(rcode, _pipe=True) | cmdy.Rscript(  # pylint: disable=expression-not-assigned
        '-',
        _exe=opts.Rscript,
        _fg=True)
Esempio n. 3
0
def test_str_error():

    c = cmdy.echo(n=123).r(STDOUT) > DEVNULL
    with pytest.raises(CmdyActionError):
        c.str()

    with pytest.raises(CmdyActionError):
        cmdy.echo(n=123).str('xyz')
Esempio n. 4
0
def test_reprs():
    c = cmdy.echo(123).h()
    assert repr(c) == "<CmdyHolding: ['echo', '123']>"

    c = cmdy.echo(123)
    assert repr(c) == "<CmdyResult: ['echo', '123']>"

    c = cmdy.echo(123).a()
    assert repr(c) == "<CmdyAsyncResult: ['echo', '123']>"
Esempio n. 5
0
def test_redirect_stderr(tmp_path):
    tmpfile = tmp_path / 'test_redirect_stderr.txt'
    c = cmdy.echo('1234 1>&2', cmdy_shell=True).r(STDERR) > tmpfile
    assert tmpfile.read_text() == '1234\n'
    assert c.stderr is None

    tmpfile2 = tmp_path / 'test_redirect_stdout.txt'
    c = cmdy.echo('1234 1>&2', cmdy_shell=True).r(STDOUT) > tmpfile2
    assert tmpfile2.read_text() == ''
Esempio n. 6
0
def test_redirect_err_to_stdout():
    with pytest.raises(CmdyActionError):
        cmdy.echo().r(STDOUT) > STDERR

    c = cmdy.echo('-n 123 1>&2', cmdy_shell=True).r(STDERR) > STDOUT
    assert c == '123'
    c = cmdy.echo('-n 123 1>&2', cmdy_shell=True)
    assert c.str(STDERR) == '123'
    assert c.str(STDERR) == '123'
Esempio n. 7
0
def test_redirect_both(tmp_path):
    outfile = tmp_path / 'test_redirect_both_out.txt'
    c = cmdy.echo('-n 123 1>&2 && echo -n 456', cmdy_shell=True).r(
        STDERR, STDOUT) ^ STDOUT > outfile
    assert outfile.read_text() == '123456'

    c = cmdy.echo('-n 123 1>&2 && echo -n 456', cmdy_shell=True).r(
        STDOUT, STDERR) >> outfile > STDOUT
    assert outfile.read_text() == '123456123456'
    assert c.stdout is None
Esempio n. 8
0
def test_normal_run():

    ret = cmdy.echo(n='1')
    assert ret == '1'
    assert ret.rc == ret._rc == 0

    ret = cmdy.echo('1')
    assert ret.strip() == '1'
    assert ret.rc == 0
    ret._stderr = 12
    assert ret.stderr == 12
Esempio n. 9
0
def test_async_value_stderr():
    c = cmdy.echo('-n 123 1>&2', cmdy_shell=True).a()
    curio.run(c.wait())
    assert curio.run(c.astr(STDERR)) == '123'

    c = cmdy.echo('-n 123 1>&2', cmdy_encoding=None, cmdy_shell=True).a()
    assert curio.run(c.astr(STDERR)) == b'123'
    assert curio.run(c.astr(STDERR)) == b'123'  # trigger cache

    with pytest.raises(CmdyActionError):
        curio.run(c.astr(111))
Esempio n. 10
0
def test_mixed_actions_hold_then():
    # actions could be
    # hold
    # async_
    # redirect
    # fg
    # iter
    # pipe
    a = cmdy.echo(123).h().a()
    assert isinstance(a, CmdyHolding)

    r = cmdy.echo(123).h().r()
    assert isinstance(r, CmdyHolding)

    fg = cmdy.echo(123).h().fg()
    assert isinstance(fg, CmdyHolding)

    it = cmdy.echo(123).h().iter()
    assert isinstance(it, CmdyHolding)

    p = cmdy.echo(123).h().p()
    # release the lock
    _CMDY_EVENT.clear()
    assert isinstance(p, CmdyHolding)

    c = cmdy.echo(123).h()
    a = c.a()
    assert isinstance(a, CmdyHolding)
    assert isinstance(a.run(), CmdyAsyncResult)

    c = cmdy.echo(123).h()
    r = c.r() > DEVNULL
    assert isinstance(r, CmdyHolding)
    assert isinstance(r.run(), CmdyResult)

    c = cmdy.echo(123).h()
    r = c.fg()
    assert isinstance(r, CmdyHolding)
    assert isinstance(r.run(), CmdyResult)

    c = cmdy.echo(123).h()
    r = c.iter()
    assert isinstance(r, CmdyHolding)
    assert isinstance(r.run(), CmdyResult)

    c = cmdy.echo(123).h()
    r = c.p()
    assert isinstance(r, CmdyHolding)
    assert _CMDY_EVENT.is_set()
Esempio n. 11
0
def test_mixed_actions_fg_then():
    # actions could be
    # hold
    # async_
    # redirect
    # fg
    # iter
    # pipe

    # fg is final, cannot do anything else
    with pytest.raises(CmdyActionError):
        cmdy.echo().h().fg().r()

    with pytest.raises(CmdyActionError):
        cmdy.echo().h.fg.r
Esempio n. 12
0
def test_module_baking_reimport():
    from cmdy import Cmdy
    sh = cmdy(n=True)
    from cmdy import echo, Cmdy

    c = echo(123, _sub=True).abc().h
    assert c.strcmd == 'echo 123 abc'
Esempio n. 13
0
def test_redirect_failing_fetching(no_iter):
    c = cmdy.echo(n=123).r(STDOUT) > DEVNULL
    assert c.holding.stdout != curio.subprocess.PIPE
    assert c.stdout is None

    c = cmdy.bash(c='echo 123 1>&2').r(STDERR) > DEVNULL
    assert c.stderr is None
Esempio n. 14
0
def test_redirect_rspt(tmp_path):
    outfile = tmp_path / 'test_redirect_respectively_out.txt'
    errfile = tmp_path / 'test_redirect_respectively_err.txt'
    c = cmdy.echo('-n 123 1>&2 && echo -n 456', cmdy_shell=True).r(
        STDOUT, STDERR) ^ outfile > errfile
    assert outfile.read_text() == '456'
    assert errfile.read_text() == '123'

    with open(outfile, 'a') as fout, open(errfile, 'w') as ferr:
        c = cmdy.echo('-n 123 1>&2 && echo -n 456', cmdy_shell=True).r(
            STDOUT, STDERR) ^ fout > ferr
        # fh should not be closed
        fout.write('78')

    assert outfile.read_text() == '45645678'
    assert errfile.read_text() == '123'
Esempio n. 15
0
def test_async_value():
    c = cmdy.echo(n=123).async_()

    async def get_value():
        return (await c.astr(), await c.aint(), await c.afloat())

    assert curio.run(get_value()) == ('123', 123, 123.0)
Esempio n. 16
0
def test_hold(tmp_path):

    tmpfile = tmp_path / 'test_redirect.txt'
    c = cmdy.echo(n='1234').h()
    cmd = c.r() > tmpfile
    cmd.run()
    assert tmpfile.read_text() == '1234'
Esempio n. 17
0
def test_iter():
    c = cmdy.echo("1\n2\n3").iter()
    assert next(c) == '1\n'
    assert next(c) == '2\n'
    assert next(c) == '3\n'

    assert c.rc == 0

    with pytest.raises(StopIteration):
        next(c)

    c = cmdy.echo("1\n2\n3").iter()
    ret = []
    for line in c:
        ret.append(line.strip())
    assert ret == ['1', '2', '3']
Esempio n. 18
0
def test_redirect_then_iter(tmp_path):

    #c = cmdy.echo('-n 123 1>&2', cmdy_shell=True).r(STDERR).h() > STDOUT
    # the right way:
    c = cmdy.echo('-n 123 1>&2', cmdy_shell=True).h()
    r = c.r(STDERR) > STDOUT
    assert list(r.run().iter()) == ['123']

    tmpfile = tmp_path / 'test_redirect_then_iter.txt'
    c = cmdy.echo(n=123).h()
    c.stdout = tmpfile.open('w')
    c.stderr = tmpfile.open('w')
    r = c.run()
    r.data.iter = Diot(which=STDOUT)
    assert r.stdout is None
    assert r.stderr is None
Esempio n. 19
0
def test_iter_from_redirect():

    c = cmdy.echo('-n 123 1>&2', cmdy_shell=True).r(STDERR) > STDOUT
    ret = []
    for line in c.iter():
        ret.append(line.strip())
    assert ret == ['123']
Esempio n. 20
0
def test_redirect_prop(tmp_path):
    tmpfile = tmp_path / 'test_redirect_prop.txt'
    c = cmdy.echo(n='1234').r > tmpfile
    assert tmpfile.read_text() == '1234'
    assert c.holding.stdout.closed
    assert c.stdout is None
    assert c.stderr == ''
Esempio n. 21
0
def test_bake_revert():

    echo = cmdy.echo.bake(n=True)
    # c = echo().h
    # assert c.strcmd == 'echo -n'
    c = echo(n=False).h
    assert c.strcmd == 'echo'
Esempio n. 22
0
def test_mixed_actions_redir_then_fg():
    with pytest.warns(UserWarning) as w:
        c = cmdy.echo(123).r.fg > DEVNULL
    assert isinstance(c, CmdyResult)
    assert c.stdout == ''
    assert str(w.pop(
        UserWarning).message) == 'Previous redirected pipe will be ignored.'
Esempio n. 23
0
def dict_to_cli_args(dic: Mapping[str, Any]) -> str:
    """Convert a python dict to a string of CLI arguments

    Examples:
        >>> {"a": 1, "ab": 2}
        >>> "-a 1 --ab 2"
    """
    return cmdy.echo(dic).stdout.strip()
Esempio n. 24
0
def test_mixed_actions_async_then_run():
    c = cmdy.echo('1 1>&2', cmdy_shell=True).h()
    r = c.a().run()
    assert isinstance(r, CmdyAsyncResult)

    a = cmdy.echo().a().fg()
    assert isinstance(a, CmdyAsyncResult)

    it = cmdy.echo().a().iter()
    assert isinstance(it, CmdyAsyncResult)

    it = cmdy.echo().h().a().run().iter()
    assert isinstance(it, CmdyAsyncResult)

    c = cmdy.echo().a().p()
    _CMDY_EVENT.clear()
    assert isinstance(c, CmdyHolding)
Esempio n. 25
0
def test_iter_stderr_dump_stdout():
    c = cmdy.echo('-n 1 1>&2', cmdy_shell=True).iter(STDERR)
    assert list(c) == ['1']
    assert c.stdout == ''

    c._stderr = 1
    with pytest.raises(TypeError):
        next(c)
Esempio n. 26
0
def test_iter_stdout_dump_stderr():
    c = cmdy.echo(n=1).iter()
    assert list(c) == ['1']
    assert c.stderr == ''

    c._stdout = 1
    with pytest.raises(TypeError):
        next(c)
Esempio n. 27
0
def test_reset():
    c = cmdy.echo(n=123).h().r(STDOUT) > DEVNULL
    c = c.run(True)
    assert isinstance(c, CmdyResult)
    assert c.stdout is None  # redirected

    reuse = c.holding.reset().run(True)
    assert isinstance(reuse, CmdyResult)
    assert reuse.stdout == '123'
Esempio n. 28
0
def test_bake():

    echo = cmdy.echo.bake(n=True)
    ret = echo(_='1')
    assert ret.cmd == ['echo', '-n', '1']
    assert ret == '1'

    with pytest.raises(TypeError):
        cmdy.echo.bake('1')
Esempio n. 29
0
def test_raise_from_redirect():
    #c = cmdy.echo('1 && exit 1', cmdy_shell=True).r(STDOUT).h() > DEVNULL
    # the right way:
    c = cmdy.echo('1 && exit 1', cmdy_shell=True).h()

    c.r(STDOUT) > DEVNULL
    with pytest.raises(CmdyReturnCodeError) as ex:
        c.run()
    assert '[STDOUT] <NA / ITERATED / REDIRECTED>' in str(ex.value)
Esempio n. 30
0
def test_piped_cmds():
    c = cmdy.echo(123).p() | cmdy.cat().r() ^ DEVNULL
    assert c.piped_cmds == [['echo', '123'], ['cat']]
    assert c.piped_strcmds == ['echo 123', 'cat']

    # works on holding objects too
    c = cmdy.echo(123).p() | cmdy.cat().h()
    assert c.piped_strcmds == ['echo 123', 'cat']
    assert c.piped_cmds == [['echo', '123'], ['cat']]

    piped_cmds = cmdy.echo(123).piped_cmds
    assert piped_cmds == [['echo', '123']]
    piped_strcmds = cmdy.echo(123).piped_strcmds
    assert piped_strcmds == ['echo 123']
    _CMDY_EVENT.clear()
    c = cmdy.echo(123)
    assert isinstance(c, CmdyResult)
    piped_cmds = c.piped_cmds
    assert piped_cmds == [['echo', '123']]