Skip to content

Commit

Permalink
Added a simple lcov writer
Browse files Browse the repository at this point in the history
  • Loading branch information
LPeter1997 committed Sep 29, 2024
1 parent a816e23 commit 3ae29b7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Draco.Compiler/Internal/Utilities/DotGraphBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,10 @@ public string ToDot()
return new StreamReader(stream).ReadToEnd();
}

public void WriteTo(Stream stream) => this.WriteTo(new StreamWriter(stream));

public void WriteTo(StreamWriter writer)
public void WriteTo(Stream stream)
{
var writer = new StreamWriter(stream);

// Header
writer.Write(this.isDirected ? "digraph" : "graph");
writer.Write(' ');
Expand Down
50 changes: 50 additions & 0 deletions src/Draco.Coverage/CoverageResult.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;

namespace Draco.Coverage;

Expand Down Expand Up @@ -33,4 +36,51 @@ public CoverageResult(ImmutableArray<SequencePoint> sequencePoints, ImmutableArr
this.SequencePoints = sequencePoints;
this.Hits = hits;
}

/// <summary>
/// Writes the coverage result in LCOV format.
/// </summary>
/// <param name="stream">The stream to write to.</param>
public void WriteLcov(Stream stream)
{
using var writer = new StreamWriter(stream);

// We need to emit the data by file
var dataByFile = this.SequencePoints
.Zip(this.Hits, (sp, hits) => (SequencePoint: sp, Hits: hits))
.GroupBy(x => x.SequencePoint.FileName);
foreach (var group in dataByFile) WriteSequencePointsForFile(group.Key, group);

void WriteSequencePointsForFile(string fileName, IEnumerable<(SequencePoint SequencePoint, int Hits)> data)
{
writer.WriteLine($"SF:{fileName}");

// For each group, sequence points are grouped and sorted by start line
var dataByLine = data
.GroupBy(x => x.SequencePoint.StartLine)
.OrderBy(x => x.Key);

// Keep track of how many entries we have and how many are hit
var entries = 0;
var entriesHit = 0;

foreach (var lineData in dataByLine)
{
var line = lineData.Key;
// NOTE: To simplify things, if there are multiple sequence points on the same line,
// we sum their hits together
var hits = lineData.Sum(x => x.Hits);
writer.WriteLine($"DA:{line},{hits}");
++entries;
if (hits != 0) ++entriesHit;
}

// Write entry count
writer.WriteLine($"LF:{entries}");
writer.WriteLine($"LH:{entriesHit}");

// End of file
writer.WriteLine("end_of_record");
}
}
}

0 comments on commit 3ae29b7

Please sign in to comment.