Skip to content
This repository has been archived by the owner on May 26, 2021. It is now read-only.

Commit

Permalink
feat(cloudformation): get capabilities through PyInquirer
Browse files Browse the repository at this point in the history
  • Loading branch information
kazhala committed Aug 10, 2020
1 parent 9f24613 commit d94da6e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 45 deletions.
31 changes: 21 additions & 10 deletions fzfaws/cloudformation/cloudformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import re
import sys
from typing import Any, Callable, Dict, Generator, List, Tuple, Union
from PyInquirer import prompt
from fzfaws.utils import prompt_style

from fzfaws.utils import (
BaseSession,
Expand Down Expand Up @@ -165,17 +167,26 @@ def _get_capabilities(self, message: str = "") -> List[str]:
:return: selected capabilities to acknowledge
:rtype: List[str]
"""
fzf = Pyfzf()
fzf.append_fzf("CAPABILITY_IAM\n")
fzf.append_fzf("CAPABILITY_NAMED_IAM\n")
fzf.append_fzf("CAPABILITY_AUTO_EXPAND")
message += "\nPlease select the capabilities to acknowledge and proceed"
message += "\nMore information: https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_CreateStack.html"
return list(
fzf.execute_fzf(
empty_allow=True, print_col=1, multi_select=True, header=message
)
print(message)
print(
"More information: https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_CreateStack.html"
)
questions = [
{
"type": "checkbox",
"name": "answer",
"message": "Select the capabilities to acknowledge and proceed",
"choices": [
{"name": "CAPABILITY_IAM"},
{"name": "CAPABILITY_NAMED_IAM"},
{"name": "CAPABILITY_AUTO_EXPAND"},
],
}
]
result = prompt(questions, style=prompt_style)
if not result:
raise KeyboardInterrupt
return result.get("answer", [])

def _get_stack_generator(
self, response: List[Dict[str, Any]]
Expand Down
62 changes: 27 additions & 35 deletions tests/cloudformation/test_cloudformation.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import io
import json
import os
from pathlib import Path
import sys
import unittest
from unittest.mock import ANY, call, patch
from pathlib import Path
from unittest.mock import ANY, patch

from botocore.paginate import Paginator
from botocore.waiter import Waiter

from fzfaws.cloudformation import Cloudformation
from fzfaws.utils import FileLoader
from fzfaws.utils.pyfzf import Pyfzf
from fzfaws.utils import FileLoader, Pyfzf, prompt_style


class TestCloudformation(unittest.TestCase):
Expand Down Expand Up @@ -231,43 +230,36 @@ def hello(**kwargs):
mocked_confirm.return_value = False
self.assertRaises(SystemExit, self.cloudformation.execute_with_capabilities)

@patch.object(Pyfzf, "execute_fzf")
@patch.object(Pyfzf, "append_fzf")
def test_get_capabilities(self, mocked_append, mocked_execute):
mocked_execute.return_value = ["CAPABILITY_IAM"]
@patch("fzfaws.cloudformation.cloudformation.prompt")
def test_get_capabilities(self, mocked_prompt):
mocked_prompt.return_value = {"answer": ["CAPABILITY_IAM"]}
result = self.cloudformation._get_capabilities(message="lol")
mocked_append.assert_has_calls(
mocked_prompt.assert_called_once_with(
[
call("CAPABILITY_IAM\n"),
call("CAPABILITY_NAMED_IAM\n"),
call("CAPABILITY_AUTO_EXPAND"),
]
)
mocked_execute.assert_called_once_with(
empty_allow=True,
print_col=1,
multi_select=True,
header="lol\nPlease select the capabilities to acknowledge and proceed\nMore information: https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_CreateStack.html",
{
"type": "checkbox",
"name": "answer",
"message": "Select the capabilities to acknowledge and proceed",
"choices": [
{"name": "CAPABILITY_IAM"},
{"name": "CAPABILITY_NAMED_IAM"},
{"name": "CAPABILITY_AUTO_EXPAND"},
],
}
],
style=prompt_style,
)
self.assertEqual(result, ["CAPABILITY_IAM"])
self.assertEqual(
self.capturedOutput.getvalue(),
"lol\nMore information: https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_CreateStack.html\n",
)

mocked_execute.reset_mock()
mocked_append.reset_mock()
mocked_execute.return_value = ["CAPABILITY_IAM", "CAPABILITY_AUTO_EXPAND"]
mocked_prompt.reset_mock()
mocked_prompt.return_value = {
"answer": ["CAPABILITY_IAM", "CAPABILITY_AUTO_EXPAND"]
}
result = self.cloudformation._get_capabilities()
mocked_append.assert_has_calls(
[
call("CAPABILITY_IAM\n"),
call("CAPABILITY_NAMED_IAM\n"),
call("CAPABILITY_AUTO_EXPAND"),
]
)
mocked_execute.assert_called_once_with(
empty_allow=True,
print_col=1,
multi_select=True,
header="\nPlease select the capabilities to acknowledge and proceed\nMore information: https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_CreateStack.html",
)
self.assertEqual(result, ["CAPABILITY_IAM", "CAPABILITY_AUTO_EXPAND"])

def test_get_stack_generator(self):
Expand Down

0 comments on commit d94da6e

Please sign in to comment.