Skip to content
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

get_handler_file_from_name when path contains periods #5849

Merged
merged 3 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 24 additions & 13 deletions localstack/services/awslambda/lambda_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,35 @@ def is_provided_runtime(runtime_details: Union[LambdaFunction, str]) -> bool:
return runtime.startswith("provided")


def format_name_to_path(handler_name: str, delimiter: str, extension: str):
file_path = handler_name.rpartition(delimiter)[0]
if delimiter == ":":
file_path = file_path.split(delimiter)[0]

if os.path.sep not in file_path:
file_path = file_path.replace(".", os.path.sep)

if file_path.startswith(".%s" % os.path.sep):
tmbobbins marked this conversation as resolved.
Show resolved Hide resolved
file_path = file_path[2:]

return "%s%s" % (file_path, extension)
tmbobbins marked this conversation as resolved.
Show resolved Hide resolved


def get_handler_file_from_name(handler_name: str, runtime: str = None):
runtime = runtime or LAMBDA_DEFAULT_RUNTIME

if runtime.startswith(LAMBDA_RUNTIME_PROVIDED):
return "bootstrap"
delimiter = "."
if runtime.startswith(LAMBDA_RUNTIME_NODEJS):
file_ext = ".js"
elif runtime.startswith(LAMBDA_RUNTIME_GOLANG):
file_ext = ""
elif runtime.startswith(tuple(DOTNET_LAMBDA_RUNTIMES)):
file_ext = ".dll"
delimiter = ":"
elif runtime.startswith(LAMBDA_RUNTIME_RUBY):
file_ext = ".rb"
else:
handler_name = handler_name.rpartition(delimiter)[0].replace(delimiter, os.path.sep)
file_ext = ".py"
return "%s%s" % (handler_name.split(delimiter)[0], file_ext)
return format_name_to_path(handler_name, ".", ".js")
if runtime.startswith(LAMBDA_RUNTIME_GOLANG):
return format_name_to_path(handler_name, ".", "")
if runtime.startswith(tuple(DOTNET_LAMBDA_RUNTIMES)):
return format_name_to_path(handler_name, ":", ".dll")
if runtime.startswith(LAMBDA_RUNTIME_RUBY):
return format_name_to_path(handler_name, ".", ".rb")

return format_name_to_path(handler_name, ".", ".py")


def is_java_lambda(lambda_details):
Expand Down
Empty file.
43 changes: 43 additions & 0 deletions tests/unit/services/awslambda/test_lambda_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from localstack.services.awslambda.lambda_utils import (
LAMBDA_RUNTIME_DOTNETCORE31,
LAMBDA_RUNTIME_GOLANG,
LAMBDA_RUNTIME_NODEJS,
LAMBDA_RUNTIME_PROVIDED,
LAMBDA_RUNTIME_RUBY,
format_name_to_path,
get_handler_file_from_name,
)


class TestLambdaUtils:
def test_format_name_to_path(self):
assert ".build/handler.js" == format_name_to_path(".build/handler.execute", ".", ".js")
assert "handler" == format_name_to_path("handler.execute", ".", "")
assert "CSharpHandlers.dll" == format_name_to_path(
"./CSharpHandlers::AwsDotnetCsharp.Handler::CreateProfileAsync",
":",
".dll",
)
assert "test/handler.rb" == format_name_to_path("test.handler.execute", ".", ".rb")
assert "test.handler.py" == format_name_to_path("./test.handler.execute", ".", ".py")
assert "../handler.js" == format_name_to_path("../handler.execute", ".", ".js")

def test_get_handler_file_from_name(self):
assert ".build/handler.js" == get_handler_file_from_name(
".build/handler.execute", LAMBDA_RUNTIME_NODEJS
)
assert ".build/handler" == get_handler_file_from_name(
"./.build/handler.execute", LAMBDA_RUNTIME_GOLANG
)
assert "CSharpHandlers.dll" == get_handler_file_from_name(
"./CSharpHandlers::AwsDotnetCsharp.Handler::CreateProfileAsync",
LAMBDA_RUNTIME_DOTNETCORE31,
)
assert "test/handler.rb" == get_handler_file_from_name(
"test.handler.execute", LAMBDA_RUNTIME_RUBY
)
assert "test.handler" == get_handler_file_from_name(
"./test.handler.execute", LAMBDA_RUNTIME_GOLANG
)
assert "../handler.py" == get_handler_file_from_name("../handler.execute")
assert "bootstrap" == get_handler_file_from_name("", LAMBDA_RUNTIME_PROVIDED)