diff --git a/ExtentReportsDotNetCLI/ExtentReportsDotNetCLI/Program.cs b/ExtentReportsDotNetCLI/ExtentReportsDotNetCLI/Program.cs index e6771a9..16b548c 100644 --- a/ExtentReportsDotNetCLI/ExtentReportsDotNetCLI/Program.cs +++ b/ExtentReportsDotNetCLI/ExtentReportsDotNetCLI/Program.cs @@ -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; @@ -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; } @@ -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(args); @@ -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 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 + "\\";