diff --git a/samcli/lib/build/bundler.py b/samcli/lib/build/bundler.py index cd69604083..ba23158d39 100644 --- a/samcli/lib/build/bundler.py +++ b/samcli/lib/build/bundler.py @@ -174,7 +174,8 @@ def _get_path_and_filename_from_handler(handler: str) -> Optional[str]: :return: string path to built handler file """ try: - path = str(Path(handler).parent / Path(handler).stem) + ".js" + path = (Path(handler).parent / Path(handler).stem).as_posix() + path = path + ".js" except (AttributeError, TypeError): return None return path diff --git a/tests/unit/lib/build_module/test_bundler.py b/tests/unit/lib/build_module/test_bundler.py index c25543b09e..8bfc35daaf 100644 --- a/tests/unit/lib/build_module/test_bundler.py +++ b/tests/unit/lib/build_module/test_bundler.py @@ -1,10 +1,11 @@ from pathlib import Path -from unittest import TestCase +from unittest import TestCase, skipIf from unittest.mock import patch, Mock from parameterized import parameterized from samcli.lib.build.bundler import EsbuildBundlerManager +from tests.testing_utils import IS_WINDOWS from tests.unit.commands.buildcmd.test_build_context import DummyStack @@ -178,7 +179,7 @@ class PostProcessHandler(TestCase): def test_get_path_and_filename_from_handler(self): handler = "src/functions/FunctionName/app.Handler" file = EsbuildBundlerManager._get_path_and_filename_from_handler(handler) - expected_path = str(Path("src") / "functions" / "FunctionName" / "app.js") + expected_path = (Path("src") / "functions" / "FunctionName" / "app.js").as_posix() self.assertEqual(file, expected_path) @patch("samcli.lib.build.bundler.Path.__init__") @@ -240,3 +241,22 @@ def test_update_function_handler(self): self.assertEqual(updated_handler_a, "app.handler") self.assertEqual(updated_handler_b, "app.handler") self.assertEqual(updated_handler_c, "functions/source/update/app.handler") + + @parameterized.expand( + [("/opt/my/path/handler.handler", "/opt/my/path/handler.js"), ("handler.handler", "handler.js")] + ) + def test_get_handler_path_unix(self, input_path, expected_path): + result_path = EsbuildBundlerManager(Mock())._get_path_and_filename_from_handler(input_path) + + self.assertEqual(result_path, expected_path) + + @parameterized.expand( + [ + ("\\opt\\my\\path\\handler.handler", "/opt/my/path/handler.js"), + ] + ) + @skipIf(not IS_WINDOWS, "Skipping POSIX converting logic since WindowsPath is not available on unix systems") + def test_get_handler_windows_returns_posix(self, input_path, expected_path): + result_path = EsbuildBundlerManager(Mock())._get_path_and_filename_from_handler(input_path) + + self.assertEqual(result_path, expected_path)