Skip to content

Commit 831f212

Browse files
authored
Unit tests for RazorProjectInfoEndpointPublisher (#10281)
* Unit tests for RazorProjectInfoEndpointPublisher * Removing unnessary using directive * Minor formatting/rename cleanup * 'using' cleanup * Use test accessors per CR suggestion * Misc renames per CR suggestions * Change test accessor to follow reference doc in Roslyn
1 parent 0108bcc commit 831f212

File tree

7 files changed

+328
-16
lines changed

7 files changed

+328
-16
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/ProjectSystem/ProjectInfoEndpoint.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,8 @@ public ProjectInfoEndpoint(ProjectConfigurationStateManager projectConfiguration
3030

3131
public Task HandleNotificationAsync(ProjectInfoParams request, RazorRequestContext requestContext, CancellationToken cancellationToken)
3232
{
33-
RazorProjectInfo? razorProjectInfo = null;
34-
35-
// ProjectInfo will be null if project is being deleted and should be removed
36-
if (request.ProjectInfo is string projectInfoBase64)
37-
{
38-
var projectInfoBytes = Convert.FromBase64String(projectInfoBase64);
39-
using var stream = new MemoryStream(projectInfoBytes);
40-
razorProjectInfo = RazorProjectInfoDeserializer.Instance.DeserializeFromStream(stream);
41-
}
33+
var razorProjectInfo =
34+
RazorProjectInfoDeserializer.Instance.DeserializeFromString(request.ProjectInfo);
4235

4336
var projectKey = ProjectKey.FromString(request.ProjectKeyId);
4437

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Serialization/IRazorProjectInfoDeserializer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Serialization;
88

99
internal interface IRazorProjectInfoDeserializer
1010
{
11+
RazorProjectInfo? DeserializeFromString(string? base64String);
1112
RazorProjectInfo? DeserializeFromFile(string filePath);
1213
RazorProjectInfo? DeserializeFromStream(Stream stream);
1314
}

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Serialization/RazorProjectInfoDeserializer.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

4+
using System;
45
using System.IO;
56
using Microsoft.AspNetCore.Razor.ProjectSystem;
67

@@ -14,6 +15,21 @@ private RazorProjectInfoDeserializer()
1415
{
1516
}
1617

18+
public RazorProjectInfo? DeserializeFromString(string? base64String)
19+
{
20+
RazorProjectInfo? razorProjectInfo = null;
21+
22+
// ProjectInfo will be null if project is being deleted and should be removed
23+
if (base64String is not null)
24+
{
25+
var projectInfoBytes = Convert.FromBase64String(base64String);
26+
using var stream = new MemoryStream(projectInfoBytes);
27+
razorProjectInfo = DeserializeFromStream(stream);
28+
}
29+
30+
return razorProjectInfo;
31+
}
32+
1733
public RazorProjectInfo? DeserializeFromFile(string filePath)
1834
{
1935
using var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/IProjectSnapshotExtensions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Collections.Immutable;
56
using System.Diagnostics;
7+
using System.IO;
68
using System.Threading;
79
using Microsoft.AspNetCore.Razor.Language;
810
using Microsoft.AspNetCore.Razor.PooledObjects;
@@ -37,6 +39,17 @@ public static RazorProjectInfo ToRazorProjectInfo(this IProjectSnapshot project,
3739
documents: documents.DrainToImmutable());
3840
}
3941

42+
public static string ToBase64EncodedProjectInfo(this IProjectSnapshot project, string serializedFilePath)
43+
{
44+
var projectInfo = project.ToRazorProjectInfo(serializedFilePath);
45+
46+
using var stream = new MemoryStream();
47+
projectInfo.SerializeTo(stream);
48+
var base64ProjectInfo = Convert.ToBase64String(stream.ToArray());
49+
50+
return base64ProjectInfo;
51+
}
52+
4053
public static ImmutableArray<TagHelperDescriptor> GetTagHelpersSynchronously(this IProjectSnapshot projectSnapshot)
4154
{
4255
var canResolveTagHelpersSynchronously = projectSnapshot is ProjectSnapshot ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT license. See License.txt in the project root for license information.
3+
4+
using System.Threading.Tasks;
5+
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
6+
7+
namespace Microsoft.VisualStudio.Razor.LanguageClient.ProjectSystem;
8+
9+
internal partial class RazorProjectInfoEndpointPublisher
10+
{
11+
internal TestAccessor GetTestAccessor()
12+
=> new(this);
13+
14+
internal sealed class TestAccessor(RazorProjectInfoEndpointPublisher instance)
15+
{
16+
/// <summary>
17+
/// Allows unit tests to imitate ProjectManager.Changed event firing
18+
/// </summary>
19+
public void ProjectManager_Changed(object sender, ProjectChangeEventArgs args)
20+
=> instance.ProjectManager_Changed(sender, args);
21+
22+
/// <summary>
23+
/// Allows unit tests to enqueue project update directly.
24+
/// </summary>
25+
public void EnqueuePublish(IProjectSnapshot projectSnapshot)
26+
=> instance.EnqueuePublish(projectSnapshot);
27+
28+
/// <summary>
29+
/// Allows unit tests to wait for all work queue items to be processed.
30+
/// </summary>
31+
public Task WaitUntilCurrentBatchCompletesAsync()
32+
=> instance._workQueue.WaitUntilCurrentBatchCompletesAsync();
33+
}
34+
}

src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/LanguageClient/ProjectSystem/RazorProjectInfoEndpointPublisher.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Collections.Immutable;
66
using System.ComponentModel.Composition;
77
using System.Diagnostics;
8-
using System.IO;
98
using System.Threading;
109
using System.Threading.Tasks;
1110
using Microsoft.AspNetCore.Razor;
@@ -158,17 +157,12 @@ private ValueTask ProcessBatchAsync(ImmutableArray<(IProjectSnapshot Project, bo
158157

159158
private void RemovePublishingData(IProjectSnapshot projectSnapshot, CancellationToken cancellationToken)
160159
{
161-
// This should never get called if we are inactive, so don't check _active flag
162160
ImmediatePublish(projectSnapshot.Key, encodedProjectInfo: null, cancellationToken);
163161
}
164162

165163
private void ImmediatePublish(IProjectSnapshot projectSnapshot, CancellationToken cancellationToken)
166164
{
167-
using var stream = new MemoryStream();
168-
169-
var projectInfo = projectSnapshot.ToRazorProjectInfo(projectSnapshot.IntermediateOutputPath);
170-
projectInfo.SerializeTo(stream);
171-
var base64ProjectInfo = Convert.ToBase64String(stream.ToArray());
165+
var base64ProjectInfo = projectSnapshot.ToBase64EncodedProjectInfo(projectSnapshot.IntermediateOutputPath);
172166

173167
ImmediatePublish(projectSnapshot.Key, base64ProjectInfo, cancellationToken);
174168
}

0 commit comments

Comments
 (0)