Skip to content

Commit d65b808

Browse files
authored
fix(cli): cfn submit updated to use Windows CLI when run on Windows (#77)
* Use powershell if /bin/bash doesn't exist * Fixed windows path typo * fix windows path for SAM command * use command.exe instead of powershell * explicit change directory to module * Use windows environment variable * Switched to os.path.join and python subprocess shell command * Add windows test for codegen. fix tests on windows * fix zipfile namelist on windows * Marked the windows only part of codegen._build to not be included in coverage report, since it will never run on non windows
1 parent 09a0561 commit d65b808

File tree

2 files changed

+49
-21
lines changed

2 files changed

+49
-21
lines changed

python/rpdk/typescript/codegen.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import os
23
import shutil
34
import sys
45
from subprocess import PIPE, CalledProcessError, run as subprocess_run # nosec
@@ -235,7 +236,7 @@ def _remove_build_artifacts(deps_path):
235236
def _make_build_command(base_path, build_command=None):
236237
command = (
237238
"npm install --optional "
238-
+ f"&& sam build --debug --build-dir {base_path}/build"
239+
+ f"&& sam build --debug --build-dir {os.path.join(base_path, 'build')}"
239240
)
240241
if build_command is not None:
241242
command = build_command
@@ -255,13 +256,30 @@ def _build(self, base_path):
255256

256257
LOG.warning("Starting build.")
257258
try:
258-
completed_proc = subprocess_run( # nosec
259-
["/bin/bash", "-c", command],
260-
stdout=PIPE,
261-
stderr=PIPE,
262-
cwd=base_path,
263-
check=True,
264-
)
259+
# On windows get the default CLI in environment variable comspec
260+
# run 1 command and exit. Building shell command manually, subprocess.run
261+
# with shell=True behavior is inconsistent on windows
262+
if sys.platform == "win32": # pragma: no cover
263+
shell = os.environ.get("comspec")
264+
shell_arg = "/C"
265+
completed_proc = subprocess_run( # nosec
266+
[shell, shell_arg, command],
267+
stdout=PIPE,
268+
stderr=PIPE,
269+
cwd=base_path,
270+
check=True,
271+
)
272+
else: # pragma: no cover
273+
# On all other OS use default shell in subprocess to run build command
274+
completed_proc = subprocess_run( # nosec
275+
[command],
276+
stdout=PIPE,
277+
stderr=PIPE,
278+
cwd=base_path,
279+
check=True,
280+
shell=True,
281+
)
282+
265283
except (FileNotFoundError, CalledProcessError) as e:
266284
raise DownstreamError("local build failed") from e
267285

tests/plugin/codegen_test.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ def test_initialize(project: Project):
101101
".rpdk-config",
102102
"foo-bar-baz.json",
103103
"example_inputs",
104-
"example_inputs/inputs_1_create.json",
105-
"example_inputs/inputs_1_invalid.json",
106-
"example_inputs/inputs_1_update.json",
104+
f"{os.path.join('example_inputs', 'inputs_1_create.json')}",
105+
f"{os.path.join('example_inputs', 'inputs_1_invalid.json')}",
106+
f"{os.path.join('example_inputs', 'inputs_1_update.json')}",
107107
"package.json",
108108
"README.md",
109109
"sam-tests",
110-
"sam-tests/create.json",
110+
f"{os.path.join('sam-tests', 'create.json')}",
111111
"src",
112-
"src/handlers.ts",
112+
f"{os.path.join('src', 'handlers.ts')}",
113113
"template.yml",
114114
"tsconfig.json",
115115
}
@@ -135,7 +135,7 @@ def test_generate(project: Project):
135135
after = get_files_in_project(project)
136136
files = after.keys() - before.keys() - {"resource-role.yaml"}
137137

138-
assert files == {"src/models.ts"}
138+
assert files == {f"{os.path.join('src', 'models.ts')}"}
139139

140140

141141
def test_package_local(project: Project):
@@ -190,10 +190,20 @@ def test__build_docker(plugin: TypescriptLanguagePlugin):
190190
plugin._build(sentinel.base_path)
191191

192192
mock_cmd.assert_called_once_with(sentinel.base_path, None)
193-
mock_subprocess_run.assert_called_once_with(
194-
["/bin/bash", "-c", " --use-container TypeFunction"],
195-
check=True,
196-
cwd=sentinel.base_path,
197-
stderr=-1,
198-
stdout=-1,
199-
)
193+
if sys.platform == "win32":
194+
mock_subprocess_run.assert_called_once_with(
195+
[os.environ.get("comspec"), "/C", " --use-container TypeFunction"],
196+
check=True,
197+
cwd=sentinel.base_path,
198+
stderr=-1,
199+
stdout=-1,
200+
)
201+
else:
202+
mock_subprocess_run.assert_called_once_with(
203+
[" --use-container TypeFunction"],
204+
check=True,
205+
cwd=sentinel.base_path,
206+
stderr=-1,
207+
stdout=-1,
208+
shell=True,
209+
)

0 commit comments

Comments
 (0)