From 3a3aaad4f77382748cd649f19c7a0c7d5607390a Mon Sep 17 00:00:00 2001 From: Sriram Madapusi Vasudevan Date: Thu, 19 Feb 2026 19:03:21 +0000 Subject: [PATCH] fix(dotnet): simplify build tool lookup issue: https://github.com/aws/aws-lambda-builders/issues/347 --- .../workflows/dotnet_clipackage/actions.py | 23 +++++++------- .../workflows/dotnet_clipackage/dotnetcli.py | 2 ++ .../dotnet_clipackage/test_actions.py | 30 +++++++++++-------- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/aws_lambda_builders/workflows/dotnet_clipackage/actions.py b/aws_lambda_builders/workflows/dotnet_clipackage/actions.py index 2f9f31601..f72ee6f0e 100644 --- a/aws_lambda_builders/workflows/dotnet_clipackage/actions.py +++ b/aws_lambda_builders/workflows/dotnet_clipackage/actions.py @@ -42,21 +42,22 @@ def execute(self): LOG.info("Skipping to update Amazon.Lambda.Tools install/update, since it is updated recently") return + try: + LOG.debug("Checking if Amazon.Lambda.Tools is already installed") + output = self.subprocess_dotnet.run(["tool", "list", "-g"]) + if "amazon.lambda.tools" in output.lower(): + LOG.info("Amazon.Lambda.Tools already installed. Skipping install.") + GlobalToolInstallAction.__tools_installed = True + return + except DotnetCLIExecutionError: + pass + try: LOG.debug("Installing Amazon.Lambda.Tools Global Tool") self.subprocess_dotnet.run(["tool", "install", "-g", "Amazon.Lambda.Tools", "--ignore-failed-sources"]) GlobalToolInstallAction.__tools_installed = True - except DotnetCLIExecutionError: - LOG.debug("Error installing probably due to already installed. Attempt to update to latest version.") - try: - self.subprocess_dotnet.run( - ["tool", "update", "-g", "Amazon.Lambda.Tools", "--ignore-failed-sources"] - ) - GlobalToolInstallAction.__tools_installed = True - except DotnetCLIExecutionError as ex: - raise ActionFailedError( - "Error configuring the Amazon.Lambda.Tools .NET Core Global Tool: " + str(ex) - ) + except DotnetCLIExecutionError as ex: + raise ActionFailedError("Error installing the Amazon.Lambda.Tools .NET Core Global Tool: " + str(ex)) class RunPackageAction(BaseAction): diff --git a/aws_lambda_builders/workflows/dotnet_clipackage/dotnetcli.py b/aws_lambda_builders/workflows/dotnet_clipackage/dotnetcli.py index f78a9b927..bc549374d 100644 --- a/aws_lambda_builders/workflows/dotnet_clipackage/dotnetcli.py +++ b/aws_lambda_builders/workflows/dotnet_clipackage/dotnetcli.py @@ -63,3 +63,5 @@ def run(self, args, cwd=None): if p.returncode != 0: raise DotnetCLIExecutionError(message=decode(err)) + + return decode(out) diff --git a/tests/unit/workflows/dotnet_clipackage/test_actions.py b/tests/unit/workflows/dotnet_clipackage/test_actions.py index c9e54a518..baaf0a14e 100644 --- a/tests/unit/workflows/dotnet_clipackage/test_actions.py +++ b/tests/unit/workflows/dotnet_clipackage/test_actions.py @@ -22,32 +22,34 @@ def tearDown(self): self.subprocess_dotnet.reset_mock() def test_global_tool_install(self): + self.subprocess_dotnet.run.return_value = "" action = GlobalToolInstallAction(self.subprocess_dotnet) action.execute() - self.subprocess_dotnet.run.assert_called_once_with( + self.subprocess_dotnet.run.assert_any_call(["tool", "list", "-g"]) + self.subprocess_dotnet.run.assert_any_call( ["tool", "install", "-g", "Amazon.Lambda.Tools", "--ignore-failed-sources"] ) - def test_global_tool_update(self): - self.subprocess_dotnet.run.side_effect = [DotnetCLIExecutionError(message="Already Installed"), None] + def test_global_tool_already_installed(self): + self.subprocess_dotnet.run.return_value = ( + "Package Id Version Commands\n" + "--------------------------------------------------------------\n" + "amazon.lambda.tools 5.3.0 dotnet-lambda\n" + ) action = GlobalToolInstallAction(self.subprocess_dotnet) action.execute() - self.subprocess_dotnet.run.assert_any_call( - ["tool", "install", "-g", "Amazon.Lambda.Tools", "--ignore-failed-sources"] - ) - self.subprocess_dotnet.run.assert_any_call( - ["tool", "update", "-g", "Amazon.Lambda.Tools", "--ignore-failed-sources"] - ) + self.subprocess_dotnet.run.assert_called_once_with(["tool", "list", "-g"]) - def test_global_tool_update_failed(self): + def test_global_tool_install_failed(self): self.subprocess_dotnet.run.side_effect = [ - DotnetCLIExecutionError(message="Already Installed"), - DotnetCLIExecutionError(message="Updated Failed"), + "", # tool list returns empty (not installed) + DotnetCLIExecutionError(message="Install Failed"), ] action = GlobalToolInstallAction(self.subprocess_dotnet) self.assertRaises(ActionFailedError, action.execute) def test_global_tool_parallel(self): + self.subprocess_dotnet.run.return_value = "" actions = [ GlobalToolInstallAction(self.subprocess_dotnet), GlobalToolInstallAction(self.subprocess_dotnet), @@ -58,9 +60,11 @@ def test_global_tool_parallel(self): for action in actions: executor.submit(action.execute) - self.subprocess_dotnet.run.assert_called_once_with( + self.subprocess_dotnet.run.assert_any_call(["tool", "list", "-g"]) + self.subprocess_dotnet.run.assert_any_call( ["tool", "install", "-g", "Amazon.Lambda.Tools", "--ignore-failed-sources"] ) + self.assertEqual(self.subprocess_dotnet.run.call_count, 2) class TestRunPackageAction(TestCase):