Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ISLCoreRpcFactory, ISLCoreJsonRpcFactory + refactoring #5296

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/SLCore.IntegrationTests/SLCoreTestProcessRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public sealed class SLCoreTestProcessRunner : IDisposable
private StreamWriter logFileStream;
private StreamWriter errorFileStream;

public ISLCoreJsonRpc Rpc { get; private set; }
internal ISLCoreJsonRpc Rpc { get; private set; }


public SLCoreTestProcessRunner(string pathToBat,
Expand Down
2 changes: 1 addition & 1 deletion src/SLCore.IntegrationTests/SLCoreTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public sealed class SLCoreTestRunner : IDisposable
private string storageRoot;
private string workDir;
private string userHome;
public SLCoreServiceProvider SlCoreServiceProvider { get; private set; }
internal SLCoreServiceProvider SlCoreServiceProvider { get; private set; }

public SLCoreTestRunner(ILogger logger, string testName)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using SonarLint.VisualStudio.SLCore.Common.Helpers;
using SonarLint.VisualStudio.TestInfrastructure;

namespace SonarLint.VisualStudio.SLCore.UnitTests.Common.Helpers;

Expand Down
1 change: 0 additions & 1 deletion src/SLCore.UnitTests/Common/TokenDtoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using SonarLint.VisualStudio.SLCore.Common.Models;

namespace SonarLint.VisualStudio.SLCore.UnitTests.Common;
Expand Down
1 change: 0 additions & 1 deletion src/SLCore.UnitTests/Common/UserAndPasswordDtoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using SonarLint.VisualStudio.SLCore.Common.Models;

namespace SonarLint.VisualStudio.SLCore.UnitTests.Common;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/

using SonarLint.VisualStudio.SLCore.Core.Process;
using SonarLint.VisualStudio.TestInfrastructure;

namespace SonarLint.VisualStudio.SLCore.UnitTests.Core.Process;

Expand Down
52 changes: 52 additions & 0 deletions src/SLCore.UnitTests/Core/SLCoreJsonRpcFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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 NSubstitute;
using SonarLint.VisualStudio.SLCore.Core;
using SonarLint.VisualStudio.SLCore.Protocol;

namespace SonarLint.VisualStudio.SLCore.UnitTests.Core;

[TestClass]
public class SLCoreJsonRpcFactoryTests
{
[TestMethod]
public void MefCtor_CheckIsExported()
{
MefTestHelpers.CheckTypeCanBeImported<SLCoreJsonRpcFactory, ISLCoreJsonRpcFactory>(
MefTestHelpers.CreateExport<IRpcMethodNameTransformer>());
}

[TestMethod]
public void MefCtor_CheckIsSingleton()
{
MefTestHelpers.CheckIsSingletonMefComponent<SLCoreJsonRpcFactory>();
}


[TestMethod]
public void CreateSLCoreJsonRpc_ReturnsNewInstance()
{
var testSubject = new SLCoreJsonRpcFactory(Substitute.For<IRpcMethodNameTransformer>());
var jsonRpc = Substitute.For<IJsonRpc>();

testSubject.CreateSLCoreJsonRpc(jsonRpc).Should().NotBeSameAs(testSubject.CreateSLCoreJsonRpc(jsonRpc));
}
}
16 changes: 11 additions & 5 deletions src/SLCore.UnitTests/Core/SLCoreJsonRpcTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using System.Threading.Tasks;
using SonarLint.VisualStudio.SLCore.Core;
using SonarLint.VisualStudio.SLCore.Protocol;
using SonarLint.VisualStudio.SLCore.UnitTests.Helpers;
using StreamJsonRpc;

namespace SonarLint.VisualStudio.SLCore.UnitTests.Core;
Expand Down Expand Up @@ -122,12 +120,12 @@ public void StartListening_CallsJsonrpcStartListening()
clientMock.Verify(x => x.StartListening(), Times.Once);
}

private static SLCoreJsonRpc CreateTestSubject(out Mock<IJsonRpc> clientMock,
private static ISLCoreJsonRpc CreateTestSubject(out Mock<IJsonRpc> clientMock,
out TaskCompletionSource<bool> clientCompletionSource,
IRpcMethodNameTransformer methodNameTransformer = null)
{
(clientMock, clientCompletionSource) = TestJsonRpcFactory.Create();
return new SLCoreJsonRpc(clientMock.Object, methodNameTransformer);
(clientMock, clientCompletionSource) = CreateJsonRpc();
return new SLCoreJsonRpcFactory(methodNameTransformer).CreateSLCoreJsonRpc(clientMock.Object);
}

private static Mock<IRpcMethodNameTransformer> CreateMethodNameTransformerMock<T>(out Func<string, string> transformer)
Expand All @@ -138,6 +136,14 @@ private static Mock<IRpcMethodNameTransformer> CreateMethodNameTransformerMock<T
return methodNameTransformerMock;
}

