Esempio n. 1
0
def test_bin_hy_circular_macro_require():
    """Confirm that macros can require themselves during expansion and when
    run from the command line."""

    # First, with no bytecode
    test_file = "tests/resources/bin/circular_macro_require.hy"
    rm(cache_from_source(test_file))
    assert not os.path.exists(cache_from_source(test_file))
    output, _ = run_cmd("hy {}".format(test_file))
    assert "42" == output.strip()

    # Now, with bytecode
    assert os.path.exists(cache_from_source(test_file))
    output, _ = run_cmd("hy {}".format(test_file))
    assert "42" == output.strip()
Esempio n. 2
0
def test_bin_hyc():
    _, err = run_cmd("hyc", expect=0)
    assert err == ''

    _, err = run_cmd("hyc -", expect=0)
    assert err == ''

    output, _ = run_cmd("hyc -h")
    assert "usage" in output

    path = "tests/resources/argparse_ex.hy"
    output, _ = run_cmd("hyc " + path)
    assert "Compiling" in output
    assert os.path.exists(cache_from_source(path))
    rm(cache_from_source(path))
Esempio n. 3
0
def test_bin_hy_circular_macro_require():
    """Confirm that macros can require themselves during expansion and when
    run from the command line."""

    # First, with no bytecode
    test_file = "tests/resources/bin/circular_macro_require.hy"
    rm(cache_from_source(test_file))
    assert not os.path.exists(cache_from_source(test_file))
    output, _ = run_cmd("hy {}".format(test_file))
    assert "42" == output.strip()

    # Now, with bytecode
    assert os.path.exists(cache_from_source(test_file))
    output, _ = run_cmd("hy {}".format(test_file))
    assert "42" == output.strip()
Esempio n. 4
0
def test_bin_hyc():
    _, err = run_cmd("hyc", expect=0)
    assert err == ''

    _, err = run_cmd("hyc -", expect=0)
    assert err == ''

    output, _ = run_cmd("hyc -h")
    assert "usage" in output

    path = "tests/resources/argparse_ex.hy"
    output, _ = run_cmd("hyc " + path)
    assert "Compiling" in output
    assert os.path.exists(cache_from_source(path))
    rm(cache_from_source(path))
Esempio n. 5
0
def test_bin_hy_macro_require():
    """Confirm that a `require` will load macros into the non-module namespace
    (i.e. `exec(code, locals)`) used by `runpy.run_path`.
    In other words, this confirms that the AST generated for a `require` will
    load macros into the unnamed namespace its run in."""

    # First, with no bytecode
    test_file = "tests/resources/bin/require_and_eval.hy"
    rm(cache_from_source(test_file))
    assert not os.path.exists(cache_from_source(test_file))
    output, _ = run_cmd("hy {}".format(test_file))
    assert "abc" == output.strip()

    # Now, with bytecode
    assert os.path.exists(cache_from_source(test_file))
    output, _ = run_cmd("hy {}".format(test_file))
    assert "abc" == output.strip()
Esempio n. 6
0
def test_bin_hy_macro_require():
    """Confirm that a `require` will load macros into the non-module namespace
    (i.e. `exec(code, locals)`) used by `runpy.run_path`.
    In other words, this confirms that the AST generated for a `require` will
    load macros into the unnamed namespace its run in."""

    # First, with no bytecode
    test_file = "tests/resources/bin/require_and_eval.hy"
    rm(cache_from_source(test_file))
    assert not os.path.exists(cache_from_source(test_file))
    output, _ = run_cmd("hy {}".format(test_file))
    assert "abc" == output.strip()

    # Now, with bytecode
    assert os.path.exists(cache_from_source(test_file))
    output, _ = run_cmd("hy {}".format(test_file))
    assert "abc" == output.strip()
