Skip to content

Commit

Permalink
#5 corrected argument parsing depending on built DLL configuration (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
gubpalma authored Apr 8, 2024
1 parent 27c0ab5 commit b0ffeb8
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.Extensions.Logging;
using Mono.Cecil;
Expand All @@ -13,6 +15,7 @@ public class FeatureExtractor : IFeatureExtractor
private const string CustomFeatureAttributeValue = "TechTalk.SpecFlow";
private const string FeatureSetupMethodName = "FeatureSetup";
private const string FeatureInfoTypeName = "TechTalk.SpecFlow.FeatureInfo";
private const string DebuggingModeAttributeName = "System.Diagnostics.DebuggableAttribute/DebuggingModes";

private readonly IScenarioExtractionHandler _scenarioExtractionHandler;
private readonly ILogger<FeatureExtractor> _logger;
Expand All @@ -24,13 +27,18 @@ public FeatureExtractor(
_scenarioExtractionHandler = scenarioExtractionHandler;
_logger = logger;
}

public SpecFlowAssembly ExtractFeatures(AssemblyDefinition assembly)
{
var assemblyName = assembly.Name.Name;

var isDebug = IsDebugBuild(assembly);

_logger
.LogInformation($"Assembly name: [{assemblyName}]");

_logger
.LogInformation($"Debug build: [{isDebug}]");

var result = new SpecFlowAssembly
{
Expand Down Expand Up @@ -123,7 +131,7 @@ public SpecFlowAssembly ExtractFeatures(AssemblyDefinition assembly)

var scenarios =
_scenarioExtractionHandler
.ExtractScenarios(type);
.ExtractScenarios(type, isDebug);

feature.Scenarios = scenarios;

Expand All @@ -142,5 +150,38 @@ public SpecFlowAssembly ExtractFeatures(AssemblyDefinition assembly)

return result;
}

private static bool IsDebugBuild(AssemblyDefinition assembly)
{
var customAttributes =
assembly
.CustomAttributes;

var debuggableAttribute =
customAttributes
.FirstOrDefault(o => o.AttributeType.FullName == typeof(DebuggableAttribute).FullName);

if (debuggableAttribute != null)
{
var debuggingMode =
debuggableAttribute
.ConstructorArguments
.FirstOrDefault(o => o.Type.FullName == DebuggingModeAttributeName);

if (debuggingMode.Value != null)
{
var attributes =
(DebuggableAttribute.DebuggingModes)Enum.Parse(
typeof(DebuggableAttribute.DebuggingModes),
debuggingMode.Value?.ToString()
);
return
attributes
.HasFlag(DebuggableAttribute.DebuggingModes.Default);
}
}

return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace SpecFlowToMarkdown.Infrastructure.AssemblyLoad.Extractors
{
public interface IScenarioExtractionHandler
{
public IEnumerable<SpecFlowScenario> ExtractScenarios(TypeDefinition type);
public IEnumerable<SpecFlowScenario> ExtractScenarios(TypeDefinition type, bool isDebug);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace SpecFlowToMarkdown.Infrastructure.AssemblyLoad.Extractors
public interface IScenarioExtractor
{
public bool IsApplicable(string attributeName);
public SpecFlowScenario ExtractScenario(MethodDefinition method);

public SpecFlowScenario ExtractScenario(MethodDefinition method, bool isDebug);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public UnitScenarioExtractor(ILogger<FeatureExtractor> logger)
_logger = logger;
}

public SpecFlowScenario ExtractScenario(MethodDefinition method)
public SpecFlowScenario ExtractScenario(MethodDefinition method, bool isDebug)
{
// Extract Scenario
var title = string.Empty;
Expand Down Expand Up @@ -90,7 +90,7 @@ public SpecFlowScenario ExtractScenario(MethodDefinition method)
// Get test case argument names
currInstr =
currInstr
.StepPrevious(3);
.StepPrevious(isDebug ? 3 : 2);

while (true)
{
Expand Down Expand Up @@ -135,7 +135,7 @@ public SpecFlowScenario ExtractScenario(MethodDefinition method)

currInstr =
currInstr
.StepPrevious(5);
.StepPrevious(isDebug ? 5 : 4);
}
else
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public ScenarioExtractionHandler(IEnumerable<IScenarioExtractor> extractors)
_extractors = extractors;
}

public IEnumerable<SpecFlowScenario> ExtractScenarios(TypeDefinition type)
public IEnumerable<SpecFlowScenario> ExtractScenarios(TypeDefinition type, bool isDebug)
{
var results = new List<SpecFlowScenario>();

Expand Down Expand Up @@ -59,7 +59,7 @@ public IEnumerable<SpecFlowScenario> ExtractScenarios(TypeDefinition type)

var scenario =
applicableExtractor
.ExtractScenario(method);
.ExtractScenario(method, isDebug);

results
.Add(scenario);
Expand Down

0 comments on commit b0ffeb8

Please sign in to comment.