Skip to content

Add DoesNotDuplicateScriptMarkersAsync regression test #1870

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

Merged
merged 1 commit into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -122,7 +122,7 @@ public AnalysisService(
/// <summary>
/// The analysis engine to use for running script analysis.
/// </summary>
private PssaCmdletAnalysisEngine AnalysisEngine => _analysisEngineLazy?.Value;
internal PssaCmdletAnalysisEngine AnalysisEngine => _analysisEngineLazy?.Value;

/// <summary>
/// Sets up a script analysis run, eventually returning the result.
Expand Down Expand Up @@ -346,7 +346,7 @@ private void ClearOpenFileMarkers()
}
}

private async Task DelayThenInvokeDiagnosticsAsync(ScriptFile[] filesToAnalyze, CancellationToken cancellationToken)
internal async Task DelayThenInvokeDiagnosticsAsync(ScriptFile[] filesToAnalyze, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
Expand Down Expand Up @@ -409,7 +409,7 @@ private void PublishScriptDiagnostics(ScriptFile scriptFile, IReadOnlyList<Scrip
diagnostics[i] = diagnostic;
}

_languageServer.TextDocument.PublishDiagnostics(new PublishDiagnosticsParams
_languageServer?.TextDocument.PublishDiagnostics(new PublishDiagnosticsParams
{
Uri = scriptFile.DocumentUri,
Diagnostics = new Container<Diagnostic>(diagnostics)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,8 @@ public async Task DebuggerBreaksInUntitledScript()
const string scriptPath = "untitled:Untitled-1";
Assert.True(ScriptFile.IsUntitledPath(scriptPath));
ScriptFile scriptFile = workspace.GetFileBuffer(scriptPath, contents);
Assert.Equal(scriptFile.DocumentUri, scriptPath);
Assert.Equal(scriptFile.Contents, contents);
Assert.Equal(scriptPath, scriptFile.DocumentUri);
Assert.Equal(contents, scriptFile.Contents);
Assert.True(workspace.TryGetFile(scriptPath, out ScriptFile _));

await debugService.SetCommandBreakpointsAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ await psesHost.ExecutePSCommandAsync(
CancellationToken.None).ConfigureAwait(true);

Assert.NotNull(commandAdded);
Assert.Equal(commandAdded.Name, commandName);
Assert.Equal(commandAdded.DisplayName, commandDisplayName);
Assert.Equal(commandName, commandAdded.Name);
Assert.Equal(commandDisplayName, commandAdded.DisplayName);

// Invoke the command
await extensionCommandService.InvokeCommandAsync(commandName, editorContext).ConfigureAwait(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// Licensed under the MIT License.

using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.PowerShell.EditorServices.Hosting;
using Microsoft.PowerShell.EditorServices.Services;
using Microsoft.PowerShell.EditorServices.Services.Analysis;
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
using Microsoft.PowerShell.EditorServices.Test;
using Xunit;
Expand All @@ -16,13 +16,15 @@ namespace PowerShellEditorServices.Test.Services.Symbols
[Trait("Category", "PSScriptAnalyzer")]
public class PSScriptAnalyzerTests
{
private readonly WorkspaceService workspaceService = new(NullLoggerFactory.Instance);
private readonly AnalysisService analysisService;
private const string script = "function Get-Widgets {}";

public PSScriptAnalyzerTests() => analysisService = new(
NullLoggerFactory.Instance,
languageServer: null,
configurationService: null,
workspaceService: null,
workspaceService: workspaceService,
new HostStartupInfo(
name: "",
profileId: "",
Expand All @@ -39,11 +41,13 @@ public class PSScriptAnalyzerTests
bundledModulePath: PsesHostFactory.BundledModulePath));

[Fact]
public async Task CanLoadPSScriptAnalyzer()
public async Task CanLoadPSScriptAnalyzerAsync()
{
PssaCmdletAnalysisEngine engine = analysisService.InstantiateAnalysisEngine();
Assert.NotNull(engine);
ScriptFileMarker[] violations = await engine.AnalyzeScriptAsync("function Get-Widgets {}").ConfigureAwait(true);
ScriptFileMarker[] violations = await analysisService
.AnalysisEngine
.AnalyzeScriptAsync(script)
.ConfigureAwait(true);

Assert.Collection(violations,
(actual) =>
{
Expand All @@ -54,5 +58,23 @@ public async Task CanLoadPSScriptAnalyzer()
Assert.Equal("PSScriptAnalyzer", actual.Source);
});
}

[Fact]
public async Task DoesNotDuplicateScriptMarkersAsync()
{
ScriptFile scriptFile = workspaceService.GetFileBuffer("untitled:Untitled-1", script);
ScriptFile[] scriptFiles = { scriptFile };

await analysisService
.DelayThenInvokeDiagnosticsAsync(scriptFiles, CancellationToken.None)
.ConfigureAwait(true);
Assert.Single(scriptFile.DiagnosticMarkers);

// This is repeated to test that the markers are not duplicated.
await analysisService
.DelayThenInvokeDiagnosticsAsync(scriptFiles, CancellationToken.None)
.ConfigureAwait(true);
Assert.Single(scriptFile.DiagnosticMarkers);
}
}
}