diff --git a/module/PowerShellEditorServices/InvokePesterStub.ps1 b/module/PowerShellEditorServices/InvokePesterStub.ps1 index 6fc1e799f..10615063a 100755 --- a/module/PowerShellEditorServices/InvokePesterStub.ps1 +++ b/module/PowerShellEditorServices/InvokePesterStub.ps1 @@ -59,10 +59,24 @@ param( [string] $OutputPath ) -$pesterModule = Microsoft.PowerShell.Core\Get-Module Pester # add one line, so the subsequent output is not shifted to the side Write-Output '' +# checking and importing PSKoans first as it will import the required Pester-version (v4 vs v5) +if ($ScriptPath -match '\.Koans\.ps1$') { + $psKoansModule = Microsoft.PowerShell.Core\Get-Module PSKoans + if (!$psKoansModule) { + Write-Output "Importing PSKoans module..." + $psKoansModule = Microsoft.PowerShell.Core\Import-Module PSKoans -ErrorAction Ignore -PassThru + } + + if (!$psKoansModule) { + Write-Warning "Failed to import PSKoans. You must install PSKoans module to run or debug tests in *.Koans.ps1 files." + return + } +} + +$pesterModule = Microsoft.PowerShell.Core\Get-Module Pester if (!$pesterModule) { Write-Output "Importing Pester module..." if ($MinimumVersion5) { diff --git a/src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs b/src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs index f38e72372..7a77d7607 100644 --- a/src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs +++ b/src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs @@ -20,9 +20,8 @@ internal class PesterDocumentSymbolProvider : IDocumentSymbolProvider IEnumerable IDocumentSymbolProvider.ProvideDocumentSymbols( ScriptFile scriptFile) { - if (!scriptFile.FilePath.EndsWith( - "tests.ps1", - StringComparison.OrdinalIgnoreCase)) + if (!scriptFile.FilePath.EndsWith(".tests.ps1", StringComparison.OrdinalIgnoreCase) && + !scriptFile.FilePath.EndsWith(".Koans.ps1", StringComparison.OrdinalIgnoreCase)) { return Enumerable.Empty(); } diff --git a/test/PowerShellEditorServices.Test.Shared/Symbols/FindSymbolsInPSKoansFile.cs b/test/PowerShellEditorServices.Test.Shared/Symbols/FindSymbolsInPSKoansFile.cs new file mode 100644 index 000000000..03d26d82f --- /dev/null +++ b/test/PowerShellEditorServices.Test.Shared/Symbols/FindSymbolsInPSKoansFile.cs @@ -0,0 +1,21 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +using Microsoft.PowerShell.EditorServices.Services.TextDocument; + +namespace Microsoft.PowerShell.EditorServices.Test.Shared.Symbols +{ + public static class FindSymbolsInPSKoansFile + { + public static readonly ScriptRegion SourceDetails = + new( + file: TestUtilities.NormalizePath("Symbols/PesterFile.Koans.ps1"), + text: string.Empty, + startLineNumber: 0, + startColumnNumber: 0, + startOffset: 0, + endLineNumber: 0, + endColumnNumber: 0, + endOffset: 0); + } +} diff --git a/test/PowerShellEditorServices.Test.Shared/Symbols/PesterFile.Koans.ps1 b/test/PowerShellEditorServices.Test.Shared/Symbols/PesterFile.Koans.ps1 new file mode 100644 index 000000000..1c745d0ac --- /dev/null +++ b/test/PowerShellEditorServices.Test.Shared/Symbols/PesterFile.Koans.ps1 @@ -0,0 +1,23 @@ +Describe "Testing Pester symbols in a PSKoans-file" { + Context "Simple demo" { + BeforeAll { + + } + + BeforeEach { + + } + + It "Should return Pester symbols" { + + } + + AfterEach { + + } + } + + AfterAll { + + } +} diff --git a/test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs b/test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs index 53993a484..523b16e0b 100644 --- a/test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs +++ b/test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs @@ -889,6 +889,15 @@ public void FindsSymbolsInPesterFile() Assert.Equal(5, symbol.ScriptRegion.StartColumnNumber); } + [Fact] + public void FindsSymbolsInPSKoansFile() + { + IEnumerable symbols = FindSymbolsInFile(FindSymbolsInPSKoansFile.SourceDetails).OfType(); + + // Pester symbols are properly tested in FindsSymbolsInPesterFile so only counting to make sure they appear + Assert.Equal(7, symbols.Count(i => i.Type == SymbolType.Function)); + } + [Fact] public void FindsSymbolsInPSDFile() {