Skip to content

Commit 9ee381f

Browse files
authored
Support Run/Debug tests in PSKoans files (#1988)
Code Lens feature only depends on Pester symbols, so we only need to make sure they appear for `*.koans.ps1` files
1 parent 368dcfa commit 9ee381f

File tree

5 files changed

+70
-4
lines changed

5 files changed

+70
-4
lines changed

module/PowerShellEditorServices/InvokePesterStub.ps1

+15-1
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,24 @@ param(
5959
[string] $OutputPath
6060
)
6161

62-
$pesterModule = Microsoft.PowerShell.Core\Get-Module Pester
6362
# add one line, so the subsequent output is not shifted to the side
6463
Write-Output ''
6564

65+
# checking and importing PSKoans first as it will import the required Pester-version (v4 vs v5)
66+
if ($ScriptPath -match '\.Koans\.ps1$') {
67+
$psKoansModule = Microsoft.PowerShell.Core\Get-Module PSKoans
68+
if (!$psKoansModule) {
69+
Write-Output "Importing PSKoans module..."
70+
$psKoansModule = Microsoft.PowerShell.Core\Import-Module PSKoans -ErrorAction Ignore -PassThru
71+
}
72+
73+
if (!$psKoansModule) {
74+
Write-Warning "Failed to import PSKoans. You must install PSKoans module to run or debug tests in *.Koans.ps1 files."
75+
return
76+
}
77+
}
78+
79+
$pesterModule = Microsoft.PowerShell.Core\Get-Module Pester
6680
if (!$pesterModule) {
6781
Write-Output "Importing Pester module..."
6882
if ($MinimumVersion5) {

src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ internal class PesterDocumentSymbolProvider : IDocumentSymbolProvider
2020
IEnumerable<SymbolReference> IDocumentSymbolProvider.ProvideDocumentSymbols(
2121
ScriptFile scriptFile)
2222
{
23-
if (!scriptFile.FilePath.EndsWith(
24-
"tests.ps1",
25-
StringComparison.OrdinalIgnoreCase))
23+
if (!scriptFile.FilePath.EndsWith(".tests.ps1", StringComparison.OrdinalIgnoreCase) &&
24+
!scriptFile.FilePath.EndsWith(".Koans.ps1", StringComparison.OrdinalIgnoreCase))
2625
{
2726
return Enumerable.Empty<SymbolReference>();
2827
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
5+
6+
namespace Microsoft.PowerShell.EditorServices.Test.Shared.Symbols
7+
{
8+
public static class FindSymbolsInPSKoansFile
9+
{
10+
public static readonly ScriptRegion SourceDetails =
11+
new(
12+
file: TestUtilities.NormalizePath("Symbols/PesterFile.Koans.ps1"),
13+
text: string.Empty,
14+
startLineNumber: 0,
15+
startColumnNumber: 0,
16+
startOffset: 0,
17+
endLineNumber: 0,
18+
endColumnNumber: 0,
19+
endOffset: 0);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Describe "Testing Pester symbols in a PSKoans-file" {
2+
Context "Simple demo" {
3+
BeforeAll {
4+
5+
}
6+
7+
BeforeEach {
8+
9+
}
10+
11+
It "Should return Pester symbols" {
12+
13+
}
14+
15+
AfterEach {
16+
17+
}
18+
}
19+
20+
AfterAll {
21+
22+
}
23+
}

test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs

+9
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,15 @@ public void FindsSymbolsInPesterFile()
890890
Assert.Equal(5, symbol.ScriptRegion.StartColumnNumber);
891891
}
892892

893+
[Fact]
894+
public void FindsSymbolsInPSKoansFile()
895+
{
896+
IEnumerable<PesterSymbolReference> symbols = FindSymbolsInFile(FindSymbolsInPSKoansFile.SourceDetails).OfType<PesterSymbolReference>();
897+
898+
// Pester symbols are properly tested in FindsSymbolsInPesterFile so only counting to make sure they appear
899+
Assert.Equal(7, symbols.Count(i => i.Type == SymbolType.Function));
900+
}
901+
893902
[Fact]
894903
public void FindsSymbolsInPSDFile()
895904
{

0 commit comments

Comments
 (0)