-
Notifications
You must be signed in to change notification settings - Fork 237
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
Changes from 9 commits
f26755f
a034574
c978a6f
725f643
62b101b
b6b4b4c
dbd42b5
61f62f5
ba32b20
565f2f8
32c69aa
69e7260
e5bddd0
39290a2
7245bd6
4ec3b3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Management.Automation; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
|
@@ -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 Lazy<bool> _pesterV4_6_0_OrHigherAvailable; | ||
|
||
/// <summary> | ||
/// Create a new Pester CodeLens provider for a given editor session. | ||
/// </summary> | ||
|
@@ -33,6 +40,7 @@ public PesterCodeLensProvider(EditorSession editorSession) | |
{ | ||
_editorSession = editorSession; | ||
_symbolProvider = new PesterDocumentSymbolProvider(); | ||
_pesterV4_6_0_OrHigherAvailable = new Lazy<bool>(() => DeterminePesterVersion()); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -45,6 +53,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.Value ? (int?)pesterSymbol.ScriptRegion.StartLineNumber : null; | ||
var codeLensResults = new CodeLens[] | ||
{ | ||
new CodeLens( | ||
|
@@ -54,7 +65,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, | ||
|
@@ -63,12 +74,43 @@ 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; | ||
} | ||
|
||
/// <summary> | ||
/// Used to determine the value of <see cref="_pesterV4_6_0_OrHigherAvailable"/> as a background task. | ||
/// </summary> | ||
private bool DeterminePesterVersion() | ||
{ | ||
var powerShell = new PSCommand(); | ||
powerShell.AddCommand("Get-Module") | ||
bergmeister marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.AddParameter("ListAvailable") | ||
.AddParameter("Name", "Pester"); | ||
|
||
IEnumerable<PSObject> result = Task.Run(() => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason you're using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make it synchronous since I moved it to the last possible point (when a .tests.ps1 file with a Describe block gets opened). If I did not call .Result (i.e. make it async) then it would default to the old Pester syntax in the first 1-2 seconds until the value has been determined (which people did not like in the first version of the PR). It is still using Task APIs because the PSCommand APIs that were suggested are all async. |
||
{ | ||
return _editorSession.PowerShellContext.ExecuteCommandAsync<PSObject>(powerShell); | ||
}).Result; | ||
|
||
if (result != null && result.Any()) | ||
bergmeister marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
foreach (PSObject module in result) | ||
{ | ||
if (module.BaseObject is PSModuleInfo psModuleInfo) | ||
{ | ||
if (psModuleInfo.Version >= new Version(4, 6)) | ||
{ | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/// <summary> | ||
/// Get all Pester CodeLenses for a given script file. | ||
/// </summary> | ||
|
Uh oh!
There was an error while loading. Please reload this page.