Skip to content

Commit

Permalink
SLVS-1553 Fix merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriela-trutan-sonarsource committed Oct 28, 2024
2 parents df9eac0 + 2468792 commit 04749b9
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,27 @@ public void AddAnalyzer_CurrentSolutionContainsAnalyzer()
var roslynWorkspaceWrapper = CreateWorkspaceWrapper();
var analyzers = ImmutableArray.Create(analyzerFileReference);

var solutionAfterAddition = roslynWorkspaceWrapper.CurrentSolution.WithAnalyzerReferences(analyzers);
var solutionAfterAddition = roslynWorkspaceWrapper.CurrentSolution.AddAnalyzerReferences(analyzers);
roslynWorkspaceWrapper.TryApplyChanges(solutionAfterAddition).Should().BeTrue();

roslynWorkspaceWrapper.CurrentSolution.GetRoslynSolution().AnalyzerReferences.Contains(analyzerFileReference).Should().BeTrue();
roslynWorkspaceWrapper.CurrentSolution.GetRoslynSolution().AnalyzerReferences.Should().Contain(analyzerFileReference);
}

[TestMethod]
public void AddExtraAnalyzer_CurrentSolutionContainsBothAnalyzers()
{
var analyzerFileReference = new AnalyzerFileReference(@"C:\abc", Substitute.For<IAnalyzerAssemblyLoader>());
var extraAnalyzerFileReference = new AnalyzerFileReference(@"C:\abc", Substitute.For<IAnalyzerAssemblyLoader>());
var roslynWorkspaceWrapper = CreateWorkspaceWrapper();
var extraAnalyzers = ImmutableArray.Create(extraAnalyzerFileReference);
roslynWorkspaceWrapper.TryApplyChanges(roslynWorkspaceWrapper.CurrentSolution.AddAnalyzerReferences(ImmutableArray.Create(analyzerFileReference))).Should().BeTrue();

var solutionAfterAddition = roslynWorkspaceWrapper.CurrentSolution.AddAnalyzerReferences(extraAnalyzers);
roslynWorkspaceWrapper.TryApplyChanges(solutionAfterAddition).Should().BeTrue();

var solutionAnalyzers = roslynWorkspaceWrapper.CurrentSolution.GetRoslynSolution().AnalyzerReferences;
solutionAnalyzers.Should().Contain(analyzerFileReference);
solutionAnalyzers.Should().Contain(extraAnalyzerFileReference);
}

[TestMethod]
Expand All @@ -54,13 +71,13 @@ public void AddAndRemoveAnalyzer_CurrentSolutionNoLongerContainsAnalyzer()
var roslynWorkspaceWrapper = CreateWorkspaceWrapper();
var analyzers = ImmutableArray.Create(analyzerFileReference);

var solutionAfterAddition = roslynWorkspaceWrapper.CurrentSolution.WithAnalyzerReferences(analyzers);
var solutionAfterAddition = roslynWorkspaceWrapper.CurrentSolution.AddAnalyzerReferences(analyzers);
roslynWorkspaceWrapper.TryApplyChanges(solutionAfterAddition).Should().BeTrue();

var solutionAfterRemoval = roslynWorkspaceWrapper.CurrentSolution.RemoveAnalyzerReferences(analyzers);
roslynWorkspaceWrapper.TryApplyChanges(solutionAfterRemoval).Should().BeTrue();

roslynWorkspaceWrapper.CurrentSolution.GetRoslynSolution().AnalyzerReferences.Contains(analyzerFileReference).Should().BeFalse();
roslynWorkspaceWrapper.CurrentSolution.GetRoslynSolution().AnalyzerReferences.Should().NotContain(analyzerFileReference);
}

[TestMethod]
Expand All @@ -73,7 +90,7 @@ public void RemoveAnalyzer_IsNotPresentInTheCurrentSolution_AppliesNoChange()
var solutionAfterRemoval = roslynWorkspaceWrapper.CurrentSolution.RemoveAnalyzerReferences(analyzers);
roslynWorkspaceWrapper.TryApplyChanges(solutionAfterRemoval).Should().BeTrue();

