2013-05-08 13:04:35 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
# Copyright (c) 2013 Julien Danjou <julien@danjou.info>
|
|
|
|
# Copyright (c) 2013 Will Kahn-Greene <willg@bluesock.org>
|
2014-03-14 14:30:03 +01:00
|
|
|
# Copyright (c) 2014 Bob Tolbert <bob@tolbert.org>
|
2013-05-08 13:04:35 +02:00
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
# copy of this software and associated documentation files (the "Software"),
|
|
|
|
# to deal in the Software without restriction, including without limitation
|
|
|
|
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
# and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
# Software is furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
# DEALINGS IN THE SOFTWARE.
|
|
|
|
import os
|
2013-04-20 22:27:10 +02:00
|
|
|
import subprocess
|
2017-03-24 17:03:12 +01:00
|
|
|
import re
|
2014-05-01 22:31:45 +02:00
|
|
|
from hy._compat import PY3
|
2013-04-20 22:27:10 +02:00
|
|
|
|
|
|
|
|
2014-11-06 04:01:10 +01:00
|
|
|
hy_dir = os.environ.get('HY_DIR', '')
|
|
|
|
|
|
|
|
|
2017-03-24 17:03:12 +01:00
|
|
|
def hr(s=""):
|
|
|
|
return "hy --repl-output-fn=hy.contrib.hy-repr.hy-repr " + s
|
|
|
|
|
|
|
|
|
2017-03-17 17:31:54 +01:00
|
|
|
def run_cmd(cmd, stdin_data=None, expect=0):
|
2014-11-06 04:01:10 +01:00
|
|
|
p = subprocess.Popen(os.path.join(hy_dir, cmd),
|
2013-06-29 23:56:58 +02:00
|
|
|
stdin=subprocess.PIPE,
|
2013-05-08 13:04:35 +02:00
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
2013-04-20 22:27:10 +02:00
|
|
|
shell=True)
|
2013-06-29 23:56:58 +02:00
|
|
|
if stdin_data is not None:
|
|
|
|
p.stdin.write(stdin_data.encode('ASCII'))
|
|
|
|
p.stdin.flush()
|
|
|
|
p.stdin.close()
|
2013-05-08 13:04:35 +02:00
|
|
|
# Read stdout and stderr otherwise if the PIPE buffer is full, we might
|
|
|
|
# wait for ever…
|
2017-03-17 17:31:54 +01:00
|
|
|
stdout = ""
|
|
|
|
stderr = ""
|
2013-05-08 13:04:35 +02:00
|
|
|
while p.poll() is None:
|
2013-12-10 17:59:06 +01:00
|
|
|
stdout += p.stdout.read().decode('utf-8')
|
|
|
|
stderr += p.stderr.read().decode('utf-8')
|
2017-03-17 17:31:54 +01:00
|
|
|
assert p.returncode == expect
|
|
|
|
return stdout, stderr
|
2013-04-24 15:21:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_bin_hy():
|
2017-03-17 17:31:54 +01:00
|
|
|
run_cmd("hy", "")
|
2013-04-24 15:21:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_bin_hy_stdin():
|
2017-03-17 17:31:54 +01:00
|
|
|
output, _ = run_cmd("hy", '(koan)')
|
|
|
|
assert "monk" in output
|
2013-04-24 15:21:42 +02:00
|
|
|
|
2017-03-24 17:03:12 +01:00
|
|
|
output, _ = run_cmd("hy --spy", '(koan)')
|
|
|
|
assert "monk" in output
|
|
|
|
assert "\\n Ummon" in output
|
|
|
|
|
|
|
|
# --spy should work even when an exception is thrown
|
|
|
|
output, _ = run_cmd("hy --spy", '(foof)')
|
|
|
|
assert "foof()" in output
|
|
|
|
|
|
|
|
|
|
|
|
def test_bin_hy_stdin_multiline():
|
|
|
|
output, _ = run_cmd("hy", '(+ "a" "b"\n"c" "d")')
|
|
|
|
assert "'abcd'" in output
|
|
|
|
|
|
|
|
|
|
|
|
def test_bin_hy_stdin_comments():
|
|
|
|
_, err_empty = run_cmd("hy", '')
|
|
|
|
|
|
|
|
output, err = run_cmd("hy", '(+ "a" "b") ; "c"')
|
|
|
|
assert "'ab'" in output
|
|
|
|
assert err == err_empty
|
|
|
|
|
|
|
|
_, err = run_cmd("hy", '; 1')
|
|
|
|
assert err == err_empty
|
|
|
|
|
|
|
|
|
|
|
|
def test_bin_hy_stdin_assignment():
|
|
|
|
# If the last form is an assignment, don't print the value.
|
|
|
|
|
|
|
|
output, _ = run_cmd("hy", '(setv x (+ "A" "Z"))')
|
|
|
|
assert "AZ" not in output
|
|
|
|
|
|
|
|
output, _ = run_cmd("hy", '(setv x (+ "A" "Z")) (+ "B" "Y")')
|
|
|
|
assert "AZ" not in output
|
|
|
|
assert "BY" in output
|
|
|
|
|
|
|
|
output, _ = run_cmd("hy", '(+ "B" "Y") (setv x (+ "A" "Z"))')
|
|
|
|
assert "AZ" not in output
|
|
|
|
assert "BY" not in output
|
|
|
|
|
|
|
|
|
2017-03-24 17:03:55 +01:00
|
|
|
def test_bin_hy_stdin_as_arrow():
|
|
|
|
# https://github.com/hylang/hy/issues/1255
|
|
|
|
output, _ = run_cmd("hy", "(as-> 0 it (inc it) (inc it))")
|
|
|
|
assert re.match(r"=>\s+2L?\s+=>", output)
|
|
|
|
|
|
|
|
|
2017-03-24 19:09:30 +01:00
|
|
|
def test_bin_hy_stdin_error_underline_alignment():
|
|
|
|
_, err = run_cmd("hy", "(defmacro mabcdefghi [x] x)\n(mabcdefghi)")
|
|
|
|
assert "\n (mabcdefghi)\n ^----------^" in err
|
|
|
|
|
|
|
|
|
2017-03-31 01:10:34 +02:00
|
|
|
def test_bin_hy_stdin_except_do():
|
|
|
|
# https://github.com/hylang/hy/issues/533
|
|
|
|
|
|
|
|
output, _ = run_cmd("hy", '(try (/ 1 0) (except [ZeroDivisionError] "hello"))') # noqa
|
|
|
|
assert "hello" in output
|
|
|
|
|
|
|
|
output, _ = run_cmd("hy", '(try (/ 1 0) (except [ZeroDivisionError] "aaa" "bbb" "ccc"))') # noqa
|
|
|
|
assert "aaa" not in output
|
|
|
|
assert "bbb" not in output
|
|
|
|
assert "ccc" in output
|
|
|
|
|
|
|
|
output, _ = run_cmd("hy", '(if True (do "xxx" "yyy" "zzz"))')
|
|
|
|
assert "xxx" not in output
|
|
|
|
assert "yyy" not in output
|
|
|
|
assert "zzz" in output
|
|
|
|
|
|
|
|
|
2017-03-24 17:03:12 +01:00
|
|
|
def test_bin_hy_stdin_hy_repr():
|
|
|
|
output, _ = run_cmd("hy", '(+ [1] [2])')
|
|
|
|
assert "[1, 2]" in output.replace('L', '')
|
|
|
|
|
|
|
|
output, _ = run_cmd(hr(), '(+ [1] [2])')
|
|
|
|
assert "[1 2]" in output
|
|
|
|
|
|
|
|
output, _ = run_cmd(hr("--spy"), '(+ [1] [2])')
|
|
|
|
assert "[1]+[2]" in output.replace('L', '').replace(' ', '')
|
|
|
|
assert "[1 2]" in output
|
|
|
|
|
|
|
|
# --spy should work even when an exception is thrown
|
|
|
|
output, _ = run_cmd(hr("--spy"), '(+ [1] [2] (foof))')
|
|
|
|
assert "[1]+[2]" in output.replace('L', '').replace(' ', '')
|
|
|
|
|
2013-04-24 15:21:42 +02:00
|
|
|
|
|
|
|
def test_bin_hy_cmd():
|
2017-03-17 17:31:54 +01:00
|
|
|
output, _ = run_cmd("hy -c \"(koan)\"")
|
|
|
|
assert "monk" in output
|
2013-04-24 15:21:42 +02:00
|
|
|
|
2017-03-17 17:31:54 +01:00
|
|
|
_, err = run_cmd("hy -c \"(koan\"", expect=1)
|
|
|
|
assert "Premature end of input" in err
|
2013-04-24 15:21:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_bin_hy_icmd():
|
2017-03-17 17:31:54 +01:00
|
|
|
output, _ = run_cmd("hy -i \"(koan)\"", "(ideas)")
|
2013-04-24 15:21:42 +02:00
|
|
|
assert "monk" in output
|
|
|
|
assert "figlet" in output
|
|
|
|
|
|
|
|
|
2015-02-27 11:59:00 +01:00
|
|
|
def test_bin_hy_icmd_file():
|
2017-03-17 17:31:54 +01:00
|
|
|
output, _ = run_cmd("hy -i resources/icmd_test_file.hy", "(ideas)")
|
2015-02-27 11:59:00 +01:00
|
|
|
assert "Hy!" in output
|
|
|
|
|
|
|
|
|
2014-01-13 16:15:43 +01:00
|
|
|
def test_bin_hy_icmd_and_spy():
|
2017-03-17 17:31:54 +01:00
|
|
|
output, _ = run_cmd("hy -i \"(+ [] [])\" --spy", "(+ 1 1)")
|
2014-01-13 16:15:43 +01:00
|
|
|
assert "([] + [])" in output
|
|
|
|
|
|
|
|
|
2013-06-29 23:56:58 +02:00
|
|
|
def test_bin_hy_missing_file():
|
2017-03-17 17:31:54 +01:00
|
|
|
_, err = run_cmd("hy foobarbaz", expect=2)
|
|
|
|
assert "No such file" in err
|
2013-06-29 23:56:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_bin_hy_file_with_args():
|
2017-03-17 17:31:54 +01:00
|
|
|
assert "usage" in run_cmd("hy tests/resources/argparse_ex.hy -h")[0]
|
|
|
|
assert "got c" in run_cmd("hy tests/resources/argparse_ex.hy -c bar")[0]
|
|
|
|
assert "foo" in run_cmd("hy tests/resources/argparse_ex.hy -i foo")[0]
|
|
|
|
assert "foo" in run_cmd("hy tests/resources/argparse_ex.hy -i foo -c bar")[0] # noqa
|
2013-06-29 23:56:58 +02:00
|
|
|
|
|
|
|
|
2013-07-26 07:33:14 +02:00
|
|
|
def test_bin_hyc():
|
2017-03-17 17:31:54 +01:00
|
|
|
_, err = run_cmd("hyc", expect=2)
|
|
|
|
assert "usage" in err
|
|
|
|
|
|
|
|
output, _ = run_cmd("hyc -h")
|
|
|
|
assert "usage" in output
|
|
|
|
|
|
|
|
output, _ = run_cmd("hyc tests/resources/argparse_ex.hy")
|
|
|
|
assert "Compiling" in output
|
2013-07-26 07:33:14 +02:00
|
|
|
assert os.path.exists("tests/resources/argparse_ex.pyc")
|
|
|
|
|
|
|
|
|
|
|
|
def test_bin_hyc_missing_file():
|
2017-03-17 17:31:54 +01:00
|
|
|
_, err = run_cmd("hyc foobarbaz", expect=2)
|
|
|
|
assert "[Errno 2]" in err
|
2013-07-26 07:33:14 +02:00
|
|
|
|
|
|
|
|
2013-05-08 13:04:35 +02:00
|
|
|
def test_hy2py():
|
|
|
|
i = 0
|
|
|
|
for dirpath, dirnames, filenames in os.walk("tests/native_tests"):
|
|
|
|
for f in filenames:
|
|
|
|
if f.endswith(".hy"):
|
2014-05-01 22:31:45 +02:00
|
|
|
if f == "py3_only_tests.hy" and not PY3:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
i += 1
|
2017-03-17 17:31:54 +01:00
|
|
|
output, err = run_cmd("hy2py -s -a " +
|
|
|
|
os.path.join(dirpath, f))
|
|
|
|
assert len(output) > 1, f
|
|
|
|
assert len(err) == 0, f
|
2013-05-08 13:04:35 +02:00
|
|
|
assert i
|
2013-06-25 17:02:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_bin_hy_builtins():
|
2013-06-26 01:23:44 +02:00
|
|
|
import hy.cmdline # NOQA
|
2013-06-25 17:02:02 +02:00
|
|
|
|
|
|
|
assert str(exit) == "Use (exit) or Ctrl-D (i.e. EOF) to exit"
|
|
|
|
assert str(quit) == "Use (quit) or Ctrl-D (i.e. EOF) to exit"
|
2014-03-11 19:37:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_bin_hy_main():
|
2017-03-17 17:31:54 +01:00
|
|
|
output, _ = run_cmd("hy tests/resources/bin/main.hy")
|
|
|
|
assert "Hello World" in output
|
2014-03-11 19:37:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_bin_hy_main_args():
|
2017-03-17 17:31:54 +01:00
|
|
|
output, _ = run_cmd("hy tests/resources/bin/main.hy test 123")
|
|
|
|
assert "test" in output
|
|
|
|
assert "123" in output
|
2014-03-11 19:37:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_bin_hy_main_exitvalue():
|
2017-03-17 17:31:54 +01:00
|
|
|
run_cmd("hy tests/resources/bin/main.hy exit1", expect=1)
|
2014-03-11 19:37:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_bin_hy_no_main():
|
2017-03-17 17:31:54 +01:00
|
|
|
output, _ = run_cmd("hy tests/resources/bin/nomain.hy")
|
|
|
|
assert "This Should Still Work" in output
|
2014-11-23 23:05:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_bin_hy_module_main():
|
2017-03-17 17:31:54 +01:00
|
|
|
output, _ = run_cmd("hy -m tests.resources.bin.main")
|
|
|
|
assert "Hello World" in output
|
2014-11-23 23:05:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_bin_hy_module_main_args():
|
2017-03-17 17:31:54 +01:00
|
|
|
output, _ = run_cmd("hy -m tests.resources.bin.main test 123")
|
|
|
|
assert "test" in output
|
|
|
|
assert "123" in output
|
2014-11-23 23:05:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_bin_hy_module_main_exitvalue():
|
2017-03-17 17:31:54 +01:00
|
|
|
run_cmd("hy -m tests.resources.bin.main exit1", expect=1)
|
2014-11-23 23:05:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_bin_hy_module_no_main():
|
2017-03-17 17:31:54 +01:00
|
|
|
output, _ = run_cmd("hy -m tests.resources.bin.nomain")
|
|
|
|
assert "This Should Still Work" in output
|