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>
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
2013-12-10 18:41:49 +01:00
|
|
|
from nose.plugins.skip import SkipTest
|
|
|
|
|
2013-04-20 22:27:10 +02:00
|
|
|
|
2013-06-29 23:56:58 +02:00
|
|
|
def run_cmd(cmd, stdin_data=None):
|
2013-05-08 13:04:35 +02:00
|
|
|
p = subprocess.Popen(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-05-08 13:04:35 +02:00
|
|
|
stdout = ""
|
|
|
|
stderr = ""
|
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…
|
|
|
|
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')
|
2013-05-08 13:04:35 +02:00
|
|
|
return p.returncode, stdout, stderr
|
2013-04-24 15:21:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_bin_hy():
|
2013-06-29 23:56:58 +02:00
|
|
|
ret = run_cmd("hy", "")
|
2013-04-24 15:21:42 +02:00
|
|
|
assert ret[0] == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_bin_hy_stdin():
|
2013-06-29 23:56:58 +02:00
|
|
|
ret = run_cmd("hy", '(koan)')
|
2013-04-24 15:21:42 +02:00
|
|
|
assert ret[0] == 0
|
2013-05-08 13:04:35 +02:00
|
|
|
assert "monk" in ret[1]
|
2013-04-24 15:21:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_bin_hy_cmd():
|
2013-06-29 23:56:58 +02:00
|
|
|
ret = run_cmd("hy -c \"(koan)\"")
|
2013-04-24 15:21:42 +02:00
|
|
|
assert ret[0] == 0
|
2013-05-08 13:04:35 +02:00
|
|
|
assert "monk" in ret[1]
|
2013-04-24 15:21:42 +02:00
|
|
|
|
2013-06-29 23:56:58 +02:00
|
|
|
ret = run_cmd("hy -c \"(koan\"")
|
2013-04-24 15:21:42 +02:00
|
|
|
assert ret[0] == 1
|
2013-12-26 17:36:45 +01:00
|
|
|
assert "Premature end of input" in ret[2]
|
2013-04-24 15:21:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_bin_hy_icmd():
|
2013-06-29 23:56:58 +02:00
|
|
|
ret = run_cmd("hy -i \"(koan)\"", "(ideas)")
|
2013-04-24 15:21:42 +02:00
|
|
|
assert ret[0] == 0
|
2013-05-08 13:04:35 +02:00
|
|
|
output = ret[1]
|
2013-04-24 15:21:42 +02:00
|
|
|
|
|
|
|
assert "monk" in output
|
|
|
|
assert "figlet" in output
|
|
|
|
|
|
|
|
|
2014-01-13 16:15:43 +01:00
|
|
|
def test_bin_hy_icmd_and_spy():
|
|
|
|
ret = run_cmd("hy -i \"(+ [] [])\" --spy", "(+ 1 1)")
|
|
|
|
assert ret[0] == 0
|
|
|
|
output = ret[1]
|
|
|
|
|
|
|
|
assert "([] + [])" in output
|
|
|
|
|
|
|
|
|
2013-06-29 23:56:58 +02:00
|
|
|
def test_bin_hy_missing_file():
|
|
|
|
ret = run_cmd("hy foobarbaz")
|
2013-07-26 07:33:14 +02:00
|
|
|
assert ret[0] == 2
|
2013-06-29 23:56:58 +02:00
|
|
|
assert "No such file" in ret[2]
|
|
|
|
|
|
|
|
|
|
|
|
def test_bin_hy_file_with_args():
|
|
|
|
ret = run_cmd("hy tests/resources/argparse_ex.hy -h")
|
|
|
|
assert ret[0] == 0
|
|
|
|
assert "usage" in ret[1]
|
|
|
|
ret = run_cmd("hy tests/resources/argparse_ex.hy -c bar")
|
|
|
|
assert ret[0] == 0
|
|
|
|
assert "got c" in ret[1]
|
|
|
|
ret = run_cmd("hy tests/resources/argparse_ex.hy -i foo")
|
|
|
|
assert ret[0] == 0
|
|
|
|
assert "foo" in ret[1]
|
|
|
|
ret = run_cmd("hy tests/resources/argparse_ex.hy -i foo -c bar")
|
|
|
|
assert ret[0] == 0
|
|
|
|
assert "foo" in ret[1]
|
|
|
|
|
|
|
|
|
2013-07-26 07:33:14 +02:00
|
|
|
def test_bin_hyc():
|
|
|
|
ret = run_cmd("hyc")
|
|
|
|
assert ret[0] == 2
|
|
|
|
assert "usage" in ret[2]
|
|
|
|
ret = run_cmd("hyc -h")
|
|
|
|
assert ret[0] == 0
|
|
|
|
assert "usage" in ret[1]
|
|
|
|
ret = run_cmd("hyc tests/resources/argparse_ex.hy")
|
|
|
|
assert ret[0] == 0
|
|
|
|
assert "Compiling" in ret[1]
|
|
|
|
assert os.path.exists("tests/resources/argparse_ex.pyc")
|
|
|
|
|
|
|
|
|
|
|
|
def test_bin_hyc_missing_file():
|
|
|
|
ret = run_cmd("hyc foobarbaz")
|
|
|
|
assert ret[0] == 2
|
|
|
|
assert "[Errno 2]" in ret[2]
|
|
|
|
|
|
|
|
|
2013-05-08 13:04:35 +02:00
|
|
|
def test_hy2py():
|
2013-06-29 23:56:58 +02:00
|
|
|
# and running this script this way doesn't work on Windows
|
|
|
|
if os.name == "nt":
|
2013-12-10 18:41:49 +01:00
|
|
|
raise SkipTest("doesn't work on Windows")
|
2013-06-29 23:56:58 +02:00
|
|
|
|
2013-05-08 13:04:35 +02:00
|
|
|
i = 0
|
|
|
|
for dirpath, dirnames, filenames in os.walk("tests/native_tests"):
|
|
|
|
for f in filenames:
|
|
|
|
if f.endswith(".hy"):
|
|
|
|
i += 1
|
|
|
|
ret = run_cmd("bin/hy2py " + os.path.join(dirpath, f))
|
|
|
|
assert ret[0] == 0, f
|
|
|
|
assert len(ret[1]) > 1, f
|
|
|
|
assert len(ret[2]) == 0, f
|
|
|
|
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"
|