From f50b8dfc833db2afbcd0d389802f4e0edfb4ecde Mon Sep 17 00:00:00 2001 From: benbierens Date: Tue, 18 Jul 2023 09:02:41 +0200 Subject: [PATCH] Sets up StatusLog --- Logging/FixtureLog.cs | 35 ++--------------- Logging/NameUtils.cs | 60 ++++++++++++++++++++++++++++ Logging/StatusLog.cs | 91 +++++++++++++++++++++++++++++++++++++++++++ Logging/TestLog.cs | 21 +--------- 4 files changed, 155 insertions(+), 52 deletions(-) create mode 100644 Logging/NameUtils.cs create mode 100644 Logging/StatusLog.cs diff --git a/Logging/FixtureLog.cs b/Logging/FixtureLog.cs index e386f01..809ff38 100644 --- a/Logging/FixtureLog.cs +++ b/Logging/FixtureLog.cs @@ -1,20 +1,14 @@ -using NUnit.Framework; - -namespace Logging +namespace Logging { public class FixtureLog : BaseLog { - private readonly DateTime start; private readonly string fullName; private readonly LogConfig config; - public FixtureLog(LogConfig config, string name = "") + public FixtureLog(LogConfig config, DateTime start, string name = "") : base(config.DebugEnabled) { - start = DateTime.UtcNow; - var folder = DetermineFolder(config); - var fixtureName = GetFixtureName(name); - fullName = Path.Combine(folder, fixtureName); + fullName = NameUtils.GetFixtureFullName(config, start, name); this.config = config; } @@ -32,28 +26,5 @@ namespace Logging { return fullName; } - - private string DetermineFolder(LogConfig config) - { - return Path.Join( - config.LogRoot, - $"{start.Year}-{Pad(start.Month)}", - Pad(start.Day)); - } - - private string GetFixtureName(string name) - { - var test = TestContext.CurrentContext.Test; - var className = test.ClassName!.Substring(test.ClassName.LastIndexOf('.') + 1); - if (!string.IsNullOrEmpty(name)) className = name; - - return $"{Pad(start.Hour)}-{Pad(start.Minute)}-{Pad(start.Second)}Z_{className.Replace('.', '-')}"; - } - - private static string Pad(int n) - { - return n.ToString().PadLeft(2, '0'); - } - } } diff --git a/Logging/NameUtils.cs b/Logging/NameUtils.cs new file mode 100644 index 0000000..0a5fcbe --- /dev/null +++ b/Logging/NameUtils.cs @@ -0,0 +1,60 @@ +using NUnit.Framework; + +namespace Logging +{ + public static class NameUtils + { + public static string GetTestMethodName(string name = "") + { + if (!string.IsNullOrEmpty(name)) return name; + var test = TestContext.CurrentContext.Test; + var args = FormatArguments(test); + return ReplaceInvalidCharacters($"{test.MethodName}{args}"); + } + + public static string GetFixtureFullName(LogConfig config, DateTime start, string name) + { + var folder = DetermineFolder(config, start); + var fixtureName = GetFixtureName(name, start); + return Path.Combine(folder, fixtureName); + } + + public static string GetRawFixtureName() + { + var test = TestContext.CurrentContext.Test; + var className = test.ClassName!.Substring(test.ClassName.LastIndexOf('.') + 1); + return className.Replace('.', '-'); + } + + private static string FormatArguments(TestContext.TestAdapter test) + { + if (test.Arguments == null || !test.Arguments.Any()) return ""; + return $"[{string.Join(',', test.Arguments)}]"; + } + + private static string ReplaceInvalidCharacters(string name) + { + return name.Replace(":", "_"); + } + + private static string DetermineFolder(LogConfig config, DateTime start) + { + return Path.Join( + config.LogRoot, + $"{start.Year}-{Pad(start.Month)}", + Pad(start.Day)); + } + + private static string GetFixtureName(string name, DateTime start) + { + var className = GetRawFixtureName(); + if (!string.IsNullOrEmpty(name)) className = name; + return $"{Pad(start.Hour)}-{Pad(start.Minute)}-{Pad(start.Second)}Z_{className.Replace('.', '-')}"; + } + + private static string Pad(int n) + { + return n.ToString().PadLeft(2, '0'); + } + } +} diff --git a/Logging/StatusLog.cs b/Logging/StatusLog.cs new file mode 100644 index 0000000..5af7542 --- /dev/null +++ b/Logging/StatusLog.cs @@ -0,0 +1,91 @@ +using Newtonsoft.Json; + +namespace Logging +{ + public class StatusLog + { + private readonly object fileLock = new object(); + private readonly string fullName; + private readonly string fixtureName; + private readonly string codexId; + + public StatusLog(LogConfig config, DateTime start, string codexId, string name = "") + { + fullName = NameUtils.GetFixtureFullName(config, start, name); + fixtureName = NameUtils.GetRawFixtureName(); + this.codexId = codexId; + } + + public void TestPassed() + { + Write("successful"); + } + + public void TestFailed() + { + Write("failed"); + } + + private void Write(string status) + { + Write(new StatusLogJson + { + time = DateTime.UtcNow.ToString("u"), + runid = GetRunId(), + status = status, + testid = GetTestId(), + codexid = codexId, + fixturename = fixtureName, + testname = GetTestName() + }); + } + + private string GetTestName() + { + return NameUtils.GetTestMethodName(); + } + + private string GetTestId() + { + return GetEnvVar("TESTID"); + } + + private string GetRunId() + { + return GetEnvVar("RUNID"); + } + + private string GetEnvVar(string name) + { + var v = Environment.GetEnvironmentVariable(name); + if (string.IsNullOrEmpty(v)) return $"'{name}' not set!"; + return v; + } + + private void Write(StatusLogJson json) + { + try + { + lock (fileLock) + { + File.AppendAllLines(fullName, new[] { JsonConvert.SerializeObject(json) }); + } + } + catch (Exception ex) + { + Console.WriteLine("Unable to write to status log: " + ex); + } + } + } + + public class StatusLogJson + { + public string time { get; set; } = string.Empty; + public string runid { get; set; } = string.Empty; + public string status { get; set; } = string.Empty; + public string testid { get; set; } = string.Empty; + public string codexid { get; set; } = string.Empty; + public string fixturename { get; set; } = string.Empty; + public string testname { get; set; } = string.Empty; + } +} diff --git a/Logging/TestLog.cs b/Logging/TestLog.cs index 1bf5d41..a8d3249 100644 --- a/Logging/TestLog.cs +++ b/Logging/TestLog.cs @@ -10,7 +10,7 @@ namespace Logging public TestLog(string folder, bool debug, string name = "") : base(debug) { - methodName = GetMethodName(name); + methodName = NameUtils.GetTestMethodName(name); fullName = Path.Combine(folder, methodName); Log($"*** Begin: {methodName}"); @@ -37,24 +37,5 @@ namespace Logging { return fullName; } - - private string GetMethodName(string name) - { - if (!string.IsNullOrEmpty(name)) return name; - var test = TestContext.CurrentContext.Test; - var args = FormatArguments(test); - return ReplaceInvalidCharacters($"{test.MethodName}{args}"); - } - - private static string FormatArguments(TestContext.TestAdapter test) - { - if (test.Arguments == null || !test.Arguments.Any()) return ""; - return $"[{string.Join(',', test.Arguments)}]"; - } - - private static string ReplaceInvalidCharacters(string name) - { - return name.Replace(":", "_"); - } } }