Esempio n. 7
0
def test_bin_hy_byte_compile(scenario, cmd_fmt):

    modname = "tests.resources.bin.bytecompile"
    fpath = modname.replace(".", "/") + ".hy"

    if scenario == 'prevent_by_option':
        cmd_fmt.insert(1, '-B')

    cmd = ' '.join(cmd_fmt).format(**locals())

    rm(cache_from_source(fpath))

    if scenario == "prevent_by_force":
        # Keep Hy from being able to byte-compile the module by
        # creating a directory at the target location.
        os.mkdir(cache_from_source(fpath))

    # Whether or not we can byte-compile the module, we should be able
    # to run it.
    output, _ = run_cmd(cmd, dontwritebytecode=(scenario == "prevent_by_env"))
    assert "Hello from macro" in output
    assert "The macro returned: boink" in output

    if scenario == "normal":
        # That should've byte-compiled the module.
        assert os.path.exists(cache_from_source(fpath))
    elif scenario == "prevent_by_env" or scenario == "prevent_by_option":
        # No byte-compiled version should've been created.
        assert not os.path.exists(cache_from_source(fpath))

    # When we run the same command again, and we've byte-compiled the
    # module, the byte-compiled version should be run instead of the
    # source, in which case the macro shouldn't be run.
    output, _ = run_cmd(cmd)
    assert ("Hello from macro" in output) ^ (scenario == "normal")
    assert "The macro returned: boink" in output
Esempio n. 8
0
def test_bin_hy_byte_compile(scenario, cmd_fmt):

    modname = "tests.resources.bin.bytecompile"
    fpath = modname.replace(".", "/") + ".hy"

    if scenario == 'prevent_by_option':
        cmd_fmt.insert(1, '-B')

    cmd = ' '.join(cmd_fmt).format(**locals())

    rm(cache_from_source(fpath))

    if scenario == "prevent_by_force":
        # Keep Hy from being able to byte-compile the module by
        # creating a directory at the target location.
        os.mkdir(cache_from_source(fpath))

    # Whether or not we can byte-compile the module, we should be able
    # to run it.
    output, _ = run_cmd(cmd, dontwritebytecode=(scenario == "prevent_by_env"))
    assert "Hello from macro" in output
    assert "The macro returned: boink" in output

    if scenario == "normal":
        # That should've byte-compiled the module.
        assert os.path.exists(cache_from_source(fpath))
    elif scenario == "prevent_by_env" or scenario == "prevent_by_option":
        # No byte-compiled version should've been created.
        assert not os.path.exists(cache_from_source(fpath))

    # When we run the same command again, and we've byte-compiled the
    # module, the byte-compiled version should be run instead of the
    # source, in which case the macro shouldn't be run.
    output, _ = run_cmd(cmd)
    assert ("Hello from macro" in output) ^ (scenario == "normal")
    assert "The macro returned: boink" in output
Esempio n. 9
0
def test_import_autocompiles():
    "Test that (import) byte-compiles the module."

    with tempfile.NamedTemporaryFile(suffix='.hy', delete=True) as f:
        f.write(b'(defn pyctest [s] (+ "X" s "Y"))')
        f.flush()

        pyc_path = cache_from_source(f.name)

        try:
            os.remove(pyc_path)
        except (IOError, OSError):
            pass

        test_loader = HyLoader("mymodule", f.name).load_module()

        assert hasattr(test_loader, 'pyctest')
        assert os.path.exists(pyc_path)

        os.remove(pyc_path)
Esempio n. 10
0
def test_import_autocompiles():
    "Test that (import) byte-compiles the module."

    with tempfile.NamedTemporaryFile(suffix='.hy', delete=True) as f:
        f.write(b'(defn pyctest [s] (+ "X" s "Y"))')
        f.flush()

        pyc_path = cache_from_source(f.name)

        try:
            os.remove(pyc_path)
        except (IOError, OSError):
            pass

        test_loader = HyLoader("mymodule", f.name).load_module()

        assert hasattr(test_loader, 'pyctest')
        assert os.path.exists(pyc_path)

        os.remove(pyc_path)
Esempio n. 11
0
 def unlink(filename):
     os.unlink(source)
     bytecode = cache_from_source(source)
     if os.path.isfile(bytecode):
         os.unlink(bytecode)
Esempio n. 12
0
 def unlink(filename):
     os.unlink(source)
     bytecode = cache_from_source(source)
     if os.path.isfile(bytecode):
         os.unlink(bytecode)