Skip to content

Fix(CI): Fix unittests on Windows #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion python/rpdk/python/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,12 @@ def _pip_build(cls, base_path):
LOG.warning("Starting pip build.")
try:
completed_proc = subprocess_run( # nosec
command, stdout=PIPE, stderr=PIPE, cwd=base_path, check=True
command,
stdout=PIPE,
stderr=PIPE,
cwd=base_path,
check=True,
shell=True,
)
except (FileNotFoundError, CalledProcessError) as e:
raise DownstreamError("pip build failed") from e
Expand Down
2 changes: 1 addition & 1 deletion src/cloudformation_cli_python_lib/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def print_or_log(message: str) -> None:
print_or_log("Base exception caught (this is usually bad) {0}".format(e))
progress = ProgressEvent.failed(HandlerErrorCode.InternalFailure)

if progress.result:
if progress.result: # pragma: no cover
progress.result = None

# use the raw event_data as a last-ditch attempt to call back if the
Expand Down
46 changes: 26 additions & 20 deletions tests/plugin/codegen_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import ast
import importlib.util
import os
from docker.errors import APIError, ContainerError, ImageLoadError
from pathlib import Path
from requests.exceptions import ConnectionError as RequestsConnectionError
Expand Down Expand Up @@ -181,14 +182,14 @@ def test_initialize_resource(resource_project):
"README.md",
"foo-bar-baz.json",
"requirements.txt",
"example_inputs/inputs_1_invalid.json",
"example_inputs/inputs_1_update.json",
"example_inputs/inputs_1_create.json",
f"{os.path.join('example_inputs', 'inputs_1_create.json')}",
f"{os.path.join('example_inputs', 'inputs_1_invalid.json')}",
f"{os.path.join('example_inputs', 'inputs_1_update.json')}",
"example_inputs",
"src",
"src/foo_bar_baz",
"src/foo_bar_baz/__init__.py",
"src/foo_bar_baz/handlers.py",
f"{os.path.join('src', 'foo_bar_baz')}",
f"{os.path.join('src', 'foo_bar_baz', '__init__.py')}",
f"{os.path.join('src', 'foo_bar_baz', 'handlers.py')}",
"template.yml",
}

Expand All @@ -204,8 +205,8 @@ def test_initialize_resource(resource_project):
assert resource_project.entrypoint in files["template.yml"].read_text()

# this is a rough check the generated Python code is valid as far as syntax
ast.parse(files["src/foo_bar_baz/__init__.py"].read_text())
ast.parse(files["src/foo_bar_baz/handlers.py"].read_text())
ast.parse(files[f"{os.path.join('src', 'foo_bar_baz', '__init__.py')}"].read_text())
ast.parse(files[f"{os.path.join('src', 'foo_bar_baz', 'handlers.py')}"].read_text())


def test_initialize_hook(hook_project):
Expand All @@ -219,9 +220,9 @@ def test_initialize_hook(hook_project):
"foo-bar-baz.json",
"requirements.txt",
"src",
"src/foo_bar_baz",
"src/foo_bar_baz/__init__.py",
"src/foo_bar_baz/handlers.py",
f"{os.path.join('src', 'foo_bar_baz')}",
f"{os.path.join('src', 'foo_bar_baz', '__init__.py')}",
f"{os.path.join('src', 'foo_bar_baz', 'handlers.py')}",
"template.yml",
}

Expand All @@ -237,8 +238,8 @@ def test_initialize_hook(hook_project):
assert hook_project.entrypoint in files["template.yml"].read_text()

# this is a rough check the generated Python code is valid as far as syntax
ast.parse(files["src/foo_bar_baz/__init__.py"].read_text())
ast.parse(files["src/foo_bar_baz/handlers.py"].read_text())
ast.parse(files[f"{os.path.join('src', 'foo_bar_baz', '__init__.py')}"].read_text())
ast.parse(files[f"{os.path.join('src', 'foo_bar_baz', 'handlers.py')}"].read_text())


def test_generate_resource(resource_project):
Expand All @@ -248,9 +249,9 @@ def test_generate_resource(resource_project):
after = get_files_in_project(resource_project)
files = after.keys() - before.keys() - {"resource-role.yaml"}
print("Project files: ", get_files_in_project(resource_project))
assert files == {"src/foo_bar_baz/models.py"}
assert files == {f"{os.path.join('src', 'foo_bar_baz', 'models.py')}"}

models_path = after["src/foo_bar_baz/models.py"]
models_path = after[f"{os.path.join('src', 'foo_bar_baz', 'models.py')}"]
# this is a rough check the generated Python code is valid as far as syntax
ast.parse(models_path.read_text())

Expand Down Expand Up @@ -280,11 +281,11 @@ def test_generate_hook(hook_project):
files = after.keys() - before.keys() - {"hook-role.yaml"}
print("Project files: ", get_files_in_project(hook_project))
assert files == {
"src/foo_bar_baz/models.py",
f"{os.path.join('src', 'foo_bar_baz', 'models.py')}",
"foo-bar-baz-configuration.json",
}

models_path = after["src/foo_bar_baz/models.py"]
models_path = after[f"{os.path.join('src', 'foo_bar_baz', 'models.py')}"]
# this is a rough check the generated Python code is valid as far as syntax
ast.parse(models_path.read_text())

Expand Down Expand Up @@ -318,7 +319,10 @@ def test_generate_resource_with_type_configuration(tmp_path):
project.init(type_name, PythonLanguagePlugin.NAME)

copyfile(
str(Path.cwd() / "tests/data/schema-with-typeconfiguration.json"),
str(
Path.cwd()
/ f"{os.path.join('tests', 'data', 'schema-with-typeconfiguration.json')}"
),
str(project.root / "schema-with-typeconfiguration.json"),
)
project.type_info = ("schema", "with", "typeconfiguration")
Expand Down Expand Up @@ -381,7 +385,8 @@ def test__pip_build_executable_not_found(tmp_path):

mock_cmd.assert_called_once_with(tmp_path)

assert isinstance(excinfo.value.__cause__, FileNotFoundError)
# FileNotFoundError raised on Windows, CalledProcessError on POSIX systems
assert isinstance(excinfo.value.__cause__, (FileNotFoundError, CalledProcessError))


def test__pip_build_called_process_error(tmp_path):
Expand All @@ -395,7 +400,8 @@ def test__pip_build_called_process_error(tmp_path):

mock_cmd.assert_called_once_with(tmp_path)

assert isinstance(excinfo.value.__cause__, CalledProcessError)
# FileNotFoundError raised on Windows, CalledProcessError on POSIX systems
assert isinstance(excinfo.value.__cause__, (FileNotFoundError, CalledProcessError))


def test__build_pip(plugin):
Expand Down