Skip to content

Commit 8cda531

Browse files
authored
Add setting to disable Pester CodeLens (#1585)
1 parent ccf1a8d commit 8cda531

File tree

6 files changed

+100
-15
lines changed

6 files changed

+100
-15
lines changed

src/PowerShellEditorServices/Services/CodeLens/PesterCodeLensProvider.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
3-
3+
using System;
44
using System.Collections.Generic;
55
using Microsoft.PowerShell.EditorServices.Services;
66
using Microsoft.PowerShell.EditorServices.Services.Symbols;
@@ -101,6 +101,12 @@ private static CodeLens[] GetPesterLens(PesterSymbolReference pesterSymbol, Scri
101101
/// <returns>All Pester CodeLenses for the given script file.</returns>
102102
public CodeLens[] ProvideCodeLenses(ScriptFile scriptFile)
103103
{
104+
// Don't return anything if codelens setting is disabled
105+
if (!this._configurationService.CurrentSettings.Pester.CodeLens)
106+
{
107+
return Array.Empty<CodeLens>();
108+
}
109+
104110
var lenses = new List<CodeLens>();
105111
foreach (SymbolReference symbol in _symbolProvider.ProvideDocumentSymbols(scriptFile))
106112
{

src/PowerShellEditorServices/Services/Symbols/SymbolsService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ public SymbolsService(
6060
var codeLensProviders = new ICodeLensProvider[]
6161
{
6262
new ReferencesCodeLensProvider(_workspaceService, this),
63-
new PesterCodeLensProvider(configurationService),
63+
new PesterCodeLensProvider(configurationService)
6464
};
65+
6566
foreach (ICodeLensProvider codeLensProvider in codeLensProviders)
6667
{
6768
_codeLensProviders.TryAdd(codeLensProvider.ProviderId, codeLensProvider);

src/PowerShellEditorServices/Services/Workspace/LanguageServerSettings.cs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void Update(
5656
logger);
5757
this.CodeFormatting = new CodeFormattingSettings(settings.CodeFormatting);
5858
this.CodeFolding.Update(settings.CodeFolding, logger);
59-
this.Pester = new PesterSettings(settings.Pester);
59+
this.Pester.Update(settings.Pester, logger);
6060
this.Cwd = settings.Cwd;
6161
}
6262
}
@@ -384,19 +384,39 @@ public void Update(
384384
/// </summary>
385385
public class PesterSettings
386386
{
387-
public PesterSettings()
388-
{
389-
}
390-
391-
public PesterSettings(PesterSettings settings)
392-
{
393-
UseLegacyCodeLens = settings.UseLegacyCodeLens;
394-
}
387+
/// <summary>
388+
/// If specified, the lenses "run tests" and "debug tests" will appear above all Pester tests
389+
/// </summary>
390+
public bool CodeLens { get; set; } = true;
395391

396392
/// <summary>
397393
/// Whether integration features specific to Pester v5 are enabled
398394
/// </summary>
399-
public bool UseLegacyCodeLens { get; set; }
395+
public bool UseLegacyCodeLens { get; set; } = false;
396+
397+
/// <summary>
398+
/// Update these settings from another settings object
399+
/// </summary>
400+
public void Update(
401+
PesterSettings settings,
402+
ILogger logger)
403+
{
404+
if (settings is null) {
405+
return;
406+
}
407+
408+
if (this.CodeLens != settings.CodeLens)
409+
{
410+
this.CodeLens = settings.CodeLens;
411+
logger.LogTrace(string.Format("Using Pester Code Lens - {0}", this.CodeLens));
412+
}
413+
414+
if (this.UseLegacyCodeLens != settings.UseLegacyCodeLens)
415+
{
416+
this.UseLegacyCodeLens = settings.UseLegacyCodeLens;
417+
logger.LogTrace(string.Format("Using Pester Legacy Code Lens - {0}", this.UseLegacyCodeLens));
418+
}
419+
}
400420
}
401421

402422
/// <summary>

test/PowerShellEditorServices.Test.E2E/LSPTestsFixures.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ public class LSPTestsFixture : IAsyncLifetime
3838
public ITestOutputHelper Output { get; set; }
3939

4040
protected PsesStdioProcess _psesProcess;
41+
public int ProcessId => _psesProcess.Id;
4142

4243
public async Task InitializeAsync()
4344
{
4445
var factory = new LoggerFactory();
4546
_psesProcess = new PsesStdioProcess(factory, IsDebugAdapterTests);
4647
await _psesProcess.Start().ConfigureAwait(false);
47-
48+
Console.WriteLine("PowerShell Editor Services Server started with PID {0}", ProcessId);
49+
// TIP: Add Breakpoint here and attach debugger using the PID from the above message
4850
Diagnostics = new List<Diagnostic>();
4951
TelemetryEvents = new List<PsesTelemetryEvent>();
5052
DirectoryInfo testdir =

test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,8 @@ public async Task CanSendPesterLegacyCodeLensRequestAsync()
661661
{
662662
""powershell"": {
663663
""pester"": {
664-
""useLegacyCodeLens"": true
664+
""useLegacyCodeLens"": true,
665+
""codeLens"": true
665666
}
666667
}
667668
}
@@ -727,7 +728,8 @@ public async Task CanSendPesterCodeLensRequestAsync()
727728
{
728729
""powershell"": {
729730
""pester"": {
730-
""useLegacyCodeLens"": false
731+
""useLegacyCodeLens"": false,
732+
""codeLens"": true
731733
}
732734
}
733735
}
@@ -825,6 +827,50 @@ public async Task CanSendPesterCodeLensRequestAsync()
825827
});
826828
}
827829

830+
[Trait("Category", "LSP")]
831+
[Fact]
832+
public async Task NoMessageIfPesterCodeLensDisabled()
833+
{
834+
// Make sure Pester legacy CodeLens is disabled because we'll need it in this test.
835+
PsesLanguageClient.Workspace.DidChangeConfiguration(
836+
new DidChangeConfigurationParams
837+
{
838+
Settings = JObject.Parse(@"
839+
{
840+
""powershell"": {
841+
""pester"": {
842+
""codeLens"": false
843+
}
844+
}
845+
}
846+
")
847+
});
848+
849+
string filePath = NewTestFile(@"
850+
Describe 'DescribeName' {
851+
Context 'ContextName' {
852+
It 'ItName' {
853+
1 | Should - Be 1
854+
}
855+
}
856+
}
857+
", isPester: true);
858+
859+
CodeLensContainer codeLenses = await PsesLanguageClient
860+
.SendRequest<CodeLensParams>(
861+
"textDocument/codeLens",
862+
new CodeLensParams
863+
{
864+
TextDocument = new TextDocumentIdentifier
865+
{
866+
Uri = new Uri(filePath)
867+
}
868+
})
869+
.Returning<CodeLensContainer>(CancellationToken.None).ConfigureAwait(false);
870+
871+
Assert.Empty(codeLenses);
872+
}
873+
828874
[Trait("Category", "LSP")]
829875
[Fact]
830876
public async Task CanSendReferencesCodeLensRequestAsync()

test/PowerShellEditorServices.Test.E2E/Processes/StdioServerProcess.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ public StdioServerProcess(ILoggerFactory loggerFactory, ProcessStartInfo serverS
4545
_serverStartInfo = serverStartInfo;
4646
}
4747

48+
/// <summary>
49+
/// The process ID of the server process, useful for attaching a debugger.
50+
/// </summary>
51+
public int Id
52+
{
53+
get {
54+
return _serverProcess.Id;
55+
}
56+
}
57+
4858
/// <summary>
4959
/// Dispose of resources being used by the launcher.
5060
/// </summary>

0 commit comments

Comments
 (0)