Skip to content

Commit d72144a

Browse files
authored
feat(cli): no-docker CLI switch (#90)
* no docker switch * Remove unsupported python 3.6 * Make no-docker and use-docker mutually exclusive and unit test
1 parent 0b9ef64 commit d72144a

File tree

5 files changed

+138
-13
lines changed

5 files changed

+138
-13
lines changed

.github/workflows/ci.yml

100755100644
File mode changed.

python/rpdk/typescript/codegen.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def __init__(self):
5555
self.package_name = None
5656
self.package_root = None
5757
self._use_docker = None
58+
self._no_docker = None
5859
self._protocol_version = "2.0.0"
5960
self._build_command = None
6061
self._lib_path = None
@@ -72,14 +73,30 @@ def _init_from_project(self, project):
7273

7374
def _init_settings(self, project):
7475
LOG.debug("Writing settings")
75-
self._use_docker = self._use_docker or input_with_validation(
76-
"Use docker for platform-independent packaging (Y/n)?\n",
77-
validate_no,
78-
"This is highly recommended unless you are experienced \n"
79-
"with cross-platform Typescript packaging.",
80-
)
76+
# If use_docker specified in .rpdk-config file or cli switch
77+
# Ensure only 1 is true, with preference to use_docker
78+
if project.settings.get("use_docker") is True:
79+
self._use_docker = True
80+
self._no_docker = False
81+
# If no_docker specified in .rpdk-config file or cli switch
82+
elif project.settings.get("no_docker") is True:
83+
self._use_docker = False
84+
self._no_docker = True
85+
else:
86+
# If neither no_docker nor use_docker specified in .rpdk-config
87+
# file or cli switch, prompt to use containers or not
88+
self._use_docker = input_with_validation(
89+
"Use docker for platform-independent packaging (Y/n)?\n",
90+
validate_no,
91+
"This is highly recommended unless you are experienced \n"
92+
"with cross-platform Typescript packaging.",
93+
)
94+
self._no_docker = not self._use_docker
95+
8196
# switched to 'use_docker' from 'useDocker' to be in line with python version
97+
# project.settings will get saved into .rpdk-config by cloudformation-cli
8298
project.settings["use_docker"] = self._use_docker
99+
project.settings["no_docker"] = self._no_docker
83100
project.settings["protocolVersion"] = self._protocol_version
84101

85102
def init(self, project):

python/rpdk/typescript/parser.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ def setup_subparser(subparsers, parents):
66
)
77
parser.set_defaults(language="typescript")
88

