Skip to content

Commit e11c14d

Browse files
TFTomSunTomCollaborationpetli
authored
Added InstrumentModulesWithoutLocalSources setting (#1360)
* ignore not available sources * remove debug.assert * readded assert * added setting to allow instrumentation of modules without local sources * Update src/coverlet.console/Program.cs Co-authored-by: Peter Liljenberg <peter@klavrekod.se> * Update CoberturaReporter.cs removed obsolete Debug.Assert Co-authored-by: adfrth5 <thomas_frenzel@siemens.com> Co-authored-by: Peter Liljenberg <peter@klavrekod.se>
1 parent 1e85144 commit e11c14d

File tree

9 files changed

+35
-5
lines changed

9 files changed

+35
-5
lines changed

src/coverlet.collector/DataCollection/CoverageWrapper.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public Coverage CreateCoverage(CoverletSettings settings, ILogger coverletLogger
3333
UseSourceLink = settings.UseSourceLink,
3434
SkipAutoProps = settings.SkipAutoProps,
3535
DoesNotReturnAttributes = settings.DoesNotReturnAttributes,
36-
DeterministicReport = settings.DeterministicReport
36+
DeterministicReport = settings.DeterministicReport,
37+
InstrumentModulesWithoutLocalSources = settings.InstrumentModulesWithoutLocalSources
3738
};
3839

3940
return new Coverage(

src/coverlet.collector/DataCollection/CoverletSettings.cs

+6
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ internal class CoverletSettings
8181
/// </summary>
8282
public bool DeterministicReport { get; set; }
8383

84+
/// <summary>
85+
/// Instruments modules even if the sources from the PDBs can't be resolved.
86+
/// </summary>
87+
public bool InstrumentModulesWithoutLocalSources { get; set; }
88+
8489
public override string ToString()
8590
{
8691
var builder = new StringBuilder();
@@ -98,6 +103,7 @@ public override string ToString()
98103
builder.AppendFormat("SkipAutoProps: '{0}'", SkipAutoProps);
99104
builder.AppendFormat("DoesNotReturnAttributes: '{0}'", string.Join(",", DoesNotReturnAttributes ?? Enumerable.Empty<string>()));
100105
builder.AppendFormat("DeterministicReport: '{0}'", DeterministicReport);
106+
builder.AppendFormat("InstrumentModulesWithoutLocalSources: '{0}'", InstrumentModulesWithoutLocalSources);
101107

102108
return builder.ToString();
103109
}

src/coverlet.collector/DataCollection/CoverletSettingsParser.cs

+13
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public CoverletSettings Parse(XmlElement configurationElement, IEnumerable<strin
4848
coverletSettings.SkipAutoProps = ParseSkipAutoProps(configurationElement);
4949
coverletSettings.DoesNotReturnAttributes = ParseDoesNotReturnAttributes(configurationElement);
5050
coverletSettings.DeterministicReport = ParseDeterministicReport(configurationElement);
51+
coverletSettings.InstrumentModulesWithoutLocalSources = ParseInstrumentModulesWithoutLocalSources(configurationElement);
5152
}
5253

5354
coverletSettings.ReportFormats = ParseReportFormats(configurationElement);
@@ -211,6 +212,18 @@ private static bool ParseDeterministicReport(XmlElement configurationElement)
211212
return deterministicReport;
212213
}
213214

215+
/// <summary>
216+
/// Parse InstrumentModulesWithoutLocalSources flag
217+
/// </summary>
218+
/// <param name="configurationElement">Configuration element</param>
219+
/// <returns>InstrumentModulesWithoutLocalSources flag</returns>
220+
private static bool ParseInstrumentModulesWithoutLocalSources(XmlElement configurationElement)
221+
{
222+
XmlElement instrumentModulesWithoutLocalSourcesElement = configurationElement[CoverletConstants.InstrumentModulesWithoutLocalSources];
223+
bool.TryParse(instrumentModulesWithoutLocalSourcesElement?.InnerText, out bool instrumentModulesWithoutLocalSources);
224+
return instrumentModulesWithoutLocalSources;
225+
}
226+
214227
/// <summary>
215228
/// Parse include test assembly flag
216229
/// </summary>

src/coverlet.collector/Utilities/CoverletConstants.cs

+1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ internal static class CoverletConstants
2626
public const string SkipAutoProps = "SkipAutoProps";
2727
public const string DoesNotReturnAttributesElementName = "DoesNotReturnAttribute";
2828
public const string DeterministicReport = "DeterministicReport";
29+
public const string InstrumentModulesWithoutLocalSources = "InstrumentModulesWithoutLocalSources";
2930
}
3031
}

