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 IProgressListener #5139

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
using SonarLint.VisualStudio.SLCore.Core;
using SonarLint.VisualStudio.TestInfrastructure;

namespace SonarLint.VisualStudio.SLCore.UnitTests
namespace SonarLint.VisualStudio.SLCore.UnitTests.Core
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved tests to core folder so it would have same structure with the main project. It's 2 tasks in 1 PR but since PR is so small I think it's OK.

{
[TestClass]
public class SLCoreListenerSetUpTests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
using SonarLint.VisualStudio.SLCore.Core;
using SonarLint.VisualStudio.TestInfrastructure;

namespace SonarLint.VisualStudio.SLCore.UnitTests;
namespace SonarLint.VisualStudio.SLCore.UnitTests.Core;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment on SLCoreListenerSetUpTests. (This also had some whitespace fixes done automatically)


[TestClass]
public class SLCoreServiceProviderTests
Expand All @@ -31,20 +31,20 @@ public class SLCoreServiceProviderTests
public void MefCtor_CheckIsExported()
{
MefTestHelpers.CheckTypeCanBeImported<SLCoreServiceProvider, ISLCoreServiceProvider>();
}
}

[TestMethod]
public void MefCtor_WriterInterface_CheckIsExported()
{
MefTestHelpers.CheckTypeCanBeImported<SLCoreServiceProvider, ISLCoreServiceProviderWriter>();
}

[TestMethod]
public void Mef_CheckIsSingleton()
{
MefTestHelpers.CheckIsSingletonMefComponent<SLCoreServiceProvider>();
}

[TestMethod]
public void TryGetTransientService_TypeNotInterface_Throws()
{
Expand All @@ -62,42 +62,42 @@ public void TryGetTransientService_NotInitialized_ReturnsFalse()

testSubject.TryGetTransientService(out ITestSLcoreService1 _).Should().BeFalse();
}

[TestMethod]
public void TryGetTransientService_ConnectionDied_ReturnsFalse()
{
var rpcMock = new Mock<ISLCoreJsonRpc>();
SetUpConnectionState(rpcMock, false);

var testSubject = CreateTestSubject(rpcMock.Object);

testSubject.TryGetTransientService(out ITestSLcoreService1 _).Should().BeFalse();
}

[TestMethod]
public void TryGetTransientService_ConnectionIsAlive_ReturnsTrueAndCreatesService()
{
var rpcMock = new Mock<ISLCoreJsonRpc>();
SetUpConnectionState(rpcMock, true);
var service1 = Mock.Of<ITestSLcoreService1>();
SetUpServiceCreation(rpcMock, service1);

var testSubject = CreateTestSubject(rpcMock.Object);

testSubject.TryGetTransientService(out ITestSLcoreService1 requestedService).Should().BeTrue();

requestedService.Should().BeSameAs(service1);
rpcMock.Verify(x => x.CreateService<ITestSLcoreService1>(), Times.Once);
}
}

[TestMethod]
public void TryGetTransientService_ServiceAlreadyCreated_ReturnsTrueAndCachedCopy()
{
var rpcMock = new Mock<ISLCoreJsonRpc>();
SetUpConnectionState(rpcMock, true);
var service1 = Mock.Of<ITestSLcoreService1>();
SetUpServiceCreation(rpcMock, service1);

var testSubject = CreateTestSubject(rpcMock.Object);
testSubject.TryGetTransientService(out ITestSLcoreService1 _);

Expand All @@ -106,15 +106,15 @@ public void TryGetTransientService_ServiceAlreadyCreated_ReturnsTrueAndCachedCop
requestedService.Should().BeSameAs(service1);
rpcMock.Verify(x => x.CreateService<ITestSLcoreService1>(), Times.Once);
}

