# Path: mako/cmd.py
# def cmdline(argv=None):
# 
#     parser = ArgumentParser()
#     parser.add_argument(
#         "--var",
#         default=[],
#         action="append",
#         help="variable (can be used multiple times, use name=value)",
#     )
#     parser.add_argument(
#         "--template-dir",
#         default=[],
#         action="append",
#         help="Directory to use for template lookup (multiple "
#         "directories may be provided). If not given then if the "
#         "template is read from stdin, the value defaults to be "
#         "the current directory, otherwise it defaults to be the "
#         "parent directory of the file provided.",
#     )
#     parser.add_argument(
#         "--output-encoding", default=None, help="force output encoding"
#     )
#     parser.add_argument(
#         "--output-file",
#         default=None,
#         help="Write to file upon successful render instead of stdout",
#     )
#     parser.add_argument("input", nargs="?", default="-")
# 
#     options = parser.parse_args(argv)
# 
#     output_encoding = options.output_encoding
#     output_file = options.output_file
# 
#     if options.input == "-":
#         lookup_dirs = options.template_dir or ["."]
#         lookup = TemplateLookup(lookup_dirs)
#         try:
#             template = Template(
#                 sys.stdin.read(),
#                 lookup=lookup,
#                 output_encoding=output_encoding,
#             )
#         except:
#             _exit()
#     else:
#         filename = options.input
#         if not isfile(filename):
#             raise SystemExit("error: can't find %s" % filename)
#         lookup_dirs = options.template_dir or [dirname(filename)]
#         lookup = TemplateLookup(lookup_dirs)
#         try:
#             template = Template(
#                 filename=filename,
#                 lookup=lookup,
#                 output_encoding=output_encoding,
#             )
#         except:
#             _exit()
# 
#     kw = dict(varsplit(var) for var in options.var)
#     try:
#         rendered = template.render(**kw)
#     except:
#         _exit()
#     else:
#         if output_file:
#             open(output_file, "wt", encoding=output_encoding).write(rendered)
#         else:
#             sys.stdout.write(rendered)
# 
# Path: mako/testing/assertions.py
# def eq_(a, b, msg=None):
#     """Assert a == b, with repr messaging on failure."""
#     assert a == b, msg or "%r != %r" % (a, b)
# 
# Path: mako/testing/assertions.py
# def expect_raises(except_cls, check_context=False):
#     return _expect_raises(except_cls, check_context=check_context)
# 
# Path: mako/testing/assertions.py
# def expect_raises_message(except_cls, msg, check_context=False):
#     return _expect_raises(except_cls, msg=msg, check_context=check_context)
# 
# Path: mako/testing/config.py
# class Config(ReadsCfg):
# 
# Path: mako/testing/fixtures.py
# class TemplateTest:
#     def _file_template(self, filename, **kw):
#         filepath = self._file_path(filename)
#         return Template(
#             uri=filename,
#             filename=filepath,
#             module_directory=config.module_base,
#             **kw,
#         )
# 
#     def _file_path(self, filename):
#         name, ext = os.path.splitext(filename)
#         py3k_path = os.path.join(config.template_base, name + "_py3k" + ext)
#         if os.path.exists(py3k_path):
#             return py3k_path
# 
#         return os.path.join(config.template_base, filename)
# 
#     def _do_file_test(
#         self,
#         filename,
#         expected,
#         filters=None,
#         unicode_=True,
#         template_args=None,
#         **kw,
#     ):
#         t1 = self._file_template(filename, **kw)
#         self._do_test(
#             t1,
#             expected,
#             filters=filters,
#             unicode_=unicode_,
#             template_args=template_args,
#         )
# 
#     def _do_memory_test(
#         self,
#         source,
#         expected,
#         filters=None,
#         unicode_=True,
#         template_args=None,
#         **kw,
#     ):
#         t1 = Template(text=source, **kw)
#         self._do_test(
#             t1,
#             expected,
#             filters=filters,
#             unicode_=unicode_,
#             template_args=template_args,
#         )
# 
#     def _do_test(
#         self,
#         template,
#         expected,
#         filters=None,
#         template_args=None,
#         unicode_=True,
#     ):
#         if template_args is None:
#             template_args = {}
#         if unicode_:
#             output = template.render_unicode(**template_args)
#         else:
#             output = template.render(**template_args)
# 
#         if filters:
#             output = filters(output)
#         eq_(output, expected)


