Skip to content

Commit 1901dfd

Browse files
committed
fix systemwrapper static
1 parent 953e77e commit 1901dfd

File tree

9 files changed

+45
-47
lines changed

9 files changed

+45
-47
lines changed

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,4 @@ public interface ISystemWrapper
5959
/// </summary>
6060
/// <param name="type"></param>
6161
void SetExecutionEnvironment<T>(T type);
62-
63-
/// <summary>
64-
/// Sets console output
65-
/// Useful for testing and checking the console output
66-
/// <code>
67-
/// var consoleOut = new StringWriter();
68-
/// SystemWrapper.Instance.SetOut(consoleOut);
69-
/// </code>
70-
/// </summary>
71-
/// <param name="writeTo">The TextWriter instance where to write to</param>
72-
void SetOut(TextWriter writeTo);
7362
}

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,20 @@ public void SetExecutionEnvironment<T>(T type)
143143
SetEnvironmentVariable(envName, envValue.ToString());
144144
}
145145

146-
/// <inheritdoc />
147-
public void SetOut(TextWriter writeTo)
146+
/// <summary>
147+
/// Sets console output
148+
/// Useful for testing and checking the console output
149+
/// <code>
150+
/// var consoleOut = new StringWriter();
151+
/// SystemWrapper.Instance.SetOut(consoleOut);
152+
/// </code>
153+
/// </summary>
154+
/// <param name="writeTo">The TextWriter instance where to write to</param>
155+
156+
public static void SetOut(TextWriter writeTo)
148157
{
149-
SystemWrapper._testOutputStream = writeTo;
150-
SystemWrapper._inTestMode = true;
158+
_testOutputStream = writeTo;
159+
_inTestMode = true;
151160
Console.SetOut(writeTo);
152161
}
153162

@@ -189,7 +198,7 @@ private static void ResetConsoleOutput()
189198
Console.SetError(errorOutput);
190199
}
191200

192-
public void ClearOutputResetFlag()
201+
public static void ClearOutputResetFlag()
193202
{
194203
_outputResetPerformed = false;
195204
}

libraries/tests/AWS.Lambda.Powertools.Common.Tests/Core/SystemWrapperTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void ClearOutputResetFlag_ResetsFlag_AllowsSubsequentReset()
7979
wrapper.Log("First message"); // This should cause a reset
8080
bool afterFirstLog = (bool)_outputResetPerformedField.GetValue(null);
8181

82-
wrapper.ClearOutputResetFlag();
82+
SystemWrapper.ClearOutputResetFlag();
8383
bool afterClear = (bool)_outputResetPerformedField.GetValue(null);
8484

