Skip to content

Commit

Permalink
refactor Log class generation for system tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gasparnagy committed Oct 14, 2024
1 parent de0bc79 commit dea281c
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 395 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,6 @@ private void AddHookBinding(ProjectBuilder project, string eventType, string nam
project.AddHookBinding(eventType, name, code, order, hookTypeAttributeTags, methodScopeAttributeTags, classScopeAttributeTags);
}

public void AddAsyncHookBindingIncludingLocking(string eventType, string name, string code = "", int? order = null, IList<string> hookTypeAttributeTags = null, IList<string> methodScopeAttributeTags = null, IList<string> classScopeAttributeTags = null)
{
AddAsyncHookBindingIncludingLocking(_solutionDriver.DefaultProject, eventType, name, code, order, hookTypeAttributeTags, methodScopeAttributeTags, classScopeAttributeTags);
}

private void AddAsyncHookBindingIncludingLocking(ProjectBuilder project, string eventType, string name, string code = "", int? order = null, IList<string> hookTypeAttributeTags = null, IList<string> methodScopeAttributeTags = null, IList<string> classScopeAttributeTags = null)
{
project.AddAsyncHookBindingIncludingLocking(eventType, name, code, order, hookTypeAttributeTags, methodScopeAttributeTags, classScopeAttributeTags);
}

public void AddFeatureFile(string featureFileContent)
{
LastFeatureFile = _solutionDriver.DefaultProject.AddFeatureFile(featureFileContent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ public ProjectFile GenerateHookBinding(string eventType, string name, string cod
return GenerateBindingClassFile(hookClass);
}

public ProjectFile GenerateAsyncHookBindingIncludingLocking(string eventType, string name, string code = null, int? order = null, IList<string> hookTypeAttributeTags = null, IList<string> methodScopeAttributeTags = null, IList<string> classScopeAttributeTags = null)
{
string hookClass = GetAsyncHookIncludingLockingBindingClass(eventType, name, code, order, hookTypeAttributeTags, methodScopeAttributeTags, classScopeAttributeTags);
return GenerateBindingClassFile(hookClass);
}

public abstract ProjectFile GenerateLoggerClass(string pathToLogFile);

protected abstract string GetBindingCode(string methodName, string methodImplementation, string attributeName, string regex, ParameterType parameterType, string argumentName);
Expand All @@ -49,15 +43,6 @@ protected abstract string GetHookBindingClass(
IList<string> methodScopeAttributeTags = null,
IList<string> classScopeAttributeTags = null);

protected abstract string GetAsyncHookIncludingLockingBindingClass(
string hookType,
string name,
string code = "",
int? order = null,
IList<string> hookTypeAttributeTags = null,
IList<string> methodScopeAttributeTags = null,
IList<string> classScopeAttributeTags = null);

protected bool IsStaticEvent(string eventType)
{
return eventType == "BeforeFeature" || eventType == "AfterFeature" || eventType == "BeforeTestRun" || eventType == "AfterTestRun";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,66 +1,13 @@
using Reqnroll.TestProjectGenerator.Data;
using System;

namespace Reqnroll.TestProjectGenerator.Factories.BindingsGenerator;

public class CSharp10BindingsGenerator : CSharpBindingsGenerator
{
public override ProjectFile GenerateLoggerClass(string pathToLogFile)
protected override string GetLogFileContent(string pathToLogFile)
{
string fileContent = $$"""
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;

internal static class Log
{
private const string LogFileLocation = @"{{pathToLogFile}}";

private static void Retry(int number, Action action)
{
try
{
action();
}
catch (Exception)
{
var i = number - 1;
if (i == 0)
throw;
Thread.Sleep(500);
Retry(i, action);
}
}
internal static void LogStep([CallerMemberName] string stepName = null!)
{
Retry(5, () => WriteToFile($@"-> step: {stepName}{Environment.NewLine}"));
}

internal static void LogHook([CallerMemberName] string stepName = null!)
{
Retry(5, () => WriteToFile($@"-> hook: {stepName}{Environment.NewLine}"));
}

internal static void LogCustom(string category, string value, [CallerMemberName] string memberName = null)
{
Retry(5, () => WriteToFile($@"-> {category}: {value}:{memberName}{Environment.NewLine}"));
}

static void WriteToFile(string line)
{
using (FileStream fs = File.Open(LogFileLocation, FileMode.Append, FileAccess.Write, FileShare.None))
{
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(line);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
}
}
}
""";
return new ProjectFile("Log.cs", "Compile", fileContent);
string logFileContent = base.GetLogFileContent(pathToLogFile);
logFileContent = "#nullable disable" + Environment.NewLine + logFileContent;
return logFileContent;
}

}
Loading

0 comments on commit dea281c

Please sign in to comment.