# Path: test/stub_environment.py
# class StubEnvironment(object):
#     def __init__(self):
#         self.cwd = None
#         self.tree = None
#         self.argument = None
# 
# Path: test/stub_stdout.py

# 
# Path: googkit/commands/command.py
# class Command(object):
#     """A base class for commands.
#     """
# 
#     def __init__(self, env):
#         """Create a command that depend the specified environment.
#         """
#         self.env = env
#         self.config = None
# 
#     @classmethod
#     def needs_project_config(cls):
#         """Whether this command depend to a project config file.
#         You should override to return True if this command use a project
#         config.
#         """
#         return False
# 
#     @classmethod
#     def supported_options(cls):
#         """Returns a set has supported options by this command.
#         In default, all command should support a --verbose option to display
#         verbose messages.
#         """
#         return set(['--verbose'])
# 
#     def _validate_options(self):
#         opts = set(self.env.argument.options.keys())
#         unsupported_opts = opts - self.__class__.supported_options()
# 
#         if len(unsupported_opts) > 0:
#             raise InvalidOptionError(unsupported_opts)
# 
#     def _load_config(self):
#         default_config = googkit.lib.path.default_config()
#         user_config = googkit.lib.path.user_config()
#         project_config = googkit.lib.path.project_config(self.env.cwd)
# 
#         if project_config is None:
#             raise GoogkitError(_('No config file found.'))
# 
#         config = Config()
#         config.load(project_config, user_config, default_config)
#         return config
# 
#     def _setup(self):
#         if self.__class__.needs_project_config():
#             if self.config is None:
#                 self.config = self._load_config()
# 
#         os.chdir(self.env.cwd)
# 
#     def run(self):
#         """Runs this command.
#         Override run_internal instead of run if you want to implement a new
#         command.
#         """
#         level = logging.DEBUG if self.env.argument.option('--verbose') else None
#         with log_level(level):
#             self._validate_options()
#             self._setup()
#             self.run_internal()
# 
#     def run_internal(self):
#         """Internal method for running command.
#         You can change a command behavior by overriding this method.
#         """
#         pass
# 
# Path: googkit/lib/argument.py
# class Argument(object):
#     """Argument class that has parsed commands and options.
#     """
# 
#     def __init__(self, commands=[], options={}):
#         """Creates a container has the sub-command list and the option dictionary.
#         """
#         self._commands = commands
#         self._options = options
# 
#     @property
#     def commands(self):
#         """Returns a command list.
#         """
#         return self._commands
# 
#     @property
#     def options(self):
#         """Returns an option dictionary.
#         """
#         return self._options
# 
#     def option(self, name):
#         """Returns an option by the specified name.
#         """
#         return self._options.get(name)
# 
# Path: googkit/lib/argument.py
# class ArgumentParser(object):
#     """Parser class that parses command line arguments.
#     """
# 
#     """RegExp to check whether the string is a parameter.
#     The parameter should have a prefixed-hyphen.
#     """
#     OPTION_PATTERN = re.compile('(--?\w+)(?:=(.+))?')
# 
#     @classmethod
#     def parse(self, argv):
#         """Split an argv into sub command parts and option parts.
#         The return value is an instance of Argument.
#         """
#         commands = []
#         options = {}
# 
#         for arg in argv[1:]:
#             if ArgumentParser._is_option(arg):
#                 key, value = ArgumentParser._parse_option(arg)
#                 options[key] = value
#             else:
#                 commands.append(arg)
# 
#         return Argument(commands, options)
# 
#     @classmethod
#     def _is_option(cls, arg):
#         return ArgumentParser.OPTION_PATTERN.match(arg)
# 
#     @classmethod
#     def _parse_option(cls, option):
#         m = ArgumentParser.OPTION_PATTERN.match(option)
#         if m is None:
#             return (option, True)
# 
#         if m.lastindex == 1:
#             return (m.group(1), True)
# 
#         return (m.group(1), m.group(2))
# 
# Path: googkit/lib/error.py
# class GoogkitError(Exception):
#     """Base class for error in Googkit."""
# 
#     def __init__(self, message):
#         """Creates a local error of googkit by the specified message.
#         """
#         self._message = message
# 
#     @property
#     def message(self):
#         return self._message
# 
#     def __str__(self):
#         return self.message


import unittest
from test.stub_environment import StubEnvironment
from test.stub_stdout import StubStdout
from googkit.commands.command import Command
from googkit.compat.unittest import mock
from googkit.lib.argument import Argument
from googkit.lib.argument import ArgumentParser
from googkit.lib.error import GoogkitError




class TestCommand(unittest.TestCase):
    def test_run(self):
        class DummyCommand(Command):
            pass

        env = StubEnvironment()
        env.argument = Argument()
        cmd = DummyCommand(env)
        cmd._setup = mock.MagicMock()
        cmd.run_internal = mock.MagicMock()

        with mock.patch('sys.stdout', new_callable=StubStdout):
            cmd.run()

        self.assertTrue(cmd._setup.called)
        self.assertTrue(cmd.run_internal.called)

    def test_validate_options(self):

