Skip to content

Commit

Permalink
Merge branch 'historystorage'
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpalme committed Feb 27, 2017
2 parents 01755e3 + 768aa24 commit 48a813a
Show file tree
Hide file tree
Showing 14 changed files with 325 additions and 118 deletions.
1 change: 1 addition & 0 deletions Readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ CHANGELOG

2.5.3.0

* New: Issue #78: Added support for custom storage of historic files
* Fix: Issue #79: Fixed IndexOutOfRangeException in CoberturaParser
* Fix: Issue #83: Added support for duplicate test methods per file

Expand Down
6 changes: 5 additions & 1 deletion ReportGenerator.Reporting/CsvSummaryReportBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Text;
using Palmmedia.ReportGenerator.Parser.Analysis;
using Palmmedia.ReportGenerator.Properties;
using Palmmedia.ReportGenerator.Reporting;

namespace Palmmedia.ReportGenerator.Reporting
{
Expand Down Expand Up @@ -47,6 +46,11 @@ public void CreateClassReport(Class @class, IEnumerable<FileAnalysis> fileAnalys
/// <param name="summaryResult">The summary result.</param>
public void CreateSummaryReport(SummaryResult summaryResult)
{
if (summaryResult == null)
{
throw new ArgumentNullException(nameof(summaryResult));
}

string targetPath = Path.Combine(this.TargetDirectory, "Summary.csv");

using (var reportTextWriter = new StreamWriter(new FileStream(targetPath, FileMode.Create), Encoding.UTF8))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Palmmedia")]
[assembly: AssemblyProduct("Test")]
[assembly: AssemblyCopyright("Copyright © Palmmedia 2015")]
[assembly: AssemblyCopyright("Copyright © Palmmedia 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand Down
19 changes: 9 additions & 10 deletions ReportGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Palmmedia.ReportGenerator.Parser;
using Palmmedia.ReportGenerator.Properties;
using Palmmedia.ReportGenerator.Reporting;
using Palmmedia.ReportGenerator.Reporting.History;

namespace Palmmedia.ReportGenerator
{
Expand Down Expand Up @@ -41,12 +42,12 @@ public bool GenerateReport(ReportConfiguration reportConfiguration)

var parser = ParserFactory.CreateParser(reportConfiguration.ReportFiles, reportConfiguration.SourceDirectories);

if (reportConfiguration.HistoryDirectory != null)
var historyStorage = new MefHistoryStorageFactory().GetHistoryStorage(reportConfiguration);

if (historyStorage != null)
{
new Reporting.HistoryParser(
parser.Assemblies,
reportConfiguration.HistoryDirectory)
.ApplyHistoricCoverage();
new HistoryParser(historyStorage)
.ApplyHistoricCoverage(parser.Assemblies);
}

new Reporting.ReportGenerator(
Expand All @@ -56,12 +57,10 @@ public bool GenerateReport(ReportConfiguration reportConfiguration)
reportConfiguration.ReportBuilderFactory.GetReportBuilders(reportConfiguration.TargetDirectory, reportConfiguration.ReportTypes))
.CreateReport(reportConfiguration.HistoryDirectory != null, executionTime);

if (reportConfiguration.HistoryDirectory != null)
if (historyStorage != null)
{
new Reporting.HistoryReportGenerator(
parser,
reportConfiguration.HistoryDirectory)
.CreateReport(executionTime);
new HistoryReportGenerator(historyStorage)
.CreateReport(parser.Assemblies, executionTime);
}

stopWatch.Stop();
Expand Down
29 changes: 28 additions & 1 deletion ReportGenerator/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions ReportGenerator/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@
<data name="ErrorDuringReadingHistoricReport" xml:space="preserve">
<value>Error during reading historic report '{0}': {1}</value>
</data>
<data name="ErrorDuringReadingHistoricReports" xml:space="preserve">
<value>Error during reading historic reports: {0}</value>
</data>
<data name="ErrorDuringReadingReport" xml:space="preserve">
<value>Error during reading report '{0}': {1}</value>
</data>
Expand All @@ -162,6 +165,9 @@
<data name="ErrorDuringRenderingSummaryReport" xml:space="preserve">
<value>Error during rendering summary report (Report type: '{0}'): {1}</value>
</data>
<data name="ErrorDuringSavingHistoricReport" xml:space="preserve">
<value>Error during saving historic report '{0}': {1}</value>
</data>
<data name="FailedReportFilePattern" xml:space="preserve">
<value>The report file pattern '{0}' is invalid.</value>
</data>
Expand Down Expand Up @@ -214,6 +220,9 @@
<data name="ReportGenerationTook" xml:space="preserve">
<value>Report generation took {0:f1} seconds</value>
</data>
<data name="SeveralCustomHistoryStorages" xml:space="preserve">
<value>Several custom history storages exist. Please ensure that only one custom storage exists.</value>
</data>
<data name="SeveralCustomReportBuildersWithSameReportType" xml:space="preserve">
<value>Several custom report builders for report type '{0}' exist. This may cause problems if they try to write to the same files/directory.</value>
</data>
Expand Down
8 changes: 6 additions & 2 deletions ReportGenerator/ReportGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@
<Compile Include="Generator.cs" />
<Compile Include="Reporting\DefaultFilter.cs" />
<Compile Include="Reporting\FileUnblocker.cs" />
<Compile Include="Reporting\HistoryParser.cs" />
<Compile Include="Reporting\History\FileHistoryStorage.cs" />
<Compile Include="Reporting\History\HistoryParser.cs" />
<Compile Include="Reporting\History\IHistoryStorage.cs" />
<Compile Include="Reporting\History\MefHistoryStorageFactory.cs" />
<Compile Include="Reporting\IFilter.cs" />
<Compile Include="Reporting\IReportBuilder.cs" />
<Compile Include="Properties\Resources.Designer.cs">
Expand All @@ -186,8 +189,9 @@
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Reporting\IReportBuilderFactory.cs" />
<Compile Include="Reporting\MefHelper.cs" />
<Compile Include="Reporting\MefReportBuilderFactory.cs" />
<Compile Include="Reporting\HistoryReportGenerator.cs" />
<Compile Include="Reporting\History\HistoryReportGenerator.cs" />
<Compile Include="Reporting\ReportGenerator.cs" />
<Compile Include="Logging\VerbosityLevel.cs" />
</ItemGroup>
Expand Down
55 changes: 55 additions & 0 deletions ReportGenerator/Reporting/History/FileHistoryStorage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Collections.Generic;
using System.IO;

namespace Palmmedia.ReportGenerator.Reporting.History
{
/// <summary>
/// Default implementation of <see cref="IHistoryStorage"/> based on file system.
/// </summary>
internal class FileHistoryStorage : IHistoryStorage
{
/// <summary>
/// The history directory.
/// </summary>
private readonly string historyDirectory;

/// <summary>
/// Initializes a new instance of the <see cref="FileHistoryStorage"/> class.
/// </summary>
/// <param name="historyDirectory">The history directory.</param>
public FileHistoryStorage(string historyDirectory)
{
this.historyDirectory = historyDirectory;
}

/// <summary>
/// Gets the history file paths.
/// </summary>
/// <returns>
/// The history file paths.
/// </returns>
public IEnumerable<string> GetHistoryFilePaths() => Directory.EnumerateFiles(this.historyDirectory, "*_CoverageHistory.xml");

/// <summary>
/// Loads the given file.
/// </summary>
/// <param name="filePath">The file path.</param>
/// <returns>
/// The file as stream.
/// </returns>
public Stream LoadFile(string filePath) => File.OpenRead(filePath);

/// <summary>
/// Saves the file with the given name.
/// </summary>
/// <param name="stream">The stream containing the file content.</param>
/// <param name="fileName">Name of the file.</param>
public void SaveFile(Stream stream, string fileName)
{
using (var output = File.OpenWrite(Path.Combine(this.historyDirectory, fileName)))
{
stream.CopyTo(output);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Palmmedia.ReportGenerator.Logging;
using Palmmedia.ReportGenerator.Parser.Analysis;
using Palmmedia.ReportGenerator.Properties;

namespace Palmmedia.ReportGenerator.Reporting
namespace Palmmedia.ReportGenerator.Reporting.History
{
/// <summary>
/// Reads all historic coverage files created by <see cref="HistoryReportGenerator"/> and adds the information to all classes.
Expand All @@ -21,60 +20,69 @@ internal class HistoryParser
private static readonly ILogger Logger = LoggerFactory.GetLogger(typeof(HistoryParser));

/// <summary>
/// The assemblies.
/// The history storage.
/// </summary>
private readonly IEnumerable<Assembly> assemblies;
private readonly IHistoryStorage historyStorage;

/// <summary>
/// The history directory.
/// Initializes a new instance of the <see cref="HistoryParser" /> class.
/// </summary>
private readonly string historyDirectory;
/// <param name="historyStorage">The history storage.</param>
internal HistoryParser(IHistoryStorage historyStorage)
{
if (historyStorage == null)
{
throw new ArgumentNullException(nameof(historyStorage));
}

this.historyStorage = historyStorage;
}

/// <summary>
/// Initializes a new instance of the <see cref="HistoryParser"/> class.
/// Reads all historic coverage files created by <see cref="HistoryReportGenerator" /> and adds the information to all classes.
/// </summary>
/// <param name="assemblies">The assemblies.</param>
/// <param name="historyDirectory">The history directory.</param>
internal HistoryParser(IEnumerable<Assembly> assemblies, string historyDirectory)
internal void ApplyHistoricCoverage(IEnumerable<Assembly> assemblies)
{
if (assemblies == null)
{
throw new ArgumentNullException(nameof(assemblies));
}

if (historyDirectory == null)
{
throw new ArgumentNullException(nameof(historyDirectory));
}

this.assemblies = assemblies;
this.historyDirectory = historyDirectory;
}

/// <summary>
/// Reads all historic coverage files created by <see cref="HistoryReportGenerator"/> and adds the information to all classes.
/// </summary>
internal void ApplyHistoricCoverage()
{
Logger.Info(Resources.ReadingHistoricReports);

var files = Directory
.EnumerateFiles(this.historyDirectory, "*_CoverageHistory.xml")
.OrderByDescending(f => f)
.Take(Settings.Default.MaximumOfHistoricCoverageFiles)
.Reverse();
IEnumerable<string> files = null;

try
{
files = this.historyStorage.GetHistoryFilePaths()
.OrderByDescending(f => f)
.Take(Settings.Default.MaximumOfHistoricCoverageFiles)
.Reverse()
.ToArray();
}
catch (Exception ex)
{
Logger.ErrorFormat(" " + Resources.ErrorDuringReadingHistoricReports, ex.Message);
return;
}

foreach (var file in files)
{
try
{
XDocument document = XDocument.Load(file);
XDocument document = null;

using (var stream = this.historyStorage.LoadFile(file))
{
document = XDocument.Load(stream);
}

DateTime date = DateTime.ParseExact(document.Root.Attribute("date").Value, "yyyy-MM-dd_HH-mm-ss", CultureInfo.InvariantCulture);

foreach (var assemblyElement in document.Root.Elements("assembly"))
{
Assembly assembly = this.assemblies
Assembly assembly = assemblies
.SingleOrDefault(a => a.Name == assemblyElement.Attribute("name").Value);

if (assembly == null)
Expand Down
Loading

0 comments on commit 48a813a

Please sign in to comment.