roslynWorkspaceWrapper.CurrentSolution.GetRoslynSolution().AnalyzerReferences.Contains(analyzerFileReference).Should().BeFalse();
roslynWorkspaceWrapper.CurrentSolution.GetRoslynSolution().AnalyzerReferences.Should().NotContain(analyzerFileReference);
}

[TestMethod]
Expand All @@ -85,10 +102,10 @@ public void AddMultipleAndRemoveOneAnalyzer_CurrentSolutionContainsOneAnalyzer()
var analyzersToRemove = ImmutableArray.Create(analyzerFileReference1);
var roslynWorkspaceWrapper = CreateWorkspaceWrapper();

roslynWorkspaceWrapper.TryApplyChanges(roslynWorkspaceWrapper.CurrentSolution.WithAnalyzerReferences(analyzersToAdd)).Should().BeTrue();
roslynWorkspaceWrapper.TryApplyChanges(roslynWorkspaceWrapper.CurrentSolution.AddAnalyzerReferences(analyzersToAdd)).Should().BeTrue();
roslynWorkspaceWrapper.TryApplyChanges(roslynWorkspaceWrapper.CurrentSolution.RemoveAnalyzerReferences(analyzersToRemove)).Should().BeTrue();
roslynWorkspaceWrapper.CurrentSolution.GetRoslynSolution().AnalyzerReferences.Contains(analyzerFileReference1).Should().BeFalse();
roslynWorkspaceWrapper.CurrentSolution.GetRoslynSolution().AnalyzerReferences.Contains(analyzerFileReference2).Should().BeTrue();
roslynWorkspaceWrapper.CurrentSolution.GetRoslynSolution().AnalyzerReferences.Should().NotContain(analyzerFileReference1);
roslynWorkspaceWrapper.CurrentSolution.GetRoslynSolution().AnalyzerReferences.Should().Contain(analyzerFileReference2);
}

private static IRoslynWorkspaceWrapper CreateWorkspaceWrapper()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public async Task OnSolutionStateChangedAsync_StandaloneSolution_BindingSet_Remo
analyzerComparer.Equals(embeddedAnalyzers, connectedAnalyzers);
v1Solution.RemoveAnalyzerReferences(embeddedAnalyzers);
roslynWorkspaceWrapper.TryApplyChanges(v2Solution);
v2Solution.WithAnalyzerReferences(connectedAnalyzers);
v2Solution.AddAnalyzerReferences(connectedAnalyzers);
roslynWorkspaceWrapper.TryApplyChanges(v3Solution);
});
embeddedRoslynAnalyzerProvider.DidNotReceiveWithAnyArgs().Get();
Expand Down Expand Up @@ -172,7 +172,7 @@ public async Task OnSolutionStateChangedAsync_SolutionClosedAndReopened_Register
await testSubject.OnSolutionStateChangedAsync(null);
await testSubject.OnSolutionStateChangedAsync(solutionName);

v2Solution.Received().WithAnalyzerReferences(embeddedAnalyzers);
v2Solution.Received().AddAnalyzerReferences(embeddedAnalyzers);
roslynWorkspaceWrapper.Received().TryApplyChanges(v3Solution);
}

Expand All @@ -192,7 +192,7 @@ public async Task OnSolutionStateChangedAsync_SolutionClosedAndReopenedAsBound_R
roslynWorkspaceWrapper.CurrentSolution.Returns(v2Solution); // simulate solution closed and opened, so this is a different version now
await testSubject.OnSolutionStateChangedAsync(solutionName);

v2Solution.Received().WithAnalyzerReferences(connectedAnalyzers);
v2Solution.Received().AddAnalyzerReferences(connectedAnalyzers);
roslynWorkspaceWrapper.Received().TryApplyChanges(v3Solution);
}

Expand All @@ -213,7 +213,7 @@ public async Task OnSolutionStateChangedAsync_DifferentSolutionOpened_RegistersA

Received.InOrder(() =>
{
v1Solution.WithAnalyzerReferences(connectedAnalyzers);
v1Solution.AddAnalyzerReferences(connectedAnalyzers);
roslynWorkspaceWrapper.TryApplyChanges(v2Solution);
});
}
Expand All @@ -228,7 +228,7 @@ public async Task HandleConnectedModeAnalyzerUpdateAsync_Standalone_Ignores()

