7
7
using Microsoft . Extensions . DependencyInjection ;
8
8
using Microsoft . Build . Framework ;
9
9
using Microsoft . Build . Locator ;
10
+ using Microsoft . Build . Utilities ;
10
11
using Moq ;
11
12
using Xunit ;
12
13
@@ -24,11 +25,14 @@ public class CoverageResultTaskTests : IAssemblyFixture<MSBuildFixture>
24
25
{
25
26
private readonly Mock < IBuildEngine > _buildEngine ;
26
27
private readonly List < BuildErrorEventArgs > _errors ;
28
+ private readonly Mock < IAssemblyAdapter > _mockAssemblyAdapter ;
27
29
28
30
public CoverageResultTaskTests ( )
29
31
{
30
32
_buildEngine = new Mock < IBuildEngine > ( ) ;
31
33
_errors = new List < BuildErrorEventArgs > ( ) ;
34
+ _mockAssemblyAdapter = new Mock < IAssemblyAdapter > ( ) ;
35
+ _mockAssemblyAdapter . Setup ( x => x . GetAssemblyName ( It . IsAny < string > ( ) ) ) . Returns ( "abc" ) ;
32
36
}
33
37
34
38
[ Fact ]
@@ -43,13 +47,14 @@ public void Execute_StateUnderTest_MissingInstrumentationState()
43
47
44
48
_buildEngine . Setup ( x => x . LogErrorEvent ( It . IsAny < BuildErrorEventArgs > ( ) ) ) . Callback < BuildErrorEventArgs > ( e => _errors . Add ( e ) ) ;
45
49
46
- var coverageResultTask = new CoverageResultTask {
47
- OutputFormat = "opencover" ,
48
- Output = "coverageDir" ,
49
- Threshold = "50" ,
50
- ThresholdType = "total" ,
51
- ThresholdStat = "total" ,
52
- InstrumenterState = null
50
+ var coverageResultTask = new CoverageResultTask
51
+ {
52
+ OutputFormat = "opencover" ,
53
+ Output = "coverageDir" ,
54
+ Threshold = "50" ,
55
+ ThresholdType = "total" ,
56
+ ThresholdStat = "total" ,
57
+ InstrumenterState = null
53
58
} ;
54
59
coverageResultTask . BuildEngine = _buildEngine . Object ;
55
60
@@ -59,8 +64,76 @@ public void Execute_StateUnderTest_MissingInstrumentationState()
59
64
// Assert
60
65
Assert . False ( success ) ;
61
66
Assert . True ( coverageResultTask . Log . HasLoggedErrors ) ;
62
- // check error message "Result of instrumentation task not found"
67
+ // check error message "Result of instrumentation task not found"
63
68
}
64
69
70
+ [ Fact ]
71
+ public void Execute_StateUnderTest_WithInstrumentationState_Fake ( )
72
+ {
73
+ // Arrange
74
+ var mockFileSystem = new Mock < IFileSystem > ( ) ;
75
+ mockFileSystem . Setup ( x => x . Exists ( It . IsAny < string > ( ) ) ) . Returns ( true ) ;
76
+ mockFileSystem . Setup ( x => x . Copy ( It . IsAny < string > ( ) , It . IsAny < string > ( ) , It . IsAny < bool > ( ) ) ) ;
77
+ var log = new TaskLoggingHelper ( _buildEngine . Object , "CoverageResultTask" ) ;
78
+
79
+ IServiceCollection serviceCollection = new ServiceCollection ( ) ;
80
+ serviceCollection . AddTransient < IFileSystem , FileSystem > ( ) ;
81
+ serviceCollection . AddTransient < IProcessExitHandler , ProcessExitHandler > ( ) ;
82
+ serviceCollection . AddTransient < Coverlet . Core . Abstractions . ILogger , MSBuildLogger > ( _ => new MSBuildLogger ( log ) ) ;
83
+ serviceCollection . AddTransient < IRetryHelper , RetryHelper > ( ) ;
84
+ serviceCollection . AddTransient < IConsole , SystemConsole > ( ) ;
85
+ serviceCollection . AddSingleton < IInstrumentationHelper , InstrumentationHelperForDebugging > ( ) ;
86
+ serviceCollection . AddSingleton < ISourceRootTranslator , SourceRootTranslator > ( serviceProvider => new SourceRootTranslator ( "empty" , serviceProvider . GetRequiredService < Coverlet . Core . Abstractions . ILogger > ( ) , mockFileSystem . Object , _mockAssemblyAdapter . Object ) ) ;
87
+ ServiceProvider serviceProvider = serviceCollection . BuildServiceProvider ( ) ;
88
+ BaseTask . ServiceProvider = serviceProvider ;
89
+ _buildEngine . Setup ( x => x . LogErrorEvent ( It . IsAny < BuildErrorEventArgs > ( ) ) ) . Callback < BuildErrorEventArgs > ( e => _errors . Add ( e ) ) ;
90
+
91
+ #pragma warning disable CS8604 // Possible null reference argument for parameter..
92
+ #pragma warning disable CS8602 // Dereference of a possibly null reference.
93
+ var InstrumenterState = new TaskItem ( Path . Combine ( AppDomain . CurrentDomain . SetupInformation . ApplicationBase , "TestAssets\\ InstrumenterState.ItemSpec.data1.xml" ) ) ;
94
+ #pragma warning restore CS8602 // Dereference of a possibly null reference.
95
+ #pragma warning restore C8S604 // Possible null reference argument for parameter.
96
+
97
+ var coverageResultTask = new CoverageResultTask
98
+ {
99
+ OutputFormat = "opencover" ,
100
+ Output = "coverageDir" ,
101
+ Threshold = "50" ,
102
+ ThresholdType = "total" ,
103
+ ThresholdStat = "total" ,
104
+ InstrumenterState = InstrumenterState
105
+ } ;
106
+ coverageResultTask . BuildEngine = _buildEngine . Object ;
107
+
108
+ // Act
109
+ bool success = coverageResultTask . Execute ( ) ;
110
+
111
+ // Assert
112
+ Assert . True ( success ) ;
113
+ Assert . False ( coverageResultTask . Log . HasLoggedErrors ) ;
114
+
115
+ Assert . Contains ( "coverageDir.opencover.xml" , coverageResultTask . ReportItems [ 0 ] . ItemSpec ) ;
116
+ Assert . Equal ( 16 , coverageResultTask . ReportItems [ 0 ] . MetadataCount ) ;
117
+
118
+ }
119
+
120
+ }
121
+ class InstrumentationHelperForDebugging : InstrumentationHelper
122
+ {
123
+ public InstrumentationHelperForDebugging ( IProcessExitHandler processExitHandler , IRetryHelper retryHelper , IFileSystem fileSystem , Coverlet . Core . Abstractions . ILogger logger , ISourceRootTranslator sourceTranslator )
124
+ : base ( processExitHandler , retryHelper , fileSystem , logger , sourceTranslator )
125
+ {
126
+
127
+ }
128
+
129
+ public override void RestoreOriginalModule ( string module , string identifier )
130
+ {
131
+ // DO NOT RESTORE
132
+ }
133
+
134
+ public override void RestoreOriginalModules ( )
135
+ {
136
+ // DO NOT RESTORE
137
+ }
65
138
}
66
139
}
0 commit comments