88using Coverlet . Core ;
99using Newtonsoft . Json ;
1010using NuGet . Packaging ;
11+ using Xunit ;
1112using Xunit . Sdk ;
1213
1314namespace Coverlet . Integration . Tests
@@ -52,7 +53,7 @@ private protected string GetPackageVersion(string filter)
5253 return manifest . Metadata . Version . OriginalVersion ;
5354 }
5455
55- private protected ClonedTemplateProject CloneTemplateProject ( )
56+ private protected ClonedTemplateProject CloneTemplateProject ( bool cleanupOnDispose = true )
5657 {
5758 DirectoryInfo finalRoot = Directory . CreateDirectory ( Guid . NewGuid ( ) . ToString ( "N" ) ) ;
5859 foreach ( string file in ( Directory . GetFiles ( $ "../../../../coverlet.integration.template", "*.cs" )
@@ -75,7 +76,7 @@ private protected ClonedTemplateProject CloneTemplateProject()
7576
7677 AddMicrosoftNETTestSdkRef ( finalRoot . FullName ) ;
7778
78- return new ClonedTemplateProject ( ) { Path = finalRoot . FullName } ;
79+ return new ClonedTemplateProject ( finalRoot . FullName , cleanupOnDispose ) ;
7980 }
8081
8182 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)
205206 return runsettingsPath ;
206207 }
207208
208- private protected void AssertCoverage ( ClonedTemplateProject clonedTemplateProject )
209+ private protected void AssertCoverage ( ClonedTemplateProject clonedTemplateProject , string filter = "coverage.json" )
209210 {
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 } \" }} }}") ;
216275 }
217276 }
218277
219278 class ClonedTemplateProject : IDisposable
220279 {
221- public string Path { get ; set ; }
280+ public string ? ProjectRootPath { get ; private set ; }
281+ public bool _cleanupOnDispose { get ; set ; }
222282
223283 // We need to have a different asm name to avoid issue with collectors, we filter [coverlet.*]* by default
224284 // https://github.com/tonerdo/coverlet/pull/410#discussion_r284526728
225285 public static string AssemblyName { get ; } = "coverletsamplelib.integration.template" ;
226286 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+
227292
228293 public string [ ] GetFiles ( string filter )
229294 {
230- return Directory . GetFiles ( this . Path , filter , SearchOption . AllDirectories ) ;
295+ return Directory . GetFiles ( ProjectRootPath , filter , SearchOption . AllDirectories ) ;
231296 }
232297
233298 public void Dispose ( )
234299 {
235- Directory . Delete ( Path , true ) ;
300+ if ( _cleanupOnDispose )
301+ {
302+ Directory . Delete ( ProjectRootPath , true ) ;
303+ }
236304 }
237305 }
238306}
0 commit comments