await testSubject.HandleConnectedModeAnalyzerUpdateAsync(new AnalyzerUpdatedForConnectionEventArgs(connectedAnalyzers));

v1Solution.DidNotReceiveWithAnyArgs().WithAnalyzerReferences(default);
v1Solution.DidNotReceiveWithAnyArgs().AddAnalyzerReferences(default);
v1Solution.DidNotReceiveWithAnyArgs().RemoveAnalyzerReferences(default);
}

Expand All @@ -253,7 +253,7 @@ public async Task HandleConnectedModeAnalyzerUpdateAsync_Connected_NewAnalyzerSe
analyzerComparer.Equals(connectedAnalyzers, differentConnectedAnalyzers);
v1Solution.RemoveAnalyzerReferences(connectedAnalyzers);
roslynWorkspaceWrapper.TryApplyChanges(v2Solution);
v2Solution.WithAnalyzerReferences(differentConnectedAnalyzers);
v2Solution.AddAnalyzerReferences(differentConnectedAnalyzers);
roslynWorkspaceWrapper.TryApplyChanges(v3Solution);
});
}
Expand Down Expand Up @@ -315,7 +315,7 @@ private void SetUpAnalyzerAddition(IRoslynSolutionWrapper originalSolution,
IRoslynSolutionWrapper resultingSolution,
ImmutableArray<AnalyzerFileReference> analyzers)
{
originalSolution.WithAnalyzerReferences(analyzers).Returns(resultingSolution);
originalSolution.AddAnalyzerReferences(analyzers).Returns(resultingSolution);
roslynWorkspaceWrapper.TryApplyChanges(resultingSolution).Returns(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void AddDependencyLocation(string fullPath)

public Assembly LoadFromPath(string fullPath)
{
return Assembly.Load(fullPath);
return Assembly.LoadFrom(fullPath);
}
}
}
6 changes: 3 additions & 3 deletions src/Infrastructure.VS/Roslyn/IRoslynSolutionWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace SonarLint.VisualStudio.Infrastructure.VS.Roslyn;
internal interface IRoslynSolutionWrapper
{
IRoslynSolutionWrapper RemoveAnalyzerReferences(ImmutableArray<AnalyzerFileReference> analyzers);
IRoslynSolutionWrapper WithAnalyzerReferences(ImmutableArray<AnalyzerFileReference> analyzers);
IRoslynSolutionWrapper AddAnalyzerReferences(ImmutableArray<AnalyzerFileReference> analyzers);
Solution GetRoslynSolution();
}

Expand All @@ -43,8 +43,8 @@ public IRoslynSolutionWrapper RemoveAnalyzerReferences(ImmutableArray<AnalyzerFi
? current.RemoveAnalyzerReference(analyzer)
: current));

public IRoslynSolutionWrapper WithAnalyzerReferences(ImmutableArray<AnalyzerFileReference> analyzers) =>
new RoslynSolutionWrapper(solution.WithAnalyzerReferences(analyzers));
public IRoslynSolutionWrapper AddAnalyzerReferences(ImmutableArray<AnalyzerFileReference> analyzers) =>
new RoslynSolutionWrapper(solution.AddAnalyzerReferences(analyzers));

