Skip to content

Commit

Permalink
SLVS-1663 Implement VCX analysis of C/C++ files with SLCore (#5858)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgii-borovinskikh-sonarsource committed Dec 9, 2024
1 parent b1ddcbd commit 249a3ef
Show file tree
Hide file tree
Showing 26 changed files with 613 additions and 1,594 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,19 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using System.IO;
using System.IO.Abstractions;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SonarLint.VisualStudio.CFamily.CMake;
using SonarLint.VisualStudio.Core;
using SonarLint.VisualStudio.Core.CFamily;
using SonarLint.VisualStudio.Core.SystemAbstractions;
using SonarLint.VisualStudio.TestInfrastructure;
using static SonarLint.VisualStudio.TestInfrastructure.Extensions.FileSystemExtensions;

namespace SonarLint.VisualStudio.CFamily.UnitTests.CMake
{
[TestClass]
public class CompilationDatabaseLocatorTests
public class CMakeCompilationDatabaseLocatorTests
{
private const string RootDirectory = "dummy root";

Expand All @@ -43,8 +40,9 @@ public class CompilationDatabaseLocatorTests
[TestMethod]
public void MefCtor_CheckIsExported()
{
MefTestHelpers.CheckTypeCanBeImported<CompilationDatabaseLocator, ICompilationDatabaseLocator>(
MefTestHelpers.CheckTypeCanBeImported<CMakeCompilationDatabaseLocator, ICMakeCompilationDatabaseLocator>(
MefTestHelpers.CreateExport<IFolderWorkspaceService>(),
MefTestHelpers.CreateExport<IFileSystemService>(),
MefTestHelpers.CreateExport<ILogger>());
}

Expand Down Expand Up @@ -72,10 +70,9 @@ public void Locate_NoCMakeSettings_ReturnsDefaultLocationIfFileExists(bool fileE

var defaultLocation = GetDefaultDatabaseFileLocation(activeConfiguration);

var fileSystem = new Mock<IFileSystem>();
fileSystem.SetFileExists(defaultLocation, fileExists);
var fileSystem = Substitute.For<IFileSystemService>().SetFileExists(defaultLocation, fileExists);

var testSubject = CreateTestSubject(RootDirectory, configProvider, cmakeSettingsProvider.Object, fileSystem.Object);
var testSubject = CreateTestSubject(RootDirectory, configProvider, cmakeSettingsProvider.Object, fileSystem);

var result = testSubject.Locate();

Expand Down Expand Up @@ -132,16 +129,15 @@ public void Locate_HasCMakeSettingsFile_ReturnsConfiguredPathIfItExists(bool fil
{
var configProvider = CreateConfigProvider("my-config");
var cmakeSettings = CreateCMakeSettings("my-config", "folder");
var cmakeSettingsProvider = CreateCMakeSettingsProvider(RootDirectory,
var cmakeSettingsProvider = CreateCMakeSettingsProvider(RootDirectory,
new CMakeSettingsSearchResult(cmakeSettings, "", ""));

var compilationDatabaseFullLocation = Path.GetFullPath(
Path.Combine("folder", CompilationDatabaseLocator.CompilationDatabaseFileName));
Path.Combine("folder", CMakeCompilationDatabaseLocator.CompilationDatabaseFileName));

var fileSystem = new Mock<IFileSystem>();
fileSystem.SetFileExists(compilationDatabaseFullLocation, fileExists);
var fileSystem = Substitute.For<IFileSystemService>().SetFileExists(compilationDatabaseFullLocation, fileExists);

var testSubject = CreateTestSubject(RootDirectory, configProvider, cmakeSettingsProvider.Object, fileSystem.Object,
var testSubject = CreateTestSubject(RootDirectory, configProvider, cmakeSettingsProvider.Object, fileSystem,
macroEvaluationService: PassthroughMacroService);

var result = testSubject.Locate();
Expand Down Expand Up @@ -199,8 +195,8 @@ public void Locate_MacroServiceIsCalled_RelativePath_ExpectedValueIsReturn(strin
context.MacroEvalService.Invocations.Count.Should().Be(1);
}

private static CMakeSettings CreateCMakeSettings(string activeConfigurationName,
string buildRoot,
private static CMakeSettings CreateCMakeSettings(string activeConfigurationName,
string buildRoot,
string generator = "generator") =>
new()
{
Expand All @@ -217,15 +213,15 @@ private static CMakeSettings CreateCMakeSettings(string activeConfigurationName,

private static string GetDefaultDatabaseFileLocation(string activeBuildConfiguration) =>
Path.GetFullPath(Path.Combine(
string.Format(CompilationDatabaseLocator.DefaultLocationFormat,
string.Format(CMakeCompilationDatabaseLocator.DefaultLocationFormat,
RootDirectory,
activeBuildConfiguration),
CompilationDatabaseLocator.CompilationDatabaseFileName));
CMakeCompilationDatabaseLocator.CompilationDatabaseFileName));

private static CompilationDatabaseLocator CreateTestSubject(string rootDirectory,
private static CMakeCompilationDatabaseLocator CreateTestSubject(string rootDirectory,
IBuildConfigProvider buildConfigProvider = null,
ICMakeSettingsProvider cMakeSettingsProvider = null,
IFileSystem fileSystem = null,
IFileSystemService fileSystem = null,
ILogger logger = null,
IMacroEvaluationService macroEvaluationService = null)
{
Expand All @@ -235,17 +231,17 @@ private static CompilationDatabaseLocator CreateTestSubject(string rootDirectory
cMakeSettingsProvider ??= Mock.Of<ICMakeSettingsProvider>();
buildConfigProvider ??= Mock.Of<IBuildConfigProvider>();
logger ??= Mock.Of<ILogger>();
fileSystem ??= new FileSystem();
fileSystem ??= new FileSystemService();
macroEvaluationService ??= Mock.Of<IMacroEvaluationService>();

return new CompilationDatabaseLocator(folderWorkspaceService.Object, buildConfigProvider, cMakeSettingsProvider, macroEvaluationService, fileSystem, logger);
return new CMakeCompilationDatabaseLocator(folderWorkspaceService.Object, buildConfigProvider, cMakeSettingsProvider, macroEvaluationService, fileSystem, logger);
}

private static IBuildConfigProvider CreateConfigProvider(string activeConfiguration)
{
var provider = new Mock<IBuildConfigProvider>();
provider.Setup(x => x.GetActiveConfig(It.IsAny<string>())).Returns(activeConfiguration);

return provider.Object;
}

Expand Down Expand Up @@ -288,10 +284,9 @@ public MacroEvalContext(string macroServiceReturnValue, string unevaluatedBuildR
new CMakeSettingsSearchResult(cmakeSettings, cmakeSettingsFilePath, cmakeListsFilePath));

// Treat all files as existing
var fileSystem = new Mock<IFileSystem>();
Func<string, bool> nonNullFilesExist = x => x != null;
fileSystem.Setup(x => x.File.Exists(It.IsAny<string>())).Returns(nonNullFilesExist);

var fileSystem = Substitute.For<IFileSystemService>();
fileSystem.File.Exists(Arg.Any<string>()).Returns(call => call.Arg<string>() != null);

MacroEvalService = new Mock<IMacroEvaluationService>();
MacroEvalService.Setup(x =>
x.Evaluate(unevaluatedBuildRoot,
Expand All @@ -307,10 +302,10 @@ public MacroEvalContext(string macroServiceReturnValue, string unevaluatedBuildR
Logger = new TestLogger(logToConsole: true);

TestSubject = CreateTestSubject(workspaceRootDir, configProvider, cmakeSettingsProvider.Object,
fileSystem.Object, Logger, MacroEvalService.Object);
fileSystem, Logger, MacroEvalService.Object);
}

public CompilationDatabaseLocator TestSubject { get; }
public CMakeCompilationDatabaseLocator TestSubject { get; }
public Mock<IMacroEvaluationService> MacroEvalService { get; }
public TestLogger Logger { get; }
}
Expand Down
226 changes: 0 additions & 226 deletions src/CFamily.UnitTests/CMake/CMakeRequestFactoryTests.cs

This file was deleted.

Loading

0 comments on commit 249a3ef

Please sign in to comment.