Skip to content

Commit

Permalink
🐛 Properly type check for test items for enqueue
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinGrote committed Jun 29, 2023
1 parent 5679ecd commit e4fb46d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
20 changes: 14 additions & 6 deletions src/pesterTestController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
IPowerShellExtensionClient,
PowerShellExtensionClient
} from './powershellExtensionClient'
import { findTestItem, forAll, getParents, getTestItems } from './testItemUtils'
import { findTestItem, forAll, getParents, getTestItems, isTestItemOptions } from './testItemUtils'
import debounce = require('debounce-promise')
import { initialize as statusBarInitialize } from './features/toggleAutoRunOnSaveCommand'
/** A wrapper for the vscode TestController API specific to PowerShell Pester Test Suite.
Expand Down Expand Up @@ -145,7 +145,7 @@ export class PesterTestController implements Disposable {
}

// Test Definitions should never show up here, they aren't resolvable in Pester as we only do it at file level
if (testItemData instanceof TestDefinition) {
if (isTestItemOptions(testItemData)) {
log.warn(
`Received a test definition ${testItemData.id} to resolve. Should not happen`
)
Expand Down Expand Up @@ -276,10 +276,16 @@ export class PesterTestController implements Disposable {
for (const testItem of testItems) {
forAll(testItem, item => {
const testItemData = TestData.get(item)
if (testItemData instanceof TestDefinition && testItemData.type === 'Test') {
run.enqueued(testItem)
if (!testItemData) {
log.error(`Test Item Data not found for ${testItem.id}, this should not happen`)
return
}
})
if (isTestItemOptions(testItemData)) {
if (testItemData.type === 'Test') {
run.enqueued(item)
}
}
}, true)
}

/** Takes the returned objects from Pester and resolves their status in the test controller **/
Expand Down Expand Up @@ -529,8 +535,9 @@ export class PesterTestController implements Disposable {
const psOutput = new PSOutput()
psOutput.success.on('data', returnHandler)
psOutput.success.once('close', () => {
log.info(`Test Run Ended (PesterInterface stream closed)`)
testRun?.end()
log.debug(`Pester close received, removing returnHandler`)
log.trace(`Removing returnHandler from PSOutput`)
psOutput.success.removeListener('data', returnHandler)
})

Expand All @@ -542,6 +549,7 @@ export class PesterTestController implements Disposable {
const endSocketAtDebugTerminate = (session: DebugSession) => {
psListenerPromise.then(socket => socket.end())
if (testRun) {
log.warn("Test run ended due to abrupt debug session end such as the user cancelling the debug session.")
testRun.end()
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/pesterTestTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ export interface TestDefinition extends TestItemOptions {
tags?: string[]
}

export class TestDefinition {}

/** The type used to represent a test run from the Pester runner, with additional status data */
export interface TestResult extends TestItemOptions {
result: TestResultState
Expand Down
5 changes: 5 additions & 0 deletions src/testItemUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TestItem, TestItemCollection } from 'vscode'
import { TestDefinition, TestTree } from './pesterTestTree'

/** Returns a Set of all TestItems and their children recursively in the collection. This assumes all your test IDs are unique, duplicates will be replaced **/
export function getUniqueTestItems(collection: TestItemCollection) {
Expand Down Expand Up @@ -66,3 +67,7 @@ export function getParents(TestItem: TestItem) {
}
return parents
}

export function isTestItemOptions(testItem: TestTree): testItem is TestDefinition {
return 'type' in testItem
}

0 comments on commit e4fb46d

Please sign in to comment.