Skip to content

Commit

Permalink
Use Substitute instead of Mock
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa-mohammed-sonarsource committed Dec 17, 2024
1 parent ff181a9 commit a51242d
Showing 1 changed file with 25 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,10 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System.Collections.Generic;
using System.IO.Abstractions;
using EnvDTE;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.VCProjectEngine;
using Moq;
using SonarLint.VisualStudio.Integration.Vsix.CFamily.VcxProject;
using SonarLint.VisualStudio.TestInfrastructure;
using static SonarLint.VisualStudio.Integration.Vsix.CFamily.UnitTests.CFamilyTestUtility;

namespace SonarLint.VisualStudio.Integration.UnitTests.CFamily.VcxProject
Expand All @@ -38,22 +33,22 @@ public class FileConfigTests
private const string ClFilePath = "C:\\path\\cl.exe";
private const string ClangClFilePath = "C:\\path\\clang-cl.exe";

private static Mock<IFileSystem> CreateFileSystemWithNoFiles()
private static IFileSystem CreateFileSystemWithNoFiles()
{
var fileSystem = new Mock<IFileSystem>();
fileSystem.Setup(x => x.File.Exists(It.IsAny<string>())).Returns(false);
var fileSystem = Substitute.For<IFileSystem>();
fileSystem.File.Exists(Arg.Any<string>()).Returns(false);
return fileSystem;
}

private static Mock<IFileSystem> CreateFileSystemWithExistingFile(string fullPath)
private static IFileSystem CreateFileSystemWithExistingFile(string fullPath)
{
var fileSystem = new Mock<IFileSystem>();
fileSystem.Setup(x => x.File.Exists(fullPath)).Returns(true);
var fileSystem = Substitute.For<IFileSystem>();
fileSystem.File.Exists(fullPath).Returns(true);
return fileSystem;
}

private static Mock<IFileSystem> CreateFileSystemWithClCompiler() => CreateFileSystemWithExistingFile(ClFilePath);
private static Mock<IFileSystem> CreateFileSystemWithClangClCompiler() => CreateFileSystemWithExistingFile(ClangClFilePath);
private static IFileSystem CreateFileSystemWithClCompiler() => CreateFileSystemWithExistingFile(ClFilePath);
private static IFileSystem CreateFileSystemWithClangClCompiler() => CreateFileSystemWithExistingFile(ClangClFilePath);

[TestMethod]
public void TryGet_NoVCProject_ReturnsNull()
Expand All @@ -65,7 +60,7 @@ public void TryGet_NoVCProject_ReturnsNull()
dteProjectItemMock.Setup(x => x.Object).Returns(Mock.Of<VCFile>());
dteProjectItemMock.Setup(x => x.ContainingProject).Returns(dteProjectMock.Object);

FileConfig.TryGet(testLogger, dteProjectItemMock.Object, "c:\\path", CreateFileSystemWithClCompiler().Object)
FileConfig.TryGet(testLogger, dteProjectItemMock.Object, "c:\\path", CreateFileSystemWithClCompiler())
.Should().BeNull();
}

Expand All @@ -79,7 +74,7 @@ public void TryGet_NoVCFile_ReturnsNull()
dteProjectItemMock.Setup(x => x.Object).Returns(null);
dteProjectItemMock.Setup(x => x.ContainingProject).Returns(dteProjectMock.Object);

FileConfig.TryGet(testLogger, dteProjectItemMock.Object, "c:\\path", CreateFileSystemWithClCompiler().Object)
FileConfig.TryGet(testLogger, dteProjectItemMock.Object, "c:\\path", CreateFileSystemWithClCompiler())
.Should().BeNull();
}

Expand All @@ -91,7 +86,7 @@ public void TryGet_UnsupportedItemType_ReturnsNull()
var projectItemMock = CreateMockProjectItem("c:\\foo\\xxx.vcxproj", projectItemConfig);

// Act
var fileConfig = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.cpp", CreateFileSystemWithClCompiler().Object);
var fileConfig = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.cpp", CreateFileSystemWithClCompiler());

// Assert
fileConfig.Should().BeNull();
Expand All @@ -106,7 +101,7 @@ public void TryGet_UnsupportedConfigurationType_ReturnsNull()
var projectItemMock = CreateMockProjectItem("c:\\foo\\xxx.vcxproj", projectItemConfig);

// Act
var fileConfig = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.cpp", CreateFileSystemWithClCompiler().Object);
var fileConfig = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.cpp", CreateFileSystemWithClCompiler());

// Assert
fileConfig.Should().BeNull();
Expand All @@ -121,7 +116,7 @@ public void TryGet_UnsupportedCustomBuild_ReturnsNull()
var projectItemMock = CreateMockProjectItem("c:\\foo\\xxx.vcxproj", projectItemConfig);

// Act
var fileConfig = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.cpp", CreateFileSystemWithClCompiler().Object);
var fileConfig = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.cpp", CreateFileSystemWithClCompiler());

// Assert
fileConfig.Should().BeNull();
Expand Down Expand Up @@ -154,7 +149,7 @@ public void TryGet_Full_Cmd()
var projectItemMock = CreateMockProjectItem("c:\\foo\\xxx.vcxproj", projectItemConfig);