8585
wrapper.Log("After clear"); // This should cause another reset
@@ -96,7 +96,7 @@ public void Log_InTestMode_WritesToTestOutput()
9696
{
9797
// Arrange
9898
var wrapper = new SystemWrapper(_mockEnvironment);
99-
wrapper.SetOut(_testWriter);
99+
SystemWrapper.SetOut(_testWriter);
100100
var message = "Test message";
101101

102102
// Act
@@ -111,7 +111,7 @@ public void LogLine_InTestMode_WritesToTestOutput()
111111
{
112112
// Arrange
113113
var wrapper = new SystemWrapper(_mockEnvironment);
114-
wrapper.SetOut(_testWriter);
114+
SystemWrapper.SetOut(_testWriter);
115115
var message = "Test line";
116116

117117
// Act
@@ -126,7 +126,7 @@ public void ResetTestMode_ResetsTestState()
126126
{
127127
// Arrange
128128
var wrapper = new SystemWrapper(_mockEnvironment);
129-
wrapper.SetOut(_testWriter);
129+
SystemWrapper.SetOut(_testWriter);
130130
var message = "This should go to console";
131131

132132
// Act
@@ -148,7 +148,7 @@ public void SetOut_EnablesTestMode()
148148
var message = "Test output";
149149

150150
// Act
151-
wrapper.SetOut(_testWriter);
151+
SystemWrapper.SetOut(_testWriter);
152152
wrapper.Log(message);
153153

154154
// Assert
@@ -160,7 +160,7 @@ public void Log_InTestMode_DoesNotCallResetConsoleOutput()
160160
{
161161
// Arrange
162162
var wrapper = new SystemWrapper(_mockEnvironment);
163-
wrapper.SetOut(_testWriter);
163+
SystemWrapper.SetOut(_testWriter);
164164
var message1 = "First test message";
165165
var message2 = "Second test message";
166166

@@ -183,7 +183,7 @@ public void Log_AfterClearingFlag_ResetsOutputAgain()
183183
wrapper.Log("First message"); // Should reset output
184184
bool afterFirstLog = (bool)_outputResetPerformedField.GetValue(null);
185185

186-
wrapper.ClearOutputResetFlag();
186+
SystemWrapper.ClearOutputResetFlag();
187187
bool afterClear = (bool)_outputResetPerformedField.GetValue(null);
188188

189189
wrapper.Log("Second message"); // Should reset again

libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Attributes/LoggingAttributeTest.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void OnEntry_WhenLambdaContextDoesNotExist_IgnoresLambdaContext()
4646
{
4747
// Arrange
4848
var consoleOut = Substitute.For<StringWriter>();
49-
SystemWrapper.Instance.SetOut(consoleOut);
49+
SystemWrapper.SetOut(consoleOut);
5050

5151
// Act
5252
_testHandlers.TestMethod();
@@ -72,7 +72,7 @@ public void OnEntry_WhenLambdaContextDoesNotExist_IgnoresLambdaContextAndLogDebu
7272
{
7373
// Arrange
7474
var consoleOut = Substitute.For<StringWriter>();
75-
SystemWrapper.Instance.SetOut(consoleOut);
75+
SystemWrapper.SetOut(consoleOut);
7676

7777
// Act
7878
_testHandlers.TestMethodDebug();
@@ -101,7 +101,7 @@ public void OnEntry_WhenEventArgDoesNotExist_DoesNotLogEventArg()
101101
{
102102
// Arrange
103103
var consoleOut = Substitute.For<StringWriter>();
104-
SystemWrapper.Instance.SetOut(consoleOut);
104+
SystemWrapper.SetOut(consoleOut);
105105

106106
// Act
107107
_testHandlers.LogEventNoArgs();
@@ -116,7 +116,7 @@ public void OnEntry_WhenEventArgExist_LogEvent()
116116
{
117117
// Arrange
118118
var consoleOut = Substitute.For<StringWriter>();
119-
SystemWrapper.Instance.SetOut(consoleOut);
119+
SystemWrapper.SetOut(consoleOut);
120120
var correlationId = Guid.NewGuid().ToString();
121121

122122
#if NET8_0_OR_GREATER
@@ -150,7 +150,7 @@ public void OnEntry_WhenEventArgExist_LogEvent_False_Should_Not_Log()
150150
{
151151
// Arrange
152152
var consoleOut = Substitute.For<StringWriter>();
153-
SystemWrapper.Instance.SetOut(consoleOut);
153+
SystemWrapper.SetOut(consoleOut);
154154

155155
#if NET8_0_OR_GREATER
156156

@@ -175,7 +175,7 @@ public void OnEntry_WhenEventArgDoesNotExist_DoesNotLogEventArgAndLogDebug()
175175
{
176176
// Arrange
177177
var consoleOut = Substitute.For<StringWriter>();
178-
SystemWrapper.Instance.SetOut(consoleOut);
178+
SystemWrapper.SetOut(consoleOut);
179179

180180
// Act
181181
_testHandlers.LogEventDebug();
@@ -190,7 +190,7 @@ public void OnExit_WhenHandler_ClearState_Enabled_ClearKeys()
190190
{
191191
// Arrange
192192
var consoleOut = Substitute.For<StringWriter>();
193-
SystemWrapper.Instance.SetOut(consoleOut);
193+
SystemWrapper.SetOut(consoleOut);
194194

195195
// Act
196196
_testHandlers.ClearState();
@@ -378,7 +378,7 @@ public void When_Setting_SamplingRate_Should_Add_Key()
378378
{
379379
// Arrange
380380
var consoleOut = Substitute.For<StringWriter>();
381-
SystemWrapper.Instance.SetOut(consoleOut);
381+
SystemWrapper.SetOut(consoleOut);
382382

383383
// Act
384384
_testHandlers.HandlerSamplingRate();
@@ -395,7 +395,7 @@ public void When_Setting_Service_Should_Update_Key()
395395
{
396396
// Arrange
397397
var consoleOut = new StringWriter();
398-
SystemWrapper.Instance.SetOut(consoleOut);
398+
SystemWrapper.SetOut(consoleOut);
399399

400400
// Act
401401
_testHandlers.HandlerService();
@@ -411,7 +411,7 @@ public void When_Setting_LogLevel_Should_Update_LogLevel()
411411
{
412412
// Arrange
413413
var consoleOut = new StringWriter();
414-
SystemWrapper.Instance.SetOut(consoleOut);
414+
SystemWrapper.SetOut(consoleOut);
415415

416416
// Act
417417
_testHandlers.TestLogLevelCritical();
@@ -427,7 +427,7 @@ public void When_Setting_LogLevel_HigherThanInformation_Should_Not_LogEvent()
427427
{
428428
// Arrange
429429
var consoleOut = Substitute.For<StringWriter>();
430-
SystemWrapper.Instance.SetOut(consoleOut);
430+
SystemWrapper.SetOut(consoleOut);
431431
var context = new TestLambdaContext()
432432
{
433433
FunctionName = "PowertoolsLoggingSample-HelloWorldFunction-Gg8rhPwO7Wa1"
@@ -445,7 +445,7 @@ public void When_LogLevel_Debug_Should_Log_Message_When_No_Context_And_LogEvent_
445445
{
446446
// Arrange
447447
var consoleOut = Substitute.For<StringWriter>();
448-
SystemWrapper.Instance.SetOut(consoleOut);
448+
SystemWrapper.SetOut(consoleOut);
449449

450450
// Act
451451
_testHandlers.TestLogEventWithoutContext();
@@ -459,7 +459,7 @@ public void Should_Log_When_Not_Using_Decorator()
459459
{
460460
// Arrange
461461
var consoleOut = Substitute.For<StringWriter>();
462-
SystemWrapper.Instance.SetOut(consoleOut);
462+
SystemWrapper.SetOut(consoleOut);
463463

464464
var test = new TestHandlers();
465465

@@ -496,7 +496,7 @@ public void When_Setting_Service_Should_Override_Env()
496496
{
497497
// Arrange
498498
var consoleOut = Substitute.For<StringWriter>();
499-
SystemWrapper.Instance.SetOut(consoleOut);
499+
SystemWrapper.SetOut(consoleOut);
500500

501501
// Act
502502
_testHandler.LogWithEnv();
@@ -517,7 +517,7 @@ public void When_Setting_Service_Should_Override_Env_And_Empty()
517517
{
518518
// Arrange
519519
var consoleOut = Substitute.For<StringWriter>();
520-
SystemWrapper.Instance.SetOut(consoleOut);
520+
SystemWrapper.SetOut(consoleOut);
521521

522522
// Act
523523
_testHandler.LogWithAndWithoutEnv();

libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Formatter/LogFormatterTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public LogFormatterTest()
4848
public void Serialize_ShouldHandleEnumValues()
4949
{
5050
var consoleOut = Substitute.For<StringWriter>();
51-
SystemWrapper.Instance.SetOut(consoleOut);
51+
SystemWrapper.SetOut(consoleOut);
5252
var lambdaContext = new TestLambdaContext
5353
{
5454
FunctionName = "funtionName",
@@ -228,7 +228,7 @@ public void Log_WhenCustomFormatter_LogsCustomFormat()
228228
public void Should_Log_CustomFormatter_When_Decorated()
229229
{
230230
var consoleOut = Substitute.For<StringWriter>();
231-
SystemWrapper.Instance.SetOut(consoleOut);
231+
SystemWrapper.SetOut(consoleOut);
232232
var lambdaContext = new TestLambdaContext
233233
{
234234
FunctionName = "funtionName",
@@ -263,7 +263,7 @@ public void Should_Log_CustomFormatter_When_Decorated()
263263
public void Should_Log_CustomFormatter_When_No_Decorated_Just_Log()
264264
{
265265
var consoleOut = Substitute.For<StringWriter>();
266-
SystemWrapper.Instance.SetOut(consoleOut);
266+
SystemWrapper.SetOut(consoleOut);
267267
var lambdaContext = new TestLambdaContext
268268
{
269269
FunctionName = "funtionName",
@@ -299,7 +299,7 @@ public void Should_Log_CustomFormatter_When_No_Decorated_Just_Log()
299299
public void Should_Log_CustomFormatter_When_Decorated_No_Context()
300300
{
301301
var consoleOut = Substitute.For<StringWriter>();
302-
SystemWrapper.Instance.SetOut(consoleOut);
302+
SystemWrapper.SetOut(consoleOut);
303303

304304
Logger.UseFormatter(new CustomLogFormatter());
305305

libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Utilities/PowertoolsLoggerHelpersTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void ObjectToDictionary_NullObject_Return_New_Dictionary()
7474
public void Should_Log_With_Anonymous()
7575
{
7676
var consoleOut = Substitute.For<StringWriter>();
77-
SystemWrapper.Instance.SetOut(consoleOut);
77+
SystemWrapper.SetOut(consoleOut);
7878

7979
// Act & Assert
8080
Logger.AppendKey("newKey", new
@@ -94,7 +94,7 @@ public void Should_Log_With_Anonymous()
9494
public void Should_Log_With_Complex_Anonymous()
9595
{
9696
var consoleOut = Substitute.For<StringWriter>();
97-
SystemWrapper.Instance.SetOut(consoleOut);
97+
SystemWrapper.SetOut(consoleOut);
9898

9999
// Act & Assert
100100
Logger.AppendKey("newKey", new

libraries/tests/AWS.Lambda.Powertools.Metrics.Tests/ClearDimensionsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public void WhenClearAllDimensions_NoDimensionsInOutput()
1313
{
1414
// Arrange
1515
var consoleOut = new StringWriter();
16-
SystemWrapper.Instance.SetOut(consoleOut);
16+
SystemWrapper.SetOut(consoleOut);
1717

1818
// Act
1919
var handler = new FunctionHandler();

libraries/tests/AWS.Lambda.Powertools.Metrics.Tests/EMFValidationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public EmfValidationTests()
3535
{
3636
_handler = new FunctionHandler();
3737
_consoleOut = new CustomConsoleWriter();
38-
SystemWrapper.Instance.SetOut(_consoleOut);
38+
SystemWrapper.SetOut(_consoleOut);
3939
}
4040

4141
[Trait("Category", value: "SchemaValidation")]

libraries/tests/AWS.Lambda.Powertools.Metrics.Tests/Handlers/FunctionHandlerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public FunctionHandlerTests()
3434
{
3535
_handler = new FunctionHandler();
3636
_consoleOut = new CustomConsoleWriter();
37-
SystemWrapper.Instance.SetOut(_consoleOut);
37+
SystemWrapper.SetOut(_consoleOut);
3838
}
3939

4040
[Fact]

0 commit comments

Comments
 (0)