public Solution GetRoslynSolution() => solution;
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private void UpdateAnalyzers(ImmutableArray<AnalyzerFileReference> analyzersToUs

private void AddAnalyzer(ImmutableArray<AnalyzerFileReference> analyzerToUse)
{
if (!roslynWorkspace.TryApplyChanges(roslynWorkspace.CurrentSolution.WithAnalyzerReferences(analyzerToUse)))
if (!roslynWorkspace.TryApplyChanges(roslynWorkspace.CurrentSolution.AddAnalyzerReferences(analyzerToUse)))
{
const string message = "Failed to add analyzer references while adding analyzers";
Debug.Assert(true, message);
Expand Down
1 change: 1 addition & 0 deletions src/Integration.Vsix/Integration.Vsix.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@
<VSIXSourceItem Include="@(ReferenceCopyLocalPaths)" Condition="'%(ReferenceCopyLocalPaths.NuGetPackageId)' == 'System.Threading.Channels'" />
<VSIXSourceItem Include="@(ReferenceCopyLocalPaths)" Condition="'%(ReferenceCopyLocalPaths.NuGetPackageId)' == 'System.Threading.Tasks.Extensions'" />
<VSIXSourceItem Include="@(ReferenceCopyLocalPaths)" Condition="'%(ReferenceCopyLocalPaths.NuGetPackageId)' == 'DiffPlex'" />
<VSIXSourceItem Include="@(ReferenceCopyLocalPaths)" Condition="'%(ReferenceCopyLocalPaths.NuGetPackageId)' == 'Google.Protobuf'" />
</ItemGroup>
</Target>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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 Newtonsoft.Json;
using SonarLint.VisualStudio.SLCore.Service.Analysis.Models;

namespace SonarLint.VisualStudio.SLCore.UnitTests.Service.Analysis;

[TestClass]
public class ShouldUseEnterpriseCSharpAnalyzerParamsTests
{
[TestMethod]
public void Serialize_AsExpected()
{
var testSubject = new ShouldUseEnterpriseCSharpAnalyzerParams("CONFIGURATION_ID");
const string expectedString = """
{
"configurationScopeId": "CONFIGURATION_ID"
}
""";

var serializedString = JsonConvert.SerializeObject(testSubject, Formatting.Indented);

serializedString.Should().Be(expectedString);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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 Newtonsoft.Json;
using SonarLint.VisualStudio.SLCore.Service.Analysis.Models;
using SonarLint.VisualStudio.SLCore.Service.Telemetry;

namespace SonarLint.VisualStudio.SLCore.UnitTests.Service.Analysis;

[TestClass]
public class ShouldUseEnterpriseCSharpAnalyzerResponseTests
{
[TestMethod]
public void Serialize_AsExpected()
{
var testSubject = new ShouldUseEnterpriseCSharpAnalyzerResponse(true);
const string expectedString = """
{
"shouldUseEnterpriseAnalyzer": true
}
""";

var serializedString = JsonConvert.SerializeObject(testSubject, Formatting.Indented);

serializedString.Should().Be(expectedString);
}

[TestMethod]
[DataRow(true)]
[DataRow(false)]
public void Deserialized_AsExpected(bool shouldUseEnterpriseAnalyzer)
{
var expected = new ShouldUseEnterpriseCSharpAnalyzerResponse(shouldUseEnterpriseAnalyzer);
var serialized = $"{{\"shouldUseEnterpriseAnalyzer\":{shouldUseEnterpriseAnalyzer.ToString().ToLower()}}}";

JsonConvert.DeserializeObject<ShouldUseEnterpriseCSharpAnalyzerResponse>(serialized).Should().BeEquivalentTo(expected);
}
}
31 changes: 31 additions & 0 deletions src/SLCore/Service/Analysis/IRoslynAnalyzerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 SonarLint.VisualStudio.SLCore.Core;
using SonarLint.VisualStudio.SLCore.Protocol;
using SonarLint.VisualStudio.SLCore.Service.Analysis.Models;

namespace SonarLint.VisualStudio.SLCore.Service.Analysis;

[JsonRpcClass("analysis")]
public interface IRoslynAnalyzerService : ISLCoreService
{
Task<ShouldUseEnterpriseCSharpAnalyzerResponse> ShouldUseEnterpriseCSharpAnalyzerAsync(ShouldUseEnterpriseCSharpAnalyzerParams shouldUseEnterpriseCsharpParams);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.
*/

namespace SonarLint.VisualStudio.SLCore.Service.Analysis.Models;

public record ShouldUseEnterpriseCSharpAnalyzerParams(string configurationScopeId);
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.
*/

namespace SonarLint.VisualStudio.SLCore.Service.Analysis.Models;

public record ShouldUseEnterpriseCSharpAnalyzerResponse(bool shouldUseEnterpriseAnalyzer);

0 comments on commit 04749b9

Please sign in to comment.