Skip to content

Commit 3d8ced8

Browse files
committed
Add replaceable Configuration / CodeCoverage to custom template
1 parent 9343399 commit 3d8ced8

File tree

2 files changed

+154
-17
lines changed

2 files changed

+154
-17
lines changed

FineCodeCoverageTests/MsCodeCoverage/RunSettingsTemplate_Tests.cs

Lines changed: 132 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ public void Should_Add_Replaceable_RunConfiguration_If_Not_Present()
211211
<DataCollectors>
212212
<DataCollector friendlyName='Code Coverage' enabled='true'>
213213
<Configuration>
214+
<CodeCoverage/>
214215
<Format>Cobertura</Format>
215216
</Configuration>
216217
</DataCollector>
@@ -236,6 +237,7 @@ public void Should_Add_Replaceable_RunConfiguration_If_Not_Present()
236237
<DataCollectors>
237238
<DataCollector friendlyName='Code Coverage' enabled='true'>
238239
<Configuration>
240+
<CodeCoverage/>
239241
<Format>Cobertura</Format>
240242
<FCCGenerated/>
241243
</Configuration>
@@ -259,6 +261,7 @@ public void Should_Add_Replaceable_ResultsDirectory_And_TestAdaptersPath_If_Not_
259261
<DataCollectors>
260262
<DataCollector friendlyName='Code Coverage' enabled='true'>
261263
<Configuration>
264+
<CodeCoverage/>
262265
<Format>Cobertura</Format>
263266
</Configuration>
264267
</DataCollector>
@@ -283,6 +286,7 @@ public void Should_Add_Replaceable_ResultsDirectory_And_TestAdaptersPath_If_Not_
283286
<DataCollectors>
284287
<DataCollector friendlyName='Code Coverage' enabled='true'>
285288
<Configuration>
289+
<CodeCoverage/>
286290
<Format>Cobertura</Format>
287291
<FCCGenerated/>
288292
</Configuration>
@@ -424,6 +428,7 @@ public void Should_AddEnabledReplacementAttributeIfNotPresent_To_Existing_Ms_Dat
424428
<DataCollectors>
425429
<DataCollector friendlyName='Code Coverage'>
426430
<Configuration>
431+
<CodeCoverage/>
427432
<Format>Cobertura</Format>
428433
<FCCGenerated/>
429434
</Configuration>
@@ -444,6 +449,7 @@ public void Should_AddEnabledReplacementAttributeIfNotPresent_To_Existing_Ms_Dat
444449
<DataCollectors>
445450
<DataCollector friendlyName='Code Coverage' enabled='{replacements.Enabled}'>
446451
<Configuration>
452+
<CodeCoverage/>
447453
<Format>Cobertura</Format>
448454
<FCCGenerated/>
449455
</Configuration>
@@ -465,6 +471,7 @@ public void Should_Add_Cobertura_Format_To_Existing_Configuration_Element_If_Not
465471
<DataCollectors>
466472
<DataCollector friendlyName='Code Coverage' enabled='true'>
467473
<Configuration>
474+
<CodeCoverage/>
468475
<FCCGenerated/>
469476
</Configuration>
470477
</DataCollector>
@@ -483,8 +490,9 @@ public void Should_Add_Cobertura_Format_To_Existing_Configuration_Element_If_Not
483490
<DataCollectors>
484491
<DataCollector friendlyName='Code Coverage' enabled='true'>
485492
<Configuration>
486-
<Format>Cobertura</Format>
493+
<CodeCoverage/>
487494
<FCCGenerated/>
495+
<Format>Cobertura</Format>
488496
</Configuration>
489497
</DataCollector>
490498
</DataCollectors>
@@ -504,6 +512,7 @@ public void Should_Correct_Existing_Format_To_Cobertura_When_Different_Format()
504512
<DataCollectors>
505513
<DataCollector friendlyName='Code Coverage' enabled='true'>
506514
<Configuration>
515+
<CodeCoverage/>
507516
<Format>Xml</Format>
508517
<FCCGenerated/>
509518
</Configuration>
@@ -523,6 +532,7 @@ public void Should_Correct_Existing_Format_To_Cobertura_When_Different_Format()
523532
<DataCollectors>
524533
<DataCollector friendlyName='Code Coverage' enabled='true'>
525534
<Configuration>
535+
<CodeCoverage/>
526536
<Format>Cobertura</Format>
527537
<FCCGenerated/>
528538
</Configuration>
@@ -544,6 +554,7 @@ public void Should_Add_FCCGenerated_To_Existing_Configuration_Element()
544554
<DataCollectors>
545555
<DataCollector friendlyName='Code Coverage' enabled='true'>
546556
<Configuration>
557+
<CodeCoverage/>
547558
<Format>Cobertura</Format>
548559
</Configuration>
549560
</DataCollector>
@@ -562,6 +573,7 @@ public void Should_Add_FCCGenerated_To_Existing_Configuration_Element()
562573
<DataCollectors>
563574
<DataCollector friendlyName='Code Coverage' enabled='true'>
564575
<Configuration>
576+
<CodeCoverage/>
565577
<Format>Cobertura</Format>
566578
<FCCGenerated/>
567579
</Configuration>
@@ -574,7 +586,7 @@ public void Should_Add_FCCGenerated_To_Existing_Configuration_Element()
574586
}
575587

