1
1
using System ;
2
2
using System . IO ;
3
+ using NSubstitute ;
3
4
using Xunit ;
4
5
5
6
namespace AWS . Lambda . Powertools . Common . Tests ;
@@ -15,13 +16,13 @@ public ConsoleWrapperTests()
15
16
// Store original console outputs
16
17
_originalOut = Console . Out ;
17
18
_originalError = Console . Error ;
18
-
19
+
19
20
// Setup test writer
20
21
_testWriter = new StringWriter ( ) ;
21
-
22
+
22
23
// Reset ConsoleWrapper state before each test
23
24
ConsoleWrapper . ResetForTest ( ) ;
24
-
25
+
25
26
// Clear any Lambda environment variables
26
27
Environment . SetEnvironmentVariable ( "AWS_LAMBDA_FUNCTION_NAME" , null ) ;
27
28
}
@@ -31,13 +32,13 @@ public void Dispose()
31
32
// Restore original console outputs
32
33
Console . SetOut ( _originalOut ) ;
33
34
Console . SetError ( _originalError ) ;
34
-
35
+
35
36
// Reset ConsoleWrapper state after each test
36
37
ConsoleWrapper . ResetForTest ( ) ;
37
-
38
+
38
39
// Clear any test environment variables
39
40
Environment . SetEnvironmentVariable ( "AWS_LAMBDA_FUNCTION_NAME" , null ) ;
40
-
41
+
41
42
_testWriter ? . Dispose ( ) ;
42
43
}
43
44
@@ -240,7 +241,8 @@ public void WriteLineStatic_GivenLogLevelAndMessage_WhenCalled_ThenFormatsWithTi
240
241
{
241
242
// When - Using reflection to call internal static method
242
243
var method = typeof ( ConsoleWrapper )
243
- . GetMethod ( "WriteLine" , System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Static ) ;
244
+ . GetMethod ( "WriteLine" ,
245
+ System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Static ) ;
244
246
245
247
if ( method == null )
246
248
{
@@ -299,64 +301,35 @@ public void ClearOutputResetFlag_GivenMultipleCalls_WhenCalled_ThenAllowsRepeate
299
301
// Then
300
302
Assert . Equal ( $ "First message{ Environment . NewLine } Second message{ Environment . NewLine } ", _testWriter . ToString ( ) ) ;
301
303
}
304
+
305
+ // from here
302
306
303
307
[ Fact ]
304
- public void HasLambdaReInterceptedConsole_GivenConsoleOutThrowsException_WhenCalled_ThenReturnsTrue ( )
308
+ public void HasLambdaReInterceptedConsole_WhenConsoleOutAccessThrows_ThenReturnsTrueFromCatchBlock ( )
305
309
{
306
- // Given
307
- Environment . SetEnvironmentVariable ( "AWS_LAMBDA_FUNCTION_NAME" , "test-function" ) ;
308
-
309
- // Create a mock TextWriter that throws when accessing its type
310
- var mockWriter = new ThrowingTextWriter ( ) ;
311
- Console . SetOut ( mockWriter ) ;
312
-
313
- var wrapper = new ConsoleWrapper ( ) ;
314
-
315
- // When - This will trigger HasLambdaReInterceptedConsole which should catch the exception
316
- var exception = Record . Exception ( ( ) => wrapper . WriteLine ( "test message" ) ) ;
317
-
318
- // Then - Should not throw, the catch block should handle it
319
- Assert . Null ( exception ) ;
310
+ // Given - A function that throws when called (simulating Console.Out access failure)
311
+ Func < TextWriter > throwingAccessor = ( ) => throw new InvalidOperationException ( "Console.Out access failed" ) ;
312
+
313
+ // When - Call the internal method with the throwing accessor
314
+ var result = ConsoleWrapper . HasLambdaReInterceptedConsole ( throwingAccessor ) ;
315
+
316
+ // Then - Should return true from the catch block (lines 102-105)
317
+ Assert . True ( result ) ;
320
318
}
321
319
322
320
[ Fact ]
323
- public void OverrideLambdaLogger_GivenConsoleOpenStandardOutputThrows_WhenCalled_ThenDoesNotThrow ( )
321
+ public void OverrideLambdaLogger_WhenOpenStandardOutputThrows_ThenSetsOverrideToFalse ( )
324
322
{
325
323
// Given
326
- Environment . SetEnvironmentVariable ( "AWS_LAMBDA_FUNCTION_NAME" , "test-function" ) ;
327
-
328
- // Create a scenario where Console.OpenStandardOutput might fail
329
- // We'll simulate this by creating conditions that trigger the catch block
330
- var wrapper = new ConsoleWrapper ( ) ;
331
-
332
- // When - Multiple calls to trigger the override logic and potential failures
333
- var exception = Record . Exception ( ( ) =>
334
- {
335
- for ( int i = 0 ; i < 5 ; i ++ )
336
- {
337
- wrapper . WriteLine ( $ "test message { i } ") ;
338
- }
339
- } ) ;
324
+ ConsoleWrapper . ResetForTest ( ) ;
340
325
341
- // Then - Should not throw even if StreamWriter creation fails
342
- Assert . Null ( exception ) ;
343
- }
326
+ // A function that throws when called (simulating Console.OpenStandardOutput failure)
327
+ Func < Stream > throwingOpener = ( ) => throw new UnauthorizedAccessException ( "Cannot open standard output" ) ;
344
328
345
- // Helper class to simulate exceptions in Console.Out.GetType()
346
- private class ThrowingTextWriter : TextWriter
347
- {
348
- public override System . Text . Encoding Encoding => System . Text . Encoding . UTF8 ;
329
+ // When - Call the internal method with the throwing opener
330
+ var exception = Record . Exception ( ( ) => ConsoleWrapper . OverrideLambdaLogger ( throwingOpener ) ) ;
349
331
350
- public override void Write ( char value )
351
- {
352
- // Simulate the scenario where accessing type information fails
353
- // by throwing when any operation is performed
354
- throw new InvalidOperationException ( "Simulated exception during console operation" ) ;
355
- }
356
-
357
- public override void WriteLine ( string value )
358
- {
359
- throw new InvalidOperationException ( "Simulated exception during console operation" ) ;
360
- }
332
+ // Then - Should not throw (catch block handles it on lines 120-123)
333
+ Assert . Null ( exception ) ;
361
334
}
362
335
}
0 commit comments