Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
georgii-borovinskikh-sonarsource committed Dec 18, 2024
1 parent 6a6c00f commit cf67fae
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -94,7 +92,7 @@ public void Get_FailsToRetrieveProjectItem_NonCriticalException_ExceptionCaughtA
{
dte.Solution.ThrowsForAnyArgs<NotImplementedException>();

var result = testSubject.Get(SourceFilePath, new CFamilyAnalyzerOptions());
var result = testSubject.Get(SourceFilePath);

result.Should().BeNull();
logger.AssertPartialOutputStringExists(nameof(NotImplementedException), SourceFilePath);
Expand All @@ -105,7 +103,7 @@ public void Get_FailsToRetrieveProjectItem_CriticalException_ExceptionThrown()
{
dte.Solution.ThrowsForAnyArgs<DivideByZeroException>();

Action act = () => testSubject.Get(SourceFilePath, new CFamilyAnalyzerOptions());
Action act = () => testSubject.Get(SourceFilePath);

act.Should().Throw<DivideByZeroException>();
}
Expand All @@ -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(() =>
Expand All @@ -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(() =>
Expand All @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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<ICompilationDatabaseHandle>();
storage.CreateDatabase(CDFile, CDDirectory, CDCommand, Arg.Any<IEnumerable<string>>()).Returns(compilationDatabaseHandle);
var testSubject = new VCXCompilationDatabaseProvider(
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
96 changes: 36 additions & 60 deletions src/Integration.Vsix/CFamily/VcxProject/FileConfigProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SDTE, DTE2, IFileConfig>(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<SDTE, DTE2, IFileConfig>(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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit cf67fae

Please sign in to comment.