576588
[Test]
577-
public void Should_Add_Configuration_Element_If_Not_Present_To_Existing_Ms_DataCollector()
589+
public void Should_Add_Replaceable_Configuration_Element_If_Not_Present_To_Existing_Ms_DataCollector()
578590
{
579591
var custom = $@"
580592
<RunSettings>
@@ -588,6 +600,7 @@ public void Should_Add_Configuration_Element_If_Not_Present_To_Existing_Ms_DataC
588600

589601
var replacements = new RunSettingsTemplateReplacements
590602
{
603+
PublicKeyTokensInclude = "<PublicKeyTokensInclude/>"
591604
};
592605

593606
var expected = $@"
@@ -597,6 +610,123 @@ public void Should_Add_Configuration_Element_If_Not_Present_To_Existing_Ms_DataC
597610
<DataCollectors>
598611
<DataCollector friendlyName='Code Coverage' enabled='true'>
599612
<Configuration>
613+
<CodeCoverage>
614+
<ModulePaths>
615+
<Exclude>
616+
</Exclude>
617+
<Include>
618+
</Include>
619+
</ModulePaths>
620+
<Functions>
621+
<Exclude>
622+
</Exclude>
623+
<Include>
624+
</Include>
625+
</Functions>
626+
<Attributes>
627+
<Exclude>
628+
</Exclude>
629+
<Include>
630+
</Include>
631+
</Attributes>
632+
<Sources>
633+
<Exclude>
634+
</Exclude>
635+
<Include>
636+
</Include>
637+
</Sources>
638+
<CompanyNames>
639+
<Exclude>
640+
</Exclude>
641+
<Include>
642+
</Include>
643+
</CompanyNames>
644+
<PublicKeyTokens>
645+
<Exclude>
646+
</Exclude>
647+
<Include>
648+
<PublicKeyTokensInclude/>
649+
</Include>
650+
</PublicKeyTokens>
651+
</CodeCoverage>
652+
<Format>Cobertura</Format>
653+
<FCCGenerated/>
654+
</Configuration>
655+
</DataCollector>
656+
</DataCollectors>
657+
</DataCollectionRunSettings>
658+
</RunSettings>";
659+
660+
ConfiguredCustomReplaceTest(custom, expected, replacements);
661+
}
662+
663+
[Test]
664+
public void Should_Add_Replaceable_CodeCoverage_Element_If_Not_Present_To_Existing_Ms_DataCollector()
665+
{
666+
var custom = $@"
667+
<RunSettings>
668+
{nonReplacedRunConfiguration}
669+
<DataCollectionRunSettings>
670+
<DataCollectors>
671+
<DataCollector friendlyName='Code Coverage' enabled='true'>
672+
<Configuration>
673+
</Configuration>
674+
</DataCollector>
675+
</DataCollectors>
676+
</DataCollectionRunSettings>
677+
</RunSettings>";
678+
679+
var replacements = new RunSettingsTemplateReplacements
680+
{
681+
PublicKeyTokensInclude = "<PublicKeyTokensInclude/>"
682+
};
683+
684+
var expected = $@"
685+
<RunSettings>
686+
{nonReplacedRunConfiguration}
687+
<DataCollectionRunSettings>
688+
<DataCollectors>
689+
<DataCollector friendlyName='Code Coverage' enabled='true'>
690+
<Configuration>
691+
<CodeCoverage>
692+
<ModulePaths>
693+
<Exclude>
694+
</Exclude>
695+
<Include>
696+
</Include>
697+
</ModulePaths>
698+
<Functions>
699+
<Exclude>
700+
</Exclude>
701+
<Include>
702+
</Include>
703+
</Functions>
704+
<Attributes>
705+
<Exclude>
706+
</Exclude>
707+
<Include>
708+
</Include>
709+
</Attributes>
710+
<Sources>
711+
<Exclude>
712+
</Exclude>
713+
<Include>
714+
</Include>
715+
</Sources>
716+
<CompanyNames>
717+
<Exclude>
718+
</Exclude>
719+
<Include>
720+
</Include>
721+
</CompanyNames>
722+
<PublicKeyTokens>
723+
<Exclude>
724+
</Exclude>
725+
<Include>
726+
<PublicKeyTokensInclude/>
727+
</Include>
728+
</PublicKeyTokens>
729+
</CodeCoverage>
600730
<Format>Cobertura</Format>
601731
<FCCGenerated/>
602732
</Configuration>

SharedProject/Core/MsTestPlatform/CodeCoverage/RunSettingsTemplate.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ private class ReplacementLookups : IRunSettingsTemplateReplacements
4242
public string MsDataCollectorElement { get; }
4343

4444
private const string fccMarkerElementName = "FCCGenerated";
45+
private string msDataCollectorConfigurationElement;
46+
private string msDataCollectorCodeCoverageElement;
4547

4648
private readonly List<(string elementName, string value)> recommendedYouDoNotChangeElementsNetCore = new List<(string elementName, string value)>
4749
{
@@ -78,11 +80,8 @@ public RunSettingsTemplate()
7880
<CollectSourceInformation>False</CollectSourceInformation>
7981
</RunConfiguration>
8082
";
81-
82-
MsDataCollectorElement = $@"
83-
<DataCollector friendlyName='Code Coverage' enabled='{replacementLookups.Enabled}'>
84-
<Configuration>
85-
<CodeCoverage>
83+
msDataCollectorCodeCoverageElement = $@"
84+
<CodeCoverage>
8685
<ModulePaths>
8786
<Exclude>
8887
{replacementLookups.ModulePathsExclude}
@@ -132,9 +131,17 @@ public RunSettingsTemplate()
132131
</Include>
133132
</PublicKeyTokens>
134133
</CodeCoverage>
134+
";
135+
msDataCollectorConfigurationElement = $@"
136+
<Configuration>
137+
{msDataCollectorCodeCoverageElement}
135138
<Format>Cobertura</Format>
136139
<{fccMarkerElementName}/>
137140
</Configuration>
141+
";
142+
MsDataCollectorElement = $@"
143+
<DataCollector friendlyName='Code Coverage' enabled='{replacementLookups.Enabled}'>
144+
{msDataCollectorConfigurationElement}
138145
</DataCollector>
139146
";
140147
DataCollectorsElement = $@"
@@ -238,9 +245,9 @@ private void EnsureRunConfiguration(XElement runSettingsElement)
238245
AddIfNotPresent(runSettingsElement, "RunConfiguration", RunConfigurationElement, EnsureRunConfigurationEssentials,true);
239246
}
240247

241-
private void AddIfNotPresent(XElement parent,string elementName,string elementAsString,Action<XElement> elsePath = null,bool addFirst = true)
248+
private void AddIfNotPresent(XElement parent,string elementName,string elementAsString,Action<XElement> presentPath = null,bool addFirst = true)
242249
{
243-
AddIfNotPresent(parent, p => p.Element(elementName), elementAsString, elsePath, addFirst);
250+
AddIfNotPresent(parent, p => p.Element(elementName), elementAsString, presentPath, addFirst);
244251
}
245252

246253
private void AddIfNotPresent(XElement parent, Func<XElement,XElement> find,string elementAsString, Action<XElement> presentPath = null,bool addFirst = true)
@@ -277,13 +284,13 @@ private void EnsureMsDataCollectorElement(XElement dataCollectors)
277284

278285
private XElement GetOrAddConfigurationElement(XElement msDataCollector)
279286
{
280-
var msDataCollectorConfiguration = msDataCollector.Element("Configuration");
281-
if (msDataCollectorConfiguration == null)
282-
{
283-
msDataCollectorConfiguration = new XElement("Configuration");
284-
msDataCollector.Add(msDataCollectorConfiguration);
285-
}
286-
return msDataCollectorConfiguration;
287+
AddIfNotPresent(msDataCollector, "Configuration", msDataCollectorConfigurationElement, AddCodeCoverageIfNotPresent,false);
288+
return msDataCollector.Element("Configuration");
289+
}
290+
291+
private void AddCodeCoverageIfNotPresent(XElement configurationElement)
292+
{
293+
AddIfNotPresent(configurationElement, "CodeCoverage", msDataCollectorCodeCoverageElement, null, true);
287294
}
288295

289296
private void AddEnabledReplacementAttributeIfNotPresent(XElement msDataCollector)
@@ -308,7 +315,7 @@ private void AddOrCorrectFormat(XElement configuration)
308315
var formatElement = configuration.Element("Format");
309316
if (formatElement == null)
310317
{
311-
configuration.AddFirst(new XElement("Format", "Cobertura"));
318+
configuration.Add(new XElement("Format", "Cobertura"));
312319
}
313320
else
314321
{

0 commit comments

Comments
 (0)