import * as vscode from 'vscode' ; import { TestAdapter , TestController , TestHub as ITestHub , TestSuiteInfo } from 'vscode-test-adapter-api' ; import { TestAdapter as LegacyTestAdapter } from 'vscode-test-adapter-api/out/legacy' ; import { TestAdapterDelegate } from './testAdapterDelegate' ; import { LegacyTestAdapterWrapper } from './legacyTestAdapterWrapper' ; export class TestHub implements ITestHub { private readonly controllers = new Set < TestController > ( ) ; private readonly adapters = new Set < TestAdapter > ( ) ; private readonly adapterSubscriptions = new Map < TestAdapter , vscode . Disposable > ( ) ; private readonly delegates = new Set < TestAdapterDelegate > ( ) ; private readonly tests = new Map < TestAdapter , TestSuiteInfo | undefined > ( ) ; private readonly legacyWrappers = new Map < LegacyTestAdapter , LegacyTestAdapterWrapper > ( ) ; registerTestController ( controller ) { this . controllers . add ( controller ) ; for ( const adapter of this . adapters ) { const delegate = new TestAdapterDelegate ( adapter , controller ) ; this . delegates . add ( delegate ) ; controller . registerTestAdapter ( delegate ) ; delegate . testsEmitter . fire ( { type : 'started' } ) ; if ( this . tests . has ( adapter ) ) { delegate . testsEmitter . fire ( { type : 'finished' , suite : this . tests . get ( adapter ) } ) ; } } } unregisterTestController ( controller ) { this . controllers . delete ( controller ) ; for ( const delegate of this . delegates ) { if ( delegate . controller === controller ) { controller . unregisterTestAdapter ( delegate ) ; delegate . dispose ( ) ; this . delegates . delete ( delegate ) ; } } } registerTestAdapter ( adapter ) { this . adapters . add ( adapter ) ; for ( const controller of this . controllers ) { const delegate = new TestAdapterDelegate ( adapter , controller ) ; this . delegates . add ( delegate ) ; controller . registerTestAdapter ( delegate ) ; } this . adapterSubscriptions . set ( adapter , adapter . tests ( event => { if ( event . type === 'started' ) { this . tests . delete ( adapter ) ; } else { this . tests . set ( adapter , event . suite ) ; } for ( const delegate of this . delegates ) { if ( delegate . adapter === adapter ) { delegate . testsEmitter . fire ( event ) ; } } } ) ) ; adapter . load ( ) ; } unregisterTestAdapter ( adapter ) { this . adapters . delete ( adapter ) ; const subscription = this . adapterSubscriptions . get ( adapter ) ; if ( subscription ) { subscription . dispose ( ) ; this . adapterSubscriptions . delete ( adapter ) ; } for ( const delegate of this . delegates ) { if ( delegate . adapter === adapter ) { delegate . controller . unregisterTestAdapter ( delegate ) ; delegate . dispose ( ) ; this . delegates . delete ( delegate ) ; } } this . tests . delete ( adapter ) ; } registerAdapter ( adapter ) { const wrapper = new LegacyTestAdapterWrapper ( adapter , this ) ; this . legacyWrappers . set ( adapter , wrapper ) ; this . registerTestAdapter ( wrapper ) ; } unregisterAdapter ( adapter ) { const wrapper = this . legacyWrappers . get ( adapter ) ; if ( wrapper ) { this . unregisterTestAdapter ( wrapper ) ; this . legacyWrappers . delete ( adapter ) ; } } getTests ( adapter ) { return this . tests . get ( adapter ) ; } }