9-
parser.add_argument(
9+
group = parser.add_mutually_exclusive_group()
10+
11+
group.add_argument(
1012
"-d",
1113
"--use-docker",
1214
action="store_true",
@@ -15,4 +17,11 @@ def setup_subparser(subparsers, parents):
1517
with cross-platform TypeScript packaging.""",
1618
)
1719

20+
group.add_argument(
21+
"--no-docker",
22+
action="store_true",
23+
help="""Generally not recommended unless you are experienced
24+
with cross-platform Typescript packaging.""",
25+
)
26+
1827
return parser

tests/plugin/codegen_test.py

Lines changed: 93 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,69 @@ def project(tmp_path: str):
5252
return project
5353

5454

55+
@pytest.fixture
56+
def project_use_docker(tmp_path: str):
57+
project_use_docker = Project(root=tmp_path)
58+
59+
patch_plugins = patch.dict(
60+
"rpdk.core.plugin_registry.PLUGIN_REGISTRY",
61+
{TypescriptLanguagePlugin.NAME: lambda: TypescriptLanguagePlugin},
62+
clear=True,
63+
)
64+
with patch_plugins:
65+
current_path = os.path.abspath(__file__)
66+
lib_abspath = os.path.abspath(os.path.join(current_path, "..", "..", ".."))
67+
TypescriptLanguagePlugin.SUPPORT_LIB_URI = f"file:{lib_abspath}"
68+
project_use_docker.init(
69+
TYPE_NAME,
70+
TypescriptLanguagePlugin.NAME,
71+
settings={"use_docker": True, "no_docker": False},
72+
)
73+
return project_use_docker
74+
75+
76+
@pytest.fixture
77+
def project_no_docker(tmp_path: str):
78+
project_no_docker = Project(root=tmp_path)
79+
80+
patch_plugins = patch.dict(
81+
"rpdk.core.plugin_registry.PLUGIN_REGISTRY",
82+
{TypescriptLanguagePlugin.NAME: lambda: TypescriptLanguagePlugin},
83+
clear=True,
84+
)
85+
with patch_plugins:
86+
current_path = os.path.abspath(__file__)
87+
lib_abspath = os.path.abspath(os.path.join(current_path, "..", "..", ".."))
88+
TypescriptLanguagePlugin.SUPPORT_LIB_URI = f"file:{lib_abspath}"
89+
project_no_docker.init(
90+
TYPE_NAME,
91+
TypescriptLanguagePlugin.NAME,
92+
settings={"use_docker": False, "no_docker": True},
93+
)
94+
return project_no_docker
95+
96+
97+
@pytest.fixture
98+
def project_both_true(tmp_path: str):
99+
project_both_true = Project(root=tmp_path)
100+
101+
patch_plugins = patch.dict(
102+
"rpdk.core.plugin_registry.PLUGIN_REGISTRY",
103+
{TypescriptLanguagePlugin.NAME: lambda: TypescriptLanguagePlugin},
104+
clear=True,
105+
)
106+
with patch_plugins:
107+
current_path = os.path.abspath(__file__)
108+
lib_abspath = os.path.abspath(os.path.join(current_path, "..", "..", ".."))
109+
TypescriptLanguagePlugin.SUPPORT_LIB_URI = f"file:{lib_abspath}"
110+
project_both_true.init(
111+
TYPE_NAME,
112+
TypescriptLanguagePlugin.NAME,
113+
settings={"use_docker": True, "no_docker": True},
114+
)
115+
return project_both_true
116+
117+
55118
def get_files_in_project(project: Project):
56119
return {
57120
str(child.relative_to(project.root)): child for child in project.root.rglob("*")
@@ -92,11 +155,35 @@ def test__remove_build_artifacts_file_not_found(tmp_path: str):
92155
mock_log.debug.assert_called_once()
93156

94157

95-
def test_initialize(project: Project):
96-
lib_path = project._plugin._lib_path
97-
assert project.settings == {"use_docker": False, "protocolVersion": "2.0.0"}
158+
@pytest.fixture
159+
def project_no_docker_use_docker_values(
160+
request, project, project_use_docker, project_no_docker, project_both_true
161+
):
162+
return [
163+
(project, True, False),
164+
(project_use_docker, False, True),
165+
(project_no_docker, True, False),
166+
(project_both_true, False, True),
167+
][request.param]
168+
169+
170+
@pytest.mark.parametrize(
171+
"project_no_docker_use_docker_values", [0, 1, 2, 3], indirect=True
172+
)
173+
def test_initialize(project_no_docker_use_docker_values):
174+
(
175+
project_value,
176+
no_docker_value,
177+
use_docker_value,
178+
) = project_no_docker_use_docker_values
179+
lib_path = project_value._plugin._lib_path
180+
assert project_value.settings == {
181+
"protocolVersion": "2.0.0",
182+
"no_docker": no_docker_value,
183+
"use_docker": use_docker_value,
184+
}
98185

99-
files = get_files_in_project(project)
186+
files = get_files_in_project(project_value)
100187
assert set(files) == {
101188
".gitignore",
102189
".npmrc",
@@ -122,12 +209,12 @@ def test_initialize(project: Project):
122209
assert lib_path in package_json
123210

124211
readme = files["README.md"].read_text()
125-
assert project.type_name in readme
212+
assert project_value.type_name in readme
126213
assert SUPPORT_LIB_NAME in readme
127214
assert "handlers.ts" in readme
128215
assert "models.ts" in readme
129216

130-
assert project.entrypoint in files["template.yml"].read_text()
217+
assert project_value.entrypoint in files["template.yml"].read_text()
131218

132219

133220
def test_generate(project: Project):

tests/plugin/parser_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import argparse
22

3+
import pytest
34
from rpdk.typescript.parser import setup_subparser
45

56

@@ -12,11 +13,22 @@ def test_setup_subparser():
1213
args = sub_parser.parse_args([])
1314
assert args.language == "typescript"
1415
assert args.use_docker is False
16+
assert args.no_docker is False
1517

1618
short_args = sub_parser.parse_args(["-d"])
1719
assert short_args.language == "typescript"
1820
assert short_args.use_docker is True
21+
assert short_args.no_docker is False
1922

2023
long_args = sub_parser.parse_args(["--use-docker"])
2124
assert long_args.language == "typescript"
2225
assert long_args.use_docker is True
26+
assert long_args.no_docker is False
27+
28+
no_docker = sub_parser.parse_args(["--no-docker"])
29+
assert no_docker.language == "typescript"
30+
assert no_docker.use_docker is False
31+
assert no_docker.no_docker is True
32+
33+
with pytest.raises(SystemExit):
34+
sub_parser.parse_args(["--no-docker", "--use-docker"])

0 commit comments

Comments
 (0)