8
8
using Coverlet . Core ;
9
9
using Newtonsoft . Json ;
10
10
using NuGet . Packaging ;
11
+ using Xunit ;
11
12
using Xunit . Sdk ;
12
13
13
14
namespace Coverlet . Integration . Tests
@@ -52,7 +53,7 @@ private protected string GetPackageVersion(string filter)
52
53
return manifest . Metadata . Version . OriginalVersion ;
53
54
}
54
55
55
- private protected ClonedTemplateProject CloneTemplateProject ( )
56
+ private protected ClonedTemplateProject CloneTemplateProject ( bool cleanupOnDispose = true )
56
57
{
57
58
DirectoryInfo finalRoot = Directory . CreateDirectory ( Guid . NewGuid ( ) . ToString ( "N" ) ) ;
58
59
foreach ( string file in ( Directory . GetFiles ( $ "../../../../coverlet.integration.template", "*.cs" )
@@ -75,7 +76,7 @@ private protected ClonedTemplateProject CloneTemplateProject()
75
76
76
77
AddMicrosoftNETTestSdkRef ( finalRoot . FullName ) ;
77
78
78
- return new ClonedTemplateProject ( ) { Path = finalRoot . FullName } ;
79
+ return new ClonedTemplateProject ( finalRoot . FullName , cleanupOnDispose ) ;
79
80
}
80
81
81
82
private protected bool RunCommand ( string command , string arguments , out string standardOutput , out string standardError , string workingDirectory = "" )
@@ -205,34 +206,101 @@ private protected string AddCollectorRunsettingsFile(string projectPath)
205
206
return runsettingsPath ;
206
207
}
207
208
208
- private protected void AssertCoverage ( ClonedTemplateProject clonedTemplateProject )
209
+ private protected void AssertCoverage ( ClonedTemplateProject clonedTemplateProject , string filter = "coverage.json" )
209
210
{
210
- Modules modules = JsonConvert . DeserializeObject < Modules > ( File . ReadAllText ( clonedTemplateProject . GetFiles ( "coverage.json" ) . Single ( ) ) ) ;
211
- modules
212
- . Document ( "DeepThought.cs" )
213
- . Class ( "Coverlet.Integration.Template.DeepThought" )
214
- . Method ( "System.Int32 Coverlet.Integration.Template.DeepThought::AnswerToTheUltimateQuestionOfLifeTheUniverseAndEverything()" )
215
- . AssertLinesCovered ( ( 6 , 1 ) , ( 7 , 1 ) , ( 8 , 1 ) ) ;
211
+ bool coverageChecked = false ;
212
+ foreach ( string coverageFile in clonedTemplateProject . GetFiles ( filter ) )
213
+ {
214
+ JsonConvert . DeserializeObject < Modules > ( File . ReadAllText ( coverageFile ) )
215
+ . Document ( "DeepThought.cs" )
216
+ . Class ( "Coverlet.Integration.Template.DeepThought" )
217
+ . Method ( "System.Int32 Coverlet.Integration.Template.DeepThought::AnswerToTheUltimateQuestionOfLifeTheUniverseAndEverything()" )
218
+ . AssertLinesCovered ( ( 6 , 1 ) , ( 7 , 1 ) , ( 8 , 1 ) ) ;
219
+ coverageChecked = true ;
220
+ }
221
+
222
+ Assert . True ( coverageChecked , "Coverage check fail" ) ;
223
+ }
224
+
225
+ private protected void UpdateProjectTargetFramework ( ClonedTemplateProject project , params string [ ] targetFrameworks )
226
+ {
227
+ if ( targetFrameworks is null || targetFrameworks . Length == 0 )
228
+ {
229
+ throw new ArgumentException ( "Invalid targetFrameworks" , nameof ( targetFrameworks ) ) ;
230
+ }
231
+
232
+ if ( ! File . Exists ( project . ProjectFileNamePath ) )
233
+ {
234
+ throw new FileNotFoundException ( "coverlet.integration.template.csproj not found" , "coverlet.integration.template.csproj" ) ;
235
+ }
236
+ XDocument xml ;
237
+ using ( var csprojStream = File . OpenRead ( project . ProjectFileNamePath ) )
238
+ {
239
+ xml = XDocument . Load ( csprojStream ) ;
240
+ }
241
+
242
+ xml . Element ( "Project" )
243
+ . Element ( "PropertyGroup" )
244
+ . Element ( "TargetFramework" )
245
+ . Remove ( ) ;
246
+
247
+ XElement targetFrameworkElement ;
248
+
249
+ if ( targetFrameworks . Length == 1 )
250
+ {
251
+ targetFrameworkElement = new XElement ( "TargetFramework" , targetFrameworks [ 0 ] ) ;
252
+ }
253
+ else
254
+ {
255
+ targetFrameworkElement = new XElement ( "TargetFrameworks" , string . Join ( ';' , targetFrameworks ) ) ;
256
+ }
257
+
258
+ xml . Element ( "Project" ) . Element ( "PropertyGroup" ) . Add ( targetFrameworkElement ) ;
259
+ xml . Save ( project . ProjectFileNamePath ) ;
260
+ }
261
+
262
+ private protected void PinSDK ( ClonedTemplateProject project , string sdkVersion )
263
+ {
264
+ if ( string . IsNullOrEmpty ( sdkVersion ) )
265
+ {
266
+ throw new ArgumentException ( "Invalid sdkVersion" , nameof ( sdkVersion ) ) ;
267
+ }
268
+
269
+ if ( ! File . Exists ( project . ProjectFileNamePath ) )
270
+ {
271
+ throw new FileNotFoundException ( "coverlet.integration.template.csproj not found" , "coverlet.integration.template.csproj" ) ;
272
+ }
273
+
274
+ File . WriteAllText ( Path . Combine ( project . ProjectRootPath , "global.json" ) , $ "{{ \" sdk\" : {{ \" version\" : \" { sdkVersion } \" }} }}") ;
216
275
}
217
276
}
218
277
219
278
class ClonedTemplateProject : IDisposable
220
279
{
221
- public string Path { get ; set ; }
280
+ public string ? ProjectRootPath { get ; private set ; }
281
+ public bool _cleanupOnDispose { get ; set ; }
222
282
223
283
// We need to have a different asm name to avoid issue with collectors, we filter [coverlet.*]* by default
224
284
// https://github.com/tonerdo/coverlet/pull/410#discussion_r284526728
225
285
public static string AssemblyName { get ; } = "coverletsamplelib.integration.template" ;
226
286
public static string ProjectFileName { get ; } = "coverlet.integration.template.csproj" ;
287
+ public string ProjectFileNamePath => Path . Combine ( ProjectRootPath , "coverlet.integration.template.csproj" ) ;
288
+
289
+ public ClonedTemplateProject ( string projectRootPath , bool cleanupOnDispose ) => ( ProjectRootPath , _cleanupOnDispose ) = ( projectRootPath , cleanupOnDispose ) ;
290
+
291
+
227
292
228
293
public string [ ] GetFiles ( string filter )
229
294
{
230
- return Directory . GetFiles ( this . Path , filter , SearchOption . AllDirectories ) ;
295
+ return Directory . GetFiles ( ProjectRootPath , filter , SearchOption . AllDirectories ) ;
231
296
}
232
297
233
298
public void Dispose ( )
234
299
{
235
- Directory . Delete ( Path , true ) ;
300
+ if ( _cleanupOnDispose )
301
+ {
302
+ Directory . Delete ( ProjectRootPath , true ) ;
303
+ }
236
304
}
237
305
}
238
306
}
0 commit comments