cs-codex-dist-tests/DistTestCore/Logs/DownloadedLog.cs

65 lines
1.7 KiB
C#
Raw Normal View History

2023-04-13 09:30:19 +00:00
using Logging;
using NUnit.Framework;
2023-04-14 07:54:07 +00:00
namespace DistTestCore.Logs
2023-04-13 09:30:19 +00:00
{
2023-06-27 13:28:00 +00:00
public interface IDownloadedLog
2023-04-13 09:30:19 +00:00
{
void AssertLogContains(string expectedString);
string[] FindLinesThatContain(params string[] tags);
void DeleteFile();
2023-04-13 09:30:19 +00:00
}
2023-06-27 13:28:00 +00:00
public class DownloadedLog : IDownloadedLog
2023-04-13 09:30:19 +00:00
{
private readonly LogFile logFile;
2023-06-27 13:28:00 +00:00
private readonly string owner;
2023-04-13 09:30:19 +00:00
2023-06-27 13:28:00 +00:00
public DownloadedLog(LogFile logFile, string owner)
2023-04-13 09:30:19 +00:00
{
this.logFile = logFile;
this.owner = owner;
2023-04-13 09:30:19 +00:00
}
public void AssertLogContains(string expectedString)
{
using var file = File.OpenRead(logFile.FullFilename);
using var streamReader = new StreamReader(file);
var line = streamReader.ReadLine();
while (line != null)
{
if (line.Contains(expectedString)) return;
line = streamReader.ReadLine();
}
2023-06-27 13:28:00 +00:00
Assert.Fail($"{owner} Unable to find string '{expectedString}' in CodexNode log file {logFile.FullFilename}");
2023-04-13 09:30:19 +00:00
}
public string[] FindLinesThatContain(params string[] tags)
{
var result = new List<string>();
using var file = File.OpenRead(logFile.FullFilename);
using var streamReader = new StreamReader(file);
var line = streamReader.ReadLine();
while (line != null)
{
if (tags.All(line.Contains))
{
result.Add(line);
}
line = streamReader.ReadLine();
}
return result.ToArray();
}
public void DeleteFile()
{
File.Delete(logFile.FullFilename);
}
2023-04-13 09:30:19 +00:00
}
}