Skip to content

Commit 4bbb53a

Browse files
committed
fix(dotnet): simplify build tool lookup
issue: #347
1 parent b4d6a42 commit 4bbb53a

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

aws_lambda_builders/workflows/dotnet_clipackage/actions.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,24 @@ def execute(self):
4242
LOG.info("Skipping to update Amazon.Lambda.Tools install/update, since it is updated recently")
4343
return
4444

45+
try:
46+
LOG.debug("Checking if Amazon.Lambda.Tools is already installed")
47+
output = self.subprocess_dotnet.run(["tool", "list", "-g"])
48+
if "amazon.lambda.tools" in output.lower():
49+
LOG.info("Amazon.Lambda.Tools already installed. Skipping install.")
50+
GlobalToolInstallAction.__tools_installed = True
51+
return
52+
except DotnetCLIExecutionError:
53+
pass
54+
4555
try:
4656
LOG.debug("Installing Amazon.Lambda.Tools Global Tool")
4757
self.subprocess_dotnet.run(["tool", "install", "-g", "Amazon.Lambda.Tools", "--ignore-failed-sources"])
4858
GlobalToolInstallAction.__tools_installed = True
49-
except DotnetCLIExecutionError:
50-
LOG.debug("Error installing probably due to already installed. Attempt to update to latest version.")
51-
try:
52-
self.subprocess_dotnet.run(
53-
["tool", "update", "-g", "Amazon.Lambda.Tools", "--ignore-failed-sources"]
54-
)
55-
GlobalToolInstallAction.__tools_installed = True
56-
except DotnetCLIExecutionError as ex:
57-
raise ActionFailedError(
58-
"Error configuring the Amazon.Lambda.Tools .NET Core Global Tool: " + str(ex)
59-
)
59+
except DotnetCLIExecutionError as ex:
60+
raise ActionFailedError(
61+
"Error installing the Amazon.Lambda.Tools .NET Core Global Tool: " + str(ex)
62+
)
6063

6164

6265
class RunPackageAction(BaseAction):

aws_lambda_builders/workflows/dotnet_clipackage/dotnetcli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,5 @@ def run(self, args, cwd=None):
6363

6464
if p.returncode != 0:
6565
raise DotnetCLIExecutionError(message=decode(err))
66+
67+
return decode(out)

tests/unit/workflows/dotnet_clipackage/test_actions.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,34 @@ def tearDown(self):
2222
self.subprocess_dotnet.reset_mock()
2323

2424
def test_global_tool_install(self):
25+
self.subprocess_dotnet.run.return_value = ""
2526
action = GlobalToolInstallAction(self.subprocess_dotnet)
2627
action.execute()
27-
self.subprocess_dotnet.run.assert_called_once_with(
28+
self.subprocess_dotnet.run.assert_any_call(["tool", "list", "-g"])
29+
self.subprocess_dotnet.run.assert_any_call(
2830
["tool", "install", "-g", "Amazon.Lambda.Tools", "--ignore-failed-sources"]
2931
)
3032

31-
def test_global_tool_update(self):
32-
self.subprocess_dotnet.run.side_effect = [DotnetCLIExecutionError(message="Already Installed"), None]
33+
def test_global_tool_already_installed(self):
34+
self.subprocess_dotnet.run.return_value = (
35+
"Package Id Version Commands\n"
36+
"--------------------------------------------------------------\n"
37+
"amazon.lambda.tools 5.3.0 dotnet-lambda\n"
38+
)
3339
action = GlobalToolInstallAction(self.subprocess_dotnet)
3440
action.execute()
35-
self.subprocess_dotnet.run.assert_any_call(
36-
["tool", "install", "-g", "Amazon.Lambda.Tools", "--ignore-failed-sources"]
37-
)
38-
self.subprocess_dotnet.run.assert_any_call(
39-
["tool", "update", "-g", "Amazon.Lambda.Tools", "--ignore-failed-sources"]
40-
)
41+
self.subprocess_dotnet.run.assert_called_once_with(["tool", "list", "-g"])
4142

42-
def test_global_tool_update_failed(self):
43+
def test_global_tool_install_failed(self):
4344
self.subprocess_dotnet.run.side_effect = [
44-
DotnetCLIExecutionError(message="Already Installed"),
45-
DotnetCLIExecutionError(message="Updated Failed"),
45+
"", # tool list returns empty (not installed)
46+
DotnetCLIExecutionError(message="Install Failed"),
4647
]
4748
action = GlobalToolInstallAction(self.subprocess_dotnet)
4849
self.assertRaises(ActionFailedError, action.execute)
4950

5051
def test_global_tool_parallel(self):
52+
self.subprocess_dotnet.run.return_value = ""
5153
actions = [
5254
GlobalToolInstallAction(self.subprocess_dotnet),
5355
GlobalToolInstallAction(self.subprocess_dotnet),
@@ -58,9 +60,11 @@ def test_global_tool_parallel(self):
5860
for action in actions:
5961
executor.submit(action.execute)
6062

61-
self.subprocess_dotnet.run.assert_called_once_with(
63+
self.subprocess_dotnet.run.assert_any_call(["tool", "list", "-g"])
64+
self.subprocess_dotnet.run.assert_any_call(
6265
["tool", "install", "-g", "Amazon.Lambda.Tools", "--ignore-failed-sources"]
6366
)
67+
self.assertEqual(self.subprocess_dotnet.run.call_count, 2)
6468

6569

6670
class TestRunPackageAction(TestCase):

0 commit comments

Comments
 (0)