# Path: test/stub_environment.py
# class StubEnvironment(object):
#     def __init__(self):
#         self.cwd = None
#         self.tree = None
#         self.argument = None
# 
# Path: test/stub_stdout.py

# 
# Path: googkit/commands/command.py
# class Command(object):
#     """A base class for commands.
#     """
# 
#     def __init__(self, env):
#         """Create a command that depend the specified environment.
#         """
#         self.env = env
#         self.config = None
# 
#     @classmethod
#     def needs_project_config(cls):
#         """Whether this command depend to a project config file.
#         You should override to return True if this command use a project
#         config.
#         """
#         return False
# 
#     @classmethod
#     def supported_options(cls):
#         """Returns a set has supported options by this command.
#         In default, all command should support a --verbose option to display
#         verbose messages.
#         """
#         return set(['--verbose'])
# 
#     def _validate_options(self):
#         opts = set(self.env.argument.options.keys())
#         unsupported_opts = opts - self.__class__.supported_options()
# 
#         if len(unsupported_opts) > 0:
#             raise InvalidOptionError(unsupported_opts)
# 
#     def _load_config(self):
#         default_config = googkit.lib.path.default_config()
#         user_config = googkit.lib.path.user_config()
#         project_config = googkit.lib.path.project_config(self.env.cwd)
# 
#         if project_config is None:
#             raise GoogkitError(_('No config file found.'))
# 
#         config = Config()
#         config.load(project_config, user_config, default_config)
#         return config
# 
#     def _setup(self):
#         if self.__class__.needs_project_config():
#             if self.config is None:
#                 self.config = self._load_config()
# 
#         os.chdir(self.env.cwd)
# 
#     def run(self):
#         """Runs this command.
#         Override run_internal instead of run if you want to implement a new
#         command.
#         """
#         level = logging.DEBUG if self.env.argument.option('--verbose') else None
#         with log_level(level):
#             self._validate_options()
#             self._setup()
#             self.run_internal()
# 
#     def run_internal(self):
#         """Internal method for running command.
#         You can change a command behavior by overriding this method.
#         """
#         pass
# 
# Path: googkit/lib/argument.py
# class Argument(object):
#     """Argument class that has parsed commands and options.
#     """
# 
#     def __init__(self, commands=[], options={}):
#         """Creates a container has the sub-command list and the option dictionary.
#         """
#         self._commands = commands
#         self._options = options
# 
#     @property
#     def commands(self):
#         """Returns a command list.
#         """
#         return self._commands
# 
#     @property
#     def options(self):
#         """Returns an option dictionary.
#         """
#         return self._options
# 
#     def option(self, name):
#         """Returns an option by the specified name.
#         """
#         return self._options.get(name)
# 
# Path: googkit/lib/argument.py
# class ArgumentParser(object):
#     """Parser class that parses command line arguments.
#     """
# 
#     """RegExp to check whether the string is a parameter.
#     The parameter should have a prefixed-hyphen.
#     """
#     OPTION_PATTERN = re.compile('(--?\w+)(?:=(.+))?')
# 
#     @classmethod
#     def parse(self, argv):
#         """Split an argv into sub command parts and option parts.
#         The return value is an instance of Argument.
#         """
#         commands = []
#         options = {}
# 
#         for arg in argv[1:]:
#             if ArgumentParser._is_option(arg):
#                 key, value = ArgumentParser._parse_option(arg)
#                 options[key] = value
#             else:
#                 commands.append(arg)
# 
#         return Argument(commands, options)
# 
#     @classmethod
#     def _is_option(cls, arg):
#         return ArgumentParser.OPTION_PATTERN.match(arg)
# 
#     @classmethod
#     def _parse_option(cls, option):
#         m = ArgumentParser.OPTION_PATTERN.match(option)
#         if m is None:
#             return (option, True)
# 
#         if m.lastindex == 1:
#             return (m.group(1), True)
# 
#         return (m.group(1), m.group(2))
# 
# Path: googkit/lib/error.py
# class GoogkitError(Exception):
#     """Base class for error in Googkit."""
# 
#     def __init__(self, message):
#         """Creates a local error of googkit by the specified message.
#         """
#         self._message = message
# 
#     @property
#     def message(self):
#         return self._message
# 
#     def __str__(self):
#         return self.message


# Path: test/commands/test_command.py
import unittest
from test.stub_environment import StubEnvironment
from test.stub_stdout import StubStdout
from googkit.commands.command import Command
from googkit.compat.unittest import mock
from googkit.lib.argument import Argument
from googkit.lib.argument import ArgumentParser
from googkit.lib.error import GoogkitError





class TestCommand(unittest.TestCase):
    def test_run(self):
        class DummyCommand(Command):
            pass

        env = StubEnvironment()
        env.argument = Argument()
        cmd = DummyCommand(env)
        cmd._setup = mock.MagicMock()
        cmd.run_internal = mock.MagicMock()

        with mock.patch('sys.stdout', new_callable=StubStdout):
            cmd.run()

        self.assertTrue(cmd._setup.called)
        self.assertTrue(cmd.run_internal.called)

    def test_validate_options(self):

        class DummyCommand(Command):