Skip to content

Commit

Permalink
Merge pull request #210 from tonyhallett/include-referenced-projects
Browse files Browse the repository at this point in the history
option to add referenced projects to include.  Also contains a correc…
  • Loading branch information
tonyhallett authored Nov 18, 2021
2 parents bce7a33 + c1d0fcf commit f8a119a
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ RunWhenTestsFail By default coverage runs when tests fail. Set to false t
RunWhenTestsExceed Specify a value to only run coverage based upon the number of executing tests. **Cannot be used in conjunction with RunInParallel**
Exclude Filter expressions to exclude specific modules and types (multiple values)
Include Filter expressions to include specific modules and types (multiple values)
IncludeReferencedProjects Set to true to add all referenced projects to Include.
ExcludeByFile Glob patterns specifying source files to exclude e.g. **/Migrations/* (multiple values)
ExcludeByAttribute Attributes to exclude from code coverage (multiple values)
IncludeTestAssembly Specifies whether to report code coverage of the test assembly
Expand Down
5 changes: 5 additions & 0 deletions SharedProject/Core/Coverlet/Console/CoverletConsoleUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ internal List<string> GetCoverletSettings(ICoverageProject project)
coverletSettings.Add($@"--include ""{value.Replace("\"", "\\\"").Trim(' ', '\'')}""");
}

foreach (var includedReferencedProject in project.IncludedReferencedProjects)
{
coverletSettings.Add($@"--include ""[{includedReferencedProject}]*""");
}

foreach (var value in (project.Settings.ExcludeByFile ?? new string[0]).Where(x => !string.IsNullOrWhiteSpace(x)))
{
coverletSettings.Add($@"--exclude-by-file ""{value.Replace("\"", "\\\"").Trim(' ', '\'')}""");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private string GetSettings()
dataCollectorSettingsBuilder
.WithResultsDirectory(coverageProject.CoverageOutputFolder);

string[] projectExcludes = coverageProject.ExcludedReferencedProjects.ToArray();
string[] projectExcludes = coverageProject.ExcludedReferencedProjects.Select(erp => $"[{erp}]*").ToArray();
if(coverageProject.Settings.Exclude != null)
{
projectExcludes = projectExcludes.Concat(coverageProject.Settings.Exclude).ToArray();
Expand All @@ -148,8 +148,15 @@ private string GetSettings()
.WithExcludeByFile(coverageProject.Settings.ExcludeByFile, runSettingsCoverletConfiguration.ExcludeByFile);
dataCollectorSettingsBuilder
.WithExcludeByAttribute(coverageProject.Settings.ExcludeByAttribute, runSettingsCoverletConfiguration.ExcludeByAttribute);

string[] projectIncludes = coverageProject.IncludedReferencedProjects.Select(irp => $"[{irp}]*").ToArray();
if(coverageProject.Settings.Include != null)
{
projectIncludes = projectIncludes.Concat(coverageProject.Settings.Include).ToArray();
}

dataCollectorSettingsBuilder
.WithInclude(coverageProject.Settings.Include, runSettingsCoverletConfiguration.Include);
.WithInclude(projectIncludes, runSettingsCoverletConfiguration.Include);
dataCollectorSettingsBuilder
.WithIncludeTestAssembly(coverageProject.Settings.IncludeTestAssembly, runSettingsCoverletConfiguration.IncludeTestAssembly);

Expand Down
31 changes: 26 additions & 5 deletions SharedProject/Core/Model/CoverageProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ public XElement ProjectFileXElement
}
}
public List<string> ExcludedReferencedProjects { get; } = new List<string>();
public List<string> IncludedReferencedProjects { get; set; } = new List<string>();
public bool Is64Bit { get; set; }
public string RunSettingsFile { get; set; }

Expand Down Expand Up @@ -384,17 +385,25 @@ public async System.Threading.Tasks.Task PrepareForCoverageAsync()
EnsureDirectories();
CleanFCCDirectory();
SynchronizeBuildOutput();
await SetExcludedReferencedProjectsAsync();
await SetIncludedExcludedReferencedProjectsAsync();
}

private async System.Threading.Tasks.Task SetExcludedReferencedProjectsAsync()
private async System.Threading.Tasks.Task SetIncludedExcludedReferencedProjectsAsync()
{
List<ReferencedProject> referencedProjects = await SafeGetReferencedProjectsFromDteAsync();
List<ReferencedProject> referencedProjects = await GetReferencedProjectsAsync();
SetExcludedReferencedProjects(referencedProjects);
SetIncludedReferencedProjects(referencedProjects);
}

if (referencedProjects == null)
private void SetIncludedReferencedProjects(List<ReferencedProject> referencedProjects)
{
if (settings.IncludeReferencedProjects)
{
referencedProjects = await GetReferencedProjectsFromProjectFileAsync();
IncludedReferencedProjects = referencedProjects.Select(referencedProject => referencedProject.AssemblyName).ToList();
}
}
private void SetExcludedReferencedProjects(List<ReferencedProject> referencedProjects)
{
foreach (var referencedProject in referencedProjects)
{
if (referencedProject.ExcludeFromCodeCoverage)
Expand All @@ -403,6 +412,18 @@ private async System.Threading.Tasks.Task SetExcludedReferencedProjectsAsync()
}
}
}

private async Task<List<ReferencedProject>> GetReferencedProjectsAsync()
{
List<ReferencedProject> referencedProjects = await SafeGetReferencedProjectsFromDteAsync();

if (referencedProjects == null)
{
referencedProjects = await GetReferencedProjectsFromProjectFileAsync();
}
return referencedProjects;
}

private async Task<List<ReferencedProject>> SafeGetReferencedProjectsFromDteAsync()
{
try
Expand Down
1 change: 1 addition & 0 deletions SharedProject/Core/Model/ICoverageProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal interface ICoverageProject
string CoverageOutputFolder { get; set; }
string DefaultCoverageOutputFolder { get; }
List<string> ExcludedReferencedProjects { get; }
List<string> IncludedReferencedProjects { get; }
string FailureDescription { get; set; }
string FailureStage { get; set; }
bool HasFailed { get; }
Expand Down
5 changes: 5 additions & 0 deletions SharedProject/Core/OpenCover/OpenCoverUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ public async Task<bool> RunOpenCoverAsync(ICoverageProject project, bool throwEr
filters.Add($@"+{value.Replace("\"", "\\\"").Trim(' ', '\'')}");
}

foreach (var includedReferencedProject in project.IncludedReferencedProjects)
{
filters.Add($@"+[{includedReferencedProject}]*");
}

if (!filters.Any())
{
filters.Add(defaultFilter);
Expand Down
4 changes: 4 additions & 0 deletions SharedProject/Options/AppOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ internal AppOptions(bool isReadOnly)
")]
public string[] Include { get; set; }

[Category(excludeIncludeCategory)]
[Description("Set to true to add all referenced projects to Include.")]
public bool IncludeReferencedProjects { get; set; }

[Category(excludeIncludeCategory)]
[Description(
@"Glob patterns specifying source files to exclude (multiple)
Expand Down
1 change: 1 addition & 0 deletions SharedProject/Options/IAppOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ public interface IAppOptions
bool NamespacedClasses { get; }
bool HideFullyCovered { get; }
bool AdjacentBuildOutput { get; }
bool IncludeReferencedProjects { get; }
}
}

0 comments on commit f8a119a

Please sign in to comment.