Skip to content

Add version to test tool #1969

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

Merged
merged 5 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .autover/changes/efa829ed-59af-46eb-a5e5-ef471698c82d.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Projects": [
{
"Name": "Amazon.Lambda.TestTool",
"Type": "Patch",
"ChangelogMessages": [
"Add --version switch to allow integrators to check what version is installed"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you missed updating the changelog

]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

using System.Diagnostics;
using System.Text.Json;
using Amazon.Lambda.TestTool.Commands.Settings;
using Amazon.Lambda.TestTool.Extensions;
using Amazon.Lambda.TestTool.Models;
Expand All @@ -28,6 +29,12 @@ public override async Task<int> ExecuteAsync(CommandContext context, RunCommandS
{
try
{
if (settings.PrintToolInfo)
{
PrintToolInfo();
return CommandReturnCodes.Success;
}

EvaluateEnvironmentVariables(settings);

if (!settings.LambdaEmulatorPort.HasValue && !settings.ApiGatewayEmulatorPort.HasValue)
Expand Down Expand Up @@ -101,6 +108,11 @@ public override async Task<int> ExecuteAsync(CommandContext context, RunCommandS
}
}

private void PrintToolInfo()
{
toolInteractiveService.WriteLine(Utilities.Utils.GenerateToolInfoJson());
}

private void EvaluateEnvironmentVariables(RunCommandSettings settings)
{
var environmentVariables = environmentManager.GetEnvironmentVariables();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,11 @@ public sealed class RunCommandSettings : CommandSettings
[CommandOption("--api-gateway-emulator-port <PORT>")]
[Description("The port number used for the test tool's API Gateway emulator.")]
public int? ApiGatewayEmulatorPort { get; set; }

/// <summary>
/// When set the tool prints metadata for the including the version number as a JSON document and then exits.
/// </summary>
[CommandOption("--tool-info")]
[Description("When set the tool prints metadata for the including the version number as a JSON document and then exits.")]
public bool PrintToolInfo { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static string DetermineToolVersion()
AssemblyInformationalVersionAttribute? attribute = null;
try
{
var assembly = Assembly.GetEntryAssembly();
var assembly = typeof(Utils).Assembly;
if (assembly == null)
return unknownVersion;
attribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
Expand All @@ -42,6 +42,23 @@ public static string DetermineToolVersion()
return version ?? unknownVersion;
}

public static string GenerateToolInfoJson()
{
var stream = new MemoryStream();
Utf8JsonWriter utf8JsonWriter = new Utf8JsonWriter(stream, options: new JsonWriterOptions()
{
Indented = false
});
utf8JsonWriter.WriteStartObject();
utf8JsonWriter.WriteString("version", Utilities.Utils.DetermineToolVersion());
utf8JsonWriter.WriteString("install-path", Directory.GetParent(typeof(Utils).Assembly.Location)!.FullName);
utf8JsonWriter.WriteEndObject();
utf8JsonWriter.Flush();

stream.Position = 0;
return new StreamReader(stream).ReadToEnd();
}

/// <summary>
/// If true it means the test tool was launched via an Aspire AppHost.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Xunit;
using Amazon.Lambda.TestTool.Services.IO;
using Amazon.Lambda.TestTool.Utilities;
using System.Text.Json.Nodes;

namespace Amazon.Lambda.TestTool.UnitTests.Commands;

Expand Down Expand Up @@ -100,4 +101,42 @@ public async Task ExecuteAsync_EnvPorts_SuccessfulLaunch()
Assert.Equal(CommandReturnCodes.Success, result);
Assert.True(isApiRunning);
}

[Fact]
public async Task VerifyToolInfo()
{
var writeCalls = 0;
string? versionInfo = null;
Mock<IToolInteractiveService> mockInteractiveService = new Mock<IToolInteractiveService>();
mockInteractiveService.Setup(i => i.WriteLine(It.IsAny<string>()))
.Callback((string message) =>
{
writeCalls++;
versionInfo = message;
});

var cancellationSource = new CancellationTokenSource();
var settings = new RunCommandSettings { PrintToolInfo = true };
var command = new RunCommand(mockInteractiveService.Object, _mockEnvironmentManager.Object);
var context = new CommandContext(new List<string>(), _mockRemainingArgs.Object, "run", null);
await command.ExecuteAsync(context, settings, cancellationSource);

Assert.Equal(1, writeCalls);
Assert.True(!string.IsNullOrEmpty(versionInfo));

JsonNode? jsonNode = JsonNode.Parse(versionInfo);
Assert.NotNull(jsonNode);

var version = jsonNode["version"]?.ToString();
Assert.NotNull(version);

// The Version.TryParse does not like the preview suffix
version = version.Replace("-preview", "");
Assert.True(Version.TryParse(version, out var _));

var installPath = jsonNode["install-path"]?.ToString();
Assert.NotNull(installPath);
Assert.True(Directory.Exists(installPath));
Assert.True(Path.IsPathFullyQualified(installPath));
}
}
Loading