From cf67fae631566156badb663072d63cdd45faf4ec Mon Sep 17 00:00:00 2001 From: Georgii Borovinskikh Date: Wed, 18 Dec 2024 15:14:52 +0100 Subject: [PATCH] cleanup --- .../VcxProject/FileConfigProviderTests.cs | 12 +-- .../VCXCompilationDatabaseProviderTests.cs | 16 ++-- .../CFamily/VcxProject/FileConfigProvider.cs | 96 +++++++------------ .../VCXCompilationDatabaseProvider.cs | 2 +- 4 files changed, 50 insertions(+), 76 deletions(-) diff --git a/src/Integration.Vsix.UnitTests/CFamily/VcxProject/FileConfigProviderTests.cs b/src/Integration.Vsix.UnitTests/CFamily/VcxProject/FileConfigProviderTests.cs index 504f458a1..31cce75a7 100644 --- a/src/Integration.Vsix.UnitTests/CFamily/VcxProject/FileConfigProviderTests.cs +++ b/src/Integration.Vsix.UnitTests/CFamily/VcxProject/FileConfigProviderTests.cs @@ -24,11 +24,9 @@ using NSubstitute.ExceptionExtensions; using NSubstitute.ReturnsExtensions; using SonarLint.VisualStudio.Core; -using SonarLint.VisualStudio.CFamily.Analysis; using SonarLint.VisualStudio.Infrastructure.VS; using SonarLint.VisualStudio.Integration.Vsix.CFamily.VcxProject; using static SonarLint.VisualStudio.Integration.Vsix.CFamily.UnitTests.CFamilyTestUtility; -using System.IO.Abstractions; using SonarLint.VisualStudio.Core.SystemAbstractions; namespace SonarLint.VisualStudio.Integration.UnitTests.CFamily.VcxProject @@ -94,7 +92,7 @@ public void Get_FailsToRetrieveProjectItem_NonCriticalException_ExceptionCaughtA { dte.Solution.ThrowsForAnyArgs(); - var result = testSubject.Get(SourceFilePath, new CFamilyAnalyzerOptions()); + var result = testSubject.Get(SourceFilePath); result.Should().BeNull(); logger.AssertPartialOutputStringExists(nameof(NotImplementedException), SourceFilePath); @@ -105,7 +103,7 @@ public void Get_FailsToRetrieveProjectItem_CriticalException_ExceptionThrown() { dte.Solution.ThrowsForAnyArgs(); - Action act = () => testSubject.Get(SourceFilePath, new CFamilyAnalyzerOptions()); + Action act = () => testSubject.Get(SourceFilePath); act.Should().Throw(); } @@ -115,7 +113,7 @@ public void Get_NoProjectItem_ReturnsNull() { dte.Solution.FindProjectItem(SourceFilePath).ReturnsNull(); - testSubject.Get(SourceFilePath, null) + testSubject.Get(SourceFilePath) .Should().BeNull(); Received.InOrder(() => @@ -132,7 +130,7 @@ public void Get_ProjectItemNotInSolution_ReturnsNull() dte.Solution.FindProjectItem(SourceFilePath).Returns(mockProjectItem.Object); fileInSolutionIndicator.IsFileInSolution(mockProjectItem.Object).Returns(false); - testSubject.Get(SourceFilePath, null) + testSubject.Get(SourceFilePath) .Should().BeNull(); Received.InOrder(() => @@ -149,7 +147,7 @@ public void Get_SuccessfulConfig_ConfigReturned() var mockProjectItem = CreateMockProjectItem(SourceFilePath); dte.Solution.FindProjectItem(SourceFilePath).Returns(mockProjectItem.Object); - testSubject.Get(SourceFilePath, null) + testSubject.Get(SourceFilePath) .Should().NotBeNull(); } } diff --git a/src/Integration.Vsix.UnitTests/CFamily/VcxProject/VCXCompilationDatabaseProviderTests.cs b/src/Integration.Vsix.UnitTests/CFamily/VcxProject/VCXCompilationDatabaseProviderTests.cs index 025ddb6d1..041d27dca 100644 --- a/src/Integration.Vsix.UnitTests/CFamily/VcxProject/VCXCompilationDatabaseProviderTests.cs +++ b/src/Integration.Vsix.UnitTests/CFamily/VcxProject/VCXCompilationDatabaseProviderTests.cs @@ -66,7 +66,7 @@ public void MefCtor_CheckIsExported() [TestMethod] public void CreateOrNull_NoFileConfig_ReturnsNull() { - fileConfigProvider.Get(SourceFilePath, default).ReturnsNull(); + fileConfigProvider.Get(SourceFilePath).ReturnsNull(); var testSubject = new VCXCompilationDatabaseProvider( storage, envVarProvider, @@ -82,7 +82,7 @@ public void CreateOrNull_NoFileConfig_ReturnsNull() public void CreateOrNull_FileConfig_CantStore_ReturnsNull() { var fileConfig = GetFileConfig(); - fileConfigProvider.Get(SourceFilePath, default).Returns(fileConfig); + fileConfigProvider.Get(SourceFilePath).Returns(fileConfig); storage.CreateDatabase(default, default, default, default).ReturnsNullForAnyArgs(); var testSubject = new VCXCompilationDatabaseProvider( storage, @@ -99,7 +99,7 @@ public void CreateOrNull_FileConfig_CantStore_ReturnsNull() public void CreateOrNull_FileConfig_StoresAndReturnsHandle() { var fileConfig = GetFileConfig(); - fileConfigProvider.Get(SourceFilePath, default).Returns(fileConfig); + fileConfigProvider.Get(SourceFilePath).Returns(fileConfig); var compilationDatabaseHandle = Substitute.For(); storage.CreateDatabase(CDFile, CDDirectory, CDCommand, Arg.Any>()).Returns(compilationDatabaseHandle); var testSubject = new VCXCompilationDatabaseProvider( @@ -115,7 +115,7 @@ public void CreateOrNull_FileConfig_StoresAndReturnsHandle() public void CreateOrNull_NoEnvIncludeInFileConfig_UsesStatic() { var fileConfig = GetFileConfig(null); - fileConfigProvider.Get(SourceFilePath, default).Returns(fileConfig); + fileConfigProvider.Get(SourceFilePath).Returns(fileConfig); envVarProvider.GetAll().Returns([("Var1", "Value1"), ("INCLUDE", "static"), ("Var2", "Value2")]); var testSubject = new VCXCompilationDatabaseProvider( storage, @@ -132,7 +132,7 @@ public void CreateOrNull_NoEnvIncludeInFileConfig_UsesStatic() public void CreateOrNull_FileConfigHasEnvInclude_UsesDynamic() { var fileConfig = GetFileConfig(EnvInclude); - fileConfigProvider.Get(SourceFilePath, default).Returns(fileConfig); + fileConfigProvider.Get(SourceFilePath).Returns(fileConfig); envVarProvider.GetAll().Returns([("Var1", "Value1"), ("INCLUDE", "static"), ("Var2", "Value2")]); var testSubject = new VCXCompilationDatabaseProvider( storage, @@ -150,7 +150,7 @@ public void CreateOrNull_FileConfigHasEnvInclude_UsesDynamic() public void CreateOrNull_NoStaticInclude_UsesDynamic() { var fileConfig = GetFileConfig(EnvInclude); - fileConfigProvider.Get(SourceFilePath, default).Returns(fileConfig); + fileConfigProvider.Get(SourceFilePath).Returns(fileConfig); envVarProvider.GetAll().Returns([("Var1", "Value1"), ("Var2", "Value2")]); var testSubject = new VCXCompilationDatabaseProvider( storage, @@ -168,7 +168,7 @@ public void CreateOrNull_NoStaticInclude_UsesDynamic() public void CreateOrNull_StaticEnvVarsAreCached() { var fileConfig = GetFileConfig(); - fileConfigProvider.Get(SourceFilePath, default).Returns(fileConfig); + fileConfigProvider.Get(SourceFilePath).Returns(fileConfig); envVarProvider.GetAll().Returns([("Var1", "Value1"), ("Var2", "Value2")]); var testSubject = new VCXCompilationDatabaseProvider( storage, @@ -187,7 +187,7 @@ public void CreateOrNull_StaticEnvVarsAreCached() public void CreateOrNull_EnvVarsContainHeaderPropertyForHeaderFiles() { var fileConfig = GetFileConfig(EnvInclude, true); - fileConfigProvider.Get(SourceFilePath, default).Returns(fileConfig); + fileConfigProvider.Get(SourceFilePath).Returns(fileConfig); envVarProvider.GetAll().Returns([("Var1", "Value1"), ("Var2", "Value2")]); var testSubject = new VCXCompilationDatabaseProvider( storage, diff --git a/src/Integration.Vsix/CFamily/VcxProject/FileConfigProvider.cs b/src/Integration.Vsix/CFamily/VcxProject/FileConfigProvider.cs index c53dcc848..eeaafa353 100644 --- a/src/Integration.Vsix/CFamily/VcxProject/FileConfigProvider.cs +++ b/src/Integration.Vsix/CFamily/VcxProject/FileConfigProvider.cs @@ -18,88 +18,64 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -using System; using System.ComponentModel.Composition; -using EnvDTE; using EnvDTE80; using Microsoft.VisualStudio.Shell.Interop; using SonarLint.VisualStudio.Core; -using SonarLint.VisualStudio.CFamily.Analysis; using SonarLint.VisualStudio.Infrastructure.VS; -using SonarLint.VisualStudio.Integration.Helpers; -using System.IO.Abstractions; using SonarLint.VisualStudio.Core.SystemAbstractions; -namespace SonarLint.VisualStudio.Integration.Vsix.CFamily.VcxProject +namespace SonarLint.VisualStudio.Integration.Vsix.CFamily.VcxProject; + +internal interface IFileConfigProvider +{ + IFileConfig Get(string analyzedFilePath); +} + +[Export(typeof(IFileConfigProvider))] +[PartCreationPolicy(CreationPolicy.Shared)] +[method: ImportingConstructor] +internal class FileConfigProvider( + IVsUIServiceOperation uiServiceOperation, + IFileInSolutionIndicator fileInSolutionIndicator, + IFileSystemService fileSystem, + ILogger logger, + IThreadHandling threadHandling) : IFileConfigProvider { - internal interface IFileConfigProvider + + public IFileConfig Get(string analyzedFilePath) { - IFileConfig Get(string analyzedFilePath, CFamilyAnalyzerOptions analyzerOptions); + return uiServiceOperation.Execute(dte => + GetInternal(analyzedFilePath, dte)); } - [Export(typeof(IFileConfigProvider))] - [PartCreationPolicy(CreationPolicy.Shared)] - [method: ImportingConstructor] - internal class FileConfigProvider( - IVsUIServiceOperation uiServiceOperation, - IFileInSolutionIndicator fileInSolutionIndicator, - IFileSystemService fileSystem, - ILogger logger, - IThreadHandling threadHandling) : IFileConfigProvider + private FileConfig GetInternal(string analyzedFilePath, DTE2 dte) { + threadHandling.ThrowIfNotOnUIThread(); - public IFileConfig Get(string analyzedFilePath, CFamilyAnalyzerOptions analyzerOptions) + try { - return uiServiceOperation.Execute(dte => - GetInternal(analyzedFilePath, dte)); - } + var projectItem = dte.Solution.FindProjectItem(analyzedFilePath); - private FileConfig GetInternal(string analyzedFilePath, DTE2 dte) - { - threadHandling.ThrowIfNotOnUIThread(); - - try + if (projectItem == null) { - var projectItem = dte.Solution.FindProjectItem(analyzedFilePath); - - if (projectItem == null) - { - return null; - } - - if (!fileInSolutionIndicator.IsFileInSolution(projectItem)) - { - logger.LogVerbose($"[VCX:FileConfigProvider] The file is not part of a VCX project. File: {analyzedFilePath}"); - return null; - } - // Note: if the C++ tools are not installed then it's likely an exception will be thrown when - // the framework tries to JIT-compile the TryGet method (since it won't be able to find the MS.VS.VCProjectEngine - // types). - return FileConfig.TryGet(logger, projectItem, analyzedFilePath, fileSystem); + return null; } - catch (Exception ex) when (!Microsoft.VisualStudio.ErrorHandler.IsCriticalException(ex)) + + if (!fileInSolutionIndicator.IsFileInSolution(projectItem)) { - logger.WriteLine(CFamilyStrings.ERROR_CreatingConfig, analyzedFilePath, ex); + logger.LogVerbose($"[VCX:FileConfigProvider] The file is not part of a VCX project. File: {analyzedFilePath}"); return null; } + // Note: if the C++ tools are not installed then it's likely an exception will be thrown when + // the framework tries to JIT-compile the TryGet method (since it won't be able to find the MS.VS.VCProjectEngine + // types). + return FileConfig.TryGet(logger, projectItem, analyzedFilePath, fileSystem); } - - private class NoOpLogger : ILogger + catch (Exception ex) when (!Microsoft.VisualStudio.ErrorHandler.IsCriticalException(ex)) { - public void WriteLine(string message) - { - // no-op - } - - public void WriteLine(string messageFormat, params object[] args) - { - // no-op - } - - public void LogVerbose(string message, params object[] args) - { - // no-op - } + logger.WriteLine(CFamilyStrings.ERROR_CreatingConfig, analyzedFilePath, ex); + return null; } } } diff --git a/src/Integration.Vsix/CFamily/VcxProject/VCXCompilationDatabaseProvider.cs b/src/Integration.Vsix/CFamily/VcxProject/VCXCompilationDatabaseProvider.cs index b4ff05f13..f0011aade 100644 --- a/src/Integration.Vsix/CFamily/VcxProject/VCXCompilationDatabaseProvider.cs +++ b/src/Integration.Vsix/CFamily/VcxProject/VCXCompilationDatabaseProvider.cs @@ -51,7 +51,7 @@ public VCXCompilationDatabaseProvider( } public ICompilationDatabaseHandle CreateOrNull(string filePath) => - fileConfigProvider.Get(filePath, null) is { } fileConfig + fileConfigProvider.Get(filePath) is { } fileConfig ? storage.CreateDatabase(fileConfig.CDFile, fileConfig.CDDirectory, fileConfig.CDCommand, GetEnvironmentEntries(fileConfig).Select(x => x.FormattedEntry)) : null;