Skip to content

Commit ea2883a

Browse files
fflatenandyleejordan
authored andcommitted
add symbols for Pester setup and teardown blocks
1 parent 0b96dae commit ea2883a

File tree

2 files changed

+70
-23
lines changed

2 files changed

+70
-23
lines changed

src/PowerShellEditorServices/Services/CodeLens/PesterCodeLensProvider.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,19 @@ public CodeLens[] ProvideCodeLenses(ScriptFile scriptFile, CancellationToken can
113113
List<CodeLens> lenses = new();
114114
foreach (SymbolReference symbol in _symbolProvider.ProvideDocumentSymbols(scriptFile))
115115
{
116+
cancellationToken.ThrowIfCancellationRequested();
117+
116118
if (symbol is not PesterSymbolReference pesterSymbol)
117119
{
118120
continue;
119121
}
120122

121-
cancellationToken.ThrowIfCancellationRequested();
123+
// Skip codelense for setup/teardown block
124+
if (!PesterSymbolReference.IsPesterTestCommand(pesterSymbol.Command))
125+
{
126+
continue;
127+
}
128+
122129
if (_configurationService.CurrentSettings.Pester.UseLegacyCodeLens
123130
&& pesterSymbol.Command != PesterCommandType.Describe)
124131
{

src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs

+62-22
Original file line numberDiff line numberDiff line change
@@ -91,33 +91,35 @@ private static PesterSymbolReference ConvertPesterAstToSymbolReference(ScriptFil
9191
return null;
9292
}
9393

94-
// Search for a name for the test
95-
// If the test has more than one argument for names, we set it to null
9694
string testName = null;
97-
bool alreadySawName = false;
98-
for (int i = 1; i < pesterCommandAst.CommandElements.Count; i++)
99-
{
100-
CommandElementAst currentCommandElement = pesterCommandAst.CommandElements[i];
101-
102-
// Check for an explicit "-Name" parameter
103-
if (currentCommandElement is CommandParameterAst)
95+
if (PesterSymbolReference.IsPesterTestCommand(commandName.Value)) {
96+
// Search for a name for the test
97+
// If the test has more than one argument for names, we set it to null
98+
bool alreadySawName = false;
99+
for (int i = 1; i < pesterCommandAst.CommandElements.Count; i++)
104100
{
105-
// Found -Name parameter, move to next element which is the argument for -TestName
106-
i++;
101+
CommandElementAst currentCommandElement = pesterCommandAst.CommandElements[i];
102+
103+
// Check for an explicit "-Name" parameter
104+
if (currentCommandElement is CommandParameterAst)
105+
{
106+
// Found -Name parameter, move to next element which is the argument for -TestName
107+
i++;
107108

109+
if (!alreadySawName && TryGetTestNameArgument(pesterCommandAst.CommandElements[i], out testName))
110+
{
111+
alreadySawName = true;
112+
}
113+
114+
continue;
115+
}
116+
117+
// Otherwise, if an argument is given with no parameter, we assume it's the name
118+
// If we've already seen a name, we set the name to null
108119
if (!alreadySawName && TryGetTestNameArgument(pesterCommandAst.CommandElements[i], out testName))
109120
{
110121
alreadySawName = true;
111122
}
112-
113-
continue;
114-
}
115-
116-
// Otherwise, if an argument is given with no parameter, we assume it's the name
117-
// If we've already seen a name, we set the name to null
118-
if (!alreadySawName && TryGetTestNameArgument(pesterCommandAst.CommandElements[i], out testName))
119-
{
120-
alreadySawName = true;
121123
}
122124
}
123125

@@ -145,7 +147,7 @@ private static bool TryGetTestNameArgument(CommandElementAst commandElementAst,
145147
}
146148

147149
/// <summary>
148-
/// Defines command types for Pester test blocks.
150+
/// Defines command types for Pester blocks.
149151
/// </summary>
150152
internal enum PesterCommandType
151153
{
@@ -162,7 +164,32 @@ internal enum PesterCommandType
162164
/// <summary>
163165
/// Identifies an It block.
164166
/// </summary>
165-
It
167+
It,
168+
169+
/// <summary>
170+
/// Identifies an BeforeAll block.
171+
/// </summary>
172+
BeforeAll,
173+
174+
/// <summary>
175+
/// Identifies an BeforeEach block.
176+
/// </summary>
177+
BeforeEach,
178+
179+
/// <summary>
180+
/// Identifies an AfterAll block.
181+
/// </summary>
182+
AfterAll,
183+
184+
/// <summary>
185+
/// Identifies an AfterEach block.
186+
/// </summary>
187+
AfterEach,
188+
189+
/// <summary>
190+
/// Identifies an BeforeDiscovery block.
191+
/// </summary>
192+
BeforeDiscovery
166193
}
167194

168195
/// <summary>
@@ -216,5 +243,18 @@ internal PesterSymbolReference(
216243
}
217244
return pesterCommandType;
218245
}
246+
247+
/// <summary>
248+
/// Checks if the PesterCommandType is a block with executable tests (Describe/Context/It).
249+
/// </summary>
250+
/// <param name="pesterCommandType">the PesterCommandType representing the Pester command</param>
251+
/// <returns>True if command type is a block used to trigger test run. False if setup/teardown/support-block.</returns>
252+
internal static bool IsPesterTestCommand(PesterCommandType pesterCommandType)
253+
{
254+
return pesterCommandType is
255+
PesterCommandType.Describe or
256+
PesterCommandType.Context or
257+
PesterCommandType.It;
258+
}
219259
}
220260
}

0 commit comments

Comments
 (0)