Esempio n. 1
0
def test_break():
    up = 0
    down = 3

    def count_down(*args, env=None, stdin=None, stdout=None, stderr=None):
        nonlocal down
        stdout.write(b"DOWN\n")
        if down <= 0:
            return 0
        down -= 1
        return 1

    def count_up(*args, env=None, stdin=None, stdout=None, stderr=None):
        nonlocal up
        stdout.write(b"UP\n")
        up += 1
        return 0

    cmd = While(
        Command([Word([Id("up")])]),
        If([(Command([Word([Id("down")])]), Command([Word([Id("break")])]))]))
    env = make_env()
    env.builtins.update({"up": count_up, "down": count_down})

    out = io.BytesIO()
    assert cmd.execute(env, output=out) == 0
    assert up == 4
    assert out.getvalue().decode(
        "utf-8") == "UP\nDOWN\nUP\nDOWN\nUP\nDOWN\nUP\nDOWN\n"
Esempio n. 2
0
def test_more_dots():
    word = Word([ConstantString("."), STAR])
    env = make_env()

    with make_dirs("a", "b", ".c", ".d", TOUCH, "e", ".f") as d:
        with cwd(d):
            ans = expand(env, word, ".")

    assert ans == [".c", ".d", ".f"]
Esempio n. 3
0
def test_word_expand_rec():
    word = Word([ConstantString("a/"), STARSTAR, ConstantString("/e")])
    env = make_env()

    with make_dirs("a/b*/cfood/e", "a/b*/cd/e/f") as d:
        with cwd(d):
            ans = expand(env, word, ".")

    assert ans == ["a/b*/cd/e", "a/b*/cfood/e"]
Esempio n. 4
0
def test_no_dots():
    word = Word([STAR, ConstantString("/..")])
    env = make_env()

    with make_dirs("a", "b", ".c", ".d", TOUCH, "e", ".f") as d:
        with cwd(d):
            ans = expand(env, word, ".")

    assert ans == ["a/..", "b/.."]
Esempio n. 5
0
def test_files_too():
    word = Word([STAR])
    env = make_env()

    with make_dirs("a", "b", ".c", ".d", TOUCH, "e", ".f") as d:
        with cwd(d):
            ans = expand(env, word, ".")

    assert ans == ["a", "b", "e"]
Esempio n. 6
0
def test_run_a_command():
    cmd = Command([
        Word([Id("echo")]),
        Word([STAR]),
    ])
    env = make_env()

    with make_dirs("a", "b", "c", "d", "e") as d:
        with cwd(d):
            assert cmd.evaluate(env) == "a b c d e"
Esempio n. 7
0
def test_fixed_pieces():
    cmd = Command([
        Word([Id("echo")]),
        Word([STAR, ConstantString("/..")]),
    ])
    env = make_env()

    with make_dirs("a", "b", "c", "d", TOUCH, "e", "f", "g") as d:
        with cwd(d):
            assert cmd.evaluate(env) == "a/.. b/.. c/.. d/.."
Esempio n. 8
0
def test_invoke_a_function():
    env = make_env()
    env.functions["f"] = Function(
        Id("f"),
        CommandSequence([Command([Word([Id("echo")]),
                                  Word([VarRef("1")])])]))

    cmd = CommandSequence([
        Command([Word([Id("f")]),
                 Word([ConstantString("hello world")])]),
    ])

    assert cmd.evaluate(env) == "hello world"
Esempio n. 9
0
def test_return():
    env = make_env()
    env['x'] = "30"
    env.functions["f"] = Function(
        Id("f"),
        CommandSequence([
            Command([Word([Id("return")]),
                     Word([VarRef("x")])]),
        ]))

    cmd = CommandSequence([
        Command([Word([Id("f")]),
                 Word([ConstantString("hello world")])]),
    ])

    assert cmd.evaluate(env) == ""
    assert env['?'] == "30"
Esempio n. 10
0
def test_locals():
    env = make_env()
    env['x'] = "1"
    env.functions["f"] = Function(
        Id("f"),
        CommandSequence([
            Command([
                Word([Id("local")]),
                Word([Id("x"), Token("="), VarRef("1")])
            ]),
            Command([Word([Id("echo")]),
                     Word([VarRef("x")])]),
        ]))

    cmd = CommandSequence([
        Command([Word([Id("f")]),
                 Word([ConstantString("hello world")])]),
    ])

    assert cmd.evaluate(env) == "hello world"
    assert env['x'] == "1"
Esempio n. 11
0
def test_variables(text, expected):
    env = make_env()
    env.update({"foo": "FOO", "bar": "BAR"})
    cmd = command.parse(text)
    words = [item.evaluate(env) for item in cmd]
    assert words == expected
Esempio n. 12
0
def test_basic(pattern, value, expected):
    env = make_env()
    env['a'] = value
    result = pattern.evaluate(env)
    assert result == expected
Esempio n. 13
0
def test_expr(text, expected):
    env = make_env()
    env.update({"a": 1, "b": 2, "cd": 3})

    assert expr.parse(text)(env) == expected
Esempio n. 14
0
def test_basic(cmd, variable, expected):
    env = make_env()
    env["a"] = variable
    assert cmd.evaluate(env) == expected