# Path: giraffez/__main__.py
# def main():
#     try:
#         MainCommand().run()
#     except KeyboardInterrupt:
#         sys.exit(1)
# 
# Path: giraffez/core.py
# class MainCommand(Command):
#     name = "giraffez"
#     description = ascii_giraffe
#     usage = "giraffez [args] <command> [options]"
# 
#     global_arguments = [
#         Argument("-D", "--dsn"),
#         Argument("-c", "--conf"),
#         Argument("-k", "--key"),
#         Argument("-v", "--verbose", action="count", default=0, dest="log_level"),
#         Argument("-h", "--help", default=False, help="Print usage"),
#     ]
# 
#     arguments = [
#         Argument("-V", "--version", action="version", help="Print version"),
#         Argument("--protect", default=False, help=("Use connection locking to protect Teradata "
#             "accounts from locking from bad attempts")),
#     ]
# 
#     commands = [
#         "commandline.CmdCommand",
#         "commandline.ConfigCommand",
#         "commandline.ExportCommand",
#         "commandline.FmtCommand",
#         "commandline.InsertCommand",
#         "commandline.LoadCommand",
#         "commandline.RunCommand",
#         "commandline.ShellCommand",
#         "commandline.SecretCommand",
#     ]
# 
#     default = False
# 
#     def run(self, test_args=None):
#         args = self.parse_args(test_args)
#         log.level = args.log_level
#         log.debug(sys.version.replace("\n", ""))
#         if args.module_name is None:
#             self.print_help()
#             self.exit()
#         if args.module_name == "secret" or (args.module_name == "config" and args.init):
#             args.run(args)
#             return
#         try:
#             with Config(conf=args.conf, key_file=args.key) as config:
#                 colorful = config.get_value("colorful", default=True)
#                 if not colorful:
#                     colors.is_colorful = False
#             args.run(args)
#         except ConfigNotFound as error:
#             log.debug(error)
#             if args.conf is None:
#                 args.conf = home_file(".girafferc")
#             if prompt_bool(("Configuration file '{}' not found, would you like to create one now "
#                     "using the default template?").format(args.conf), default=True):
#                 result = Config.write_default(args.conf)
#                 if result:
#                     log.write(colors.green(result))
#                     log.write(message_write_default.format(args.conf))
#                     self.run()
#                 else:
#                     log.write(colors.fail("Was not successful"))
#         except KeyNotFound as error:
#             log.debug(error)
#             if args.key is None:
#                 args.key = home_file(".giraffepg")
#             if prompt_bool("Key file '{}' not found, would you like to create one now?".format(args.key),
#                     default=True):
#                 create_key_file(args.key)
#                 log.write("Key file '{}' created successfully.".format(args.key))
#                 self.run()
#             else:
#                 raise error
#         except (_teradata.InvalidCredentialsError, _teradatapt.InvalidCredentialsError) as error:
#             if args.protect:
#                 Config.lock_connection(args.conf, args.dsn, args.key)
#             raise error


import os
import platform
import pytest
from giraffez.__main__ import main
from giraffez.core import MainCommand
from giraffez.errors import *
from giraffez.encrypt import *
from giraffez._teradata import TeradataError




@pytest.mark.usefixtures('tmpfiles')
class TestCommandLine(object):
    def test_print_help(self, mocker):
        # Should raise a SystemExit and cause help to print
        with pytest.raises(SystemExit):
            main()

    @pytest.mark.usefixtures('config')
    def test_cmd_error(self, mocker, tmpfiles):
        # Windows throws a PermissionError when trying to reopen this
        # file. On other unix-like systems this does not fail because
        # the underlying directory entry is removed but the file
        # remains until no longer in use. For now, this test should
        # be skipped on Windows systems.
        if platform.system() == 'Windows':
            return
        os.remove(tmpfiles.key)
        create_key_file(tmpfiles.key)
        with pytest.raises(TeradataError):
            MainCommand().run(test_args=["cmd", "select * from dbc.dbcinfo", "--conf", tmpfiles.conf, "--key", tmpfiles.key])

    def test_config_not_found(self, mocker, tmpfiles):
        mock_prompt = mocker.patch('giraffez.core.prompt_bool')
        mock_prompt.return_value = False
        mock_prompt.side_effect = ConfigNotFound("Did it!")
        with pytest.raises(ConfigNotFound):

