Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
Add glob pattern support to -f parameter (#69)
Browse files Browse the repository at this point in the history
* Add glob pattern support to -f parameter

* fix

* resolve comments

* resolve comments
  • Loading branch information
hanabi1224 authored and AdmiringWorm committed Jun 10, 2019
1 parent 896cc12 commit 1145119
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 4 deletions.
102 changes: 102 additions & 0 deletions Source/Codecov.Tests/Coverage/Tool/CoverageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,107 @@ public void Should_Read_Single_Coverage_Report()
// Clean Up
File.Delete("./coverage.xml");
}

[Fact]
public void Should_Read_No_Coverage_Reports_With_WildCard_Path()
{
// Given
ICoverageOptions options = Substitute.For<ICoverageOptions>();
options.Files.Returns(new[] { "*.opencover.xml", });
File.WriteAllText("./coverageUnit.xml", "Unit Tests.");
File.WriteAllText("./coverageIntegration.xml", "Integration Tests.");
Codecov.Coverage.Tool.Coverage coverage = new Codecov.Coverage.Tool.Coverage(options);

// When
Action coverageReport = () =>
{
System.Collections.Generic.IEnumerable<ReportFile> x = coverage.CoverageReports;
};

// Then
coverageReport.Should().Throw<Exception>().WithMessage("No Report detected.");

// Clean Up
File.Delete("./coverageUnit.xml");
File.Delete("./coverageIntegration.xml");
}

[Fact]
public void Should_Read_Multiple_Coverage_Reports_With_WildCard_Path()
{
// Given
ICoverageOptions options = Substitute.For<ICoverageOptions>();
options.Files.Returns(new[] { "*.opencover.xml", });
File.WriteAllText("./coverageUnit.opencover.xml", "Unit Tests.");
File.WriteAllText("./coverageIntegration.opencover.xml", "Integration Tests.");
Codecov.Coverage.Tool.Coverage coverage = new Codecov.Coverage.Tool.Coverage(options);

// When
System.Collections.Generic.List<ReportFile> coverageReport = coverage.CoverageReports.OrderBy(_ => _.File).ToList();

// Then
coverageReport.Count.Should().Be(2);
coverageReport[0].File.Should().Contain("coverageIntegration.opencover.xml");
coverageReport[0].Content.Should().Be("Integration Tests.");
coverageReport[1].File.Should().Contain("coverageUnit.opencover.xml");
coverageReport[1].Content.Should().Be("Unit Tests.");

// Clean Up
File.Delete("./coverageUnit.opencover.xml");
File.Delete("./coverageIntegration.opencover.xml");
}

[Fact]
public void Should_Read_Single_Coverage_Reports_With_WildCard_Path()
{
// Given
ICoverageOptions options = Substitute.For<ICoverageOptions>();
options.Files.Returns(new[] { "*.opencover.xml", });
File.WriteAllText("./coverageUnit.opencover.xml", "Unit Tests.");
File.WriteAllText("./coverageIntegration.xml", "Integration Tests.");
Codecov.Coverage.Tool.Coverage coverage = new Codecov.Coverage.Tool.Coverage(options);

// When
System.Collections.Generic.List<ReportFile> coverageReport = coverage.CoverageReports.OrderBy(_ => _.File).ToList();

// Then
coverageReport.Count.Should().Be(1);
coverageReport[0].File.Should().Contain("coverageUnit.opencover.xml");
coverageReport[0].Content.Should().Be("Unit Tests.");

// Clean Up
File.Delete("./coverageUnit.opencover.xml");
File.Delete("./coverageIntegration.xml");
}

[Fact]
public void Should_Read_Coverage_Reports_With_Multi_Level_WildCard_Path()
{
if (!Directory.Exists("reports"))
{
Directory.CreateDirectory("reports");
}

// Given
ICoverageOptions options = Substitute.For<ICoverageOptions>();
options.Files.Returns(new[] { "reports/*.OpenCover.xml", });
File.WriteAllText("reports/coverageUnit.opencover.xml", "Unit Tests.");
File.WriteAllText("reports/coverageIntegration.opencover.xml", "Integration Tests.");
Codecov.Coverage.Tool.Coverage coverage = new Codecov.Coverage.Tool.Coverage(options);

// When
System.Collections.Generic.List<ReportFile> coverageReport = coverage.CoverageReports.OrderBy(_ => _.File).ToList();

// Then
coverageReport.Count.Should().Be(2);
coverageReport[0].File.Should().Contain("coverageIntegration.opencover.xml");
coverageReport[0].Content.Should().Be("Integration Tests.");
coverageReport[1].File.Should().Contain("coverageUnit.opencover.xml");
coverageReport[1].Content.Should().Be("Unit Tests.");

// Clean Up
File.Delete("reports\\coverageUnit.opencover.xml");
File.Delete("reports\\coverageIntegration.opencover.xml");
}
}
}
1 change: 1 addition & 0 deletions Source/Codecov/Codecov.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.5.0" />
<PackageReference Include="Glob" Version="1.1.3" />
<PackageReference Include="Serilog" Version="2.8.0" />
<PackageReference Include="Serilog.Sinks.ColoredConsole" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
Expand Down
43 changes: 39 additions & 4 deletions Source/Codecov/Coverage/Tool/Coverage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using GlobExpressions;

namespace Codecov.Coverage.Tool
{
Expand All @@ -21,15 +22,39 @@ public Coverage(ICoverageOptions coverageOptions)

private ICoverageOptions CoverageOptions { get; }

private static bool VerifyReportFile(string path)
private static bool VerifyReportFileAndExpandGlobPatterns(string path, out IEnumerable<string> expandedPath)
{
HashSet<string> expanded = new HashSet<string> { path };
expandedPath = expanded;
if (string.IsNullOrWhiteSpace(path))
{
Logger.Log.Warning("Invalid report path.");
return false;
}

if (!File.Exists(path))
if (path.Contains('*')
|| path.Contains('?')
|| path.Contains('!')
|| path.Contains(','))
{
//Logger.Log.Information($"Using wildcard path {path}");
List<string> matches = Glob.Files(Environment.CurrentDirectory, path, GlobOptions.Compiled | GlobOptions.CaseInsensitive)?.ToList();
if (matches?.Any() != true)
{
//Logger.Log.Warning($"There are no files that match the wildcard {path}.");
return false;
}

expanded.Clear();
matches.ForEach(_ =>
{
//Logger.Log.Information($"Adding file {_} that matches wildcard path {path}");
expanded.Add(_);
});

return true;
}
else if (!File.Exists(path))
{
Logger.Log.Warning($"The file {path} does not exist.");
return false;
Expand All @@ -40,8 +65,18 @@ private static bool VerifyReportFile(string path)

private IEnumerable<ReportFile> LoadReportFile()
{
var report = CoverageOptions.Files.Where(x => VerifyReportFile(x)).Select(x => new ReportFile(x, File.ReadAllText(x))).ToArray();
if (!report.Any())
ReportFile[] report = CoverageOptions.Files?
.SelectMany(x =>
{
if (VerifyReportFileAndExpandGlobPatterns(x, out IEnumerable<string> expanded))
{
return expanded;
}
return Enumerable.Empty<string>();
})?
.Select(x => new ReportFile(x, File.ReadAllText(x)))?.ToArray();
if (report?.Any() != true)
{
throw new Exception("No Report detected.");
}
Expand Down

0 comments on commit 1145119

Please sign in to comment.