Skip to content

Commit 05f6bc7

Browse files
authored
Breaking change: Add the new tool command and move the existing default behavior to a start command
1 parent 837ad64 commit 05f6bc7

File tree

9 files changed

+170
-39
lines changed

9 files changed

+170
-39
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"Projects": [
3+
{
4+
"Name": "Amazon.Lambda.TestTool",
5+
"Type": "Patch",
6+
"ChangelogMessages": [
7+
"Breaking change: Switch to use commands to invoke the tool. For example to run the Lambda emulator use the command 'dotnet lambda-test-tool start --lambda-emulator-port 5050'",
8+
"Add new info command to get metadata about the tool. For example getting the version number of the tool."
9+
]
10+
}
11+
]
12+
}

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Commands/RunCommand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
using System.Diagnostics;
5+
using System.Text.Json;
56
using Amazon.Lambda.TestTool.Commands.Settings;
67
using Amazon.Lambda.TestTool.Extensions;
78
using Amazon.Lambda.TestTool.Models;

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Commands/Settings/RunCommandSettings.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Amazon.Lambda.TestTool.Commands.Settings;
99

1010
/// <summary>
11-
/// Represents the settings for configuring the <see cref="RunCommand"/>, which is the default command.
11+
/// Represents the settings for configuring the <see cref="RunCommand"/>.
1212
/// </summary>
1313
public sealed class RunCommandSettings : CommandSettings
1414
{
@@ -36,22 +36,6 @@ public sealed class RunCommandSettings : CommandSettings
3636
[Description("Disable auto launching the test tool's web interface in a browser.")]
3737
public bool NoLaunchWindow { get; set; }
3838

39-
/// <summary>
40-
/// If set to true the test tool will pause waiting for a key input before exiting.
41-
/// The is useful when executing from an IDE so you can avoid having the output window immediately disappear after executing the Lambda code.
42-
/// The default value is true.
43-
/// </summary>
44-
[CommandOption("--pause-exit")]
45-
[Description("If set to true the test tool will pause waiting for a key input before exiting. The is useful when executing from an IDE so you can avoid having the output window immediately disappear after executing the Lambda code. The default value is true.")]
46-
public bool PauseExit { get; set; }
47-
48-
/// <summary>
49-
/// Disables logging in the application
50-
/// </summary>
51-
[CommandOption("--disable-logs")]
52-
[Description("Disables logging in the application")]
53-
public bool DisableLogs { get; set; }
54-
5539
/// <summary>
5640
/// The API Gateway Emulator Mode specifies the format of the event that API Gateway sends to a Lambda integration,
5741
/// and how API Gateway interprets the response from Lambda.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using Amazon.Lambda.TestTool.Models;
5+
using Spectre.Console.Cli;
6+
using System.ComponentModel;
7+
8+
namespace Amazon.Lambda.TestTool.Commands.Settings;
9+
10+
/// <summary>
11+
/// Represents the settings for configuring the <see cref="ToolInfoCommand"/>.
12+
/// </summary>
13+
public sealed class ToolInfoCommandSettings : CommandSettings
14+
{
15+
public enum InfoFormat
16+
{
17+
Text,
18+
Json
19+
}
20+
21+
/// <summary>
22+
/// The format the info is displayed as.
23+
/// The available formats are: Text, Json.
24+
/// </summary>
25+
[CommandOption("--format <FORMAT>")]
26+
[Description(
27+
"The format the info is displayed as. " +
28+
"The available formats are: Text, Json.")]
29+
public InfoFormat? Format { get; set; }
30+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using System.Text;
5+
using System.Text.Json;
6+
using Amazon.Lambda.TestTool.Commands.Settings;
7+
using Amazon.Lambda.TestTool.Models;
8+
using Amazon.Lambda.TestTool.Services;
9+
using Amazon.Lambda.TestTool.Utilities;
10+
using Spectre.Console.Cli;
11+
12+
namespace Amazon.Lambda.TestTool.Commands;
13+
14+
/// <summary>
15+
/// Command to display tool information like the version of the tool.
16+
/// </summary>
17+
/// <param name="toolInteractiveService"></param>
18+
public class ToolInfoCommand(IToolInteractiveService toolInteractiveService)
19+
: Command<ToolInfoCommandSettings>
20+
{
21+
/// <summary>
22+
/// The method responsible for executing the <see cref="RunCommand"/>.
23+
/// </summary>
24+
public override int Execute(CommandContext context, ToolInfoCommandSettings settings)
25+
{
26+
var info = CollectInformation();
27+
28+
var formattedInfo = settings.Format switch
29+
{
30+
ToolInfoCommandSettings.InfoFormat.Text => GenerateToolInfoText(info),
31+
ToolInfoCommandSettings.InfoFormat.Json => GenerateToolInfoJson(info),
32+
_ => GenerateToolInfoText(info)
33+
};
34+
35+
toolInteractiveService.WriteLine(formattedInfo);
36+
return CommandReturnCodes.Success;
37+
}
38+
39+
private string GenerateToolInfoText(IDictionary<string, string> info)
40+
{
41+
var stringBuilder = new StringBuilder();
42+
foreach(var kvp in info)
43+
{
44+
stringBuilder.AppendLine($"{kvp.Key}: {kvp.Value}");
45+
}
46+
47+
return stringBuilder.ToString();
48+
}
49+
50+
private string GenerateToolInfoJson(IDictionary<string, string> info)
51+
{
52+
var stream = new MemoryStream();
53+
Utf8JsonWriter utf8JsonWriter = new Utf8JsonWriter(stream, options: new JsonWriterOptions()
54+
{
55+
Indented = false
56+
});
57+
58+
utf8JsonWriter.WriteStartObject();
59+
60+
foreach (var kvp in info)
61+
{
62+
utf8JsonWriter.WriteString(kvp.Key, kvp.Value);
63+
}
64+
65+
utf8JsonWriter.WriteEndObject();
66+
utf8JsonWriter.Flush();
67+
68+
stream.Position = 0;
69+
return new StreamReader(stream).ReadToEnd();
70+
}
71+
72+
private Dictionary<string, string> CollectInformation()
73+
{
74+
var info = new Dictionary<string, string>();
75+
info["Version"] = Utils.DetermineToolVersion();
76+
info["InstallPath"] = GetInstallPath();
77+
return info;
78+
}
79+
80+
private string GetInstallPath() => Directory.GetParent(typeof(Utils).Assembly.Location)!.FullName;
81+
}

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Program.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@
1313

1414
var registrar = new TypeRegistrar(serviceCollection);
1515

16-
var app = new CommandApp<RunCommand>(registrar);
16+
var app = new CommandApp(registrar);
1717
app.Configure(config =>
1818
{
19+
config.AddCommand<RunCommand>("start")
20+
.WithDescription("Start the Lambda and/or API Gateway emulator.");
21+
config.AddCommand<ToolInfoCommand>("info")
22+
.WithDescription("Display information about the tool including the version number.");
23+
1924
config.SetApplicationName(Constants.ToolName);
2025
});
2126

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Utilities/Utils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static string DetermineToolVersion()
2121
AssemblyInformationalVersionAttribute? attribute = null;
2222
try
2323
{
24-
var assembly = Assembly.GetEntryAssembly();
24+
var assembly = typeof(Utils).Assembly;
2525
if (assembly == null)
2626
return unknownVersion;
2727
attribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();

Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/Commands/RunCommandTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Xunit;
1212
using Amazon.Lambda.TestTool.Services.IO;
1313
using Amazon.Lambda.TestTool.Utilities;
14+
using System.Text.Json.Nodes;
1415

1516
namespace Amazon.Lambda.TestTool.UnitTests.Commands;
1617

@@ -100,4 +101,41 @@ public async Task ExecuteAsync_EnvPorts_SuccessfulLaunch()
100101
Assert.Equal(CommandReturnCodes.Success, result);
101102
Assert.True(isApiRunning);
102103
}
104+
105+
[Fact]
106+
public void VerifyToolInfo()
107+
{
108+
var writeCalls = 0;
109+
string? versionInfo = null;
110+
Mock<IToolInteractiveService> mockInteractiveService = new Mock<IToolInteractiveService>();
111+
mockInteractiveService.Setup(i => i.WriteLine(It.IsAny<string>()))
112+
.Callback((string message) =>
113+
{
114+
writeCalls++;
115+
versionInfo = message;
116+
});
117+
118+
var settings = new ToolInfoCommandSettings { Format = ToolInfoCommandSettings.InfoFormat.Json };
119+
var command = new ToolInfoCommand(mockInteractiveService.Object);
120+
var context = new CommandContext(new List<string>(), _mockRemainingArgs.Object, "run", null);
121+
command.Execute(context, settings);
122+
123+
Assert.Equal(1, writeCalls);
124+
Assert.True(!string.IsNullOrEmpty(versionInfo));
125+
126+
JsonNode? jsonNode = JsonNode.Parse(versionInfo);
127+
Assert.NotNull(jsonNode);
128+
129+
var version = jsonNode["Version"]?.ToString();
130+
Assert.NotNull(version);
131+
132+
// The Version.TryParse does not like the preview suffix
133+
version = version.Replace("-preview", "");
134+
Assert.True(Version.TryParse(version, out var _));
135+
136+
var installPath = jsonNode["InstallPath"]?.ToString();
137+
Assert.NotNull(installPath);
138+
Assert.True(Directory.Exists(installPath));
139+
Assert.True(Path.IsPathFullyQualified(installPath));
140+
}
103141
}

Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/Commands/Settings/RunCommandSettingsTests.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,6 @@ public void NoLaunchWindow_DefaultsToFalse()
2828
Assert.False(settings.NoLaunchWindow);
2929
}
3030

31-
[Fact]
32-
public void DisableLogs_DefaultsToFalse()
33-
{
34-
// Arrange
35-
var settings = new RunCommandSettings();
36-
37-
// Assert
38-
Assert.False(settings.DisableLogs);
39-
}
40-
41-
[Fact]
42-
public void PauseExit_DefaultsToFalse()
43-
{
44-
// Arrange
45-
var settings = new RunCommandSettings();
46-
47-
// Assert
48-
Assert.False(settings.PauseExit);
49-
}
50-
5131
[Fact]
5232
public void ApiGatewayEmulatorMode_DefaultsToNull()
5333
{

0 commit comments

Comments
 (0)