Exemplo n.º 1
0
    def test_get_ctx_defaults_non_nested(self):
        provider = MagicMock()

        mock_context1 = MockContext(info_name="sam", parent=None)
        mock_context2 = MockContext(info_name="local", parent=mock_context1)
        mock_context3 = MockContext(info_name="start-api", parent=mock_context2)

        get_ctx_defaults("start-api", provider, mock_context3, "default")

        provider.assert_called_with(SamConfig.config_dir(), "default", ["local", "start-api"])
    def test_get_ctx_defaults_nested(self):
        provider = MagicMock()

        mock_context1 = MockContext(info_name="sam", parent=None)
        mock_context2 = MockContext(info_name="local", parent=mock_context1)
        mock_context3 = MockContext(info_name="generate-event", parent=mock_context2)
        mock_context4 = MockContext(info_name="alexa-skills-kit", parent=mock_context3)

        get_ctx_defaults("intent-answer", provider, mock_context4, "default")

        provider.assert_called_with(None, "default", ["local", "generate-event", "alexa-skills-kit", "intent-answer"])
    def get_command(self, ctx, cmd_name):
        """
        gets the Click Commands underneath a service name

        Parameters
        ----------
        ctx: Context
            context object passed in
        cmd_name: string
            the service name
        Returns
        -------
        cmd: Click.Command
            the Click Commands that can be called from the CLI
        """

        if cmd_name not in self.subcmd_definition:
            return None
        parameters = []
        for param_name in self.subcmd_definition[cmd_name][self.TAGS].keys():
            default = self.subcmd_definition[cmd_name][
                self.TAGS][param_name]["default"]
            parameters.append(
                click.Option(
                    ["--{}".format(param_name)],
                    default=default,
                    help=
                    "Specify the {} name you'd like, otherwise the default = {}"
                    .format(param_name, default),
                ))

        command_callback = functools.partial(self.cmd_implementation,
                                             self.events_lib,
                                             self.top_level_cmd_name, cmd_name)

        config = get_ctx_defaults(
            cmd_name=cmd_name,
            provider=TomlProvider(section="parameters"),
            ctx=ctx,
            config_env_name=samconfig.DEFAULT_ENV,
        )

        cmd = click.Command(
            name=cmd_name,
            short_help=self.subcmd_definition[cmd_name]["help"],
            context_settings={"default_map": config},
            params=parameters,
            callback=command_callback,
        )

        cmd = debug_option(cmd)
        return cmd