// Act
var request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.cpp", CreateFileSystemWithClCompiler().Object);
var request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.cpp", CreateFileSystemWithClCompiler());

// Assert
request.Should().NotBeNull();
Expand Down Expand Up @@ -190,7 +185,7 @@ public void TryGet_HeaderFileOptions_ReturnsValidConfig()
var projectItemMock = CreateMockProjectItem("c:\\foo\\xxx.vcxproj", projectItemConfig);

// Act
var request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.h", CreateFileSystemWithClCompiler().Object);
var request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.h", CreateFileSystemWithClCompiler());

// Assert
request.Should().NotBeNull();
Expand All @@ -203,7 +198,7 @@ public void TryGet_HeaderFileOptions_ReturnsValidConfig()
projectItemConfig.FileConfigProperties["ForcedIncludeFiles"] = "FHeader.h";

// Act
request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.h", CreateFileSystemWithClCompiler().Object);
request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.h", CreateFileSystemWithClCompiler());

// Assert
Assert.AreEqual("\"C:\\path\\cl.exe\" /FI\"FHeader.h\" /Yu\"pch.h\" /EHsc /RTCu /TC \"c:\\dummy\\file.h\"", request.CDCommand);
Expand All @@ -214,7 +209,7 @@ public void TryGet_HeaderFileOptions_ReturnsValidConfig()
projectItemConfig.FileConfigProperties["CompileAs"] = "CompileAsCpp";

// Act
request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.h", CreateFileSystemWithClCompiler().Object);
request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.h", CreateFileSystemWithClCompiler());

// Assert
Assert.AreEqual("\"C:\\path\\cl.exe\" /FI\"FHeader.h\" /Yu\"pch.h\" /EHsc /RTCu /TP \"c:\\dummy\\file.h\"", request.CDCommand);
Expand All @@ -240,7 +235,7 @@ public void TryGet_CompilerName_VS2017()
var projectItemMock = CreateMockProjectItem("c:\\foo\\xxx.vcxproj", projectItemConfig);

// Act
var request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.cpp", CreateFileSystemWithExistingFile("C:\\path\\x86\\cl.exe").Object);
var request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.cpp", CreateFileSystemWithExistingFile("C:\\path\\x86\\cl.exe"));

// Assert
request.Should().NotBeNull();
Expand All @@ -250,7 +245,7 @@ public void TryGet_CompilerName_VS2017()
projectItemConfig.PlatformName = "x64";
projectItemMock = CreateMockProjectItem("c:\\foo\\xxx.vcxproj", projectItemConfig);
// Act
request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.cpp", CreateFileSystemWithExistingFile("C:\\path\\x64\\cl.exe").Object);
request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.cpp", CreateFileSystemWithExistingFile("C:\\path\\x64\\cl.exe"));

// Assert
request.Should().NotBeNull();
Expand All @@ -277,7 +272,7 @@ public void TryGet_CompilerName_ClangCL()
var projectItemMock = CreateMockProjectItem("c:\\foo\\xxx.vcxproj", projectItemConfig);

// Act
var request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.cpp", CreateFileSystemWithClangClCompiler().Object);
var request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.cpp", CreateFileSystemWithClangClCompiler());

// Assert
request.Should().NotBeNull();
Expand All @@ -287,14 +282,14 @@ public void TryGet_CompilerName_ClangCL()
projectItemConfig.ProjectConfigProperties["ClCompilerPath"] = "\\clang-cl.exe";

// Act
request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.cpp", CreateFileSystemWithClangClCompiler().Object);
request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.cpp", CreateFileSystemWithClangClCompiler());

// Assert
request.Should().NotBeNull();
Assert.IsTrue(request.CDCommand.StartsWith("\"C:\\path\\clang-cl.exe\""));

// Act
request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.cpp", CreateFileSystemWithNoFiles().Object);
request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.cpp", CreateFileSystemWithNoFiles());

// Assert
request.Should().BeNull();
Expand All @@ -304,7 +299,7 @@ public void TryGet_CompilerName_ClangCL()
projectItemConfig.ProjectConfigProperties["ClToolExe"] = null;

// Act
request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.cpp", CreateFileSystemWithExistingFile("C:\\path\\x86\\cl.exe").Object);
request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.cpp", CreateFileSystemWithExistingFile("C:\\path\\x86\\cl.exe"));

// Assert
request.Should().NotBeNull();
Expand All @@ -314,7 +309,7 @@ public void TryGet_CompilerName_ClangCL()
projectItemConfig.ProjectConfigProperties["ClToolExe"] = null;

// Act
request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.cpp", CreateFileSystemWithNoFiles().Object);
request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.cpp", CreateFileSystemWithNoFiles());

// Assert
request.Should().BeNull();
Expand All @@ -341,7 +336,7 @@ public void TryGet_CompilerName_CL_No_ClCompilerPath()
var projectItemMock = CreateMockProjectItem("c:\\foo\\xxx.vcxproj", projectItemConfig);

// Act
var request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.cpp", CreateFileSystemWithClCompiler().Object);
var request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.cpp", CreateFileSystemWithClCompiler());

// Assert
request.Should().NotBeNull();
Expand Down

0 comments on commit a51242d

Please sign in to comment.