Skip to content

Commit

Permalink
Merge pull request #7 from extent-framework/allow-merge
Browse files Browse the repository at this point in the history
Allow merge via --merge flag
  • Loading branch information
anshooarora authored Mar 3, 2019
2 parents 7651eec + fffaa13 commit 5476c2a
Showing 1 changed file with 46 additions and 24 deletions.
70 changes: 46 additions & 24 deletions ExtentReportsDotNetCLI/ExtentReportsDotNetCLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using AventStack.ExtentReports.CLI.Model;
using AventStack.ExtentReports.CLI.Parser;
using AventStack.ExtentReports.Reporter;

using McMaster.Extensions.CommandLineUtils;

using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -11,12 +13,13 @@ namespace AventStack.ExtentReports.CLI
{
internal class Program
{

private const string DefaultBaseDirectory = "Reports";
private Logger _logger;
private ExtentReports _extent = new ExtentReports();
private int _filesProcessed = 0;

[Option(ShortName = "p")]
private TestFramework Parser { get; set; } = Model.TestFramework.NUnit;
private TestFramework Parser { get; set; } = TestFramework.NUnit;

[Option(ShortName = "i")]
private string TestRunnerResultsFile { get; set; }
Expand All @@ -33,6 +36,10 @@ internal class Program
[Option(ShortName = "l")]
private LoggingLevel LoggingLevel { get; set; } = LoggingLevel.Normal;

[Option("--merge")]
private bool Merge { get; set; } = false;


private static void Main(string[] args)
{
CommandLineApplication.Execute<Program>(args);
Expand All @@ -41,54 +48,69 @@ private static void Main(string[] args)
private void OnExecute()
{
_logger = new Logger(LoggingLevel);

_logger.WriteLine(LoggingLevel.Verbose, "extentreports-cli initializing ...");

string output = string.IsNullOrWhiteSpace(Output) ? $".\\{DefaultBaseDirectory}" : Output;

bool foundResultFiles = false;

if (!string.IsNullOrEmpty(TestRunnerResultsDirectory) && File.GetAttributes(TestRunnerResultsDirectory) == FileAttributes.Directory && Parser.Equals(TestFramework.NUnit))
{

string filePattern = "*." + KnownFileExtensions.GetExtension(Parser);
_logger.WriteLine(LoggingLevel.Normal, $"Getting test runner result files in folder '{TestRunnerResultsDirectory}' matching pattern '{filePattern}' ...");

List<string> files = Directory.GetFiles(TestRunnerResultsDirectory, filePattern, SearchOption.AllDirectories).ToList();

foreach (string file in files)
if (Merge)
{
files.ForEach(x => ProcessSingle(x, output, true));
}
else
{
_logger.WriteLine(LoggingLevel.Normal, $"Parsing test runner result file '{file}' ...");
var extent = new ExtentReports();
var dir = Path.Combine(output, Path.GetFileNameWithoutExtension(file));
InitializeReporter(extent, dir);
new NUnitParser(extent).ParseTestRunnerOutput(file);
extent.Flush();
_logger.WriteLine(LoggingLevel.Normal, $"Report for '{file}' is complete.");
foundResultFiles = true;
files.ForEach(x =>
{
var dir = Path.Combine(output, Path.GetFileNameWithoutExtension(x));
ProcessSingle(x, dir, false);
});
}
}

if (!string.IsNullOrWhiteSpace(TestRunnerResultsFile))
{
_logger.WriteLine(LoggingLevel.Normal, $"Parsing test runner result file '{TestRunnerResultsFile}' ...");
var extent = new ExtentReports();
InitializeReporter(extent, output);
new NUnitParser(extent).ParseTestRunnerOutput(TestRunnerResultsFile);
extent.Flush();
_logger.WriteLine(LoggingLevel.Normal, $"Report for '{TestRunnerResultsFile}' is complete.");
foundResultFiles = true;
ProcessSingle(TestRunnerResultsFile, output, true);
}

if (!foundResultFiles)
if (_filesProcessed == 0)
{
_logger.WriteLine(LoggingLevel.Normal, "Nothing to do!");
}

_logger.WriteLine(LoggingLevel.Verbose, "extentreports-cli finished.");
}

private void InitializeReporter(ExtentReports extent, string path)
private void ProcessSingle(string testResultsFilePath, string output, bool merge = false)
{
_logger.WriteLine(LoggingLevel.Normal, $"Parsing test runner result file '{TestRunnerResultsFile}' ...");

// if a single report is required for each test results file (ie, no merges):
// must instantiate every time to clear pre-existing data
if (!merge)
{
_extent = new ExtentReports();
}

if ((merge && !_extent.StartedReporterList.Any()) || !merge)
{
InitializeReporter(_extent, output);
}

new NUnitParser(_extent).ParseTestRunnerOutput(testResultsFilePath);
_extent.Flush();
_filesProcessed++;

_logger.WriteLine(LoggingLevel.Normal, $"Report for '{testResultsFilePath}' is complete.");
}

private void InitializeReporter(ExtentReports extent, string path)
{
if (Reporters.Contains("html"))
{
var output = path.EndsWith("\\") || path.EndsWith("/") ? path : path + "\\";
Expand Down

0 comments on commit 5476c2a

Please sign in to comment.