-
Notifications
You must be signed in to change notification settings - Fork 217
/
PSScriptAnalyzerTests.cs
107 lines (95 loc) · 4.06 KB
/
PSScriptAnalyzerTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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);
});
}
}
}