From 38e863716d4bdbe4d1573e9b86acf7ea2c9a9d0e Mon Sep 17 00:00:00 2001 From: Gabriel Palma Date: Thu, 28 Mar 2024 13:24:57 +1100 Subject: [PATCH] Added method to specify vague subdirectory matching --- README.md | 6 ++- .../SpecFlowApplication.cs | 4 +- .../ProgramArguments.cs | 14 +++++-- .../AssemblyScanner.cs | 20 ++++++++-- .../IAssemblyScanner.cs | 5 ++- ...arkdown.Infrastructure.AssemblyLoad.csproj | 1 + .../FileFinder.cs | 38 +++++++++++++++++++ .../IFileFinder.cs | 7 ++++ ...pecFlowToMarkdown.Infrastructure.Io.csproj | 4 ++ .../Arguments/ProgramArgumentsParser.cs | 30 ++++++++++----- .../Results/ITestExecutionParser.cs | 5 ++- .../Results/JsonTestExecutionParser.cs | 20 +++++++++- ...owToMarkdown.Infrastructure.Parsing.csproj | 1 + src/6.0/SpecFlowToMarkdown.Tool/Startup.cs | 1 + 14 files changed, 129 insertions(+), 27 deletions(-) create mode 100644 src/6.0/SpecFlowToMarkdown.Infrastructure.Io/FileFinder.cs create mode 100644 src/6.0/SpecFlowToMarkdown.Infrastructure.Io/IFileFinder.cs diff --git a/README.md b/README.md index 82eb3f6..cf3e55b 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,12 @@ You can install the `specflow-to-markdown` tool via the following .NET command dotnet tool install -g Gman.SpecFlowToMarkdown ``` ### Running the tool -The tool takes three arguments: +The tool takes five arguments: ``` -specflow-to-markdown +specflow-to-markdown ``` - `PATH_TO_TEST_ASSEMBLY` - this will be the location of the built .NET DLL containing the SpecFlow tests. +- `TEST_ASSEMBLY_FILE` - this will be the name of the assembly file. Supports wildcards. - `PATH_TO_TEST_RESULTS_FILE` - this will be the location of the (JSON) test execution results file. +- `TEST_RESULTS_FILE` - this will be the name of the results file. Supports wildcards. - `PATH_TO_OUTPUT_FILE` - this will be the path to the generated output file where the markdown should be generated; includes the full file name. The file _does not have to be a markdown_ (`.md`) file. \ No newline at end of file diff --git a/src/6.0/SpecFlowToMarkdown.Application/SpecFlowApplication.cs b/src/6.0/SpecFlowToMarkdown.Application/SpecFlowApplication.cs index 3fa233d..4f2fe6e 100644 --- a/src/6.0/SpecFlowToMarkdown.Application/SpecFlowApplication.cs +++ b/src/6.0/SpecFlowToMarkdown.Application/SpecFlowApplication.cs @@ -47,11 +47,11 @@ public void Perform(string[] args) var specFlowAssembly = _assemblyScanner - .Perform(arguments.TestAssemblyPath); + .Perform(arguments); var testResults = _testExecutionParser - .Parse(arguments.TestResultsPath); + .Parse(arguments); var markdown = _markdownRenderer diff --git a/src/6.0/SpecFlowToMarkdown.Domain/ProgramArguments.cs b/src/6.0/SpecFlowToMarkdown.Domain/ProgramArguments.cs index 31be330..56a8c46 100644 --- a/src/6.0/SpecFlowToMarkdown.Domain/ProgramArguments.cs +++ b/src/6.0/SpecFlowToMarkdown.Domain/ProgramArguments.cs @@ -2,17 +2,23 @@ { public class ProgramArguments { - public string TestAssemblyPath { get; set; } + public string TestAssemblyFolder { get; set; } - public string TestResultsPath { get; set; } + public string TestAssemblyFile { get; set; } + + public string TestResultsFolder { get; set; } + + public string TestResultsFile { get; set; } public string OutputFilePath { get; set; } public override string ToString() { return - $"TestAssemblyPath: '{TestAssemblyPath}'\r\n" + - $"TestResultsPath: '{TestResultsPath}'\r\n" + + $"TestAssemblyFolder: '{TestAssemblyFolder}'\r\n" + + $"TestAssemblyFile: '{TestAssemblyFile}'\r\n" + + $"TestResultsFolder: '{TestResultsFolder}'\r\n" + + $"TestResultsFile: '{TestResultsFile}'\r\n" + $"OutputFilePath: '{OutputFilePath}'"; } } diff --git a/src/6.0/SpecFlowToMarkdown.Infrastructure.AssemblyLoad/AssemblyScanner.cs b/src/6.0/SpecFlowToMarkdown.Infrastructure.AssemblyLoad/AssemblyScanner.cs index ea1c74b..6a8a04f 100644 --- a/src/6.0/SpecFlowToMarkdown.Infrastructure.AssemblyLoad/AssemblyScanner.cs +++ b/src/6.0/SpecFlowToMarkdown.Infrastructure.AssemblyLoad/AssemblyScanner.cs @@ -1,23 +1,37 @@ using Mono.Cecil; +using SpecFlowToMarkdown.Domain; using SpecFlowToMarkdown.Domain.TestAssembly; using SpecFlowToMarkdown.Infrastructure.AssemblyLoad.Extractors; +using SpecFlowToMarkdown.Infrastructure.Io; namespace SpecFlowToMarkdown.Infrastructure.AssemblyLoad { public class AssemblyScanner : IAssemblyScanner { private readonly IFeatureExtractor _featureExtractor; + private readonly IFileFinder _fileFinder; - public AssemblyScanner(IFeatureExtractor featureExtractor) + public AssemblyScanner( + IFeatureExtractor featureExtractor, + IFileFinder fileFinder + ) { _featureExtractor = featureExtractor; + _fileFinder = fileFinder; } - public SpecFlowAssembly Perform(string assemblyPath) + public SpecFlowAssembly Perform(ProgramArguments arguments) { + var foundFilePath = + _fileFinder + .GetFirstFound( + arguments.TestAssemblyFolder, + arguments.TestAssemblyFile + ); + var assembly = AssemblyDefinition - .ReadAssembly(assemblyPath); + .ReadAssembly(foundFilePath); var result = _featureExtractor diff --git a/src/6.0/SpecFlowToMarkdown.Infrastructure.AssemblyLoad/IAssemblyScanner.cs b/src/6.0/SpecFlowToMarkdown.Infrastructure.AssemblyLoad/IAssemblyScanner.cs index 33be59f..4038927 100644 --- a/src/6.0/SpecFlowToMarkdown.Infrastructure.AssemblyLoad/IAssemblyScanner.cs +++ b/src/6.0/SpecFlowToMarkdown.Infrastructure.AssemblyLoad/IAssemblyScanner.cs @@ -1,9 +1,10 @@ -using SpecFlowToMarkdown.Domain.TestAssembly; +using SpecFlowToMarkdown.Domain; +using SpecFlowToMarkdown.Domain.TestAssembly; namespace SpecFlowToMarkdown.Infrastructure.AssemblyLoad { public interface IAssemblyScanner { - public SpecFlowAssembly Perform(string assemblyPath); + public SpecFlowAssembly Perform(ProgramArguments arguments); } } \ No newline at end of file diff --git a/src/6.0/SpecFlowToMarkdown.Infrastructure.AssemblyLoad/SpecFlowToMarkdown.Infrastructure.AssemblyLoad.csproj b/src/6.0/SpecFlowToMarkdown.Infrastructure.AssemblyLoad/SpecFlowToMarkdown.Infrastructure.AssemblyLoad.csproj index 8beaba1..236a0b1 100644 --- a/src/6.0/SpecFlowToMarkdown.Infrastructure.AssemblyLoad/SpecFlowToMarkdown.Infrastructure.AssemblyLoad.csproj +++ b/src/6.0/SpecFlowToMarkdown.Infrastructure.AssemblyLoad/SpecFlowToMarkdown.Infrastructure.AssemblyLoad.csproj @@ -11,6 +11,7 @@ + diff --git a/src/6.0/SpecFlowToMarkdown.Infrastructure.Io/FileFinder.cs b/src/6.0/SpecFlowToMarkdown.Infrastructure.Io/FileFinder.cs new file mode 100644 index 0000000..e6271dd --- /dev/null +++ b/src/6.0/SpecFlowToMarkdown.Infrastructure.Io/FileFinder.cs @@ -0,0 +1,38 @@ +using System; +using System.IO; +using System.Linq; +using System.Text.RegularExpressions; +using Microsoft.Extensions.Logging; + +namespace SpecFlowToMarkdown.Infrastructure.Io +{ + public class FileFinder : IFileFinder + { + private readonly ILogger _logger; + + public FileFinder(ILogger logger) + { + _logger = logger; + } + + public string GetFirstFound(string pathName, string fileName) + { + var foundFilePath = + Directory + .EnumerateFiles( + pathName, + fileName, + SearchOption.AllDirectories + ) + .FirstOrDefault(); + + if (foundFilePath == null) + throw new Exception($"Could not find file matching path {pathName} and file {fileName}"); + + _logger + .LogInformation($"Found file path at {foundFilePath}"); + + return foundFilePath; + } + } +} \ No newline at end of file diff --git a/src/6.0/SpecFlowToMarkdown.Infrastructure.Io/IFileFinder.cs b/src/6.0/SpecFlowToMarkdown.Infrastructure.Io/IFileFinder.cs new file mode 100644 index 0000000..34e46f9 --- /dev/null +++ b/src/6.0/SpecFlowToMarkdown.Infrastructure.Io/IFileFinder.cs @@ -0,0 +1,7 @@ +namespace SpecFlowToMarkdown.Infrastructure.Io +{ + public interface IFileFinder + { + string GetFirstFound(string pathName, string fileName); + } +} \ No newline at end of file diff --git a/src/6.0/SpecFlowToMarkdown.Infrastructure.Io/SpecFlowToMarkdown.Infrastructure.Io.csproj b/src/6.0/SpecFlowToMarkdown.Infrastructure.Io/SpecFlowToMarkdown.Infrastructure.Io.csproj index 3d65bc1..e306e67 100644 --- a/src/6.0/SpecFlowToMarkdown.Infrastructure.Io/SpecFlowToMarkdown.Infrastructure.Io.csproj +++ b/src/6.0/SpecFlowToMarkdown.Infrastructure.Io/SpecFlowToMarkdown.Infrastructure.Io.csproj @@ -5,4 +5,8 @@ false + + + + diff --git a/src/6.0/SpecFlowToMarkdown.Infrastructure.Parsing/Arguments/ProgramArgumentsParser.cs b/src/6.0/SpecFlowToMarkdown.Infrastructure.Parsing/Arguments/ProgramArgumentsParser.cs index 3c5117c..520f6d4 100644 --- a/src/6.0/SpecFlowToMarkdown.Infrastructure.Parsing/Arguments/ProgramArgumentsParser.cs +++ b/src/6.0/SpecFlowToMarkdown.Infrastructure.Parsing/Arguments/ProgramArgumentsParser.cs @@ -15,27 +15,37 @@ public ProgramArgumentsParser(ILogger logger) public ProgramArguments Parse(string[] args) { - if (args.Length < 3) - throw new Exception("Expected 3 arguments"); + if (args.Length < 5) + throw new Exception("Expected 5 arguments"); - var assemblyPath = args[0]; - var executionResultsPath = args[1]; - var outputPath = args[2]; + var testAssemblyFolder = args[0]; + var testAssemblyFile = args[1]; + var testResultsFolder = args[2]; + var testResultsFile = args[3]; + var outputPath = args[4]; - if (string.IsNullOrEmpty(assemblyPath)) + if (string.IsNullOrEmpty(testAssemblyFolder)) throw new Exception("Assembly path argument invalid"); - if (string.IsNullOrEmpty(executionResultsPath)) + if (string.IsNullOrEmpty(testAssemblyFile)) + throw new Exception("Assembly file argument invalid"); + + if (string.IsNullOrEmpty(testResultsFolder)) throw new Exception("Results path argument invalid"); + if (string.IsNullOrEmpty(testResultsFile)) + throw new Exception("Results file argument invalid"); + if (string.IsNullOrEmpty(outputPath)) throw new Exception("Output path argument invalid"); var result = new ProgramArguments { - TestAssemblyPath = args[0], - TestResultsPath = args[1], - OutputFilePath = args[2] + TestAssemblyFolder = testAssemblyFolder, + TestAssemblyFile = testAssemblyFile, + TestResultsFolder = testResultsFolder, + TestResultsFile = testResultsFile, + OutputFilePath = outputPath }; _logger diff --git a/src/6.0/SpecFlowToMarkdown.Infrastructure.Parsing/Results/ITestExecutionParser.cs b/src/6.0/SpecFlowToMarkdown.Infrastructure.Parsing/Results/ITestExecutionParser.cs index b9101bc..d318c23 100644 --- a/src/6.0/SpecFlowToMarkdown.Infrastructure.Parsing/Results/ITestExecutionParser.cs +++ b/src/6.0/SpecFlowToMarkdown.Infrastructure.Parsing/Results/ITestExecutionParser.cs @@ -1,9 +1,10 @@ -using SpecFlowToMarkdown.Domain.Result; +using SpecFlowToMarkdown.Domain; +using SpecFlowToMarkdown.Domain.Result; namespace SpecFlowToMarkdown.Infrastructure.Parsing.Results { public interface ITestExecutionParser { - public TestExecution Parse(string executionResultsPath); + public TestExecution Parse(ProgramArguments arguments); } } \ No newline at end of file diff --git a/src/6.0/SpecFlowToMarkdown.Infrastructure.Parsing/Results/JsonTestExecutionParser.cs b/src/6.0/SpecFlowToMarkdown.Infrastructure.Parsing/Results/JsonTestExecutionParser.cs index abe3789..7ff108a 100644 --- a/src/6.0/SpecFlowToMarkdown.Infrastructure.Parsing/Results/JsonTestExecutionParser.cs +++ b/src/6.0/SpecFlowToMarkdown.Infrastructure.Parsing/Results/JsonTestExecutionParser.cs @@ -1,16 +1,32 @@ using System.IO; using Newtonsoft.Json; +using SpecFlowToMarkdown.Domain; using SpecFlowToMarkdown.Domain.Result; +using SpecFlowToMarkdown.Infrastructure.Io; namespace SpecFlowToMarkdown.Infrastructure.Parsing.Results { public class JsonTestExecutionParser : ITestExecutionParser { - public TestExecution Parse(string executionResultsPath) + private readonly IFileFinder _fileFinder; + + public JsonTestExecutionParser(IFileFinder fileFinder) + { + _fileFinder = fileFinder; + } + + public TestExecution Parse(ProgramArguments arguments) { + var foundFilePath = + _fileFinder + .GetFirstFound( + arguments.TestResultsFolder, + arguments.TestResultsFile + ); + var jsonString = File - .ReadAllText(executionResultsPath); + .ReadAllText(foundFilePath); var result = JsonConvert diff --git a/src/6.0/SpecFlowToMarkdown.Infrastructure.Parsing/SpecFlowToMarkdown.Infrastructure.Parsing.csproj b/src/6.0/SpecFlowToMarkdown.Infrastructure.Parsing/SpecFlowToMarkdown.Infrastructure.Parsing.csproj index 6c28190..792974a 100644 --- a/src/6.0/SpecFlowToMarkdown.Infrastructure.Parsing/SpecFlowToMarkdown.Infrastructure.Parsing.csproj +++ b/src/6.0/SpecFlowToMarkdown.Infrastructure.Parsing/SpecFlowToMarkdown.Infrastructure.Parsing.csproj @@ -7,6 +7,7 @@ + diff --git a/src/6.0/SpecFlowToMarkdown.Tool/Startup.cs b/src/6.0/SpecFlowToMarkdown.Tool/Startup.cs index b44132b..7eb1935 100644 --- a/src/6.0/SpecFlowToMarkdown.Tool/Startup.cs +++ b/src/6.0/SpecFlowToMarkdown.Tool/Startup.cs @@ -22,6 +22,7 @@ public static IServiceCollection AddServices() .AddSingleton() .AddSingleton() .AddSingleton() + .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton()