Skip to content

Commit

Permalink
✨Add Running Test Indicator and Clean up Exclusion Handling (#68)
Browse files Browse the repository at this point in the history
Tests now indicate "running" status on a per-test basis.
  • Loading branch information
JustinGrote authored Sep 25, 2021
1 parent fb4707c commit 5d49a6c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
16 changes: 16 additions & 0 deletions Scripts/PesterInterface.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,22 @@ $MyPlugin = @{
}
}

EachTestSetup = {
param($Context)
#Indicate the test is now running
$testItem = New-TestObject $context.test
$testItem.result = 2
[string]$jsonObject = ConvertTo-Json $testItem -Compress -Depth 1
if (!$DryRun) {
if (!$pipeName -or $pipeName -eq 'stdout') {
[void][Console]::Out.WriteLineAsync($jsonObject)
} else {
$__TestAdapterNamedPipeWriter.WriteLine($jsonObject)
}
} else {
$jsonObject >> $PipeName
}
}
EachTestTeardownEnd = {
param($Context)
if (-not $Context) { continue }
Expand Down
15 changes: 15 additions & 0 deletions sample/Tests/Basic.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ Context 'RootLevelContextWithTags' -Tag 'ContextTag', 'ContextTag2' {
$true
}
}

Context 'Long Running Test' {
It 'Runs for 0.5 second' {
Start-Sleep 0.5
$true | Should -Be $true
}
It 'Runs for random' {
Start-Sleep -Milliseconds (Get-Random -Min 500 -Max 2000)
$true | Should -Be $true
}
It 'Runs for 1 second' {
Start-Sleep 1
$true | Should -Be $true
}
}
# Describe 'Duplicate DescribeWithContext' {
# Context 'DupeContext' {
# It 'DupeContext' { $true }
Expand Down
25 changes: 20 additions & 5 deletions src/pesterTestController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
IPowerShellExtensionClient,
PowerShellExtensionClient
} from './powershellExtensionClient'
import { findTestItem } from './testItemUtils'
import { findTestItem, forAll, getTestItems } from './testItemUtils'
import debounce = require('debounce-promise')
/** A wrapper for the vscode TestController API specific to PowerShell Pester Test Suite.
* This should only be instantiated once in the extension activate method.
Expand Down Expand Up @@ -201,11 +201,21 @@ export class PesterTestController implements Disposable {
throw new Error('No profile provided. This is (currently) a bug.')
}
const isDebug = request.profile.kind === TestRunProfileKind.Debug
const testItems = this.getRunRequestTestItems(request)
const exclude = new Set<TestItem>(request.exclude)
// If nothing was included, assume it means "run all tests"
// TODO, do this at the ternary step
const include = request.include ?? getTestItems(this.testController.items)

// TODO: Make this cleaner and replace getRunRequestTestItems
// If there are no excludes we don't need to do any fancy exclusion test filtering
const testItems =
request.exclude === undefined || request.exclude.length === 0
? include
: Array.from(this.getRunRequestTestItems(request))

// Indicate that the tests are ready to run
testItems.forEach(run.enqueued)
for (const testItem of testItems) {
forAll(testItem, run.enqueued)
}

/** Takes the returned objects from Pester and resolves their status in the test controller **/
const runResultHandler = (item: unknown) => {
Expand All @@ -226,10 +236,15 @@ export class PesterTestController implements Disposable {
)
return
}
const exclude = new Set<TestItem>(request.exclude)
if (exclude.has(testRequestItem)) {
log.warn(`${testResult.id} was run in Pester but excluded from results`)
return
}
if (testResult.result === TestResultState.Running) {
run.started(testRequestItem)
return
}

if (testResult.result === TestResultState.Passed) {
run.passed(testRequestItem, testResult.duration)
Expand Down Expand Up @@ -277,7 +292,7 @@ export class PesterTestController implements Disposable {
}
}

testItems.forEach(run.started)
// testItems.forEach(run.started)
// TODO: Adjust testItems parameter to a Set
await this.startPesterInterface(
Array.from(testItems),
Expand Down
10 changes: 10 additions & 0 deletions src/testItemUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ export function getUniqueTestItems(collection: TestItemCollection) {
return TestItems
}

/** Returns an array of TestItems at the root of the TestItemConnection. This does not fetch children */
export function getTestItems(collection: TestItemCollection) {
const TestItems = new Array<TestItem>()
const addTestItem = (TestItem: TestItem) => {
TestItems.push(TestItem)
}
collection.forEach(addTestItem)
return TestItems
}

/** Performs a breadth-first search for a test item in a given collection. It assumes your test IDs are unique and will only return the first one it finds **/
export function findTestItem(id: string, collection: TestItemCollection) {
const queue = new Array<TestItemCollection>(collection)
Expand Down

0 comments on commit 5d49a6c

Please sign in to comment.