Skip to content

Commit 126a283

Browse files
authored
Merge pull request #840 from hjgraca/fix/execution-env-version
chore: Fix execution env verion and ConsoleWrapper for test mode
2 parents b06fa34 + f4c5ed4 commit 126a283

File tree

8 files changed

+218
-132
lines changed

8 files changed

+218
-132
lines changed

.github/workflows/publish-artifacts-examples-tests.yml

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ jobs:
7979
8080
# Ensure we preserve access to NuGet.org
8181
- name: Configure NuGet.org source
82+
continue-on-error: true
8283
run: |
8384
dotnet nuget add source https://api.nuget.org/v3/index.json --name nuget.org
8485

libraries/src/AWS.Lambda.Powertools.Common/Core/ConsoleWrapper.cs

+62-12
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,77 @@ namespace AWS.Lambda.Powertools.Common;
2222
public class ConsoleWrapper : IConsoleWrapper
2323
{
2424
private static bool _override;
25+
private static TextWriter _testOutputStream;
26+
private static bool _outputResetPerformed = false;
27+
private static bool _inTestMode = false;
2528

2629
/// <inheritdoc />
2730
public void WriteLine(string message)
2831
{
29-
OverrideLambdaLogger();
30-
Console.WriteLine(message);
32+
if (_inTestMode && _testOutputStream != null)
33+
{
34+
_testOutputStream.WriteLine(message);
35+
}
36+
else
37+
{
38+
EnsureConsoleOutputOnce();
39+
Console.WriteLine(message);
40+
}
3141
}
3242

3343
/// <inheritdoc />
3444
public void Debug(string message)
3545
{
36-
OverrideLambdaLogger();
37-
System.Diagnostics.Debug.WriteLine(message);
46+
if (_inTestMode && _testOutputStream != null)
47+
{
48+
_testOutputStream.WriteLine(message);
49+
}
50+
else
51+
{
52+
EnsureConsoleOutputOnce();
53+
System.Diagnostics.Debug.WriteLine(message);
54+
}
3855
}
3956

4057
/// <inheritdoc />
4158
public void Error(string message)
4259
{
43-
if (!_override)
60+
if (_inTestMode && _testOutputStream != null)
4461
{
45-
var errordOutput = new StreamWriter(Console.OpenStandardError());
46-
errordOutput.AutoFlush = true;
47-
Console.SetError(errordOutput);
62+
_testOutputStream.WriteLine(message);
63+
}
64+
else
65+
{
66+
if (!_override)
67+
{
68+
var errordOutput = new StreamWriter(Console.OpenStandardError());
69+
errordOutput.AutoFlush = true;
70+
Console.SetError(errordOutput);
71+
}
72+
Console.Error.WriteLine(message);
4873
}
49-
50-
Console.Error.WriteLine(message);
5174
}
5275

53-
internal static void SetOut(StringWriter consoleOut)
76+
/// <summary>
77+
/// Set the ConsoleWrapper to use a different TextWriter
78+
/// This is useful for unit tests where you want to capture the output
79+
/// </summary>
80+
public static void SetOut(TextWriter consoleOut)
5481
{
82+
_testOutputStream = consoleOut;
83+
_inTestMode = true;
5584
_override = true;
5685
Console.SetOut(consoleOut);
5786
}
5887

59-
private void OverrideLambdaLogger()
88+
private static void EnsureConsoleOutputOnce()
89+
{
90+
if (_outputResetPerformed) return;
91+
OverrideLambdaLogger();
92+
_outputResetPerformed = true;
93+
}
94+
95+
private static void OverrideLambdaLogger()
6096
{
6197
if (_override)
6298
{
@@ -73,8 +109,22 @@ internal static void WriteLine(string logLevel, string message)
73109
Console.WriteLine($"{DateTime.UtcNow:yyyy-MM-ddTHH:mm:ss.fffZ}\t{logLevel}\t{message}");
74110
}
75111

112+
/// <summary>
113+
/// Reset the ConsoleWrapper to its original state
114+
/// </summary>
76115
public static void ResetForTest()
77116
{
78117
_override = false;
118+
_inTestMode = false;
119+
_testOutputStream = null;
120+
_outputResetPerformed = false;
121+
}
122+
123+
/// <summary>
124+
/// Clear the output reset flag
125+
/// </summary>
126+
public static void ClearOutputResetFlag()
127+
{
128+
_outputResetPerformed = false;
79129
}
80130
}

libraries/tests/AWS.Lambda.Powertools.BatchProcessing.Tests/Internal/BatchProcessingInternalTests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void BatchProcessing_Set_Execution_Environment_Context_SQS()
3535
var sqsBatchProcessor = new SqsBatchProcessor(conf);
3636

3737
// Assert
38-
Assert.Equal($"{Constants.FeatureContextIdentifier}/BatchProcessing/1.0.0",
38+
Assert.Equal($"{Constants.FeatureContextIdentifier}/BatchProcessing/{env.GetAssemblyVersion(this)}",
3939
env.GetEnvironmentVariable("AWS_EXECUTION_ENV"));
4040

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

5454
// Assert
55-
Assert.Equal($"{Constants.FeatureContextIdentifier}/BatchProcessing/1.0.0",
55+
Assert.Equal($"{Constants.FeatureContextIdentifier}/BatchProcessing/{env.GetAssemblyVersion(this)}",
5656
env.GetEnvironmentVariable("AWS_EXECUTION_ENV"));
5757

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

7171
// Assert
72-
Assert.Equal($"{Constants.FeatureContextIdentifier}/BatchProcessing/1.0.0",
72+
Assert.Equal($"{Constants.FeatureContextIdentifier}/BatchProcessing/{env.GetAssemblyVersion(this)}",
7373
env.GetEnvironmentVariable("AWS_EXECUTION_ENV"));
7474

7575
Assert.NotNull(dynamoDbStreamBatchProcessor);

0 commit comments

Comments
 (0)