Skip to content

Commit

Permalink
Refactor into a cleaner set of parsers.
Browse files Browse the repository at this point in the history
  • Loading branch information
csMACnz committed Apr 18, 2017
1 parent 5b80e95 commit 46f76cb
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 95 deletions.
13 changes: 9 additions & 4 deletions src/csmacnz.Coveralls.Tests/ChutzpahJsonParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Reflection;
using csmacnz.Coveralls.Parsers;
using Xunit;
using System.Collections.Generic;

namespace csmacnz.Coveralls.Tests
{
Expand All @@ -23,20 +24,24 @@ public void GenerateSourceFiles_CorrectCoverage()
Assert.Equal(null, results.First().Coverage[7]);
}

private static string LoadFromResource(string embeddedResource)
private static string[] LoadFromResource(string embeddedResource)
{
var executingAssembly = Assembly.GetExecutingAssembly();
string contents;
List<string> contents = new List<string>();
using (var stream = executingAssembly.GetManifestResourceStream(embeddedResource))
{
Assert.NotNull(stream);
Debug.Assert(stream != null, "stream != null");
using (var reader = new StreamReader(stream))
{
contents = reader.ReadToEnd();
string line;
while ((line = reader.ReadLine()) != null)
{
contents.Add(line);
}
}
}
return contents;
return contents.ToArray();
}
}
}
15 changes: 5 additions & 10 deletions src/csmacnz.Coveralls.Tests/OpenCoverParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void EmptyReportLoadsNoSourceFiles()
{
var document = LoadDocumentFromResource("csmacnz.Coveralls.Tests.EmptyReport.xml");

var results = CreateOpenCoverParser().GenerateSourceFiles(document);
var results = OpenCoverParser.GenerateSourceFiles(document);

Assert.Equal(0, results.Count);
}
Expand All @@ -24,7 +24,7 @@ public void SingleFileReportLoadsSingleSourceFiles()
{
var document = LoadDocumentFromResource("csmacnz.Coveralls.Tests.SingleFileReport.xml");

var results = CreateOpenCoverParser().GenerateSourceFiles(document);
var results = OpenCoverParser.GenerateSourceFiles(document);

Assert.Equal(1, results.Count);
}
Expand All @@ -34,7 +34,7 @@ public void SingleFileReportWithSingleMethodLineCoveredWithoutSourceLoadsCorrect
{
var document = LoadDocumentFromResource("csmacnz.Coveralls.Tests.SingleFileReportOneLineCovered.xml");

var results = CreateOpenCoverParser().GenerateSourceFiles(document);
var results = OpenCoverParser.GenerateSourceFiles(document);

Assert.Equal(1, results[0].Coverage[8]);
}
Expand All @@ -44,16 +44,11 @@ public void SingleFileReportWithSingleMethodLineUncoveredWithoutSourceLoadsCorre
{
var document = LoadDocumentFromResource("csmacnz.Coveralls.Tests.SingleFileReportOneLineUncovered.xml");

var results = CreateOpenCoverParser().GenerateSourceFiles(document);
var results = OpenCoverParser.GenerateSourceFiles(document);

Assert.Equal(0, results[0].Coverage[8]);
}

private OpenCoverParser CreateOpenCoverParser()
{
return new OpenCoverParser();
}


private static XDocument LoadDocumentFromResource(string embeddedResource)
{
XDocument document;
Expand Down
2 changes: 1 addition & 1 deletion src/csmacnz.Coveralls.Tests/TestFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public bool WriteFile(string outputFile, string fileData)
return true;
}

