1+ using System . Text ;
2+ using Microsoft . Extensions . DependencyInjection ;
3+ using Microsoft . Extensions . Logging ;
4+ using ModularPipelines . Context ;
5+ using ModularPipelines . Extensions ;
6+ using ModularPipelines . Modules ;
7+ using ModularPipelines . TestHelpers ;
8+ using NReco . Logging . File ;
9+ using TUnit . Assertions . Extensions ;
10+ using File = ModularPipelines . FileSystem . File ;
11+ using Type = System . Type ;
12+
13+ namespace ModularPipelines . UnitTests ;
14+
15+ public class AfterPipelineLoggerTests
16+ {
17+ private class AfterPipelineLoggingModule : Module
18+ {
19+ protected override async Task < IDictionary < string , object > ? > ExecuteAsync ( IPipelineContext context , CancellationToken cancellationToken )
20+ {
21+ context . LogOnPipelineEnd ( "Blah!" ) ;
22+ await Task . CompletedTask ;
23+ return null ;
24+ }
25+ }
26+
27+ private class AfterPipelineLoggingWithExceptionModule : Module
28+ {
29+ protected override async Task < IDictionary < string , object > ? > ExecuteAsync ( IPipelineContext context , CancellationToken cancellationToken )
30+ {
31+ context . LogOnPipelineEnd ( "Blah!" ) ;
32+ await Task . CompletedTask ;
33+ throw new Exception ( ) ;
34+ }
35+ }
36+
37+ [ Test ]
38+ public async Task LogsAfterPipeline ( )
39+ {
40+ var stringBuilder = new StringBuilder ( ) ;
41+ var host = await TestPipelineHostBuilder . Create ( )
42+ . ConfigureServices ( ( _ , collection ) =>
43+ {
44+ collection . AddSingleton ( stringBuilder ) ;
45+ collection . AddSingleton ( typeof ( ILogger < > ) , typeof ( StringLogger < > ) ) ;
46+ } )
47+ . AddModule < AfterPipelineLoggingModule > ( )
48+ . BuildHostAsync ( ) ;
49+
50+ await host . ExecutePipelineAsync ( ) ;
51+
52+ await host . DisposeAsync ( ) ;
53+
54+ await Assert . That ( stringBuilder . ToString ( ) . Trim ( ) ) . Does . EndWith ( "Blah!" ) ;
55+ }
56+
57+ [ Test ]
58+ public async Task LogsAfterPipelineWithException ( )
59+ {
60+ var stringBuilder = new StringBuilder ( ) ;
61+ var host = await TestPipelineHostBuilder . Create ( )
62+ . ConfigureServices ( ( _ , collection ) =>
63+ {
64+ collection . AddSingleton ( stringBuilder ) ;
65+ collection . AddSingleton ( typeof ( ILogger < > ) , typeof ( StringLogger < > ) ) ;
66+ } )
67+ . AddModule < AfterPipelineLoggingWithExceptionModule > ( )
68+ . BuildHostAsync ( ) ;
69+
70+ try
71+ {
72+ await host . ExecutePipelineAsync ( ) ;
73+ }
74+ catch
75+ {
76+ // Ignored
77+ }
78+
79+ await host . DisposeAsync ( ) ;
80+
81+ await Assert . That ( stringBuilder . ToString ( ) . Trim ( ) ) . Does . EndWith ( "Blah!" ) ;
82+ }
83+ }
0 commit comments