from contextlib import contextmanager
from unittest import mock
from mako.cmd import cmdline
from mako.testing.assertions import eq_
from mako.testing.assertions import expect_raises
from mako.testing.assertions import expect_raises_message
from mako.testing.config import config
from mako.testing.fixtures import TemplateTest
import os
                [
                    "--var",
                    "x=5",
                    os.path.join(config.template_base, "cmd_good.mako"),
                ]
            )

        eq_(stdout.write.mock_calls[0][1][0], "hello world 5")

    def test_file_syntax_err(self):
        with self._capture_output_fixture("stderr") as stderr:
            with expect_raises(SystemExit):
                cmdline(
                    [
                        "--var",
                        "x=5",
                        os.path.join(config.template_base, "cmd_syntax.mako"),
                    ]
                )

        assert "SyntaxException: Expected" in stderr.write.mock_calls[0][1][0]
        assert "Traceback" in stderr.write.mock_calls[0][1][0]

    def test_file_rt_err(self):
        with self._capture_output_fixture("stderr") as stderr:
            with expect_raises(SystemExit):
                cmdline(
                    [
                        "--var",
                        "x=5",

# Path: mako/cmd.py
# def cmdline(argv=None):
# 
#     parser = ArgumentParser()
#     parser.add_argument(
#         "--var",
#         default=[],
#         action="append",
#         help="variable (can be used multiple times, use name=value)",
#     )
#     parser.add_argument(
#         "--template-dir",
#         default=[],
#         action="append",
#         help="Directory to use for template lookup (multiple "
#         "directories may be provided). If not given then if the "
#         "template is read from stdin, the value defaults to be "
#         "the current directory, otherwise it defaults to be the "
#         "parent directory of the file provided.",
#     )
#     parser.add_argument(
#         "--output-encoding", default=None, help="force output encoding"
#     )
#     parser.add_argument(
#         "--output-file",
#         default=None,
#         help="Write to file upon successful render instead of stdout",
#     )
#     parser.add_argument("input", nargs="?", default="-")
# 
#     options = parser.parse_args(argv)
# 
#     output_encoding = options.output_encoding
#     output_file = options.output_file
# 
#     if options.input == "-":
#         lookup_dirs = options.template_dir or ["."]
#         lookup = TemplateLookup(lookup_dirs)
#         try:
#             template = Template(
#                 sys.stdin.read(),
#                 lookup=lookup,
#                 output_encoding=output_encoding,
#             )
#         except:
#             _exit()
#     else:
#         filename = options.input
#         if not isfile(filename):
#             raise SystemExit("error: can't find %s" % filename)
#         lookup_dirs = options.template_dir or [dirname(filename)]
#         lookup = TemplateLookup(lookup_dirs)
#         try:
#             template = Template(
#                 filename=filename,
#                 lookup=lookup,
#                 output_encoding=output_encoding,
#             )
#         except:
#             _exit()
# 
#     kw = dict(varsplit(var) for var in options.var)
#     try:
#         rendered = template.render(**kw)
#     except:
#         _exit()
#     else:
#         if output_file:
#             open(output_file, "wt", encoding=output_encoding).write(rendered)
#         else:
#             sys.stdout.write(rendered)
# 
# Path: mako/testing/assertions.py
# def eq_(a, b, msg=None):
#     """Assert a == b, with repr messaging on failure."""
#     assert a == b, msg or "%r != %r" % (a, b)
# 
# Path: mako/testing/assertions.py
# def expect_raises(except_cls, check_context=False):
#     return _expect_raises(except_cls, check_context=check_context)
# 
# Path: mako/testing/assertions.py
# def expect_raises_message(except_cls, msg, check_context=False):
#     return _expect_raises(except_cls, msg=msg, check_context=check_context)
# 
# Path: mako/testing/config.py
# class Config(ReadsCfg):
# 
# Path: mako/testing/fixtures.py
# class TemplateTest:
#     def _file_template(self, filename, **kw):
#         filepath = self._file_path(filename)
#         return Template(
#             uri=filename,
#             filename=filepath,
#             module_directory=config.module_base,
#             **kw,
#         )
# 
#     def _file_path(self, filename):
#         name, ext = os.path.splitext(filename)
#         py3k_path = os.path.join(config.template_base, name + "_py3k" + ext)
#         if os.path.exists(py3k_path):
#             return py3k_path
# 
#         return os.path.join(config.template_base, filename)
# 
#     def _do_file_test(
#         self,
#         filename,
#         expected,
#         filters=None,
#         unicode_=True,
#         template_args=None,
#         **kw,
#     ):
#         t1 = self._file_template(filename, **kw)
#         self._do_test(
#             t1,
#             expected,
#             filters=filters,
#             unicode_=unicode_,
#             template_args=template_args,
#         )
# 
#     def _do_memory_test(
#         self,
#         source,
#         expected,
#         filters=None,
#         unicode_=True,
#         template_args=None,
#         **kw,
#     ):
#         t1 = Template(text=source, **kw)
#         self._do_test(
#             t1,
#             expected,
#             filters=filters,
#             unicode_=unicode_,
#             template_args=template_args,
#         )
# 
#     def _do_test(
#         self,
#         template,
#         expected,
#         filters=None,
#         template_args=None,
#         unicode_=True,
#     ):
#         if template_args is None:
#             template_args = {}
#         if unicode_:
#             output = template.render_unicode(**template_args)
#         else:
#             output = template.render(**template_args)
# 
#         if filters:
#             output = filters(output)
#         eq_(output, expected)


# Path: test/test_cmd.py
from contextlib import contextmanager
from unittest import mock
from mako.cmd import cmdline
from mako.testing.assertions import eq_
from mako.testing.assertions import expect_raises
from mako.testing.assertions import expect_raises_message
from mako.testing.config import config
from mako.testing.fixtures import TemplateTest
import os

                [
                    "--var",
                    "x=5",
                    os.path.join(config.template_base, "cmd_good.mako"),
                ]
            )

        eq_(stdout.write.mock_calls[0][1][0], "hello world 5")

    def test_file_syntax_err(self):
        with self._capture_output_fixture("stderr") as stderr:
            with expect_raises(SystemExit):
                cmdline(
                    [
                        "--var",
                        "x=5",
                        os.path.join(config.template_base, "cmd_syntax.mako"),
                    ]
                )

        assert "SyntaxException: Expected" in stderr.write.mock_calls[0][1][0]
        assert "Traceback" in stderr.write.mock_calls[0][1][0]

    def test_file_rt_err(self):
        with self._capture_output_fixture("stderr") as stderr:
            with expect_raises(SystemExit):
                cmdline(
                    [
                        "--var",
                        "x=5",

                        os.path.join(config.template_base, "cmd_runtime.mako"),