public Option<string[]> ReadAllLines(string filePath)
public Option<string[]> TryReadAllLinesFromFile(string filePath)
{
if (_files.ContainsKey(filePath))
{
Expand Down
2 changes: 1 addition & 1 deletion src/csmacnz.Coveralls/Adapters/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public bool WriteFile(string outputFile, string fileData)
return true;
}

public Option<string[]> ReadAllLines(string filePath)
public Option<string[]> TryReadAllLinesFromFile(string filePath)
{
if (File.Exists(filePath))
{
Expand Down
146 changes: 76 additions & 70 deletions src/csmacnz.Coveralls/CoverageLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using csmacnz.Coveralls.Data;
using csmacnz.Coveralls.Parsers;
using csmacnz.Coveralls.Ports;
using System;

namespace csmacnz.Coveralls
{
Expand All @@ -21,80 +22,101 @@ public CoverageLoader(IFileSystem fileSystem)
public Result<List<CoverageFile>, LoadCoverageFilesError> LoadCoverageFiles(CoverageMode mode,
PathProcessor pathProcessor, string modeInput, bool useRelativePaths)
{
List<FileCoverageData> coverageData;
if (mode == CoverageMode.MonoCov)
var loadResult = LoadCoverageData(mode, modeInput);
if (loadResult.Successful)
{
var folderFiles = _fileSystem.GetFiles(modeInput);
if (!folderFiles.HasValue)
{
return LoadCoverageFilesError.InputFileNotFound;
}
var documents = LoadXDocuments(folderFiles);
List<FileCoverageData> coverageData = loadResult.Value;

var files = BuildCoverageFiles(pathProcessor, useRelativePaths, coverageData);

coverageData = MonoCoverParser.GenerateSourceFiles(documents);
return files;
}
else if (mode == CoverageMode.ReportGenerator)
else
{
var folderFiles = _fileSystem.GetFiles(modeInput);
if (!folderFiles.HasValue)
{
return LoadCoverageFilesError.InputFileNotFound;
}
var documents = LoadXDocuments(folderFiles);

coverageData = ReportGeneratorParser.GenerateSourceFiles(documents);
return loadResult.Error;
}
else if (mode == CoverageMode.Chutzpah)
}

private Result<List<FileCoverageData>, LoadCoverageFilesError> LoadCoverageData(CoverageMode mode, string modeInput)
{
Option<List<FileCoverageData>> result;
switch (mode)
{
if (!File.Exists(modeInput))
{
return LoadCoverageFilesError.InputFileNotFound;
}
var source = _fileSystem.TryLoadFile(modeInput);
if (!source.HasValue)
{
return LoadCoverageFilesError.InputFileNotFound;
}
coverageData = ChutzpahJsonParser.GenerateSourceFiles((string)source);
case CoverageMode.MonoCov:
result = LoadData(modeInput, MonoCoverParser.GenerateSourceFiles);
break;
case CoverageMode.ReportGenerator:
result = LoadData(modeInput, ReportGeneratorParser.GenerateSourceFiles);
break;
case CoverageMode.Chutzpah:
result = LoadData(modeInput, ChutzpahJsonParser.GenerateSourceFiles);
break;
case CoverageMode.LCov:
result = LoadData(modeInput, LcovParser.GenerateSourceFiles);
break;
case CoverageMode.OpenCover:
result = LoadData(modeInput, OpenCoverParser.GenerateSourceFiles);
break;
case CoverageMode.DynamicCodeCoverage:
result = LoadData(modeInput, DynamicCodeCoverageParser.GenerateSourceFiles);
break;
case CoverageMode.ExportCodeCoverage:
result = LoadData(modeInput, ExportCodeCoverageParser.GenerateSourceFiles);
break;
default:
return LoadCoverageFilesError.ModeNotSupported;
}
else if (mode == CoverageMode.LCov)

if (!result.HasValue)
{
var lines = _fileSystem.ReadAllLines(modeInput);
return LoadCoverageFilesError.InputFileNotFound;
}

if (lines.HasValue)
{
return LoadCoverageFilesError.InputFileNotFound;
}
var coverageData = (List<FileCoverageData>)result;

coverageData = LcovParser.GenerateSourceFiles((string[])lines);
if (coverageData == null)
{
return LoadCoverageFilesError.UnknownFilesMissingError;
}
else

return coverageData;
}

private Option<List<FileCoverageData>> LoadData(string modeInput, Func<Dictionary<string, XDocument>, List<FileCoverageData>> generateFunc)
{
var folderFiles = _fileSystem.GetFiles(modeInput);
if (!folderFiles.HasValue)
{
//common xml-based single file formats
var xmlParser = LoadXmlParser(mode);
if (xmlParser == null)
{
return LoadCoverageFilesError.ModeNotSupported;
}
return Option<List<FileCoverageData>>.None;
}
var documents = LoadXDocuments(folderFiles);

var document = _fileSystem.TryLoadXDocumentFromFile(modeInput);
return generateFunc(documents);
}

if (!document.HasValue)
{
return LoadCoverageFilesError.InputFileNotFound;
}
private Option<List<FileCoverageData>> LoadData(string modeInput, Func<string[], List<FileCoverageData>> generateFunc)
{
var lines = _fileSystem.TryReadAllLinesFromFile(modeInput);

coverageData = xmlParser.GenerateSourceFiles((XDocument)document);
}
if (coverageData == null)
if (!lines.HasValue)
{
return LoadCoverageFilesError.UnknownFilesMissingError;
return Option<List<FileCoverageData>>.None;
}
var files = BuildCoverageFiles(pathProcessor, useRelativePaths, coverageData);

return files;
return generateFunc((string[])lines);
}

private Option<List<FileCoverageData>> LoadData(string modeInput, Func<XDocument, List<FileCoverageData>> generateFunc)
{
var document = _fileSystem.TryLoadXDocumentFromFile(modeInput);

if (!document.HasValue)
{
return Option<List<FileCoverageData>>.None;
}

return generateFunc((XDocument)document);
}
private Dictionary<string, XDocument> LoadXDocuments(Option<FileInfo[]> folderFiles)
{
return ((FileInfo[]) folderFiles).Where(f => f.Name.EndsWith(".xml"))
Expand Down Expand Up @@ -130,21 +152,5 @@ private List<CoverageFile> BuildCoverageFiles(PathProcessor pathProcessor, bool
}).ToList();
return files;
}


private IXmlCoverageParser LoadXmlParser(CoverageMode mode)
{
switch (mode)
{
case CoverageMode.OpenCover:
return new OpenCoverParser();
case CoverageMode.DynamicCodeCoverage:
return new DynamicCodeCoverageParser();
case CoverageMode.ExportCodeCoverage:
return new ExportCodeCoverageParser();
default:
return null;
}
}
}
}
5 changes: 3 additions & 2 deletions src/csmacnz.Coveralls/Parsers/ChutzpahJsonParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using csmacnz.Coveralls.Data;
using Newtonsoft.Json;
using System;