private static (Mock<IJsonRpc> clientMock, TaskCompletionSource<bool> clientCompletionSource) CreateJsonRpc()
{
var mock = new Mock<IJsonRpc>();
var tcs = new TaskCompletionSource<bool>();
mock.SetupGet(x => x.Completion).Returns(tcs.Task);
return (mock, tcs);
}

public interface ITestSLCoreService : ISLCoreService {}

public class TestSLCoreListener : ISLCoreListener {}
Expand Down
1 change: 0 additions & 1 deletion src/SLCore.UnitTests/Core/SLCoreListenerSetUpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/

using SonarLint.VisualStudio.SLCore.Core;
using SonarLint.VisualStudio.TestInfrastructure;

namespace SonarLint.VisualStudio.SLCore.UnitTests.Core
{
Expand Down
2 changes: 0 additions & 2 deletions src/SLCore.UnitTests/Core/SLCoreServiceProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using SonarLint.VisualStudio.Core;
using SonarLint.VisualStudio.SLCore.Core;
using SonarLint.VisualStudio.TestInfrastructure;

namespace SonarLint.VisualStudio.SLCore.UnitTests.Core;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,5 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System.Threading.Tasks;
using SonarLint.VisualStudio.SLCore.Core;

namespace SonarLint.VisualStudio.SLCore.UnitTests.Helpers;

internal static class TestJsonRpcFactory
{
public static (Mock<IJsonRpc> clientMock, TaskCompletionSource<bool> clientCompletionSource) Create()
{
var mock = new Mock<IJsonRpc>();
var tcs = new TaskCompletionSource<bool>();
mock.SetupGet(x => x.Completion).Returns(tcs.Task);
return (mock, tcs);
}
}
global using System;
global using SonarLint.VisualStudio.TestInfrastructure;
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using Newtonsoft.Json;
using SonarLint.VisualStudio.SLCore.Common.Models;
using SonarLint.VisualStudio.SLCore.Listener.Credentials;
Expand Down
1 change: 0 additions & 1 deletion src/SLCore.UnitTests/Protocol/EitherJsonConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using Newtonsoft.Json;
using SonarLint.VisualStudio.SLCore.Protocol;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

using SonarLint.VisualStudio.SLCore.Core;
using SonarLint.VisualStudio.SLCore.Protocol;
using SonarLint.VisualStudio.TestInfrastructure;
using StreamJsonRpc;

namespace SonarLint.VisualStudio.SLCore.UnitTests.Protocol;

Expand Down
132 changes: 132 additions & 0 deletions src/SLCore.UnitTests/SLCoreRpcFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* 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 NSubstitute;
using SonarLint.VisualStudio.SLCore.Core;
using SonarLint.VisualStudio.SLCore.Core.Process;

namespace SonarLint.VisualStudio.SLCore.UnitTests;

[TestClass]
public class SLCoreRpcFactoryTests
{
[TestMethod]
public void MefCtor_CheckIsExported()
{
MefTestHelpers.CheckTypeCanBeImported<SLCoreRpcFactory, ISLCoreRpcFactory>(
MefTestHelpers.CreateExport<ISLCoreProcessFactory>(),
MefTestHelpers.CreateExport<ISLCoreLocator>(),
MefTestHelpers.CreateExport<ISLCoreJsonRpcFactory>(),
MefTestHelpers.CreateExport<ISLCoreServiceProviderWriter>(),
MefTestHelpers.CreateExport<ISLCoreListenerSetUp>());
}

[TestMethod]
public void MefCtor_CheckIsSingleton()
{
MefTestHelpers.CheckIsSingletonMefComponent<SLCoreRpcFactory>();
}

[TestMethod]
public void StartNewRpcInstance_CorrectlyComposesSLCoreRpc()
{
var testSubject = CreateTestSubject(out var slCoreProcessFactory,
out var slCoreLocator,
out var slCoreJsonRpcFactory,
out var slCoreServiceProviderWriter,
out var slCoreListenerSetUp);

SetUpDependencies(slCoreLocator,
slCoreProcessFactory,
slCoreJsonRpcFactory,
out var slCoreLaunchParameters,
out var slCoreProcess,
out var jsonRpc,
out var slCoreJsonRpc);

testSubject.StartNewRpcInstance().Should().NotBeNull();

Received.InOrder(() =>
{
slCoreLocator.LocateExecutable();
slCoreProcessFactory.StartNewProcess(slCoreLaunchParameters);
slCoreProcess.AttachJsonRpc();
slCoreJsonRpcFactory.CreateSLCoreJsonRpc(jsonRpc);
slCoreServiceProviderWriter.SetCurrentConnection(slCoreJsonRpc);
slCoreListenerSetUp.Setup(slCoreJsonRpc);
slCoreJsonRpc.StartListening();
});
}

[TestMethod]
public void SLCoreRpc_DisposesProcess()
{
var testSubject = CreateTestSubject(out var slCoreProcessFactory,
out var slCoreLocator,
out var slCoreJsonRpcFactory,
out _,
out _);

SetUpDependencies(slCoreLocator,
slCoreProcessFactory,
slCoreJsonRpcFactory,
out _,
out var slCoreProcess,
out _,
out _);

var slCoreRpc = testSubject.StartNewRpcInstance();
slCoreRpc.Dispose();

slCoreProcess.Received(1).Dispose();
}

private static void SetUpDependencies(ISLCoreLocator slCoreLocator,
ISLCoreProcessFactory slCoreProcessFactory,
ISLCoreJsonRpcFactory slCoreJsonRpcFactory,
out SLCoreLaunchParameters slCoreLaunchParameters,
out ISLCoreProcess slCoreProcess,
out IJsonRpc jsonRpc,
out ISLCoreJsonRpc slCoreJsonRpc)
{
slCoreLaunchParameters = new SLCoreLaunchParameters(default, default);
slCoreLocator.LocateExecutable().Returns(slCoreLaunchParameters);
slCoreProcess = Substitute.For<ISLCoreProcess>();
slCoreProcessFactory.StartNewProcess(slCoreLaunchParameters).Returns(slCoreProcess);
jsonRpc = Substitute.For<IJsonRpc>();
slCoreProcess.AttachJsonRpc().Returns(jsonRpc);
slCoreJsonRpc = Substitute.For<ISLCoreJsonRpc>();
slCoreJsonRpcFactory.CreateSLCoreJsonRpc(jsonRpc).Returns(slCoreJsonRpc);
}

private static SLCoreRpcFactory CreateTestSubject(out ISLCoreProcessFactory slCoreProcessFactory,
out ISLCoreLocator slCoreLocator,
out ISLCoreJsonRpcFactory slCoreJsonRpcFactory,
out ISLCoreServiceProviderWriter slCoreServiceProviderWriter,
out ISLCoreListenerSetUp slCoreListenerSetUp)
{
slCoreProcessFactory = Substitute.For<ISLCoreProcessFactory>();
slCoreLocator = Substitute.For<ISLCoreLocator>();
slCoreJsonRpcFactory = Substitute.For<ISLCoreJsonRpcFactory>();
slCoreServiceProviderWriter = Substitute.For<ISLCoreServiceProviderWriter>();
slCoreListenerSetUp = Substitute.For<ISLCoreListenerSetUp>();
return new SLCoreRpcFactory(slCoreProcessFactory, slCoreLocator, slCoreJsonRpcFactory, slCoreServiceProviderWriter, slCoreListenerSetUp);
}
}
2 changes: 0 additions & 2 deletions src/SLCore.UnitTests/State/ActiveConfigScopeTrackerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using System.Linq;
using System.Threading.Tasks;
using SonarLint.VisualStudio.Core;
Expand All @@ -28,7 +27,6 @@
using SonarLint.VisualStudio.SLCore.Service.Project.Models;
using SonarLint.VisualStudio.SLCore.Service.Project.Params;
using SonarLint.VisualStudio.SLCore.State;
using SonarLint.VisualStudio.TestInfrastructure;

namespace SonarLint.VisualStudio.SLCore.UnitTests.State;

Expand Down
2 changes: 0 additions & 2 deletions src/SLCore.UnitTests/State/AliveConnectionTrackerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using System.Linq;
using System.Threading.Tasks;
using SonarLint.VisualStudio.Core;
Expand All @@ -29,7 +28,6 @@
using SonarLint.VisualStudio.SLCore.Service.Connection;
using SonarLint.VisualStudio.SLCore.Service.Connection.Models;
using SonarLint.VisualStudio.SLCore.State;
using SonarLint.VisualStudio.TestInfrastructure;

namespace SonarLint.VisualStudio.SLCore.UnitTests.State;

Expand Down
2 changes: 0 additions & 2 deletions src/SLCore.UnitTests/State/ServerConnectionsProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using NSubstitute;
using SonarLint.VisualStudio.Core.Binding;
using SonarLint.VisualStudio.SLCore.Common.Helpers;
using SonarLint.VisualStudio.SLCore.Service.Connection.Models;
using SonarLint.VisualStudio.SLCore.State;
using SonarLint.VisualStudio.TestInfrastructure;
using SonarQube.Client.Models;

namespace SonarLint.VisualStudio.SLCore.UnitTests.State;
Expand Down
Loading
Loading