[TestMethod]
public void TryGetTransientService_ConnectionReset_ReturnsTrueAndCreatesService()
{
var service1 = Mock.Of<ITestSLcoreService1>();
var rpcMock1 = new Mock<ISLCoreJsonRpc>();
SetUpConnectionState(rpcMock1, true);
SetUpServiceCreation(rpcMock1, service1);

var testSubject = CreateTestSubject(rpcMock1.Object);
testSubject.TryGetTransientService(out ITestSLcoreService1 _); //caching
var service2 = Mock.Of<ITestSLcoreService1>();
Expand Down Expand Up @@ -146,7 +146,7 @@ public void SetCurrentConnection_ClearsAllCachedServices()
testSubject.TryGetTransientService(out ITestSLcoreService1 _).Should().BeTrue();
testSubject.TryGetTransientService(out ITestSLcoreService2 _).Should().BeTrue();
testSubject.TryGetTransientService(out ITestSLcoreService3 _).Should().BeTrue();

var service1New = Mock.Of<ITestSLcoreService1>();
var service2New = Mock.Of<ITestSLcoreService2>();
var service3New = Mock.Of<ITestSLcoreService3>();
Expand All @@ -155,9 +155,9 @@ public void SetCurrentConnection_ClearsAllCachedServices()
SetUpServiceCreation(rpcMock2, service1New);
SetUpServiceCreation(rpcMock2, service2New);
SetUpServiceCreation(rpcMock2, service3New);

testSubject.SetCurrentConnection(rpcMock2.Object);

testSubject.TryGetTransientService(out ITestSLcoreService1 requestedService1).Should().BeTrue();
requestedService1.Should().BeSameAs(service1New).And.NotBeSameAs(service1);
testSubject.TryGetTransientService(out ITestSLcoreService2 requestedService2).Should().BeTrue();
Expand All @@ -170,12 +170,12 @@ private static void SetUpServiceCreation<T>(Mock<ISLCoreJsonRpc> rpcMock, T serv
{
rpcMock.Setup(x => x.CreateService<T>()).Returns(service);
}

private static void SetUpConnectionState(Mock<ISLCoreJsonRpc> rpcMock, bool isAlive)
{
rpcMock.SetupGet(x => x.IsAlive).Returns(isAlive);
}

private SLCoreServiceProvider CreateTestSubject(ISLCoreJsonRpc jsonRpc = null)
{
var testSubject = new SLCoreServiceProvider();
Expand All @@ -188,21 +188,17 @@ private SLCoreServiceProvider CreateTestSubject(ISLCoreJsonRpc jsonRpc = null)

public class TestSLCoreService : ISLCoreService
{

}

public interface ITestSLcoreService1 : ISLCoreService
{

}

public interface ITestSLcoreService2 : ISLCoreService
{

}

public interface ITestSLcoreService3 : ISLCoreService
{

}
}
68 changes: 68 additions & 0 deletions src/SLCore.UnitTests/Listener/ProgressListenerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* SonarLint for Visual Studio
* Copyright (C) 2016-2023 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 System.Threading.Tasks;
using SonarLint.VisualStudio.SLCore.Listener;
using SonarLint.VisualStudio.TestInfrastructure;

namespace SonarLint.VisualStudio.SLCore.UnitTests.Listener
{
[TestClass]
public class ProgressListenerTests
{
[TestMethod]
public void MefCtor_CheckIsExported()
{
MefTestHelpers.CheckTypeCanBeImported<ProgressListener, IProgressListener>();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong export contract

}

[TestMethod]
public void Mef_CheckIsSingleton()
{
MefTestHelpers.CheckIsSingletonMefComponent<ProgressListener>();
}

[TestMethod]
[DataRow(null)]
[DataRow(5)]
[DataRow("something")]
public void StartProgress_ReturnsCompletedTaskAlways(object parameter)
{
var testSubject = new ProgressListener();

var result = testSubject.StartProgress(parameter);

result.Should().Be(Task.CompletedTask);
}

[TestMethod]
[DataRow(null)]
[DataRow(5)]
[DataRow("something")]
public void ReportProgress_ReturnsCompletedTaskAlways(object parameter)
{
var testSubject = new ProgressListener();

var result = testSubject.ReportProgress(parameter);

result.Should().Be(Task.CompletedTask);
}
}
}
2 changes: 1 addition & 1 deletion src/SLCore.UnitTests/SLCore.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\SonarLint.Test.props" />

Expand Down
51 changes: 51 additions & 0 deletions src/SLCore/Listener/IProgressListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* SonarLint for Visual Studio
* Copyright (C) 2016-2023 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 System.ComponentModel.Composition;
using System.Threading.Tasks;
using SonarLint.VisualStudio.SLCore.Core;
using StreamJsonRpc;

namespace SonarLint.VisualStudio.SLCore.Listener
{
public interface IProgressListener : ISLCoreListener

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this interface.

{
[JsonRpcMethod(UseSingleObjectParameterDeserialization = true)]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I don't know if this is going to work when you put it on the interface instead of putting it on the class. 2. We can set this property in JsonRpcTargetOptions. It didn't work for me in the PoC, but I have a suspicion I did something wrong

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try to make the PoC work. Will get back to you.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed on slack

Task StartProgress(object parameters);

[JsonRpcMethod(UseSingleObjectParameterDeserialization = true)]
Task ReportProgress(object parameters);
}

[Export(typeof(IProgressListener))]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong export contract

[PartCreationPolicy(CreationPolicy.Shared)]
public class ProgressListener : IProgressListener
{
public Task StartProgress(object parameters)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to add a comment with why this is an object and not some real model ("we don't support this method")

{
return Task.CompletedTask;
}

public Task ReportProgress(object parameters)
{
return Task.CompletedTask;
}
}
}
Loading