Skip to content

Run single test in Pester v5 #1167

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 13 commits into from
Mar 12, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System;
using System.Collections.Generic;
using Microsoft.PowerShell.EditorServices.Services;
using Microsoft.PowerShell.EditorServices.Services.Symbols;
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
using Microsoft.PowerShell.EditorServices.Utility;
Expand All @@ -14,6 +16,7 @@ namespace Microsoft.PowerShell.EditorServices.CodeLenses
{
internal class PesterCodeLensProvider : ICodeLensProvider
{
private readonly ConfigurationService _configurationService;

/// <summary>
/// The symbol provider to get symbols from to build code lenses with.
Expand All @@ -29,8 +32,9 @@ internal class PesterCodeLensProvider : ICodeLensProvider
/// <summary>
/// Create a new Pester CodeLens provider for a given editor session.
/// </summary>
public PesterCodeLensProvider()
public PesterCodeLensProvider(ConfigurationService configurationService)
{
_configurationService = configurationService;
_symbolProvider = new PesterDocumentSymbolProvider();
}

Expand All @@ -42,7 +46,7 @@ public PesterCodeLensProvider()
/// <returns>All CodeLenses for the given Pester symbol.</returns>
private CodeLens[] GetPesterLens(PesterSymbolReference pesterSymbol, ScriptFile scriptFile)
{

string word = pesterSymbol.Command == PesterCommandType.It ? "test" : "tests";
var codeLensResults = new CodeLens[]
{
new CodeLens()
Expand All @@ -55,7 +59,7 @@ private CodeLens[] GetPesterLens(PesterSymbolReference pesterSymbol, ScriptFile
Command = new Command()
{
Name = "PowerShell.RunPesterTests",
Title = "Run tests",
Title = $"Run {word}",
Arguments = JArray.FromObject(new object[] {
scriptFile.DocumentUri,
false /* No debug */,
Expand All @@ -74,7 +78,7 @@ private CodeLens[] GetPesterLens(PesterSymbolReference pesterSymbol, ScriptFile
Command = new Command()
{
Name = "PowerShell.RunPesterTests",
Title = "Debug tests",
Title = $"Debug {word}",
Arguments = JArray.FromObject(new object[] {
scriptFile.DocumentUri,
true /* No debug */,
Expand All @@ -97,15 +101,18 @@ public CodeLens[] ProvideCodeLenses(ScriptFile scriptFile)
var lenses = new List<CodeLens>();
foreach (SymbolReference symbol in _symbolProvider.ProvideDocumentSymbols(scriptFile))
{
if (symbol is PesterSymbolReference pesterSymbol)
if (!(symbol is PesterSymbolReference pesterSymbol))
{
if (pesterSymbol.Command != PesterCommandType.Describe)
{
continue;
}
continue;
}

lenses.AddRange(GetPesterLens(pesterSymbol, scriptFile));
if (_configurationService.CurrentSettings.Pester.EnableLegacyCodeLens
&& pesterSymbol.Command != PesterCommandType.Describe)
{
continue;
}

lenses.AddRange(GetPesterLens(pesterSymbol, scriptFile));
}

return lenses.ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ internal class SymbolsService
public SymbolsService(
ILoggerFactory factory,
PowerShellContextService powerShellContextService,
WorkspaceService workspaceService)
WorkspaceService workspaceService,
ConfigurationService configurationService)
{
_logger = factory.CreateLogger<SymbolsService>();
_powerShellContextService = powerShellContextService;
Expand All @@ -61,7 +62,7 @@ public SymbolsService(
var codeLensProviders = new ICodeLensProvider[]
{
new ReferencesCodeLensProvider(_workspaceService, this),
new PesterCodeLensProvider(),
new PesterCodeLensProvider(configurationService),
};
foreach (ICodeLensProvider codeLensProvider in codeLensProviders)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal class CodeLensHandlers : ICodeLensHandler, ICodeLensResolveHandler

private CodeLensCapability _capability;

public CodeLensHandlers(ILoggerFactory factory, SymbolsService symbolsService, WorkspaceService workspaceService)
public CodeLensHandlers(ILoggerFactory factory, SymbolsService symbolsService, WorkspaceService workspaceService, ConfigurationService configurationService)
{
_logger = factory.CreateLogger<FoldingRangeHandler>();
_workspaceService = workspaceService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ internal class LanguageServerSettings

public CodeFoldingSettings CodeFolding { get; set; }

public PesterSettings Pester { get; set; }

public LanguageServerSettings()
{
this.ScriptAnalysis = new ScriptAnalysisSettings();
this.CodeFormatting = new CodeFormattingSettings();
this.CodeFolding = new CodeFoldingSettings();
this.Pester = new PesterSettings();
}

public void Update(
Expand All @@ -49,6 +52,7 @@ public void Update(
logger);
this.CodeFormatting = new CodeFormattingSettings(settings.CodeFormatting);
this.CodeFolding.Update(settings.CodeFolding, logger);
this.Pester = new PesterSettings(settings.Pester);
}
}
}
Expand Down Expand Up @@ -356,6 +360,26 @@ public void Update(
}
}

/// <summary>
/// Pester settings
/// </summary>
public class PesterSettings
{
public PesterSettings()
{
}

public PesterSettings(PesterSettings settings)
{
EnableLegacyCodeLens = settings.EnableLegacyCodeLens;
}

/// <summary>
/// Whether integration features specific to Pester v5 are enabled
/// </summary>
public bool EnableLegacyCodeLens { get; set; }
}

/// <summary>
/// Additional settings from the Language Client that affect Language Server operations but
/// do not exist under the 'powershell' section
Expand Down
6 changes: 3 additions & 3 deletions test/PowerShellEditorServices.Test.E2E/LSPTestsFixures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public async override Task CustomInitializeAsync(
// Make sure Script Analysis is enabled because we'll need it in the tests.
LanguageClient.Workspace.DidChangeConfiguration(JObject.Parse(@"
{
""PowerShell"": {
""ScriptAnalysis"": {
""Enable"": true
""powershell"": {
""scriptAnalysis"": {
""enable"": true
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,9 @@ public async Task CanReceiveDiagnosticsFromConfigurationChange()
{
Settings = JToken.Parse(@"
{
""PowerShell"": {
""ScriptAnalysis"": {
""Enable"": false
""powershell"": {
""scriptAnalysis"": {
""enable"": false
}
}
}
Expand All @@ -216,9 +216,9 @@ public async Task CanReceiveDiagnosticsFromConfigurationChange()
{
Settings = JToken.Parse(@"
{
""PowerShell"": {
""ScriptAnalysis"": {
""Enable"": true
""powershell"": {
""scriptAnalysis"": {
""enable"": true
}
}
}
Expand Down Expand Up @@ -550,8 +550,19 @@ await LanguageClient.SendRequest<RunspaceResponse[]>(
}

[Fact]
public async Task CanSendPesterCodeLensRequest()
public async Task CanSendPesterLegacyCodeLensRequest()
{
// Make sure LegacyCodeLens is enabled because we'll need it in this test.
LanguageClient.Workspace.DidChangeConfiguration(JObject.Parse(@"
{
""powershell"": {
""pester"": {
""enableLegacyCodeLens"": true
}
}
}
"));

string filePath = NewTestFile(@"
Describe 'DescribeName' {
Context 'ContextName' {
Expand Down Expand Up @@ -597,6 +608,109 @@ public async Task CanSendPesterCodeLensRequest()
});
}

[Fact]
public async Task CanSendPesterCodeLensRequest()
{
// Make sure Pester legacy CodeLens is disabled because we'll need it in this test.
LanguageClient.Workspace.DidChangeConfiguration(JObject.Parse(@"
{
""powershell"": {
""pester"": {
""enableLegacyCodeLens"": false
}
}
}
"));

string filePath = NewTestFile(@"
Describe 'DescribeName' {
Context 'ContextName' {
It 'ItName' {
1 | Should - Be 1
}
}
}
", isPester: true);

CodeLensContainer codeLenses = await LanguageClient.SendRequest<CodeLensContainer>(
"textDocument/codeLens",
new CodeLensParams
{
TextDocument = new TextDocumentIdentifier
{
Uri = new Uri(filePath)
}
});

Assert.Collection(codeLenses,
codeLens =>
{
Range range = codeLens.Range;

Assert.Equal(1, range.Start.Line);
Assert.Equal(0, range.Start.Character);
Assert.Equal(7, range.End.Line);
Assert.Equal(1, range.End.Character);

Assert.Equal("Run tests", codeLens.Command.Title);
},
codeLens =>
{
Range range = codeLens.Range;

Assert.Equal(1, range.Start.Line);
Assert.Equal(0, range.Start.Character);
Assert.Equal(7, range.End.Line);
Assert.Equal(1, range.End.Character);

Assert.Equal("Debug tests", codeLens.Command.Title);
},
codeLens =>
{
Range range = codeLens.Range;

Assert.Equal(2, range.Start.Line);
Assert.Equal(4, range.Start.Character);
Assert.Equal(6, range.End.Line);
Assert.Equal(5, range.End.Character);

Assert.Equal("Run tests", codeLens.Command.Title);
},
codeLens =>
{
Range range = codeLens.Range;

Assert.Equal(2, range.Start.Line);
Assert.Equal(4, range.Start.Character);
Assert.Equal(6, range.End.Line);
Assert.Equal(5, range.End.Character);

Assert.Equal("Debug tests", codeLens.Command.Title);
},
codeLens =>
{
Range range = codeLens.Range;

Assert.Equal(3, range.Start.Line);
Assert.Equal(8, range.Start.Character);
Assert.Equal(5, range.End.Line);
Assert.Equal(9, range.End.Character);

Assert.Equal("Run test", codeLens.Command.Title);
},
codeLens =>
{
Range range = codeLens.Range;

Assert.Equal(3, range.Start.Line);
Assert.Equal(8, range.Start.Character);
Assert.Equal(5, range.End.Line);
Assert.Equal(9, range.End.Character);

Assert.Equal("Debug test", codeLens.Command.Title);
});
}

[Fact]
public async Task CanSendReferencesCodeLensRequest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public LanguageServiceTests()
var logger = NullLogger.Instance;
powerShellContext = PowerShellContextFactory.Create(logger);
workspace = new WorkspaceService(NullLoggerFactory.Instance);
symbolsService = new SymbolsService(NullLoggerFactory.Instance, powerShellContext, workspace);
symbolsService = new SymbolsService(NullLoggerFactory.Instance, powerShellContext, workspace, new ConfigurationService());
completionHandler = new CompletionHandler(NullLoggerFactory.Instance, powerShellContext, workspace);
}

Expand Down