Skip to content

Commit

Permalink
SLVS-1705 Adapt AnalysisStatusNotifier to issue streaming (#5899)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgii-borovinskikh-sonarsource authored Dec 17, 2024
1 parent fff1cb0 commit 044fcb8
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 45 deletions.
4 changes: 4 additions & 0 deletions src/Core.UnitTests/Analysis/HotspotsPublisherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public void TestInitialize()
testSubject = new HotspotPublisher(issueConsumerStorage);
}

[TestMethod]
public void FindingsType_ReturnsCorrectValue() =>
testSubject.FindingsType.Should().Be(CoreStrings.FindingType_Hotspot);

[TestMethod]
public void PublishHotspots_NoConsumerInStorage_DoesNothing()
{
Expand Down
4 changes: 4 additions & 0 deletions src/Core.UnitTests/Analysis/IssuePublisherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public void TestInitialize()
testSubject = new IssuePublisher(issueConsumerStorage);
}

[TestMethod]
public void FindingsType_ReturnsCorrectValue() =>
testSubject.FindingsType.Should().Be(CoreStrings.FindingType_Issue);

[TestMethod]
public void PublishIssues_NoConsumerInStorage_DoesNothing()
{
Expand Down
2 changes: 2 additions & 0 deletions src/Core/Analysis/HotspotPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace SonarLint.VisualStudio.Core.Analysis;
[method:ImportingConstructor]
internal class HotspotPublisher(IIssueConsumerStorage issueConsumerStorage) : IHotspotPublisher
{
public string FindingsType => CoreStrings.FindingType_Hotspot;

public void Publish(string filePath, Guid analysisId, IEnumerable<IAnalysisIssue> findings)
{
if (issueConsumerStorage.TryGet(filePath, out var currentAnalysisId, out var issueConsumer)
Expand Down
3 changes: 2 additions & 1 deletion src/Core/Analysis/IAnalysisStatusNotifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ namespace SonarLint.VisualStudio.Core.Analysis
public interface IAnalysisStatusNotifier
{
void AnalysisStarted();
void AnalysisFinished(int issueCount, TimeSpan analysisTime);
void AnalysisProgressed(int issueCount, string findingType, bool isIntermediate);
void AnalysisFinished(TimeSpan analysisTime);
void AnalysisCancelled();
void AnalysisFailed(Exception ex);
void AnalysisFailed(string failureMessage);
Expand Down
1 change: 1 addition & 0 deletions src/Core/Analysis/IFindingsPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace SonarLint.VisualStudio.Core.Analysis;

public interface IFindingsPublisher
{
string FindingsType { get; }
/// <summary>
/// Handles analysis results
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Core/Analysis/IssuePublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace SonarLint.VisualStudio.Core.Analysis;
[method:ImportingConstructor]
internal class IssuePublisher(IIssueConsumerStorage issueConsumerStorage) : IIssuePublisher
{
public string FindingsType => CoreStrings.FindingType_Issue;

public void Publish(string filePath, Guid analysisId, IEnumerable<IAnalysisIssue> findings)
{
if (issueConsumerStorage.TryGet(filePath, out var currentAnalysisId, out var issueConsumer)
Expand Down
19 changes: 18 additions & 1 deletion src/Core/CoreStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/Core/CoreStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,10 @@
<data name="SonarCloudUrl" xml:space="preserve">
<value>https://sonarcloud.io</value>
</data>
<data name="FindingType_Issue" xml:space="preserve">
<value>issue</value>
</data>
<data name="FindingType_Hotspot" xml:space="preserve">
<value>hotspot</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,9 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using System.Collections.Generic;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SonarLint.VisualStudio.Core;
using SonarLint.VisualStudio.Integration.Vsix.Analysis;
using SonarLint.VisualStudio.Integration.Vsix.Helpers;
using SonarLint.VisualStudio.TestInfrastructure;

namespace SonarLint.VisualStudio.Integration.UnitTests.Analysis
{
Expand Down Expand Up @@ -65,16 +59,34 @@ public void AnalysisStarted_LogToOutputWindow()
logger.OutputStrings.Count.Should().Be(1);
}

[DataRow(true)]
[DataRow(false)]
[DataTestMethod]
public void AnalysisProgressed_LogToOutputWindow(bool isIntermediate)
{
const string analyzerName = "some analyzer";
const string filePath = "c:\\test\\foo-started.cpp";
var logger = new TestLogger();
var analysisId = Guid.NewGuid();

var testSubject = CreateTestSubject(filePath, analysisId, analyzerName, logger: logger);
testSubject.AnalysisProgressed(123, "finding", isIntermediate);

var expectedMessage = string.Format(AnalysisStrings.MSG_FoundIssues, 123, "finding", filePath, analysisId, !isIntermediate);
logger.AssertPartialOutputStringExists(expectedMessage);
logger.AssertPartialOutputStringExists(analyzerName);
}

[TestMethod]
[DataRow("foo-finished.cpp", "foo-finished.cpp")]
[DataRow("c:\\test\\foo-finished.cpp", "foo-finished.cpp")]
[DataRow("..\\test\\foo-finished.cpp", "foo-finished.cpp")]
public void AnalysisFinished_DisplayMessageAndStopSpinner(string filePath, string expectedNotifiedFileName)
{
var statusBarMock = new Mock<IStatusBarNotifier>();

var testSubject = CreateTestSubject(filePath, Guid.NewGuid(), statusBarNotifier: statusBarMock.Object);
testSubject.AnalysisFinished(1, TimeSpan.Zero);

testSubject.AnalysisFinished(TimeSpan.FromSeconds(3));

var expectedMessage = string.Format(AnalysisStrings.Notifier_AnalysisFinished, expectedNotifiedFileName);

Expand All @@ -91,17 +103,14 @@ public void AnalysisFinished_LogToOutputWindow()

var testSubject = CreateTestSubject(filePath, analysisId, analyzerName, logger: logger);

testSubject.AnalysisFinished(123, TimeSpan.FromSeconds(6.54321));
testSubject.AnalysisFinished(TimeSpan.FromSeconds(6.54321));

var expectedMessage = string.Format(AnalysisStrings.MSG_AnalysisComplete, filePath, analysisId, 6.543);
logger.AssertPartialOutputStringExists(expectedMessage);

expectedMessage = string.Format($"Found {123} issue(s) for {filePath}");
logger.AssertPartialOutputStringExists(expectedMessage);

logger.AssertPartialOutputStringExists(analyzerName);

logger.OutputStrings.Count.Should().Be(2);
logger.OutputStrings.Count.Should().Be(1);
}

[TestMethod]
Expand Down Expand Up @@ -136,7 +145,7 @@ public void AnalysisCancelled_LogToOutputWindow()
logger.AssertPartialOutputStringExists(expectedMessage);
logger.OutputStrings.Count.Should().Be(1);
}

[TestMethod]
[DataRow("foo-timedout.cpp")]
[DataRow("c:\\test\\foo-timedout.cpp")]
Expand All @@ -151,7 +160,7 @@ public void AnalysisNotReady_RemoveMessageAndStopSpinner(string filePath)

VerifyStatusBarMessageAndIcon(statusBarMock, "", false);
}

[TestMethod]
public void AnalysisNotReady_LogToOutputWindow()
{
Expand All @@ -170,7 +179,7 @@ public void AnalysisNotReady_LogToOutputWindow()
logger.AssertPartialOutputStringExists(expectedMessage);
logger.OutputStrings.Count.Should().Be(1);
}


[TestMethod]
[DataRow("foo-failed.cpp", "foo-failed.cpp")]
Expand All @@ -188,8 +197,8 @@ public void AnalysisFailed_DisplayMessageAndStopSpinner(string filePath, string

VerifyStatusBarMessageAndIcon(statusBarMock, expectedMessage, false);
}



[TestMethod]
[DataRow("foo-failed.cpp", "foo-failed.cpp")]
Expand Down
13 changes: 9 additions & 4 deletions src/Integration.Vsix/Analysis/AnalysisStatusNotifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,23 @@ public void AnalysisStarted()
Notify(AnalysisStrings.Notifier_AnalysisStarted, true);
}

public void AnalysisFinished(int issueCount, TimeSpan analysisTime)
public void AnalysisProgressed(
int issueCount,
string findingType,
bool isIntermediate) =>
Log(AnalysisStrings.MSG_FoundIssues, issueCount, findingType, filePath, analysisId, !isIntermediate);

public void AnalysisFinished(TimeSpan analysisTime)
{
Log(AnalysisStrings.MSG_AnalysisComplete, filePath, analysisId, Math.Round(analysisTime.TotalSeconds, 3));
Log(AnalysisStrings.MSG_FoundIssues, issueCount, filePath);

Notify(AnalysisStrings.Notifier_AnalysisFinished, false);
}

public void AnalysisCancelled()
{
Log(AnalysisStrings.MSG_AnalysisAborted, filePath, analysisId);

Notify("", false);
}

Expand All @@ -80,7 +85,7 @@ public void AnalysisFailed(string failureMessage)
public void AnalysisNotReady(string reason)
{
Log(AnalysisStrings.MSG_AnalysisNotReady, filePath, analysisId, reason);

Notify("", false);
}

Expand Down
3 changes: 1 addition & 2 deletions src/Integration.Vsix/Analysis/AnalysisStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Integration.Vsix/Analysis/AnalysisStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
<value>Analyzing {0} with id {1}</value>
</data>
<data name="MSG_FoundIssues" xml:space="preserve">
<value>Found {0} issue(s) for {1}</value>
<value>Found {0} {1}(s) in {2} [id: {3}, final: {4}]</value>
</data>
<data name="ConfigMonitor_BindingChanged" xml:space="preserve">
<value>Binding has changed. Open documents will be re-analysed.</value>
Expand Down
Loading

0 comments on commit 044fcb8

Please sign in to comment.