src/coverlet.console/Program.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ static int Main(string[] args)
7070
CommandOption mergeWith = app.Option("--merge-with", "Path to existing coverage result to merge.", CommandOptionType.SingleValue);
7171
CommandOption useSourceLink = app.Option("--use-source-link", "Specifies whether to use SourceLink URIs in place of file system paths.", CommandOptionType.NoValue);
7272
CommandOption doesNotReturnAttributes = app.Option("--does-not-return-attribute", "Attributes that mark methods that do not return.", CommandOptionType.MultipleValue);
73+
CommandOption instrumentModulesWithoutLocalSources = app.Option("--instrument-modules-without-local-sources", "Specifies whether modules should be instrumented even if the sources from the PDBs can't be found locally.", CommandOptionType.NoValue);
7374

7475
app.OnExecute(() =>
7576
{
@@ -97,7 +98,8 @@ static int Main(string[] args)
9798
MergeWith = mergeWith.Value(),
9899
UseSourceLink = useSourceLink.HasValue(),
99100
SkipAutoProps = skipAutoProp.HasValue(),
100-
DoesNotReturnAttributes = doesNotReturnAttributes.Values.ToArray()
101+
DoesNotReturnAttributes = doesNotReturnAttributes.Values.ToArray(),
102+
InstrumentModulesWithoutLocalSources = instrumentModulesWithoutLocalSources.HasValue(),
101103
};
102104

103105
ISourceRootTranslator sourceRootTranslator = serviceProvider.GetRequiredService<ISourceRootTranslator>();

src/coverlet.core/Coverage.cs

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ internal class CoverageParameters
4343
public bool SkipAutoProps { get; set; }
4444
[DataMember]
4545
public bool DeterministicReport { get; set; }
46+
[DataMember]
47+
public bool InstrumentModulesWithoutLocalSources { get; set; }
4648
}
4749

4850
internal class Coverage

src/coverlet.core/Instrumentation/Instrumenter.cs

+5
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ public bool CanInstrument()
9494
{
9595
if (_instrumentationHelper.HasPdb(_module, out bool embeddedPdb))
9696
{
97+
if (this._parameters.InstrumentModulesWithoutLocalSources)
98+
{
99+
return true;
100+
}
101+
97102
if (embeddedPdb)
98103
{
99104
if (_instrumentationHelper.EmbeddedPortablePdbHasLocalSource(_module, out string firstNotFoundDocument))

src/coverlet.core/Reporters/CoberturaReporter.cs

-3
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,6 @@ private static string GetRelativePathFromBase(IEnumerable<string> basePaths, str
227227
return path.Substring(basePath.Length);
228228
}
229229
}
230-
231-
Debug.Assert(false, "Unexpected, we should find at least one path starts with one pre-build roots list");
232-
233230
return path;
234231
}
235232
}

src/coverlet.msbuild.tasks/InstrumentationTask.cs

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public class InstrumentationTask : BaseTask
4747

4848
public bool DeterministicReport { get; set; }
4949

50+
public bool InstrumentModulesWithoutLocalSources { get; set; }
51+
5052
[Output]
5153
public ITaskItem InstrumenterState { get; set; }
5254

@@ -99,6 +101,7 @@ public override bool Execute()
99101
UseSourceLink = UseSourceLink,
100102
SkipAutoProps = SkipAutoProps,
101103
DeterministicReport = DeterministicReport,
104+
InstrumentModulesWithoutLocalSources = InstrumentModulesWithoutLocalSources,
102105
DoesNotReturnAttributes = DoesNotReturnAttribute?.Split(',')
103106
};
104107

0 commit comments

Comments
 (0)