Skip to content

Commit 3c0519f

Browse files
committed
Add PSScriptAnalyzer unit test
1 parent 0cd8652 commit 3c0519f

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public async Task<string> GetCommentHelpText(string functionText, string helpLoc
199199
return null;
200200
}
201201

202-
Hashtable commentHelpSettings = AnalysisService.GetCommentHelpRuleSettings(helpLocation, forBlockComment);
202+
Hashtable commentHelpSettings = GetCommentHelpRuleSettings(helpLocation, forBlockComment);
203203

204204
ScriptFileMarker[] analysisResults = await AnalysisEngine.AnalyzeScriptAsync(functionText, commentHelpSettings).ConfigureAwait(false);
205205

@@ -282,7 +282,7 @@ private void InitializeAnalysisEngineToCurrentSettings()
282282
_analysisEngineLazy = new Lazy<PssaCmdletAnalysisEngine>(() => RecreateAnalysisEngine(currentAnalysisEngine));
283283
}
284284

285-
private PssaCmdletAnalysisEngine InstantiateAnalysisEngine()
285+
internal PssaCmdletAnalysisEngine InstantiateAnalysisEngine()
286286
{
287287
PssaCmdletAnalysisEngine.Builder pssaCmdletEngineBuilder = new(_loggerFactory);
288288

@@ -317,15 +317,15 @@ private PssaCmdletAnalysisEngine RecreateAnalysisEngine(PssaCmdletAnalysisEngine
317317

318318
private bool TryFindSettingsFile(out string settingsFilePath)
319319
{
320-
string configuredPath = _configurationService.CurrentSettings.ScriptAnalysis.SettingsPath;
320+
string configuredPath = _configurationService?.CurrentSettings.ScriptAnalysis.SettingsPath;
321321

322322
if (string.IsNullOrEmpty(configuredPath))
323323
{
324324
settingsFilePath = null;
325325
return false;
326326
}
327327

328-
settingsFilePath = _workspaceService.ResolveWorkspacePath(configuredPath);
328+
settingsFilePath = _workspaceService?.ResolveWorkspacePath(configuredPath);
329329

330330
if (settingsFilePath == null
331331
|| !File.Exists(settingsFilePath))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.Extensions.Logging.Abstractions;
7+
using Microsoft.PowerShell.EditorServices.Hosting;
8+
using Microsoft.PowerShell.EditorServices.Services;
9+
using Microsoft.PowerShell.EditorServices.Services.Analysis;
10+
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
11+
using Microsoft.PowerShell.EditorServices.Test;
12+
using Xunit;
13+
14+
namespace PowerShellEditorServices.Test.Services.Symbols
15+
{
16+
[Trait("Category", "PSScriptAnalyzer")]
17+
public class PSScriptAnalyzerTests
18+
{
19+
private readonly AnalysisService analysisService;
20+
21+
public PSScriptAnalyzerTests() => analysisService = new(
22+
NullLoggerFactory.Instance,
23+
languageServer: null,
24+
configurationService: null,
25+
workspaceService: null,
26+
new HostStartupInfo(
27+
name: "",
28+
profileId: "",
29+
version: null,
30+
psHost: null,
31+
profilePaths: null,
32+
featureFlags: null,
33+
additionalModules: null,
34+
initialSessionState: null,
35+
logPath: null,
36+
logLevel: 0,
37+
consoleReplEnabled: false,
38+
usesLegacyReadLine: false,
39+
bundledModulePath: PsesHostFactory.BundledModulePath));
40+
41+
[Fact]
42+
public async Task CanLoadPSScriptAnalyzer()
43+
{
44+
PssaCmdletAnalysisEngine engine = analysisService.InstantiateAnalysisEngine();
45+
Assert.NotNull(engine);
46+
ScriptFileMarker[] violations = await engine.AnalyzeScriptAsync("function Get-Widgets {Write-Host 'Hello'}").ConfigureAwait(true);
47+
Assert.Collection(violations,
48+
(actual) =>
49+
{
50+
Assert.Empty(actual.Corrections);
51+
Assert.Equal(ScriptFileMarkerLevel.Warning, actual.Level);
52+
Assert.Equal("PSAvoidUsingWriteHost", actual.RuleName);
53+
Assert.Equal("PSScriptAnalyzer", actual.Source);
54+
},
55+
(actual) =>
56+
{
57+
Assert.Single(actual.Corrections);
58+
Assert.Equal("Singularized correction of 'Get-Widgets'", actual.Corrections.First().Name);
59+
Assert.Equal(ScriptFileMarkerLevel.Warning, actual.Level);
60+
Assert.Equal("PSUseSingularNouns", actual.RuleName);
61+
Assert.Equal("PSScriptAnalyzer", actual.Source);
62+
});
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)