Skip to content

Send Pester describe line and info whether Pester 4.6.0 is installed to PowerShell.RunPesterTests command #856

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

Closed
wants to merge 16 commits into from
Closed
Changes from 4 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -25,6 +26,12 @@ internal class PesterCodeLensProvider : FeatureProviderBase, ICodeLensProvider
/// </summary>
private IDocumentSymbolProvider _symbolProvider;

/// <summary>
/// Pester 4.6.0 introduced a new ScriptblockFilter parameter to be able to run a test based on a line,
/// therefore knowing this information is important.
/// </summary>
private bool _pesterV4_6_0_OrHigherAvailable;

/// <summary>
/// Create a new Pester CodeLens provider for a given editor session.
/// </summary>
Expand All @@ -33,6 +40,38 @@ public PesterCodeLensProvider(EditorSession editorSession)
{
_editorSession = editorSession;
_symbolProvider = new PesterDocumentSymbolProvider();

DeterminePesterVersion();
}

/// <summary>
/// Used to determine the value of <see cref="_pesterV4_6_0_OrHigherAvailable"/> as a background task.
/// </summary>
private void DeterminePesterVersion()
{
Task.Run(() =>
{
using (var powerShell = System.Management.Automation.PowerShell.Create())
{
powerShell.AddCommand("Get-Module")
.AddParameter("ListAvailable")
.AddParameter("Name", "Pester");
var result = powerShell.Invoke();
if (result != null && result.Count > 0)
{
foreach (var module in result)
{
if (module.BaseObject is PSModuleInfo psmoduleInfo)
{
if (psmoduleInfo.Version > new Version(4, 6))
{
_pesterV4_6_0_OrHigherAvailable = true;
}
}
}
}
}
});
}

/// <summary>
Expand All @@ -45,6 +84,9 @@ private CodeLens[] GetPesterLens(
PesterSymbolReference pesterSymbol,
ScriptFile scriptFile)
{
// A value of null is a signal to PSES that the available Pester version does not support
// running Describe blocks by name (the test name will used instead then)
int? describeBlockLineNumber = _pesterV4_6_0_OrHigherAvailable ? pesterSymbol.ScriptRegion.StartLineNumber : default(int?);
var codeLensResults = new CodeLens[]
{
new CodeLens(
Expand All @@ -54,7 +96,7 @@ private CodeLens[] GetPesterLens(
new ClientCommand(
"PowerShell.RunPesterTests",
"Run tests",
new object[] { scriptFile.ClientFilePath, false /* No debug */, pesterSymbol.TestName })),
new object[] { scriptFile.ClientFilePath, false /* No debug */, pesterSymbol.TestName, describeBlockLineNumber })),

new CodeLens(
this,
Expand All @@ -63,7 +105,7 @@ private CodeLens[] GetPesterLens(
new ClientCommand(
"PowerShell.RunPesterTests",
"Debug tests",
new object[] { scriptFile.ClientFilePath, true /* Run in debugger */, pesterSymbol.TestName })),
new object[] { scriptFile.ClientFilePath, true /* Run in debugger */, pesterSymbol.TestName, describeBlockLineNumber })),
};

return codeLensResults;
Expand Down