Skip to content

Commit

Permalink
add some
Browse files Browse the repository at this point in the history
  • Loading branch information
georgii-borovinskikh-sonarsource committed Dec 23, 2024
1 parent 9a545e1 commit 1d9ff1a
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 164 deletions.
4 changes: 2 additions & 2 deletions src/Core/Logging/LogContextManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace SonarLint.VisualStudio.Core.Logging;
public class LogContextManager : ILogContextManager
{
private readonly ImmutableList<string> contexts;
private readonly ImmutableList<string> verboseContexts;
private readonly Lazy<string> formatedContext;
private readonly Lazy<string> formatedVerboseContext;
private readonly ImmutableList<string> verboseContexts;

[ImportingConstructor]
public LogContextManager() : this(ImmutableList<string>.Empty, ImmutableList<string>.Empty) { }
Expand All @@ -31,4 +31,4 @@ private LogContextManager(ImmutableList<string> contexts, ImmutableList<string>
public ILogContextManager CreateAugmentedVerboseContext(IEnumerable<string> additionalVerboseContexts) => new LogContextManager(contexts, verboseContexts.AddRange(additionalVerboseContexts));

private static string MergeContextsIntoSingleProperty(ImmutableList<string> contexts) => contexts.Count > 0 ? string.Join(" > ", contexts) : null;
}
}
212 changes: 51 additions & 161 deletions src/Integration.UnitTests/Helpers/SonarLintOutputTests.cs
Original file line number Diff line number Diff line change
@@ -1,161 +1,51 @@
// /*
// * SonarLint for Visual Studio
// * Copyright (C) 2016-2024 SonarSource SA
// * mailto:info AT sonarsource DOT com
// *
// * This program is free software; you can redistribute it and/or
// * modify it under the terms of the GNU Lesser General Public
// * License as published by the Free Software Foundation; either
// * version 3 of the License, or (at your option) any later version.
// *
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// * Lesser General Public License for more details.
// *
// * You should have received a copy of the GNU Lesser General Public License
// * along with this program; if not, write to the Free Software Foundation,
// * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// */
//
// using System;
// using Microsoft.VisualStudio.Shell;
// using Microsoft.VisualStudio.Shell.Interop;
// using Microsoft.VisualStudio.TestTools.UnitTesting;
// using Moq;
// using SonarLint.VisualStudio.Core;
// using SonarLint.VisualStudio.Integration.Helpers;
// using SonarLint.VisualStudio.TestInfrastructure;
//
// namespace SonarLint.VisualStudio.Integration.UnitTests.Helpers
// {
// [TestClass]
// public class SonarLintOutputTests
// {
// [TestMethod]
// public void MefCtor_CheckIsExported()
// {
// MefTestHelpers.CheckTypeCanBeImported<SonarLintOutputLogger, ILogger>(
// MefTestHelpers.CreateExport<SVsServiceProvider>(),
// MefTestHelpers.CreateExport<ISonarLintSettings>());
// }
//
// [TestMethod]
// public void Write_OutputsToWindow()
// {
// // Arrange
// var windowMock = new ConfigurableVsOutputWindow();
// var sonarLintSettings = CreateSonarLintSettings(DaemonLogLevel.Info);
// var serviceProviderMock = CreateConfiguredServiceProvider(windowMock);
//
// var testSubject = CreateTestSubject(serviceProviderMock, sonarLintSettings);
//
// // Act
// testSubject.WriteLine("123");
// testSubject.WriteLine("abc");
//
// // Assert
// var outputPane = windowMock.AssertPaneExists(VsShellUtils.SonarLintOutputPaneGuid);
// outputPane.AssertOutputStrings("123", "abc");
// }
//
// [TestMethod]
// [DataRow(DaemonLogLevel.Info)]
// [DataRow(DaemonLogLevel.Minimal)]
// [DataRow(DaemonLogLevel.Verbose)]
// public void LogVerbose_OnlyOutputsToWindowIfLogLevelIsVerbose(DaemonLogLevel logLevel)
// {
// // Arrange
// var windowMock = new ConfigurableVsOutputWindow();
// var serviceProviderMock = CreateConfiguredServiceProvider(windowMock);
//
// var sonarLintSettings = CreateSonarLintSettings(logLevel);
//
// var testSubject = CreateTestSubject(serviceProviderMock, sonarLintSettings);
//
// testSubject.WriteLine("create window pane");
// var outputPane = windowMock.AssertPaneExists(VsShellUtils.SonarLintOutputPaneGuid);
// outputPane.Reset();
//
// // Act
// testSubject.LogVerbose("123 {0} {1}", "param 1", 2);
// testSubject.LogVerbose("{0} {1} abc", 1, "param 2");
//
// // Assert
// if (logLevel == DaemonLogLevel.Verbose)
// {
// var currentThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId;
//
// outputPane.AssertOutputStrings(
// $"[ThreadId {currentThreadId}] [DEBUG] 123 param 1 2",
// $"[ThreadId {currentThreadId}] [DEBUG] 1 param 2 abc");
// outputPane.AssertOutputStrings(2);
// }
// else
// {
// outputPane.AssertOutputStrings(0);
// }
// }
//
// [TestMethod]
// [DataRow(DaemonLogLevel.Info)]
// [DataRow(DaemonLogLevel.Minimal)]
// [DataRow(DaemonLogLevel.Verbose)]
// public void WriteLine_ThreadIdIsAddedIfLogLevelIsVerbose(DaemonLogLevel logLevel)
// {
// // Arrange
// var windowMock = new ConfigurableVsOutputWindow();
// var serviceProviderMock = CreateConfiguredServiceProvider(windowMock);
//
// var sonarLintSettings = CreateSonarLintSettings(logLevel);
//
// var testSubject = CreateTestSubject(serviceProviderMock, sonarLintSettings);
//
// testSubject.WriteLine("create window pane");
// var outputPane = windowMock.AssertPaneExists(VsShellUtils.SonarLintOutputPaneGuid);
// outputPane.Reset();
//
// // Act
// testSubject.WriteLine("writeline, no params");
// testSubject.WriteLine("writeline, with params: {0}", "zzz");
//
// outputPane.AssertOutputStrings(2);
//
// string expectedPrefix;
// if (logLevel == DaemonLogLevel.Verbose)
// {
// var currentThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId;
// expectedPrefix = $"[ThreadId {currentThreadId}] ";
// }
// else
// {
// expectedPrefix = string.Empty;
// }
//
// outputPane.AssertOutputStrings(
// $"{expectedPrefix}writeline, no params",
// $"{expectedPrefix}writeline, with params: zzz");
// }
//
// private static IServiceProvider CreateConfiguredServiceProvider(IVsOutputWindow outputWindow)
// {
// var serviceProvider = new ConfigurableServiceProvider(assertOnUnexpectedServiceRequest: true);
// serviceProvider.RegisterService(typeof(SVsOutputWindow), outputWindow);
// return serviceProvider;
// }
//
// private static ISonarLintSettings CreateSonarLintSettings(DaemonLogLevel logLevel)
// {
// var sonarLintSettings = new Mock<ISonarLintSettings>();
// sonarLintSettings.Setup(x => x.DaemonLogLevel).Returns(logLevel);
// return sonarLintSettings.Object;
// }
//
// private static SonarLintOutputLogger CreateTestSubject(IServiceProvider serviceProvider,
// ISonarLintSettings sonarLintSettings = null)
// {
// sonarLintSettings ??= Mock.Of<ISonarLintSettings>();
// return new SonarLintOutputLogger(serviceProvider, sonarLintSettings, Substitute.For<ILogContextManager>());
// }
// }
// }
/*
* SonarLint for Visual Studio
* Copyright (C) 2016-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using SonarLint.VisualStudio.Core.Logging;
using SonarLint.VisualStudio.Integration.Helpers;

namespace SonarLint.VisualStudio.Integration.UnitTests.Helpers
{
[TestClass]
public class SonarLintOutputTests
{
// the normal check for export does not work here because this is a special Property export instead of the normal Class export
// [TestMethod]
// public void MefCtor_CheckIsExported()
// {
// MefTestHelpers.CheckTypeCanBeImported<SonarLintOutputLoggerFactory, ILogger>(
// MefTestHelpers.CreateExport<ILoggerFactory>(),
// MefTestHelpers.CreateExport<SVsServiceProvider>(),
// MefTestHelpers.CreateExport<ISonarLintSettings>());
// }

[TestMethod]
public void Ctor_CreatesLoggerWithExpectedParameters()
{
var loggerFactory = Substitute.For<ILoggerFactory>();

var testSubject = new SonarLintOutputLoggerFactory(loggerFactory, Substitute.For<IServiceProvider>(), Substitute.For<ISonarLintSettings>());

testSubject.Instance.Should().NotBeNull();
loggerFactory.Create(Arg.Any<SonarLintOutputWindowLogWriter>(), Arg.Any<SonarLintSettingsLogVerbosityIndicator>());
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* SonarLint for Visual Studio
* Copyright (C) 2016-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using Microsoft.VisualStudio.Shell.Interop;
using SonarLint.VisualStudio.Integration.Helpers;
using SonarLint.VisualStudio.TestInfrastructure;

namespace SonarLint.VisualStudio.Integration.UnitTests.Helpers;

[TestClass]
public class SonarLintOutputWindowLogWriterTests
{
private ConfigurableVsOutputWindow windowMock;
private IServiceProvider serviceProviderMock;
private SonarLintOutputWindowLogWriter testSubject;

[TestInitialize]
public void TestInitialize()
{
windowMock = new ConfigurableVsOutputWindow();
serviceProviderMock = CreateConfiguredServiceProvider(windowMock);
testSubject = new SonarLintOutputWindowLogWriter(serviceProviderMock);
}

[TestMethod]
public void WriteLine_Empty_PutsEmptyLineToCorrectPane()
{
var message = string.Empty;

testSubject.WriteLine(message);

var outputPane = windowMock.AssertPaneExists(VsShellUtils.SonarLintOutputPaneGuid);
outputPane.AssertOutputStrings(message);
}

[TestMethod]
public void WriteLine_Simple_PutsSingleLineToCorrectPane()
{
var message = "ABOBA";

testSubject.WriteLine(message);

var outputPane = windowMock.AssertPaneExists(VsShellUtils.SonarLintOutputPaneGuid);
outputPane.AssertOutputStrings(message);
}

[TestMethod]
public void WriteLine_Multiline_PutsMultiLineToCorrectPane()
{
var message =
"""
A
B
OBA
""";

testSubject.WriteLine(message);

var outputPane = windowMock.AssertPaneExists(VsShellUtils.SonarLintOutputPaneGuid);
outputPane.AssertOutputStrings(message);
}

private static IServiceProvider CreateConfiguredServiceProvider(IVsOutputWindow outputWindow)
{
var serviceProvider = new ConfigurableServiceProvider(assertOnUnexpectedServiceRequest: true);
serviceProvider.RegisterService(typeof(SVsOutputWindow), outputWindow);
return serviceProvider;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* SonarLint for Visual Studio
* Copyright (C) 2016-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using SonarLint.VisualStudio.Integration.Helpers;

namespace SonarLint.VisualStudio.Integration.UnitTests.Helpers;

[TestClass]
public class SonarLintSettingsLogVerbosityIndicatorTests
{
private ISonarLintSettings sonarLintSettings;
private SonarLintSettingsLogVerbosityIndicator testSubject;

[TestInitialize]
public void TestInitialize()
{
sonarLintSettings = Substitute.For<ISonarLintSettings>();
testSubject = new SonarLintSettingsLogVerbosityIndicator(sonarLintSettings);
}

[DataRow(DaemonLogLevel.Verbose, true)]
[DataRow(DaemonLogLevel.Info, false)]
[DataRow(DaemonLogLevel.Minimal, false)]
[DataTestMethod]
public void IsVerboseEnabled_IsThreadIdEnabled_ReturnsFor(DaemonLogLevel logLevel, bool isVerbose)
{
sonarLintSettings.DaemonLogLevel.Returns(logLevel);

testSubject.IsVerboseEnabled.Should().Be(isVerbose);
testSubject.IsThreadIdEnabled.Should().Be(isVerbose);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal class SonarLintSettingsLogVerbosityIndicator(ISonarLintSettings sonarLi

[PartCreationPolicy(CreationPolicy.Shared)]
[method: ImportingConstructor]
internal class SonarLintOutputLogger(
internal class SonarLintOutputLoggerFactory(
ILoggerFactory logFactory,
[Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider,
ISonarLintSettings sonarLintSettings)
Expand Down

0 comments on commit 1d9ff1a

Please sign in to comment.