initial
This commit is contained in:
commit
baa0056244
352 changed files with 47928 additions and 0 deletions
|
|
@ -0,0 +1,76 @@
|
|||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.PowerShell.EditorServices.Services;
|
||||
using Microsoft.PowerShell.EditorServices.Services.Symbols;
|
||||
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
|
||||
using Microsoft.PowerShell.EditorServices.Test.Shared;
|
||||
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
|
||||
using Xunit;
|
||||
|
||||
namespace PowerShellEditorServices.Test.Services.Symbols
|
||||
{
|
||||
[Trait("Category", "AstOperations")]
|
||||
public class AstOperationsTests
|
||||
{
|
||||
private readonly ScriptFile scriptFile;
|
||||
|
||||
public AstOperationsTests()
|
||||
{
|
||||
WorkspaceService workspace = new(NullLoggerFactory.Instance);
|
||||
scriptFile = workspace.GetFile(TestUtilities.GetSharedPath("References/FunctionReference.ps1"));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1, 15, "fn BasicFunction")]
|
||||
[InlineData(2, 3, "fn BasicFunction")]
|
||||
[InlineData(4, 31, "fn FunctionWithExtraSpace")]
|
||||
[InlineData(7, 18, "fn FunctionWithExtraSpace")]
|
||||
[InlineData(12, 22, "fn FunctionNameOnDifferentLine")]
|
||||
[InlineData(22, 13, "fn FunctionNameOnDifferentLine")]
|
||||
[InlineData(24, 30, "fn IndentedFunction")]
|
||||
[InlineData(24, 52, "fn IndentedFunction")]
|
||||
public void CanFindSymbolAtPosition(int line, int column, string expectedName)
|
||||
{
|
||||
SymbolReference symbol = scriptFile.References.TryGetSymbolAtPosition(line, column);
|
||||
Assert.NotNull(symbol);
|
||||
Assert.Equal(expectedName, symbol.Id);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(FindReferencesOfSymbolAtPositionData))]
|
||||
public void CanFindReferencesOfSymbolAtPosition(int line, int column, Range[] symbolRange)
|
||||
{
|
||||
SymbolReference symbol = scriptFile.References.TryGetSymbolAtPosition(line, column);
|
||||
|
||||
IEnumerable<SymbolReference> references = scriptFile.References.TryGetReferences(symbol);
|
||||
Assert.NotEmpty(references);
|
||||
|
||||
int positionsIndex = 0;
|
||||
foreach (SymbolReference reference in references.OrderBy((i) => i.ScriptRegion.ToRange().Start))
|
||||
{
|
||||
Assert.Equal(symbolRange[positionsIndex].Start.Line, reference.NameRegion.StartLineNumber);
|
||||
Assert.Equal(symbolRange[positionsIndex].Start.Character, reference.NameRegion.StartColumnNumber);
|
||||
Assert.Equal(symbolRange[positionsIndex].End.Line, reference.NameRegion.EndLineNumber);
|
||||
Assert.Equal(symbolRange[positionsIndex].End.Character, reference.NameRegion.EndColumnNumber);
|
||||
|
||||
positionsIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
public static object[][] FindReferencesOfSymbolAtPositionData { get; } = new object[][]
|
||||
{
|
||||
new object[] { 1, 15, new[] { new Range(1, 10, 1, 23), new Range(2, 1, 2, 14) } },
|
||||
new object[] { 2, 3, new[] { new Range(1, 10, 1, 23), new Range(2, 1, 2, 14) } },
|
||||
new object[] { 4, 31, new[] { new Range(4, 19, 4, 41), new Range(7, 3, 7, 25) } },
|
||||
new object[] { 7, 18, new[] { new Range(4, 19, 4, 41), new Range(7, 3, 7, 25) } },
|
||||
new object[] { 22, 13, new[] { new Range(12, 8, 12, 35), new Range(22, 5, 22, 32) } },
|
||||
new object[] { 12, 22, new[] { new Range(12, 8, 12, 35), new Range(22, 5, 22, 32) } },
|
||||
new object[] { 24, 30, new[] { new Range(24, 22, 24, 38), new Range(24, 44, 24, 60) } },
|
||||
new object[] { 24, 52, new[] { new Range(24, 22, 24, 38), new Range(24, 44, 24, 60) } },
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,107 @@
|
|||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
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.TextDocument;
|
||||
using Microsoft.PowerShell.EditorServices.Test;
|
||||
using Xunit;
|
||||
|
||||
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 Do-Work {}";
|
||||
|
||||
public PSScriptAnalyzerTests() => analysisService = new(
|
||||
NullLoggerFactory.Instance,
|
||||
languageServer: null,
|
||||
configurationService: null,
|
||||
workspaceService: workspaceService,
|
||||
new HostStartupInfo(
|
||||
name: "",
|
||||
profileId: "",
|
||||
version: null,
|
||||
psHost: null,
|
||||
profilePaths: null,
|
||||
featureFlags: null,
|
||||
additionalModules: null,
|
||||
initialSessionState: null,
|
||||
logPath: null,
|
||||
logLevel: 0,
|
||||
consoleReplEnabled: false,
|
||||
useNullPSHostUI: true,
|
||||
usesLegacyReadLine: false,
|
||||
bundledModulePath: PsesHostFactory.BundledModulePath));
|
||||
|
||||
[Fact]
|
||||
public void IncludesDefaultRules()
|
||||
{
|
||||
Assert.Null(analysisService.AnalysisEngine._settingsParameter);
|
||||
Assert.Equal(AnalysisService.s_defaultRules, analysisService.AnalysisEngine._rulesToInclude);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CanLoadPSScriptAnalyzerAsync()
|
||||
{
|
||||
ScriptFileMarker[] violations = await analysisService
|
||||
.AnalysisEngine
|
||||
.AnalyzeScriptAsync(script);
|
||||
|
||||
Assert.Collection(violations,
|
||||
(actual) =>
|
||||
{
|
||||
Assert.Empty(actual.Corrections);
|
||||
Assert.Equal(ScriptFileMarkerLevel.Warning, actual.Level);
|
||||
Assert.Equal("The cmdlet 'Do-Work' uses an unapproved verb.", actual.Message);
|
||||
Assert.Equal("PSUseApprovedVerbs", actual.RuleName);
|
||||
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);
|
||||
Assert.Single(scriptFile.DiagnosticMarkers);
|
||||
|
||||
// This is repeated to test that the markers are not duplicated.
|
||||
await analysisService
|
||||
.DelayThenInvokeDiagnosticsAsync(scriptFiles, CancellationToken.None);
|
||||
Assert.Single(scriptFile.DiagnosticMarkers);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DoesNotClearParseErrorsAsync()
|
||||
{
|
||||
// Causing a missing closing } parser error
|
||||
ScriptFile scriptFile = workspaceService.GetFileBuffer("untitled:Untitled-2", script.TrimEnd('}'));
|
||||
ScriptFile[] scriptFiles = { scriptFile };
|
||||
|
||||
await analysisService
|
||||
.DelayThenInvokeDiagnosticsAsync(scriptFiles, CancellationToken.None);
|
||||
|
||||
Assert.Collection(scriptFile.DiagnosticMarkers,
|
||||
(actual) =>
|
||||
{
|
||||
Assert.Equal("Missing closing '}' in statement block or type definition.", actual.Message);
|
||||
Assert.Equal("PowerShell", actual.Source);
|
||||
},
|
||||
(actual) =>
|
||||
{
|
||||
Assert.Equal("PSUseApprovedVerbs", actual.RuleName);
|
||||
Assert.Equal("PSScriptAnalyzer", actual.Source);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue