Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .github/workflows/publish-artifacts-examples-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ jobs:

# Ensure we preserve access to NuGet.org
- name: Configure NuGet.org source
continue-on-error: true
run: |
dotnet nuget add source https://api.nuget.org/v3/index.json --name nuget.org

Expand Down
74 changes: 62 additions & 12 deletions libraries/src/AWS.Lambda.Powertools.Common/Core/ConsoleWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,77 @@ namespace AWS.Lambda.Powertools.Common;
public class ConsoleWrapper : IConsoleWrapper
{
private static bool _override;
private static TextWriter _testOutputStream;
private static bool _outputResetPerformed = false;
private static bool _inTestMode = false;

/// <inheritdoc />
public void WriteLine(string message)
{
OverrideLambdaLogger();
Console.WriteLine(message);
if (_inTestMode && _testOutputStream != null)
{
_testOutputStream.WriteLine(message);
}
else
{
EnsureConsoleOutputOnce();
Console.WriteLine(message);
}
}

/// <inheritdoc />
public void Debug(string message)
{
OverrideLambdaLogger();
System.Diagnostics.Debug.WriteLine(message);
if (_inTestMode && _testOutputStream != null)
{
_testOutputStream.WriteLine(message);
}
else
{
EnsureConsoleOutputOnce();
System.Diagnostics.Debug.WriteLine(message);
}
}

/// <inheritdoc />
public void Error(string message)
{
if (!_override)
if (_inTestMode && _testOutputStream != null)
{
var errordOutput = new StreamWriter(Console.OpenStandardError());
errordOutput.AutoFlush = true;
Console.SetError(errordOutput);
_testOutputStream.WriteLine(message);
}
else
{
if (!_override)
{
var errordOutput = new StreamWriter(Console.OpenStandardError());
errordOutput.AutoFlush = true;
Console.SetError(errordOutput);
}
Console.Error.WriteLine(message);
}

Console.Error.WriteLine(message);
}

internal static void SetOut(StringWriter consoleOut)
/// <summary>
/// Set the ConsoleWrapper to use a different TextWriter
/// This is useful for unit tests where you want to capture the output
/// </summary>
public static void SetOut(TextWriter consoleOut)
{
_testOutputStream = consoleOut;
_inTestMode = true;
_override = true;
Console.SetOut(consoleOut);
}

private void OverrideLambdaLogger()
private static void EnsureConsoleOutputOnce()
{
if (_outputResetPerformed) return;
OverrideLambdaLogger();
_outputResetPerformed = true;
}

private static void OverrideLambdaLogger()
{
if (_override)
{
Expand All @@ -73,8 +109,22 @@ internal static void WriteLine(string logLevel, string message)
Console.WriteLine($"{DateTime.UtcNow:yyyy-MM-ddTHH:mm:ss.fffZ}\t{logLevel}\t{message}");
}

/// <summary>
/// Reset the ConsoleWrapper to its original state
/// </summary>
public static void ResetForTest()
{
_override = false;
_inTestMode = false;
_testOutputStream = null;
_outputResetPerformed = false;
}

/// <summary>
/// Clear the output reset flag
/// </summary>
public static void ClearOutputResetFlag()
{
_outputResetPerformed = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void BatchProcessing_Set_Execution_Environment_Context_SQS()
var sqsBatchProcessor = new SqsBatchProcessor(conf);

// Assert
Assert.Equal($"{Constants.FeatureContextIdentifier}/BatchProcessing/1.0.0",
Assert.Equal($"{Constants.FeatureContextIdentifier}/BatchProcessing/{env.GetAssemblyVersion(this)}",
env.GetEnvironmentVariable("AWS_EXECUTION_ENV"));

Assert.NotNull(sqsBatchProcessor);
Expand All @@ -52,7 +52,7 @@ public void BatchProcessing_Set_Execution_Environment_Context_Kinesis()
var KinesisEventBatchProcessor = new KinesisEventBatchProcessor(conf);

// Assert
Assert.Equal($"{Constants.FeatureContextIdentifier}/BatchProcessing/1.0.0",
Assert.Equal($"{Constants.FeatureContextIdentifier}/BatchProcessing/{env.GetAssemblyVersion(this)}",
env.GetEnvironmentVariable("AWS_EXECUTION_ENV"));

Assert.NotNull(KinesisEventBatchProcessor);
Expand All @@ -69,7 +69,7 @@ public void BatchProcessing_Set_Execution_Environment_Context_DynamoDB()
var dynamoDbStreamBatchProcessor = new DynamoDbStreamBatchProcessor(conf);

// Assert
Assert.Equal($"{Constants.FeatureContextIdentifier}/BatchProcessing/1.0.0",
Assert.Equal($"{Constants.FeatureContextIdentifier}/BatchProcessing/{env.GetAssemblyVersion(this)}",
env.GetEnvironmentVariable("AWS_EXECUTION_ENV"));

Assert.NotNull(dynamoDbStreamBatchProcessor);
Expand Down
Loading
Loading