1from contextlib import contextmanager
2import os
3from unittest import mock
4
5from mako.cmd import cmdline
6from mako.testing.assertions import eq_
7from mako.testing.assertions import expect_raises
8from mako.testing.assertions import expect_raises_message
9from mako.testing.config import config
10from mako.testing.fixtures import TemplateTest
11
12
13class CmdTest(TemplateTest):
14    @contextmanager
15    def _capture_output_fixture(self, stream="stdout"):
16        with mock.patch("sys.%s" % stream) as stdout:
17            yield stdout
18
19    def test_stdin_success(self):
20        with self._capture_output_fixture() as stdout:
21            with mock.patch(
22                "sys.stdin",
23                mock.Mock(read=mock.Mock(return_value="hello world ${x}")),
24            ):
25                cmdline(["--var", "x=5", "-"])
26
27        eq_(stdout.write.mock_calls[0][1][0], "hello world 5")
28
29    def test_stdin_syntax_err(self):
30        with mock.patch(
31            "sys.stdin", mock.Mock(read=mock.Mock(return_value="${x"))
32        ):
33            with self._capture_output_fixture("stderr") as stderr:
34                with expect_raises(SystemExit):
35                    cmdline(["--var", "x=5", "-"])
36
37            assert (
38                "SyntaxException: Expected" in stderr.write.mock_calls[0][1][0]
39            )
40            assert "Traceback" in stderr.write.mock_calls[0][1][0]
41
42    def test_stdin_rt_err(self):
43        with mock.patch(
44            "sys.stdin", mock.Mock(read=mock.Mock(return_value="${q}"))
45        ):
46            with self._capture_output_fixture("stderr") as stderr:
47                with expect_raises(SystemExit):
48                    cmdline(["--var", "x=5", "-"])
49
50            assert "NameError: Undefined" in stderr.write.mock_calls[0][1][0]
51            assert "Traceback" in stderr.write.mock_calls[0][1][0]
52
53    def test_file_success(self):
54        with self._capture_output_fixture() as stdout:
55            cmdline(
56                [
57                    "--var",
58                    "x=5",
59                    os.path.join(config.template_base, "cmd_good.mako"),
60                ]
61            )
62
63        eq_(stdout.write.mock_calls[0][1][0], "hello world 5")
64
65    def test_file_syntax_err(self):
66        with self._capture_output_fixture("stderr") as stderr:
67            with expect_raises(SystemExit):
68                cmdline(
69                    [
70                        "--var",
71                        "x=5",
72                        os.path.join(config.template_base, "cmd_syntax.mako"),
73                    ]
74                )
75
76        assert "SyntaxException: Expected" in stderr.write.mock_calls[0][1][0]
77        assert "Traceback" in stderr.write.mock_calls[0][1][0]
78
79    def test_file_rt_err(self):
80        with self._capture_output_fixture("stderr") as stderr:
81            with expect_raises(SystemExit):
82                cmdline(
83                    [
84                        "--var",
85                        "x=5",
86                        os.path.join(config.template_base, "cmd_runtime.mako"),
87                    ]
88                )
89
90        assert "NameError: Undefined" in stderr.write.mock_calls[0][1][0]
91        assert "Traceback" in stderr.write.mock_calls[0][1][0]
92
93    def test_file_notfound(self):
94        with expect_raises_message(
95            SystemExit, "error: can't find fake.lalala"
96        ):
97            cmdline(["--var", "x=5", "fake.lalala"])
98