# Path: giraffez/__main__.py
# def main():
#     try:
#         MainCommand().run()
#     except KeyboardInterrupt:
#         sys.exit(1)
# 
# Path: giraffez/core.py
# class MainCommand(Command):
#     name = "giraffez"
#     description = ascii_giraffe
#     usage = "giraffez [args] <command> [options]"
# 
#     global_arguments = [
#         Argument("-D", "--dsn"),
#         Argument("-c", "--conf"),
#         Argument("-k", "--key"),
#         Argument("-v", "--verbose", action="count", default=0, dest="log_level"),
#         Argument("-h", "--help", default=False, help="Print usage"),
#     ]
# 
#     arguments = [
#         Argument("-V", "--version", action="version", help="Print version"),
#         Argument("--protect", default=False, help=("Use connection locking to protect Teradata "
#             "accounts from locking from bad attempts")),
#     ]
# 
#     commands = [
#         "commandline.CmdCommand",
#         "commandline.ConfigCommand",
#         "commandline.ExportCommand",
#         "commandline.FmtCommand",
#         "commandline.InsertCommand",
#         "commandline.LoadCommand",
#         "commandline.RunCommand",
#         "commandline.ShellCommand",
#         "commandline.SecretCommand",
#     ]
# 
#     default = False
# 
#     def run(self, test_args=None):
#         args = self.parse_args(test_args)
#         log.level = args.log_level
#         log.debug(sys.version.replace("\n", ""))
#         if args.module_name is None:
#             self.print_help()
#             self.exit()
#         if args.module_name == "secret" or (args.module_name == "config" and args.init):
#             args.run(args)
#             return
#         try:
#             with Config(conf=args.conf, key_file=args.key) as config:
#                 colorful = config.get_value("colorful", default=True)
#                 if not colorful:
#                     colors.is_colorful = False
#             args.run(args)
#         except ConfigNotFound as error:
#             log.debug(error)
#             if args.conf is None:
#                 args.conf = home_file(".girafferc")
#             if prompt_bool(("Configuration file '{}' not found, would you like to create one now "
#                     "using the default template?").format(args.conf), default=True):
#                 result = Config.write_default(args.conf)
#                 if result:
#                     log.write(colors.green(result))
#                     log.write(message_write_default.format(args.conf))
#                     self.run()
#                 else:
#                     log.write(colors.fail("Was not successful"))
#         except KeyNotFound as error:
#             log.debug(error)
#             if args.key is None:
#                 args.key = home_file(".giraffepg")
#             if prompt_bool("Key file '{}' not found, would you like to create one now?".format(args.key),
#                     default=True):
#                 create_key_file(args.key)
#                 log.write("Key file '{}' created successfully.".format(args.key))
#                 self.run()
#             else:
#                 raise error
#         except (_teradata.InvalidCredentialsError, _teradatapt.InvalidCredentialsError) as error:
#             if args.protect:
#                 Config.lock_connection(args.conf, args.dsn, args.key)
#             raise error


# Path: tests/test_commandline.py
import os
import platform
import pytest
from giraffez.__main__ import main
from giraffez.core import MainCommand
from giraffez.errors import *
from giraffez.encrypt import *
from giraffez._teradata import TeradataError





@pytest.mark.usefixtures('tmpfiles')
class TestCommandLine(object):
    def test_print_help(self, mocker):
        # Should raise a SystemExit and cause help to print
        with pytest.raises(SystemExit):
            main()

    @pytest.mark.usefixtures('config')
    def test_cmd_error(self, mocker, tmpfiles):
        # Windows throws a PermissionError when trying to reopen this
        # file. On other unix-like systems this does not fail because
        # the underlying directory entry is removed but the file
        # remains until no longer in use. For now, this test should
        # be skipped on Windows systems.
        if platform.system() == 'Windows':
            return
        os.remove(tmpfiles.key)
        create_key_file(tmpfiles.key)
        with pytest.raises(TeradataError):
            MainCommand().run(test_args=["cmd", "select * from dbc.dbcinfo", "--conf", tmpfiles.conf, "--key", tmpfiles.key])

    def test_config_not_found(self, mocker, tmpfiles):
        mock_prompt = mocker.patch('giraffez.core.prompt_bool')
        mock_prompt.return_value = False
        mock_prompt.side_effect = ConfigNotFound("Did it!")
        with pytest.raises(ConfigNotFound):

            MainCommand().run(test_args=["cmd", "select * from dbc.dbcinfo", "--conf", tmpfiles.noconf])