Skip to content

Commit 7853976

Browse files
Add DoesNotDuplicateScriptMarkersAsync regression test (#1870)
1 parent c5a55fe commit 7853976

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
lines changed

Diff for: src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public AnalysisService(
122122
/// <summary>
123123
/// The analysis engine to use for running script analysis.
124124
/// </summary>
125-
private PssaCmdletAnalysisEngine AnalysisEngine => _analysisEngineLazy?.Value;
125+
internal PssaCmdletAnalysisEngine AnalysisEngine => _analysisEngineLazy?.Value;
126126

127127
/// <summary>
128128
/// Sets up a script analysis run, eventually returning the result.
@@ -346,7 +346,7 @@ private void ClearOpenFileMarkers()
346346
}
347347
}
348348

349-
private async Task DelayThenInvokeDiagnosticsAsync(ScriptFile[] filesToAnalyze, CancellationToken cancellationToken)
349+
internal async Task DelayThenInvokeDiagnosticsAsync(ScriptFile[] filesToAnalyze, CancellationToken cancellationToken)
350350
{
351351
if (cancellationToken.IsCancellationRequested)
352352
{
@@ -409,7 +409,7 @@ private void PublishScriptDiagnostics(ScriptFile scriptFile, IReadOnlyList<Scrip
409409
diagnostics[i] = diagnostic;
410410
}
411411

412-
_languageServer.TextDocument.PublishDiagnostics(new PublishDiagnosticsParams
412+
_languageServer?.TextDocument.PublishDiagnostics(new PublishDiagnosticsParams
413413
{
414414
Uri = scriptFile.DocumentUri,
415415
Diagnostics = new Container<Diagnostic>(diagnostics)

Diff for: test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,8 @@ public async Task DebuggerBreaksInUntitledScript()
532532
const string scriptPath = "untitled:Untitled-1";
533533
Assert.True(ScriptFile.IsUntitledPath(scriptPath));
534534
ScriptFile scriptFile = workspace.GetFileBuffer(scriptPath, contents);
535-
Assert.Equal(scriptFile.DocumentUri, scriptPath);
536-
Assert.Equal(scriptFile.Contents, contents);
535+
Assert.Equal(scriptPath, scriptFile.DocumentUri);
536+
Assert.Equal(contents, scriptFile.Contents);
537537
Assert.True(workspace.TryGetFile(scriptPath, out ScriptFile _));
538538

539539
await debugService.SetCommandBreakpointsAsync(

Diff for: test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ await psesHost.ExecutePSCommandAsync(
7272
CancellationToken.None).ConfigureAwait(true);
7373

7474
Assert.NotNull(commandAdded);
75-
Assert.Equal(commandAdded.Name, commandName);
76-
Assert.Equal(commandAdded.DisplayName, commandDisplayName);
75+
Assert.Equal(commandName, commandAdded.Name);
76+
Assert.Equal(commandDisplayName, commandAdded.DisplayName);
7777

7878
// Invoke the command
7979
await extensionCommandService.InvokeCommandAsync(commandName, editorContext).ConfigureAwait(true);

Diff for: test/PowerShellEditorServices.Test/Services/Symbols/PSScriptAnalyzerTests.cs

+28-6
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// Licensed under the MIT License.
33

44
using System.Linq;
5+
using System.Threading;
56
using System.Threading.Tasks;
67
using Microsoft.Extensions.Logging.Abstractions;
78
using Microsoft.PowerShell.EditorServices.Hosting;
89
using Microsoft.PowerShell.EditorServices.Services;
9-
using Microsoft.PowerShell.EditorServices.Services.Analysis;
1010
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
1111
using Microsoft.PowerShell.EditorServices.Test;
1212
using Xunit;
@@ -16,13 +16,15 @@ namespace PowerShellEditorServices.Test.Services.Symbols
1616
[Trait("Category", "PSScriptAnalyzer")]
1717
public class PSScriptAnalyzerTests
1818
{
19+
private readonly WorkspaceService workspaceService = new(NullLoggerFactory.Instance);
1920
private readonly AnalysisService analysisService;
21+
private const string script = "function Get-Widgets {}";
2022

2123
public PSScriptAnalyzerTests() => analysisService = new(
2224
NullLoggerFactory.Instance,
2325
languageServer: null,
2426
configurationService: null,
25-
workspaceService: null,
27+
workspaceService: workspaceService,
2628
new HostStartupInfo(
2729
name: "",
2830
profileId: "",
@@ -39,11 +41,13 @@ public class PSScriptAnalyzerTests
3941
bundledModulePath: PsesHostFactory.BundledModulePath));
4042

4143
[Fact]
42-
public async Task CanLoadPSScriptAnalyzer()
44+
public async Task CanLoadPSScriptAnalyzerAsync()
4345
{
44-
PssaCmdletAnalysisEngine engine = analysisService.InstantiateAnalysisEngine();
45-
Assert.NotNull(engine);
46-
ScriptFileMarker[] violations = await engine.AnalyzeScriptAsync("function Get-Widgets {}").ConfigureAwait(true);
46+
ScriptFileMarker[] violations = await analysisService
47+
.AnalysisEngine
48+
.AnalyzeScriptAsync(script)
49+
.ConfigureAwait(true);
50+
4751
Assert.Collection(violations,
4852
(actual) =>
4953
{
@@ -54,5 +58,23 @@ public async Task CanLoadPSScriptAnalyzer()
5458
Assert.Equal("PSScriptAnalyzer", actual.Source);
5559
});
5660
}
61+
62+
[Fact]
63+
public async Task DoesNotDuplicateScriptMarkersAsync()
64+
{
65+
ScriptFile scriptFile = workspaceService.GetFileBuffer("untitled:Untitled-1", script);
66+
ScriptFile[] scriptFiles = { scriptFile };
67+
68+
await analysisService
69+
.DelayThenInvokeDiagnosticsAsync(scriptFiles, CancellationToken.None)
70+
.ConfigureAwait(true);
71+
Assert.Single(scriptFile.DiagnosticMarkers);
72+
73+
// This is repeated to test that the markers are not duplicated.
74+
await analysisService
75+
.DelayThenInvokeDiagnosticsAsync(scriptFiles, CancellationToken.None)
76+
.ConfigureAwait(true);
77+
Assert.Single(scriptFile.DiagnosticMarkers);
78+
}
5779
}
5880
}

0 commit comments

Comments
 (0)