2023-03-26 06:52:53 +00:00
|
|
|
|
using CodexDistTestCore.Config;
|
|
|
|
|
using NUnit.Framework;
|
2023-03-20 10:37:02 +00:00
|
|
|
|
|
2023-03-21 12:20:21 +00:00
|
|
|
|
namespace CodexDistTestCore
|
2023-03-20 10:37:02 +00:00
|
|
|
|
{
|
|
|
|
|
public class TestLog
|
|
|
|
|
{
|
2023-03-26 08:08:10 +00:00
|
|
|
|
private readonly NumberSource subfileNumberSource = new NumberSource(0);
|
2023-03-22 08:22:18 +00:00
|
|
|
|
private readonly LogFile file;
|
2023-03-26 08:08:10 +00:00
|
|
|
|
private readonly DateTime now;
|
2023-03-20 10:37:02 +00:00
|
|
|
|
|
2023-03-22 08:22:18 +00:00
|
|
|
|
public TestLog()
|
2023-03-20 10:37:02 +00:00
|
|
|
|
{
|
2023-03-26 08:08:10 +00:00
|
|
|
|
now = DateTime.UtcNow;
|
|
|
|
|
|
2023-03-20 10:37:02 +00:00
|
|
|
|
var name = GetTestName();
|
2023-03-26 08:08:10 +00:00
|
|
|
|
file = new LogFile(now, name);
|
2023-03-20 10:37:02 +00:00
|
|
|
|
|
|
|
|
|
Log($"Begin: {name}");
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 08:22:18 +00:00
|
|
|
|
public void Log(string message)
|
2023-03-20 10:37:02 +00:00
|
|
|
|
{
|
2023-03-22 08:22:18 +00:00
|
|
|
|
file.Write(message);
|
|
|
|
|
}
|
2023-03-20 10:37:02 +00:00
|
|
|
|
|
2023-03-22 08:22:18 +00:00
|
|
|
|
public void Error(string message)
|
|
|
|
|
{
|
|
|
|
|
Log($"[ERROR] {message}");
|
|
|
|
|
}
|
2023-03-21 12:20:21 +00:00
|
|
|
|
|
2023-03-26 08:45:01 +00:00
|
|
|
|
public void EndTest()
|
2023-03-22 08:22:18 +00:00
|
|
|
|
{
|
2023-03-20 10:37:02 +00:00
|
|
|
|
var result = TestContext.CurrentContext.Result;
|
|
|
|
|
|
|
|
|
|
Log($"Finished: {GetTestName()} = {result.Outcome.Status}");
|
2023-03-22 08:50:24 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(result.Message))
|
|
|
|
|
{
|
|
|
|
|
Log(result.Message);
|
|
|
|
|
Log($"{result.StackTrace}");
|
2023-03-20 10:37:02 +00:00
|
|
|
|
}
|
2023-03-29 08:07:16 +00:00
|
|
|
|
|
|
|
|
|
if (result.Outcome.Status == NUnit.Framework.Interfaces.TestStatus.Failed)
|
|
|
|
|
{
|
|
|
|
|
RenameLogFile();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RenameLogFile()
|
|
|
|
|
{
|
|
|
|
|
file.ConcatToFilename("_FAILED");
|
2023-03-20 10:37:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-30 08:43:17 +00:00
|
|
|
|
public LogFile CreateSubfile(string ext = "log")
|
2023-03-26 08:08:10 +00:00
|
|
|
|
{
|
2023-03-30 08:43:17 +00:00
|
|
|
|
return new LogFile(now, $"{GetTestName()}_{subfileNumberSource.GetNextNumber().ToString().PadLeft(6, '0')}", ext);
|
2023-03-26 08:08:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-20 10:37:02 +00:00
|
|
|
|
private static string GetTestName()
|
|
|
|
|
{
|
|
|
|
|
var test = TestContext.CurrentContext.Test;
|
|
|
|
|
var className = test.ClassName!.Substring(test.ClassName.LastIndexOf('.') + 1);
|
2023-03-22 09:38:10 +00:00
|
|
|
|
var args = FormatArguments(test);
|
|
|
|
|
return $"{className}.{test.MethodName}{args}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string FormatArguments(TestContext.TestAdapter test)
|
|
|
|
|
{
|
|
|
|
|
if (test.Arguments == null || !test.Arguments.Any()) return "";
|
|
|
|
|
return $"[{string.Join(',', test.Arguments)}]";
|
2023-03-20 10:37:02 +00:00
|
|
|
|
}
|
2023-03-21 15:09:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-20 10:37:02 +00:00
|
|
|
|
public class LogFile
|
|
|
|
|
{
|
2023-03-29 08:07:16 +00:00
|
|
|
|
private readonly DateTime now;
|
|
|
|
|
private string name;
|
2023-03-30 08:43:17 +00:00
|
|
|
|
private readonly string ext;
|
2023-03-20 14:42:42 +00:00
|
|
|
|
private readonly string filepath;
|
2023-03-20 10:37:02 +00:00
|
|
|
|
|
2023-03-30 08:43:17 +00:00
|
|
|
|
public LogFile(DateTime now, string name, string ext = "log")
|
2023-03-20 10:37:02 +00:00
|
|
|
|
{
|
2023-03-29 08:07:16 +00:00
|
|
|
|
this.now = now;
|
|
|
|
|
this.name = name;
|
2023-03-30 08:43:17 +00:00
|
|
|
|
this.ext = ext;
|
2023-03-29 08:07:16 +00:00
|
|
|
|
|
2023-03-20 14:42:42 +00:00
|
|
|
|
filepath = Path.Join(
|
2023-03-26 06:52:53 +00:00
|
|
|
|
LogConfig.LogRoot,
|
2023-03-20 10:37:02 +00:00
|
|
|
|
$"{now.Year}-{Pad(now.Month)}",
|
|
|
|
|
Pad(now.Day));
|
|
|
|
|
|
|
|
|
|
Directory.CreateDirectory(filepath);
|
|
|
|
|
|
2023-03-29 08:07:16 +00:00
|
|
|
|
GenerateFilename();
|
2023-03-20 10:37:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-29 08:07:16 +00:00
|
|
|
|
public string FullFilename { get; private set; } = string.Empty;
|
|
|
|
|
public string FilenameWithoutPath { get; private set; } = string.Empty;
|
2023-03-26 08:08:10 +00:00
|
|
|
|
|
2023-03-20 10:37:02 +00:00
|
|
|
|
public void Write(string message)
|
|
|
|
|
{
|
|
|
|
|
WriteRaw($"{GetTimestamp()} {message}");
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-26 08:08:10 +00:00
|
|
|
|
public void WriteRaw(string message)
|
2023-03-20 10:37:02 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-03-26 08:45:01 +00:00
|
|
|
|
File.AppendAllLines(FullFilename, new[] { message });
|
2023-03-20 10:37:02 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Writing to log has failed: " + ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-29 08:07:16 +00:00
|
|
|
|
public void ConcatToFilename(string toAdd)
|
|
|
|
|
{
|
|
|
|
|
var oldFullName = FullFilename;
|
|
|
|
|
|
|
|
|
|
name += toAdd;
|
|
|
|
|
|
|
|
|
|
GenerateFilename();
|
|
|
|
|
|
|
|
|
|
File.Move(oldFullName, FullFilename);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-20 10:37:02 +00:00
|
|
|
|
private static string Pad(int n)
|
|
|
|
|
{
|
|
|
|
|
return n.ToString().PadLeft(2, '0');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string GetTimestamp()
|
|
|
|
|
{
|
|
|
|
|
return $"[{DateTime.UtcNow.ToString("u")}]";
|
|
|
|
|
}
|
2023-03-29 08:07:16 +00:00
|
|
|
|
|
|
|
|
|
private void GenerateFilename()
|
|
|
|
|
{
|
2023-03-30 08:43:17 +00:00
|
|
|
|
FilenameWithoutPath = $"{Pad(now.Hour)}-{Pad(now.Minute)}-{Pad(now.Second)}Z_{name.Replace('.', '-')}.{ext}";
|
2023-03-29 08:07:16 +00:00
|
|
|
|
FullFilename = Path.Combine(filepath, FilenameWithoutPath);
|
|
|
|
|
}
|
2023-03-20 10:37:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|