Skip to content
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

Support Run/Debug tests in PSKoans-files #1988

Merged
merged 2 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 15 additions & 1 deletion module/PowerShellEditorServices/InvokePesterStub.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ internal class PesterDocumentSymbolProvider : IDocumentSymbolProvider
IEnumerable<SymbolReference> 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<SymbolReference>();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Describe "Testing Pester symbols in a PSKoans-file" {
Context "Simple demo" {
BeforeAll {

}

BeforeEach {

}

It "Should return Pester symbols" {

}

AfterEach {

}
}

AfterAll {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,15 @@ public void FindsSymbolsInPesterFile()
Assert.Equal(5, symbol.ScriptRegion.StartColumnNumber);
}

[Fact]
public void FindsSymbolsInPSKoansFile()
{
IEnumerable<PesterSymbolReference> symbols = FindSymbolsInFile(FindSymbolsInPSKoansFile.SourceDetails).OfType<PesterSymbolReference>();

// 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()
{
Expand Down