namespace csmacnz.Coveralls.Parsers
{
Expand All @@ -18,11 +19,11 @@ public class ChutzpahJsonFileItem

public static class ChutzpahJsonParser
{
public static List<FileCoverageData> GenerateSourceFiles(string content)
public static List<FileCoverageData> GenerateSourceFiles(string[] content)
{
var files = new List<FileCoverageData>();

var deserializedString = JsonConvert.DeserializeObject<dynamic>(content);
var deserializedString = JsonConvert.DeserializeObject<dynamic>(string.Join(Environment.NewLine, content));

foreach (var file in deserializedString)
{
Expand Down
4 changes: 2 additions & 2 deletions src/csmacnz.Coveralls/Parsers/DynamicCodeCoverageParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

namespace csmacnz.Coveralls.Parsers
{
public class DynamicCodeCoverageParser: IXmlCoverageParser
public static class DynamicCodeCoverageParser
{
public List<FileCoverageData> GenerateSourceFiles(XDocument document)
public static List<FileCoverageData> GenerateSourceFiles(XDocument document)
{
var files = new List<FileCoverageData>();
var xElement = document.Root?.Element("modules");
Expand Down
4 changes: 2 additions & 2 deletions src/csmacnz.Coveralls/Parsers/ExportCodeCoverageParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace csmacnz.Coveralls.Parsers
{
public class ExportCodeCoverageParser: IXmlCoverageParser
public static class ExportCodeCoverageParser
{
public List<FileCoverageData> GenerateSourceFiles(XDocument document)
public static List<FileCoverageData> GenerateSourceFiles(XDocument document)
{
var files = new List<FileCoverageData>();
if (document.Root != null)
Expand Down
4 changes: 2 additions & 2 deletions src/csmacnz.Coveralls/Parsers/OpenCoverParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace csmacnz.Coveralls.Parsers
{
public class OpenCoverParser: IXmlCoverageParser
public static class OpenCoverParser
{
public List<FileCoverageData> GenerateSourceFiles(XDocument document)
public static List<FileCoverageData> GenerateSourceFiles(XDocument document)
{
var files = new List<FileCoverageData>();
var xElement = document.Root?.Element("Modules");
Expand Down
2 changes: 1 addition & 1 deletion src/csmacnz.Coveralls/Ports/IFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public interface IFileSystem
Option<XDocument> TryLoadXDocumentFromFile(string filePath);
Option<FileInfo[]> GetFiles(string directory);
bool WriteFile(string outputFile, string fileData);
Option<string[]> ReadAllLines(string filePath);
Option<string[]> TryReadAllLinesFromFile(string filePath);
}
}

0 comments on commit 46f76cb

Please sign in to comment.