From e6a95d50e72978a92582db70ca4b71cb882b90b4 Mon Sep 17 00:00:00 2001 From: Fredi Kats Date: Thu, 7 Sep 2023 21:10:38 +0400 Subject: [PATCH 1/2] Implement logging --- Sources/Directory.Packages.props | 6 +++++ .../DependencyBuilder.cs | 26 +++++++++++++++++-- .../Kysect.Configuin.Console.csproj | 5 ++++ .../CliExecution/CmdExecutionResult.cs | 5 ++++ .../CliExecution/CmdProcess.cs | 25 ++++++++++++++++-- .../CodeStyleGeneration/CodeStyleGenerator.cs | 24 +++++++++++++++-- .../Markdown/MarkdownCodeStyleFormatter.cs | 10 +++++++ .../Markdown/MarkdownCodeStyleWriter.cs | 11 +++++--- .../DotnetFormat/DotnetFormatCli.cs | 13 ++++++++-- .../DotnetFormatReportComparator.cs | 14 ++++++++-- .../EditorConfigSettingsParser.cs | 10 +++++-- .../Settings/IEditorConfigSetting.cs | 16 ++++++++++-- .../Kysect.Configuin.Core.csproj | 1 + .../MsLearnDocumentationParser.cs | 13 +++++++++- .../Kysect.Configuin.Tests/CmdProcessTests.cs | 12 ++++++--- .../CodeStyleGeneratorTests.cs | 13 +++++++--- .../MarkdownCodeStyleFormatterTests.cs | 3 ++- .../DotnetFormat/DotnetFormatCliTests.cs | 3 ++- .../DotnetFormatReportComparatorTests.cs | 10 ++++++- .../EditorConfigSettingsParserTests.cs | 3 ++- .../Kysect.Configuin.Tests.csproj | 5 ++++ .../MsLearnDocumentationParserTests.cs | 2 +- .../Tools/TestLogger.cs | 21 +++++++++++++++ 23 files changed, 221 insertions(+), 30 deletions(-) create mode 100644 Sources/Kysect.Configuin.Tests/Tools/TestLogger.cs diff --git a/Sources/Directory.Packages.props b/Sources/Directory.Packages.props index 8e85d67..479ab82 100644 --- a/Sources/Directory.Packages.props +++ b/Sources/Directory.Packages.props @@ -3,6 +3,7 @@ true + @@ -11,8 +12,13 @@ + + + + + diff --git a/Sources/Kysect.Configuin.Console/DependencyBuilder.cs b/Sources/Kysect.Configuin.Console/DependencyBuilder.cs index befbc0b..1a3e080 100644 --- a/Sources/Kysect.Configuin.Console/DependencyBuilder.cs +++ b/Sources/Kysect.Configuin.Console/DependencyBuilder.cs @@ -8,7 +8,10 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using Serilog; +using ILogger = Microsoft.Extensions.Logging.ILogger; namespace Kysect.Configuin.Console; @@ -22,6 +25,7 @@ public static IServiceProvider InitializeServiceProvider() IServiceCollection serviceCollection = builder.Services; serviceCollection.AddOptionsWithValidation(nameof(ConfiguinConfiguration)); + serviceCollection.AddSingleton(CreateLogger); serviceCollection.AddSingleton(sp => { @@ -39,15 +43,33 @@ public static IServiceProvider InitializeServiceProvider() return new MsLearnDocumentationInfoLocalProvider(options.Value.MsLearnRepositoryPath); }); - serviceCollection.AddSingleton(_ => new MsLearnDocumentationParser(PlainTextExtractor.Create())); + serviceCollection.AddSingleton(sp => + { + ILogger logger = sp.GetRequiredService(); + return new MsLearnDocumentationParser(PlainTextExtractor.Create(), logger); + }); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(sp => { IOptions options = sp.GetRequiredService>(); + ILogger logger = sp.GetRequiredService(); - return new MarkdownCodeStyleWriter(options.Value.OutputPath); + return new MarkdownCodeStyleWriter(options.Value.OutputPath, logger); }); return serviceCollection.BuildServiceProvider(); } + + public static ILogger CreateLogger(IServiceProvider serviceProvider) + { + LoggerConfiguration loggerConfiguration = new LoggerConfiguration() + .MinimumLevel.Verbose() + .WriteTo.Console(); + + ILoggerFactory loggerFactory = new LoggerFactory() + .DemystifyExceptions() + .AddSerilog(loggerConfiguration.CreateLogger()); + + return loggerFactory.CreateLogger("Tests"); + } } \ No newline at end of file diff --git a/Sources/Kysect.Configuin.Console/Kysect.Configuin.Console.csproj b/Sources/Kysect.Configuin.Console/Kysect.Configuin.Console.csproj index 5c1039c..53b8b95 100644 --- a/Sources/Kysect.Configuin.Console/Kysect.Configuin.Console.csproj +++ b/Sources/Kysect.Configuin.Console/Kysect.Configuin.Console.csproj @@ -8,11 +8,16 @@ + + + + + diff --git a/Sources/Kysect.Configuin.Core/CliExecution/CmdExecutionResult.cs b/Sources/Kysect.Configuin.Core/CliExecution/CmdExecutionResult.cs index 45492fb..9625672 100644 --- a/Sources/Kysect.Configuin.Core/CliExecution/CmdExecutionResult.cs +++ b/Sources/Kysect.Configuin.Core/CliExecution/CmdExecutionResult.cs @@ -29,4 +29,9 @@ public void ThrowIfAnyError() throw new CmdProcessException($"Return {ExitCode} exit code."); } } + + public bool IsAnyError() + { + return Errors.Any() || ExitCode != 0; + } } \ No newline at end of file diff --git a/Sources/Kysect.Configuin.Core/CliExecution/CmdProcess.cs b/Sources/Kysect.Configuin.Core/CliExecution/CmdProcess.cs index 89e70d0..23f7605 100644 --- a/Sources/Kysect.Configuin.Core/CliExecution/CmdProcess.cs +++ b/Sources/Kysect.Configuin.Core/CliExecution/CmdProcess.cs @@ -1,25 +1,46 @@ -using System.Diagnostics; +using Kysect.CommonLib.Logging; +using Microsoft.Extensions.Logging; +using System.Diagnostics; using System.Runtime.InteropServices; namespace Kysect.Configuin.Core.CliExecution; public class CmdProcess { + private readonly ILogger _logger; + + public CmdProcess(ILogger logger) + { + _logger = logger; + } + public CmdExecutionResult ExecuteCommand(string command) { + using var process = new Process(); ProcessStartInfo startInfo = CreateProcessStartInfo(command); + _logger.LogTrace("Execute cmd command {command} {arguments}", startInfo.FileName, startInfo.Arguments); + process.StartInfo = startInfo; process.Start(); process.WaitForExit(); int exitCode = process.ExitCode; IReadOnlyCollection errors = GetErrors(process); + var cmdExecutionResult = new CmdExecutionResult(exitCode, errors); + + if (cmdExecutionResult.IsAnyError()) + { + _logger.LogError("Finished with {exitCode} and {errorCount} errors.", exitCode, errors.Count); + foreach (string error in cmdExecutionResult.Errors) + _logger.LogTabError(1, error); + } + process.Close(); - return new CmdExecutionResult(exitCode, errors); + return cmdExecutionResult; } private ProcessStartInfo CreateProcessStartInfo(string command) diff --git a/Sources/Kysect.Configuin.Core/CodeStyleGeneration/CodeStyleGenerator.cs b/Sources/Kysect.Configuin.Core/CodeStyleGeneration/CodeStyleGenerator.cs index 467a188..5bdb6f2 100644 --- a/Sources/Kysect.Configuin.Core/CodeStyleGeneration/CodeStyleGenerator.cs +++ b/Sources/Kysect.Configuin.Core/CodeStyleGeneration/CodeStyleGenerator.cs @@ -3,22 +3,34 @@ using Kysect.Configuin.Core.EditorConfigParsing; using Kysect.Configuin.Core.EditorConfigParsing.Settings; using Kysect.Configuin.Core.RoslynRuleModels; +using Microsoft.Extensions.Logging; namespace Kysect.Configuin.Core.CodeStyleGeneration; public class CodeStyleGenerator : ICodeStyleGenerator { + private readonly ILogger _logger; + + public CodeStyleGenerator(ILogger logger) + { + _logger = logger; + } + public CodeStyle Generate(EditorConfigSettings editorConfigSettings, RoslynRules roslynRules) { + _logger.LogInformation("Start code style generating."); + IReadOnlyCollection roslynRuleOptions = roslynRules.GetOptions(); IReadOnlyCollection notProcessedSettings = editorConfigSettings.Settings; + _logger.LogInformation("Try parse {count} settings", notProcessedSettings.Count); notProcessedSettings = notProcessedSettings.Where(IsSupported).ToList(); IReadOnlyCollection optionConfigurations = notProcessedSettings .OfType() .Select(o => ParseOptionSettings(o, roslynRuleOptions)) .ToList(); + _logger.LogInformation("Parsed {count} option configurations", optionConfigurations.Count); notProcessedSettings = notProcessedSettings.Where(r => r is not RoslynOptionEditorConfigSetting).ToList(); @@ -26,6 +38,7 @@ public CodeStyle Generate(EditorConfigSettings editorConfigSettings, RoslynRules .OfType() .Select(severitySetting => ParseRuleSettings(severitySetting, optionConfigurations, roslynRules)) .ToList(); + _logger.LogInformation("Parsed {count} rule severity", ruleConfiguration.Count); notProcessedSettings = notProcessedSettings.Where(r => r is not RoslynSeverityEditorConfigSetting).ToList(); @@ -48,16 +61,23 @@ private bool IsSupported(IEditorConfigSetting setting) if (setting is RoslynSeverityEditorConfigSetting severityEditorConfigRule && severityEditorConfigRule.RuleId.Equals(RoslynRuleId.Parse("IDE1006"))) { + _logger.LogWarning("Rule IDE0055 is not supported and will be skipped."); return false; } // TODO: #35 Probably, most of this rules related to IDE1006 - if (setting is CompositeRoslynOptionEditorConfigSetting) + if (setting is CompositeRoslynOptionEditorConfigSetting compositeSetting) + { + _logger.LogWarning("{setting} is not supported and will be skipped.", compositeSetting.ToDisplayString()); return false; + } // TODO: Maybe we need to support it in some way - if (setting is GeneralEditorConfigSetting) + if (setting is GeneralEditorConfigSetting generalSetting) + { + _logger.LogWarning("{option} is not supported and will be skipped.", generalSetting.ToDisplayString()); return false; + } return true; } diff --git a/Sources/Kysect.Configuin.Core/CodeStyleGeneration/Markdown/MarkdownCodeStyleFormatter.cs b/Sources/Kysect.Configuin.Core/CodeStyleGeneration/Markdown/MarkdownCodeStyleFormatter.cs index a64a137..ae1b66c 100644 --- a/Sources/Kysect.Configuin.Core/CodeStyleGeneration/Markdown/MarkdownCodeStyleFormatter.cs +++ b/Sources/Kysect.Configuin.Core/CodeStyleGeneration/Markdown/MarkdownCodeStyleFormatter.cs @@ -1,12 +1,22 @@ using Kysect.CommonLib.Exceptions; using Kysect.Configuin.Core.CodeStyleGeneration.Models; +using Microsoft.Extensions.Logging; namespace Kysect.Configuin.Core.CodeStyleGeneration.Markdown; public class MarkdownCodeStyleFormatter : ICodeStyleFormatter { + private readonly ILogger _logger; + + public MarkdownCodeStyleFormatter(ILogger logger) + { + _logger = logger; + } + public string Format(CodeStyle codeStyle) { + _logger.LogInformation("Formatting code style to markdown."); + var strings = codeStyle.Elements .Select(FormatRule) .ToList(); diff --git a/Sources/Kysect.Configuin.Core/CodeStyleGeneration/Markdown/MarkdownCodeStyleWriter.cs b/Sources/Kysect.Configuin.Core/CodeStyleGeneration/Markdown/MarkdownCodeStyleWriter.cs index f622cab..b420544 100644 --- a/Sources/Kysect.Configuin.Core/CodeStyleGeneration/Markdown/MarkdownCodeStyleWriter.cs +++ b/Sources/Kysect.Configuin.Core/CodeStyleGeneration/Markdown/MarkdownCodeStyleWriter.cs @@ -1,21 +1,26 @@ using Kysect.Configuin.Core.CodeStyleGeneration.Models; +using Microsoft.Extensions.Logging; namespace Kysect.Configuin.Core.CodeStyleGeneration.Markdown; public class MarkdownCodeStyleWriter : ICodeStyleWriter { - private readonly MarkdownCodeStyleFormatter _formatter = new MarkdownCodeStyleFormatter(); - private readonly string _filePath; + private readonly ILogger _logger; + + private readonly MarkdownCodeStyleFormatter _formatter; - public MarkdownCodeStyleWriter(string filePath) + public MarkdownCodeStyleWriter(string filePath, ILogger logger) { + _formatter = new MarkdownCodeStyleFormatter(logger); _filePath = filePath; + _logger = logger; } public void Write(CodeStyle codeStyle) { string content = _formatter.Format(codeStyle); + _logger.LogInformation("Writing code style as markdown to file {filePath}", _filePath); File.WriteAllText(_filePath, content); } } \ No newline at end of file diff --git a/Sources/Kysect.Configuin.Core/DotnetFormat/DotnetFormatCli.cs b/Sources/Kysect.Configuin.Core/DotnetFormat/DotnetFormatCli.cs index bd238c9..47f7991 100644 --- a/Sources/Kysect.Configuin.Core/DotnetFormat/DotnetFormatCli.cs +++ b/Sources/Kysect.Configuin.Core/DotnetFormat/DotnetFormatCli.cs @@ -1,19 +1,28 @@ using Kysect.Configuin.Core.CliExecution; +using Microsoft.Extensions.Logging; namespace Kysect.Configuin.Core.DotnetFormat; public class DotnetFormatCli { - private readonly CmdProcess _cmdProcess = new CmdProcess(); + private readonly ILogger _logger; + private readonly CmdProcess _cmdProcess; + + public DotnetFormatCli(ILogger logger) + { + _cmdProcess = new CmdProcess(logger); + _logger = logger; + } public void Validate() { + _logger.LogInformation("Try to use dotnet format"); _cmdProcess.ExecuteCommand("dotnet format -h").ThrowIfAnyError(); } public void GenerateWarnings(string pathToSolution, string pathToJson) { - + _logger.LogInformation("Generate warnings for {pathToSolution} and write result to {pathToJson}", pathToSolution, pathToJson); _cmdProcess.ExecuteCommand($"dotnet format \"{pathToSolution}\" --verify-no-changes --report \"{pathToJson}\"").ThrowIfAnyError(); } } \ No newline at end of file diff --git a/Sources/Kysect.Configuin.Core/DotnetFormat/DotnetFormatReportComparator.cs b/Sources/Kysect.Configuin.Core/DotnetFormat/DotnetFormatReportComparator.cs index 74df9e2..610f743 100644 --- a/Sources/Kysect.Configuin.Core/DotnetFormat/DotnetFormatReportComparator.cs +++ b/Sources/Kysect.Configuin.Core/DotnetFormat/DotnetFormatReportComparator.cs @@ -1,18 +1,28 @@ using Kysect.CommonLib.BaseTypes.Extensions; using Kysect.CommonLib.Collections.Diff; +using Microsoft.Extensions.Logging; using System.Text.Json; namespace Kysect.Configuin.Core.DotnetFormat; public class DotnetFormatReportComparator { - public static CollectionDiff Compare(string left, string right) + private readonly ILogger _logger; + + public DotnetFormatReportComparator(ILogger logger) + { + _logger = logger; + } + + public CollectionDiff Compare(string left, string right) { + _logger.LogInformation("Comparing dotnet format report between {left} and {right}", left, right); + IReadOnlyCollection leftReports = JsonSerializer.Deserialize>(left).ThrowIfNull(); IReadOnlyCollection rightReports = JsonSerializer.Deserialize>(right).ThrowIfNull(); var diff = CollectionDiff.Create(leftReports, rightReports, DotnetFormatFileReportComparator.Instance); - + _logger.LogInformation("Same: {same}, added: {added}, removed: {removed}", diff.Same, diff.Added, diff.Removed); return diff; } diff --git a/Sources/Kysect.Configuin.Core/EditorConfigParsing/EditorConfigSettingsParser.cs b/Sources/Kysect.Configuin.Core/EditorConfigParsing/EditorConfigSettingsParser.cs index 2dddb95..821695a 100644 --- a/Sources/Kysect.Configuin.Core/EditorConfigParsing/EditorConfigSettingsParser.cs +++ b/Sources/Kysect.Configuin.Core/EditorConfigParsing/EditorConfigSettingsParser.cs @@ -1,17 +1,21 @@ using Kysect.Configuin.Core.EditorConfigParsing.Settings; using Kysect.Configuin.Core.IniParsing; using Kysect.Configuin.Core.RoslynRuleModels; +using Microsoft.Extensions.Logging; namespace Kysect.Configuin.Core.EditorConfigParsing; public class EditorConfigSettingsParser : IEditorConfigSettingsParser { - private readonly IniParser _iniParser = new IniParser(); + private readonly ILogger _logger; + private readonly IniParser _iniParser = new IniParser(); private readonly HashSet _generalRuleKeys; - public EditorConfigSettingsParser() + public EditorConfigSettingsParser(ILogger logger) { + _logger = logger; + // TODO: Investigate other rules _generalRuleKeys = new HashSet { @@ -23,6 +27,8 @@ public EditorConfigSettingsParser() public EditorConfigSettings Parse(string content) { + _logger.LogInformation("Parse .editorconfig file"); + IReadOnlyCollection iniFileLines = _iniParser.Parse(content); var rules = iniFileLines diff --git a/Sources/Kysect.Configuin.Core/EditorConfigParsing/Settings/IEditorConfigSetting.cs b/Sources/Kysect.Configuin.Core/EditorConfigParsing/Settings/IEditorConfigSetting.cs index 0bb36ae..27dd9b7 100644 --- a/Sources/Kysect.Configuin.Core/EditorConfigParsing/Settings/IEditorConfigSetting.cs +++ b/Sources/Kysect.Configuin.Core/EditorConfigParsing/Settings/IEditorConfigSetting.cs @@ -9,11 +9,23 @@ public interface IEditorConfigSetting public record CompositeRoslynOptionEditorConfigSetting( IReadOnlyCollection KeyParts, string Value, - RoslynRuleSeverity? Severity) : IEditorConfigSetting; + RoslynRuleSeverity? Severity) : IEditorConfigSetting +{ + public string ToDisplayString() + { + return $"{string.Join('.', KeyParts)} = {Value} (Severity: {Severity})"; + } +} public record GeneralEditorConfigSetting( string Key, - string Value) : IEditorConfigSetting; + string Value) : IEditorConfigSetting +{ + public string ToDisplayString() + { + return $"{Key} = {Value}"; + } +} public record RoslynOptionEditorConfigSetting( string Key, diff --git a/Sources/Kysect.Configuin.Core/Kysect.Configuin.Core.csproj b/Sources/Kysect.Configuin.Core/Kysect.Configuin.Core.csproj index 95e76c4..0a98f71 100644 --- a/Sources/Kysect.Configuin.Core/Kysect.Configuin.Core.csproj +++ b/Sources/Kysect.Configuin.Core/Kysect.Configuin.Core.csproj @@ -10,6 +10,7 @@ + diff --git a/Sources/Kysect.Configuin.Core/MsLearnDocumentation/MsLearnDocumentationParser.cs b/Sources/Kysect.Configuin.Core/MsLearnDocumentation/MsLearnDocumentationParser.cs index 8dec8c7..07a6707 100644 --- a/Sources/Kysect.Configuin.Core/MsLearnDocumentation/MsLearnDocumentationParser.cs +++ b/Sources/Kysect.Configuin.Core/MsLearnDocumentation/MsLearnDocumentationParser.cs @@ -8,24 +8,31 @@ using Kysect.Configuin.Core.RoslynRuleModels; using Markdig.Extensions.Tables; using Markdig.Syntax; +using Microsoft.Extensions.Logging; namespace Kysect.Configuin.Core.MsLearnDocumentation; public class MsLearnDocumentationParser : IMsLearnDocumentationParser { + private readonly ILogger _logger; + private readonly MarkdownTableParser _markdownTableParser; private readonly MsLearnTableParser _msLearnTableParser; private readonly IMarkdownTextExtractor _textExtractor; - public MsLearnDocumentationParser(IMarkdownTextExtractor textExtractor) + public MsLearnDocumentationParser(IMarkdownTextExtractor textExtractor, ILogger logger) { _textExtractor = textExtractor; + _logger = logger; + _markdownTableParser = new MarkdownTableParser(textExtractor); _msLearnTableParser = new MsLearnTableParser(); } public RoslynRules Parse(MsLearnDocumentationRawInfo rawInfo) { + _logger.LogInformation("Parsing roslyn rules from MS Learn"); + IReadOnlyCollection dotnetFormattingOptions = ParseAdditionalFormattingOptions(rawInfo.DotnetFormattingOptionsContent); IReadOnlyCollection sharpFormattingOptions = ParseAdditionalFormattingOptions(rawInfo.SharpFormattingOptionsContent); @@ -163,9 +170,13 @@ private string GetStyleOverviewText(IReadOnlyCollection mar { MarkdownHeadedBlock? overviewBlock = markdownHeadedBlocks.FirstOrDefault(h => h.HeaderText == "Overview"); if (overviewBlock is null) + { // TODO: Rule IDE0055 does not contains this block //throw new ConfiguinException("Style rule page does not contains Overview block."); + + _logger.LogWarning("Skip overview parsing for IDE0055"); return string.Empty; + } string overviewText = overviewBlock .Content diff --git a/Sources/Kysect.Configuin.Tests/CmdProcessTests.cs b/Sources/Kysect.Configuin.Tests/CmdProcessTests.cs index 5e54eca..5a3d6f8 100644 --- a/Sources/Kysect.Configuin.Tests/CmdProcessTests.cs +++ b/Sources/Kysect.Configuin.Tests/CmdProcessTests.cs @@ -1,17 +1,23 @@ using FluentAssertions; using Kysect.Configuin.Core.CliExecution; +using Kysect.Configuin.Tests.Tools; using NUnit.Framework; namespace Kysect.Configuin.Tests; public class CmdProcessTests { + private readonly CmdProcess _cmdProcess; + + public CmdProcessTests() + { + _cmdProcess = new CmdProcess(TestLogger.ProviderForTests()); + } + [Test] public void Execute_ForInvalidCommand_ThrowError() { - var cmdProcess = new CmdProcess(); - - CmdExecutionResult cmdExecutionResult = cmdProcess.ExecuteCommand("qwerasdf1234"); + CmdExecutionResult cmdExecutionResult = _cmdProcess.ExecuteCommand("qwerasdf1234"); cmdExecutionResult.ExitCode.Should().NotBe(0); } diff --git a/Sources/Kysect.Configuin.Tests/CodeStyleGeneration/CodeStyleGeneratorTests.cs b/Sources/Kysect.Configuin.Tests/CodeStyleGeneration/CodeStyleGeneratorTests.cs index d40ecdb..1428084 100644 --- a/Sources/Kysect.Configuin.Tests/CodeStyleGeneration/CodeStyleGeneratorTests.cs +++ b/Sources/Kysect.Configuin.Tests/CodeStyleGeneration/CodeStyleGeneratorTests.cs @@ -13,22 +13,27 @@ namespace Kysect.Configuin.Tests.CodeStyleGeneration; public class CodeStyleGeneratorTests { - private readonly MsLearnDocumentationParser _msLearnDocumentationParser = new(TestImplementations.GetTextExtractor()); - private readonly EditorConfigSettingsParser _editorConfigSettingsParser = new(); + private readonly MsLearnDocumentationParser _msLearnDocumentationParser = new MsLearnDocumentationParser(TestImplementations.GetTextExtractor(), TestLogger.ProviderForTests()); + private readonly EditorConfigSettingsParser _editorConfigSettingsParser = new EditorConfigSettingsParser(TestLogger.ProviderForTests()); private readonly MsLearnDocumentationInfoLocalProvider _repositoryPathProvider = TestImplementations.CreateDocumentationInfoLocalProvider(); + private readonly CodeStyleGenerator _sut; + + public CodeStyleGeneratorTests() + { + _sut = new CodeStyleGenerator(TestLogger.ProviderForTests()); + } [Test] public void Generate_ForAllMsLearnDocumentation_FinishWithoutErrors() { string pathToIniFile = Path.Combine("Resources", "Editor-config-sample.ini"); - var sut = new CodeStyleGenerator(); MsLearnDocumentationRawInfo msLearnDocumentationRawInfo = _repositoryPathProvider.Provide(); RoslynRules roslynRules = _msLearnDocumentationParser.Parse(msLearnDocumentationRawInfo); string fileText = File.ReadAllText(pathToIniFile); EditorConfigSettings editorConfigSettings = _editorConfigSettingsParser.Parse(fileText); - CodeStyle codeStyle = sut.Generate(editorConfigSettings, roslynRules); + CodeStyle codeStyle = _sut.Generate(editorConfigSettings, roslynRules); ICodeStyleElement codeStyleElement = codeStyle.Elements.ElementAt(2); codeStyleElement.Should().BeOfType(); diff --git a/Sources/Kysect.Configuin.Tests/CodeStyleGeneration/MarkdownCodeStyleFormatterTests.cs b/Sources/Kysect.Configuin.Tests/CodeStyleGeneration/MarkdownCodeStyleFormatterTests.cs index f727e36..418dfc9 100644 --- a/Sources/Kysect.Configuin.Tests/CodeStyleGeneration/MarkdownCodeStyleFormatterTests.cs +++ b/Sources/Kysect.Configuin.Tests/CodeStyleGeneration/MarkdownCodeStyleFormatterTests.cs @@ -3,13 +3,14 @@ using Kysect.Configuin.Core.CodeStyleGeneration.Models; using Kysect.Configuin.Core.RoslynRuleModels; using Kysect.Configuin.Tests.Resources; +using Kysect.Configuin.Tests.Tools; using NUnit.Framework; namespace Kysect.Configuin.Tests.CodeStyleGeneration; public class MarkdownCodeStyleFormatterTests { - private readonly MarkdownCodeStyleFormatter _formatter = new MarkdownCodeStyleFormatter(); + private readonly MarkdownCodeStyleFormatter _formatter = new MarkdownCodeStyleFormatter(TestLogger.ProviderForTests()); [Test] public void FormatStyleRule_ForIDE0040_ReturnExpected() diff --git a/Sources/Kysect.Configuin.Tests/DotnetFormat/DotnetFormatCliTests.cs b/Sources/Kysect.Configuin.Tests/DotnetFormat/DotnetFormatCliTests.cs index 5ef4184..5ba0781 100644 --- a/Sources/Kysect.Configuin.Tests/DotnetFormat/DotnetFormatCliTests.cs +++ b/Sources/Kysect.Configuin.Tests/DotnetFormat/DotnetFormatCliTests.cs @@ -1,4 +1,5 @@ using Kysect.Configuin.Core.DotnetFormat; +using Kysect.Configuin.Tests.Tools; using NUnit.Framework; namespace Kysect.Configuin.Tests.DotnetFormat; @@ -9,7 +10,7 @@ public class DotnetFormatCliTests public DotnetFormatCliTests() { - _dotnetFormatCli = new DotnetFormatCli(); + _dotnetFormatCli = new DotnetFormatCli(TestLogger.ProviderForTests()); } [Test] diff --git a/Sources/Kysect.Configuin.Tests/DotnetFormatReportComparatorTests.cs b/Sources/Kysect.Configuin.Tests/DotnetFormatReportComparatorTests.cs index 7e86997..db93b55 100644 --- a/Sources/Kysect.Configuin.Tests/DotnetFormatReportComparatorTests.cs +++ b/Sources/Kysect.Configuin.Tests/DotnetFormatReportComparatorTests.cs @@ -1,19 +1,27 @@ using FluentAssertions; using Kysect.CommonLib.Collections.Diff; using Kysect.Configuin.Core.DotnetFormat; +using Kysect.Configuin.Tests.Tools; using NUnit.Framework; namespace Kysect.Configuin.Tests; public class DotnetFormatReportComparatorTests { + private readonly DotnetFormatReportComparator _dotnetFormatReportComparator; + + public DotnetFormatReportComparatorTests() + { + _dotnetFormatReportComparator = new DotnetFormatReportComparator(TestLogger.ProviderForTests()); + } + [Test] public void Compare_ChangedFile_ReturnExpectedDiff() { string left = File.ReadAllText(Path.Combine("Resources", "DotnetFormatOutput-one-report.json")); string right = File.ReadAllText(Path.Combine("Resources", "DotnetFormatOutput-two-report.json")); - CollectionDiff diff = DotnetFormatReportComparator.Compare(left, right); + CollectionDiff diff = _dotnetFormatReportComparator.Compare(left, right); diff.Added.Should().HaveCount(1); diff.Same.Should().HaveCount(1); diff --git a/Sources/Kysect.Configuin.Tests/EditorConfigSettingsParsing/EditorConfigSettingsParserTests.cs b/Sources/Kysect.Configuin.Tests/EditorConfigSettingsParsing/EditorConfigSettingsParserTests.cs index ee1a5c1..948e087 100644 --- a/Sources/Kysect.Configuin.Tests/EditorConfigSettingsParsing/EditorConfigSettingsParserTests.cs +++ b/Sources/Kysect.Configuin.Tests/EditorConfigSettingsParsing/EditorConfigSettingsParserTests.cs @@ -2,13 +2,14 @@ using Kysect.Configuin.Core.EditorConfigParsing; using Kysect.Configuin.Core.EditorConfigParsing.Settings; using Kysect.Configuin.Core.RoslynRuleModels; +using Kysect.Configuin.Tests.Tools; using NUnit.Framework; namespace Kysect.Configuin.Tests.EditorConfigSettingsParsing; public class EditorConfigSettingsParserTests { - private readonly EditorConfigSettingsParser _parser = new EditorConfigSettingsParser(); + private readonly EditorConfigSettingsParser _parser = new EditorConfigSettingsParser(TestLogger.ProviderForTests()); [Test] public void Parse_TabWidth_ReturnGeneralEditorRule() diff --git a/Sources/Kysect.Configuin.Tests/Kysect.Configuin.Tests.csproj b/Sources/Kysect.Configuin.Tests/Kysect.Configuin.Tests.csproj index d2e7a58..2d71e3c 100644 --- a/Sources/Kysect.Configuin.Tests/Kysect.Configuin.Tests.csproj +++ b/Sources/Kysect.Configuin.Tests/Kysect.Configuin.Tests.csproj @@ -10,6 +10,7 @@ + @@ -21,6 +22,10 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + + + + diff --git a/Sources/Kysect.Configuin.Tests/MsLearnDocumentation/MsLearnDocumentationParserTests.cs b/Sources/Kysect.Configuin.Tests/MsLearnDocumentation/MsLearnDocumentationParserTests.cs index 4fe6a48..67f6695 100644 --- a/Sources/Kysect.Configuin.Tests/MsLearnDocumentation/MsLearnDocumentationParserTests.cs +++ b/Sources/Kysect.Configuin.Tests/MsLearnDocumentation/MsLearnDocumentationParserTests.cs @@ -12,7 +12,7 @@ public class MsLearnDocumentationParserTests { private static readonly MsLearnRepositoryPathProvider MsLearnRepositoryPathProvider = TestImplementations.CreateRepositoryPathProvider(); - private readonly MsLearnDocumentationParser _parser = new MsLearnDocumentationParser(TestImplementations.GetTextExtractor()); + private readonly MsLearnDocumentationParser _parser = new MsLearnDocumentationParser(TestImplementations.GetTextExtractor(), TestLogger.ProviderForTests()); [Test] public void ParseStyleRule_IDE0040_ReturnExpectedResult() diff --git a/Sources/Kysect.Configuin.Tests/Tools/TestLogger.cs b/Sources/Kysect.Configuin.Tests/Tools/TestLogger.cs new file mode 100644 index 0000000..c666189 --- /dev/null +++ b/Sources/Kysect.Configuin.Tests/Tools/TestLogger.cs @@ -0,0 +1,21 @@ +using Microsoft.Extensions.Logging; +using Serilog; +using ILogger = Microsoft.Extensions.Logging.ILogger; + +namespace Kysect.Configuin.Tests.Tools; + +public static class TestLogger +{ + public static ILogger ProviderForTests() + { + LoggerConfiguration loggerConfiguration = new LoggerConfiguration() + .MinimumLevel.Verbose() + .WriteTo.Console(); + + ILoggerFactory loggerFactory = new LoggerFactory() + .DemystifyExceptions() + .AddSerilog(loggerConfiguration.CreateLogger()); + + return loggerFactory.CreateLogger("Tests"); + } +} \ No newline at end of file From 64a65569758e71ab888ad2841e5562f30306a844 Mon Sep 17 00:00:00 2001 From: Fredi Kats Date: Thu, 7 Sep 2023 21:19:02 +0400 Subject: [PATCH 2/2] Remove editorconfig from tests --- Sources/Kysect.Configuin.Tests/Kysect.Configuin.Tests.csproj | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/Kysect.Configuin.Tests/Kysect.Configuin.Tests.csproj b/Sources/Kysect.Configuin.Tests/Kysect.Configuin.Tests.csproj index 2d71e3c..03019d0 100644 --- a/Sources/Kysect.Configuin.Tests/Kysect.Configuin.Tests.csproj +++ b/Sources/Kysect.Configuin.Tests/Kysect.Configuin.Tests.csproj @@ -13,7 +13,6 @@ -