diff --git a/src/Asv.Cfg.Test/Asv.Cfg.Test.csproj b/src/Asv.Cfg.Test/Asv.Cfg.Test.csproj index 119a22c..9a5bf8b 100644 --- a/src/Asv.Cfg.Test/Asv.Cfg.Test.csproj +++ b/src/Asv.Cfg.Test/Asv.Cfg.Test.csproj @@ -9,6 +9,7 @@ + diff --git a/src/Asv.Cfg.Test/Asv.Cfg.Test.csproj.DotSettings b/src/Asv.Cfg.Test/Asv.Cfg.Test.csproj.DotSettings new file mode 100644 index 0000000..b5f9866 --- /dev/null +++ b/src/Asv.Cfg.Test/Asv.Cfg.Test.csproj.DotSettings @@ -0,0 +1,4 @@ + + True + True + True \ No newline at end of file diff --git a/src/Asv.Cfg.Test/ConfigurationTestBase.cs b/src/Asv.Cfg.Test/ConfigurationBaseTest.cs similarity index 97% rename from src/Asv.Cfg.Test/ConfigurationTestBase.cs rename to src/Asv.Cfg.Test/ConfigurationBaseTest.cs index a2a1680..1510d1d 100644 --- a/src/Asv.Cfg.Test/ConfigurationTestBase.cs +++ b/src/Asv.Cfg.Test/ConfigurationBaseTest.cs @@ -7,7 +7,7 @@ namespace Asv.Cfg.Test; -public abstract class ConfigurationTestBase +public abstract class ConfigurationBaseTest where T:IConfiguration { protected abstract IDisposable CreateForTest(out T configuration); @@ -65,7 +65,7 @@ public void Check_Available_Parts_Value() Thread.Sleep(50); var expectedResult = new string[] { "test1", "test2", "test3", "test4" }; - var actualResult = cfg.AvailableParts.OrderBy(_=>_).ToArray(); // items can be reordered in any way, so we need to sort them + var actualResult = cfg.AvailableParts.OrderBy(x=>x).ToArray(); // items can be reordered in any way, so we need to sort them Assert.Equal(expectedResult, actualResult); } diff --git a/src/Asv.Cfg.Test/InMemory/InMemoryConfigurationTest.cs b/src/Asv.Cfg.Test/InMemory/InMemoryConfigurationTest.cs new file mode 100644 index 0000000..a9be407 --- /dev/null +++ b/src/Asv.Cfg.Test/InMemory/InMemoryConfigurationTest.cs @@ -0,0 +1,20 @@ +using System; +using System.Reactive.Disposables; +using JetBrains.Annotations; +using Xunit.Abstractions; + +namespace Asv.Cfg.Test +{ + [TestSubject(typeof(InMemoryConfiguration))] + public class InMemoryConfigurationTest(ITestOutputHelper log) : ConfigurationBaseTest + { + private readonly ITestOutputHelper _log = log; + + protected override IDisposable CreateForTest(out InMemoryConfiguration configuration) + { + configuration = new InMemoryConfiguration(new TestLogger(_log,TimeProvider.System, "IM_MEMORY")); + var cfg = configuration; + return Disposable.Create(() => {cfg.Dispose(); }); + } + } +} \ No newline at end of file diff --git a/src/Asv.Cfg.Test/InMemoryTests.cs b/src/Asv.Cfg.Test/InMemoryTests.cs deleted file mode 100644 index 427223a..0000000 --- a/src/Asv.Cfg.Test/InMemoryTests.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Reactive.Disposables; -using Xunit.Abstractions; - -namespace Asv.Cfg.Test -{ - public class InMemoryTests:ConfigurationTestBase - { - private readonly ITestOutputHelper _testOutputHelper; - - public InMemoryTests(ITestOutputHelper testOutputHelper) - { - _testOutputHelper = testOutputHelper; - } - - protected override IDisposable CreateForTest(out InMemoryConfiguration configuration) - { - configuration = new InMemoryConfiguration(); - var cfg = configuration; - return Disposable.Create(() => {cfg.Dispose(); }); - } - } -} \ No newline at end of file diff --git a/src/Asv.Cfg.Test/JsonConfigurationTests.cs b/src/Asv.Cfg.Test/Json/JsonConfigurationTest.cs similarity index 87% rename from src/Asv.Cfg.Test/JsonConfigurationTests.cs rename to src/Asv.Cfg.Test/Json/JsonConfigurationTest.cs index 464428d..027e14d 100644 --- a/src/Asv.Cfg.Test/JsonConfigurationTests.cs +++ b/src/Asv.Cfg.Test/Json/JsonConfigurationTest.cs @@ -5,7 +5,7 @@ using System.Linq; using System.Reactive.Disposables; using System.Threading; -using Asv.Cfg.Json; +using JetBrains.Annotations; using Xunit; using Xunit.Abstractions; @@ -56,16 +56,11 @@ public class TestMultiThreadClassFour } #endregion - public class JsonConfigurationTests:ConfigurationTestBase + + [TestSubject(typeof(JsonConfiguration))] + public class JsonConfigurationTest(ITestOutputHelper log) : ConfigurationBaseTest { - private readonly ITestOutputHelper _testOutputHelper; - private readonly IFileSystem _fileSystem; - - public JsonConfigurationTests(ITestOutputHelper testOutputHelper) - { - _testOutputHelper = testOutputHelper; - _fileSystem = new MockFileSystem(); - } + private readonly IFileSystem _fileSystem = new MockFileSystem(); protected override IDisposable CreateForTest(out JsonConfiguration configuration) { @@ -80,8 +75,8 @@ protected override IDisposable CreateForTest(out JsonConfiguration configuration _fileSystem.Directory.Delete(workingDir); } - _testOutputHelper.WriteLine($"Working directory: {workingDir}"); - configuration = new JsonConfiguration(workingDir, fileSystem: _fileSystem); + log.WriteLine($"Working directory: {workingDir}"); + configuration = new JsonConfiguration(workingDir, logger:new TestLogger(log,TimeProvider.System, "JSON"), fileSystem: _fileSystem); var cfg = configuration; return Disposable.Create(() => { diff --git a/src/Asv.Cfg.Test/JsonOneFileConfigurationTests.cs b/src/Asv.Cfg.Test/Json/JsonOneFileConfigurationTest.cs similarity index 92% rename from src/Asv.Cfg.Test/JsonOneFileConfigurationTests.cs rename to src/Asv.Cfg.Test/Json/JsonOneFileConfigurationTest.cs index 8ae55b8..42ee086 100644 --- a/src/Asv.Cfg.Test/JsonOneFileConfigurationTests.cs +++ b/src/Asv.Cfg.Test/Json/JsonOneFileConfigurationTest.cs @@ -5,23 +5,17 @@ using System.IO.Abstractions.TestingHelpers; using System.Linq; using System.Reactive.Disposables; -using Asv.Cfg.Json; +using JetBrains.Annotations; using Newtonsoft.Json; using Xunit; using Xunit.Abstractions; namespace Asv.Cfg.Test { - public class JsonOneFileConfigurationTests : ConfigurationTestBase + [TestSubject(typeof(JsonOneFileConfiguration))] + public class JsonOneFileConfigurationTest(ITestOutputHelper log) : ConfigurationBaseTest { - private readonly ITestOutputHelper _testOutputHelper; - private readonly IFileSystem _fileSystem; - - public JsonOneFileConfigurationTests(ITestOutputHelper testOutputHelper) - { - _testOutputHelper = testOutputHelper; - _fileSystem = new MockFileSystem(); - } + private readonly IFileSystem _fileSystem = new MockFileSystem(); protected override IDisposable CreateForTest(out JsonOneFileConfiguration configuration) { @@ -30,6 +24,7 @@ protected override IDisposable CreateForTest(out JsonOneFileConfiguration config filePath, true, null, + logger:new TestLogger(log,TimeProvider.System, "JSON_ONE_FILE"), fileSystem: _fileSystem ); diff --git a/src/Asv.Cfg.Test/ZipJsonConfigurationTests.cs b/src/Asv.Cfg.Test/Json/ZipJsonConfigurationTest.cs similarity index 78% rename from src/Asv.Cfg.Test/ZipJsonConfigurationTests.cs rename to src/Asv.Cfg.Test/Json/ZipJsonConfigurationTest.cs index 45f7394..2d9b175 100644 --- a/src/Asv.Cfg.Test/ZipJsonConfigurationTests.cs +++ b/src/Asv.Cfg.Test/Json/ZipJsonConfigurationTest.cs @@ -3,22 +3,17 @@ using System.IO.Abstractions; using System.IO.Abstractions.TestingHelpers; using System.Reactive.Disposables; -using Asv.Cfg.Json; +using JetBrains.Annotations; using Xunit; using Xunit.Abstractions; namespace Asv.Cfg.Test { - public class ZipJsonConfigurationTests: ConfigurationTestBase + [TestSubject(typeof(ZipJsonConfiguration))] + public class ZipJsonConfigurationTest(ITestOutputHelper log) + : ConfigurationBaseTest { - private readonly ITestOutputHelper _testOutputHelper; - private readonly IFileSystem _fileSystem; - - public ZipJsonConfigurationTests(ITestOutputHelper testOutputHelper) - { - _testOutputHelper = testOutputHelper; - _fileSystem = new MockFileSystem(); - } + private readonly IFileSystem _fileSystem = new MockFileSystem(); [Fact] public void Configuration_Should_Throw_Argument_Exception_If_Null() @@ -38,7 +33,7 @@ protected override IDisposable CreateForTest(out ZipJsonConfiguration configurat _fileSystem.Directory.CreateDirectory(dir ?? throw new InvalidOperationException()); } var file = _fileSystem.File.Open(filePath, FileMode.OpenOrCreate); - configuration = new ZipJsonConfiguration(file, true, null, fileSystem: _fileSystem); + configuration = new ZipJsonConfiguration(file, true, new TestLogger(log,TimeProvider.System, "ZIP_JSON"), fileSystem: _fileSystem); var cfg = configuration; return Disposable.Create(() => { diff --git a/src/Asv.Cfg.Test/Json/ZipJsonVersionedFileTest.cs b/src/Asv.Cfg.Test/Json/ZipJsonVersionedFileTest.cs new file mode 100644 index 0000000..35267ab --- /dev/null +++ b/src/Asv.Cfg.Test/Json/ZipJsonVersionedFileTest.cs @@ -0,0 +1,19 @@ +using System; +using System.IO.Abstractions; +using System.IO.Abstractions.TestingHelpers; +using Asv.Cfg; +using JetBrains.Annotations; +using Xunit; + +namespace Asv.Cfg.Test; + +[TestSubject(typeof(ZipJsonVersionedFile))] +public class ZipJsonVersionedFileTest:ConfigurationBaseTest +{ + + + protected override IDisposable CreateForTest(out ZipJsonVersionedFile configuration) + { + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/src/Asv.Cfg.Test/LoggerWrapper.cs b/src/Asv.Cfg.Test/LoggerWrapper.cs new file mode 100644 index 0000000..7fe760c --- /dev/null +++ b/src/Asv.Cfg.Test/LoggerWrapper.cs @@ -0,0 +1,61 @@ +using System; +using System.Reactive.Disposables; +using Microsoft.Extensions.Logging; +using Xunit.Abstractions; + +namespace Asv.Cfg.Test; + +public class TestLoggerFactory(ITestOutputHelper testOutputHelper, TimeProvider time, string prefix) : ILoggerFactory +{ + public TimeProvider Time { get; } = time; + private readonly string _prefix = prefix; + + public void Dispose() + { + + } + + public ILogger CreateLogger(string categoryName) + { + return new TestLogger(testOutputHelper,Time, $"{prefix}.{categoryName}"); + } + + public void AddProvider(ILoggerProvider provider) + { + + } +} + +public class TestLogger(ITestOutputHelper testOutputHelper, TimeProvider time, string? categoryName) : ILogger +{ + public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None; + + public IDisposable BeginScope(TState state) where TState : notnull => Disposable.Empty; + + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) + { + try + { + testOutputHelper.WriteLine($"{time.GetLocalNow().DateTime:HH:mm:ss.fff,15} |={ConvertToStr(logLevel)}=| {categoryName,-8} | {formatter(state, exception)}"); + } + catch + { + // This can happen when the test is not active + } + } + + private string ConvertToStr(LogLevel logLevel) + { + return logLevel switch + { + LogLevel.Trace => "TRC", + LogLevel.Debug => "DBG", + LogLevel.Information => "INF", + LogLevel.Warning => "WRN", + LogLevel.Error => "ERR", + LogLevel.Critical => "CRT", + LogLevel.None => "NON", + _ => throw new ArgumentOutOfRangeException(nameof(logLevel), logLevel, null) + }; + } +} \ No newline at end of file diff --git a/src/Asv.Cfg/Asv.Cfg.csproj.DotSettings b/src/Asv.Cfg/Asv.Cfg.csproj.DotSettings index 9a1a8b7..52467af 100644 --- a/src/Asv.Cfg/Asv.Cfg.csproj.DotSettings +++ b/src/Asv.Cfg/Asv.Cfg.csproj.DotSettings @@ -1,2 +1,3 @@  - True \ No newline at end of file + True + True \ No newline at end of file diff --git a/src/Asv.Cfg/Json/JsonConfiguration.cs b/src/Asv.Cfg/Json/JsonConfiguration.cs index 720809e..2a0b11e 100644 --- a/src/Asv.Cfg/Json/JsonConfiguration.cs +++ b/src/Asv.Cfg/Json/JsonConfiguration.cs @@ -10,7 +10,7 @@ using Newtonsoft.Json; using ZLogger; -namespace Asv.Cfg.Json +namespace Asv.Cfg { public class JsonConfiguration:IConfiguration { diff --git a/src/Asv.Cfg/Json/JsonHelper.cs b/src/Asv.Cfg/Json/JsonHelper.cs index 7d0c70f..c079d40 100644 --- a/src/Asv.Cfg/Json/JsonHelper.cs +++ b/src/Asv.Cfg/Json/JsonHelper.cs @@ -2,7 +2,7 @@ using Newtonsoft.Json.Converters; using Newtonsoft.Json.Serialization; -namespace Asv.Cfg.Json; +namespace Asv.Cfg; public static class JsonHelper { diff --git a/src/Asv.Cfg/Json/JsonOneFileConfiguration.cs b/src/Asv.Cfg/Json/JsonOneFileConfiguration.cs index b0b386b..f24267d 100644 --- a/src/Asv.Cfg/Json/JsonOneFileConfiguration.cs +++ b/src/Asv.Cfg/Json/JsonOneFileConfiguration.cs @@ -9,12 +9,12 @@ using Asv.Common; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; -using ZLogger; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Linq; +using ZLogger; -namespace Asv.Cfg.Json +namespace Asv.Cfg { public class JsonOneFileConfiguration : DisposableOnce, IConfiguration diff --git a/src/Asv.Cfg/Json/ZipJsonConfiguration.cs b/src/Asv.Cfg/Json/ZipJsonConfiguration.cs index 72e2a40..9d41d5d 100644 --- a/src/Asv.Cfg/Json/ZipJsonConfiguration.cs +++ b/src/Asv.Cfg/Json/ZipJsonConfiguration.cs @@ -11,7 +11,7 @@ using Newtonsoft.Json; using ZLogger; -namespace Asv.Cfg.Json +namespace Asv.Cfg { public class ZipJsonConfiguration:DisposableOnce, IConfiguration { diff --git a/src/Asv.Cfg/Json/ZipJsonVersionedFile.cs b/src/Asv.Cfg/Json/ZipJsonVersionedFile.cs index eedc60a..cf0a009 100644 --- a/src/Asv.Cfg/Json/ZipJsonVersionedFile.cs +++ b/src/Asv.Cfg/Json/ZipJsonVersionedFile.cs @@ -3,7 +3,7 @@ using Asv.Common; using Newtonsoft.Json; -namespace Asv.Cfg.Json +namespace Asv.Cfg { [method: JsonConstructor] public readonly struct ZipJsonFileInfo(string fileVersion, string fileType) : IEquatable diff --git a/src/Asv.Common.Test/Asv.Common.Test.csproj b/src/Asv.Common.Test/Asv.Common.Test.csproj index 73218a2..931f79c 100644 --- a/src/Asv.Common.Test/Asv.Common.Test.csproj +++ b/src/Asv.Common.Test/Asv.Common.Test.csproj @@ -8,6 +8,7 @@ latest + diff --git a/src/Asv.Common.Test/LoggerWrapper.cs b/src/Asv.Common.Test/LoggerWrapper.cs new file mode 100644 index 0000000..bb4a53e --- /dev/null +++ b/src/Asv.Common.Test/LoggerWrapper.cs @@ -0,0 +1,61 @@ +using System; +using System.Reactive.Disposables; +using Microsoft.Extensions.Logging; +using Xunit.Abstractions; + +namespace Asv.Common.Test; + +public class TestLoggerFactory(ITestOutputHelper testOutputHelper, TimeProvider time, string prefix) : ILoggerFactory +{ + public TimeProvider Time { get; } = time; + private readonly string _prefix = prefix; + + public void Dispose() + { + + } + + public ILogger CreateLogger(string categoryName) + { + return new TestLogger(testOutputHelper,Time, $"{prefix}.{categoryName}"); + } + + public void AddProvider(ILoggerProvider provider) + { + + } +} + +public class TestLogger(ITestOutputHelper testOutputHelper, TimeProvider time, string? categoryName) : ILogger +{ + public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None; + + public IDisposable BeginScope(TState state) where TState : notnull => Disposable.Empty; + + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) + { + try + { + testOutputHelper.WriteLine($"{time.GetLocalNow().DateTime:HH:mm:ss.fff,15} |={ConvertToStr(logLevel)}=| {categoryName,-8} | {formatter(state, exception)}"); + } + catch + { + // This can happen when the test is not active + } + } + + private string ConvertToStr(LogLevel logLevel) + { + return logLevel switch + { + LogLevel.Trace => "TRC", + LogLevel.Debug => "DBG", + LogLevel.Information => "INF", + LogLevel.Warning => "WRN", + LogLevel.Error => "ERR", + LogLevel.Critical => "CRT", + LogLevel.None => "NON", + _ => throw new ArgumentOutOfRangeException(nameof(logLevel), logLevel, null) + }; + } +} \ No newline at end of file