def test_ls_env():
    with capture_output() as out:
        try:
            main(["argv0", "--ls_env"])
        except SystemExit:
            pass  # Expected behaviour is to call sys.exit().
    assert "llvm-" in out.stdout
Beispiel #2
0
def io_check(input, output, rnd_seed=100):
    """Run the shell with the given input and check the output matches the
    output regex"""
    seed(rnd_seed)
    old_stdin = sys.stdin
    try:
        with capture_output() as out:
            try:
                sys.stdin = StringIO(input)
                main(["argv0", "--env=llvm-v0"])
            except SystemExit:
                pass  # Expected behaviour is to call sys.exit().
        print(out.stdout)

        pattern = (
            r"""Initialized environment in [0-9.mu]*s
Welcome to the CompilerGym Shell!
---------------------------------
Type help or \? for more information.
The 'tutorial' command will give a step by step guide.

"""
            + output
        )
        if not re.match(pattern, out.stdout):
            pytest.fail(
                f"Failed to match regex:\n{pattern}\n"
                + ("*" * 80)
                + f"\nUsing output:\n{out.stdout}\n"
            )

    finally:
        sys.stdin = old_stdin
def io_check(input, output, rnd_seed=100):
    """Run the shell with the given input and check the output matches the
    output regex"""
    seed(rnd_seed)
    old_stdin = sys.stdin
    try:
        with capture_output() as out:
            try:
                sys.stdin = StringIO(input)
                main(["argv0", "--env=llvm-v0"])
            except SystemExit:
                pass  # Expected behaviour is to call sys.exit().
        print(out.stdout)

        pattern = (r"""Initialized environment in [0-9.mu]*s
Welcome to the CompilerGym Shell!
---------------------------------
Type help or \? for more information.
The 'tutorial' command will give a step by step guide.

""" + output + r"""

compiler_gym:[a-zA-Z0-9/-]+> Exiting
""")

        # Strip ANSI escape sequences from output that are used for formatting.
        ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
        stdout = ansi_escape.sub("", out.stdout)
        # Strip trailing whitespace from output.
        stdout = "\n".join(n.rstrip() for n in stdout.split("\n"))

        if not re.match(pattern, stdout):
            # Create a diff between the expected regex and the actual output.
            # Diffing a regex will create a lot of false-positives, since any
            # character groups or other expressions will be different, but can
            # still be helful for tracking down the important differences.
            diff = unified_diff(
                pattern.split("\n"),
                stdout.split("\n"),
                fromfile="Expected output regex",
                tofile="Actual output",
            )
            pytest.fail("\n".join(diff))

    finally:
        sys.stdin = old_stdin
def test_missing_required_flag():
    FLAGS.unparse_flags()
    with pytest.raises(app.UsageError) as ctx:
        main(["argv0"])
    assert str(ctx.value) == "--env must be set"
def test_unrecognized_flags():
    FLAGS.unparse_flags()
    with pytest.raises(app.UsageError) as ctx:
        main(["argv0", "unknown-option"])
    assert str(
        ctx.value) == "Unknown command line arguments: ['unknown-option']"
def test_missing_required_flag():
    with pytest.raises(app.UsageError) as ctx:
        main(["argv0"])
    assert str(ctx.value) == "Neither --env or --local_service_binary is set"