-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmanager.ts
More file actions
243 lines (213 loc) · 9.57 KB
/
manager.ts
File metadata and controls
243 lines (213 loc) · 9.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import { basename, dirname, relative } from 'node:path'
import { TEST_ID_SEPARATOR } from '@vscode-wdio/constants'
import { log } from '@vscode-wdio/logger'
import * as vscode from 'vscode'
import { convertPathToUri } from './converter.js'
import { MetadataRepository } from './metadata.js'
import { TestRepository } from './repository.js'
import { createRunProfile } from './utils.js'
import type { ExtensionConfigManager } from '@vscode-wdio/config'
import type { IWorkerManager } from '@vscode-wdio/types/server'
import type { IRepositoryManager, ITestRepository } from '@vscode-wdio/types/test'
/**
* workspace -- managed by this class
* - configuration file (e.g. wdio.conf.js) -- managed by this class
* -- managed by repository class
* - spec file (e.g. e2e.spec.js) -- managed by repository class
* - test (e.g. describe('test', ()=>{...})) -- managed by repository class
*/
const LOADING_TEST_ITEM_ID = '_resolving'
export class RepositoryManager implements IRepositoryManager {
private readonly _repos = new Set<ITestRepository>()
private _loadingTestItem: vscode.TestItem
private _workspaceTestItems: vscode.TestItem[] = []
private _wdioConfigTestItems: vscode.TestItem[] = []
private _isInitialized = false
private _isCreatedDefaultProfile = false
constructor(
public readonly controller: vscode.TestController,
public readonly configManager: ExtensionConfigManager,
private readonly _workerManager: IWorkerManager,
private readonly _metadata = new MetadataRepository()
) {
this._loadingTestItem = this.controller.createTestItem(LOADING_TEST_ITEM_ID, 'Resolving WebdriverIO Tests...')
this._loadingTestItem.sortText = '.0' // show at first line
this._loadingTestItem.busy = true
this.controller.items.add(this._loadingTestItem)
this.controller.refreshHandler = async () => {
log.info('Refreshing WebdriverIO tests...')
await this.refreshTests()
}
configManager.on('update:configFilePattern', async () => {
if (!this._isInitialized) {
return
}
this.controller.items.replace([this._loadingTestItem])
await this.dispose()
configManager.dispose()
await configManager
.initialize()
.then(async () => await this.initialize())
.then(async () => await Promise.all(this.repos.map(async (repo) => await repo.discoverAllTests())))
.then(() => this.registerToTestController())
.then(() => this._workerManager.reorganize(configManager.getWdioConfigPaths()))
})
}
public get repos() {
return Array.from(this._repos)
}
public getMetadata(testItem: vscode.TestItem) {
return this._metadata.getMetadata(testItem)
}
public getRepository(testItem: vscode.TestItem): ITestRepository {
return this._metadata.getRepository(testItem)
}
public async initialize() {
const workspaces = this.configManager.workspaces
if (workspaces.length < 1) {
log.info('No workspaces is detected.')
return
}
log.debug('Start initialize the RepositoryManager')
this._workspaceTestItems = []
this._wdioConfigTestItems = []
this._workspaceTestItems = await Promise.all(
workspaces.map(async (workspace) => {
const workspaceTestItem = this._createWorkspaceTestItem(workspace.workspaceFolder)
for (const wdioConfigFile of workspace.wdioConfigFiles) {
await this._createWdioConfigTestItem(workspace.workspaceFolder, workspaceTestItem, wdioConfigFile)
this._isCreatedDefaultProfile = true
}
return workspaceTestItem
})
)
this._isInitialized = true
log.debug('Finish initialize the RepositoryManager')
}
public async addWdioConfig(workspaceFolder: vscode.WorkspaceFolder, wdioConfigPath: string) {
const affectedWorkspaceItems = this._workspaceTestItems.filter((item) => {
return item.uri?.fsPath === workspaceFolder.uri.fsPath
})
for (const workspaceTestItem of affectedWorkspaceItems) {
const configTestItem = await this._createWdioConfigTestItem(
workspaceFolder,
workspaceTestItem,
wdioConfigPath
)
const repo = this._metadata.getRepository(configTestItem)
await repo.discoverAllTests()
if (!this.configManager.isMultiWorkspace) {
this.controller.items.add(configTestItem)
}
}
}
public removeWdioConfig(workspaceFolder: vscode.WorkspaceFolder, wdioConfigPath: string) {
const affectedWorkspaceItems = this._workspaceTestItems.filter((item) => {
return item.uri?.fsPath === workspaceFolder.uri.fsPath
})
log.debug(`Remove the config file from ${affectedWorkspaceItems.length} workspace(s)`)
const configUri = convertPathToUri(wdioConfigPath)
for (const workspaceItem of affectedWorkspaceItems) {
const config = workspaceItem.children.get(this._generateConfigTestItemId(workspaceItem, configUri))
if (!config) {
continue
}
log.debug(`Remove the TestItem: ${config.id}`)
const targetRepo = this._metadata.getRepository(config)
targetRepo.dispose()
this._repos.delete(targetRepo)
workspaceItem.children.delete(config.id)
if (this.configManager.isMultiWorkspace) {
if (workspaceItem.children.size < 1) {
log.debug(`Remove Workspace from the controller: ${workspaceItem.id}`)
this.controller.items.delete(workspaceItem.id)
}
} else {
const targetId = this._generateConfigTestItemId(workspaceItem, configUri)
log.debug(`Remove Configuration from the controller: ${targetId}`)
this.controller.items.delete(targetId)
}
}
}
/**
* The test is reflected in the UI by registering the loaded test in the controller.
*/
public registerToTestController() {
log.debug('Registering the TestItems to Test controller.')
this.controller.items.delete(LOADING_TEST_ITEM_ID)
if (this._workspaceTestItems.length === 1) {
this.controller.items.replace(this._wdioConfigTestItems)
} else if (this._workspaceTestItems.length > 1) {
this.controller.items.replace(this._workspaceTestItems)
}
log.debug('Successfully registered.')
}
private _createWorkspaceTestItem(workspaceFolder: vscode.WorkspaceFolder) {
const workspaceItem = this.controller.createTestItem(
`workspace:${workspaceFolder.uri.fsPath}`,
workspaceFolder.name,
workspaceFolder.uri
)
this._metadata.createWorkspaceMetadata(workspaceItem, { uri: workspaceFolder.uri })
return workspaceItem
}
private async _createWdioConfigTestItem(
workspaceFolder: vscode.WorkspaceFolder,
workspaceTestItem: vscode.TestItem,
wdioConfigPath: string
) {
const uri = convertPathToUri(wdioConfigPath)
const configItem = this.controller.createTestItem(
this._generateConfigTestItemId(workspaceTestItem, uri),
basename(wdioConfigPath),
uri
)
workspaceTestItem.children.add(configItem)
this._wdioConfigTestItems.push(configItem)
const repository = new TestRepository(
this.configManager,
this.controller,
wdioConfigPath,
configItem,
this._workerManager,
workspaceFolder
)
this._repos.add(repository)
configItem.description = relative(workspaceTestItem.uri!.fsPath, dirname(wdioConfigPath))
const runProfiles = createRunProfile.call(this, configItem, !this._isCreatedDefaultProfile)
this._metadata.createWdioConfigFileMetadata(configItem, { uri, repository, runProfiles })
return configItem
}
private _generateConfigTestItemId(workspaceTestItem: vscode.TestItem, configUri: vscode.Uri) {
return [workspaceTestItem.id, `config:${configUri.fsPath}`].join(TEST_ID_SEPARATOR)
}
/**
* Refresh WebdriverIO tests
*/
public async refreshTests(): Promise<void> {
this.controller.items.replace([this._loadingTestItem])
try {
if (!this._isInitialized) {
await this.initialize()
}
await Promise.all(
this.repos.map(async (repo) => {
return await repo.discoverAllTests()
})
)
this.registerToTestController()
await this._workerManager.reorganize(this.configManager.getWdioConfigPaths())
} catch (error) {
this.controller.items.replace([])
const errorMessage = error instanceof Error ? error.message : String(error)
log.error(`Failed to reload tests: ${errorMessage}`)
vscode.window.showErrorMessage(`Failed to reload WebdriverIO tests: ${errorMessage}`)
}
}
public async dispose() {
await Promise.all(this.repos.map(async (repo) => repo.dispose()))
this._repos.clear()
this._workspaceTestItems = []
this._wdioConfigTestItems = []
}
}