forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestController.ts
More file actions
174 lines (155 loc) · 5.61 KB
/
Copy pathtestController.ts
File metadata and controls
174 lines (155 loc) · 5.61 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
import * as vscode from "vscode"
import { DeviceScriptExtensionState } from "./state"
import {
CHANGE,
DeviceScriptTestControllerServer,
SRV_DEVS_TEST,
} from "jacdac-ts"
import { readFileText } from "./fs"
import { Utils } from "vscode-uri"
interface TestData {
type: "describe" | "test" | "it"
// identifier used in devicescript
testId: string
indent: string
}
export function activateTestController(
extensionState: DeviceScriptExtensionState
) {
const { context, devtools, bus } = extensionState
const { subscriptions } = context
const testData = new WeakMap<vscode.TestItem, TestData>()
const controller = vscode.tests.createTestController(
"deviceScriptTests",
"DeviceScript"
)
subscriptions.push(controller)
devtools.subscribe(CHANGE, parseTests)
// When text documents are open, parse tests in them.
subscriptions.push(
vscode.workspace.onDidOpenTextDocument(parseTestsInDocument)
)
// We could also listen to document changes to re-parse unsaved changes:
subscriptions.push(
vscode.workspace.onDidChangeTextDocument(e =>
parseTestsInDocument(e.document)
)
)
// add run, debug profiles
subscriptions.push(
controller.createRunProfile(
"Run",
vscode.TestRunProfileKind.Run,
(request, token) => runHandler(false, request, token)
),
controller.createRunProfile(
"Debug",
vscode.TestRunProfileKind.Debug,
(request, token) => runHandler(true, request, token)
)
)
function parseTestsInDocument(e: vscode.TextDocument) {
const { currentFile } = devtools
if (e.uri.scheme === "file" && e.uri === currentFile) {
parseTests()
}
}
async function parseTests() {
const { currentFile, projectFolder } = devtools
// clear all tests
if (!currentFile) {
controller.items.replace([])
return
}
const suite = `${Utils.basename(projectFolder)}/${Utils.basename(
currentFile
)}`
const content = await readFileText(currentFile)
const lines = content.split("\n")
let parent: vscode.TestItem
for (const line of lines) {
const mopen =
/^(?<indent>\s*)(?<type>describe|it|test)\(['"](?<name>.*?)['"],/.exec(
line
)
if (mopen) {
const { indent, name, type } = mopen.groups
const id = `${parent?.id || suite}/${name}`
let test = controller.items.get(id)
if (!controller.items.get(id)) {
const parentData = parent && testData.get(parent)
const testId = parentData
? `${parentData.testId}/${name}`
: name
test = controller.createTestItem(id, name, currentFile)
if (parent) parent.children.add(test)
else controller.items.add(test)
testData.set(test, { type, indent, testId } as TestData)
}
if (type === "describe") parent = test
continue
}
const mclose = /^(?<indent>\s*)}\s*\)\s*;?\s*$/.exec(line)
// don't pop top level test
if (mclose) {
const { indent } = mclose.groups
const parentData = testData.get(parent)
if (!parentData) continue
if (indent.length <= parentData.indent.length) {
parent = parent.parent
}
continue
}
}
}
async function runHandler(
shouldDebug: boolean,
request: vscode.TestRunRequest,
token: vscode.CancellationToken
) {
// recursively expand all tests
const tests: vscode.TestItem[] = []
{
const todo: vscode.TestItem[] = []
const { include, exclude } = request
if (include) todo.push(...include)
else controller.items.forEach(item => todo.push(item))
while (todo.length) {
const next = todo.pop()
if (exclude?.includes(next)) continue
tests.push(next)
next.children.forEach(child => todo.push(child))
}
console.log({ tests, include, exclude })
}
const testControllerService = bus.services({
serviceClass: SRV_DEVS_TEST,
})[0]
const testController = bus
.findServiceProvider(testControllerService.device.deviceId)
.service(
testControllerService.serviceIndex
) as DeviceScriptTestControllerServer
const run = controller.createTestRun(request)
try {
// mark tests as undefined
tests.forEach(test => run.enqueued(test))
const testIds = tests.map(test => testData.get(test)?.testId)
console.log({ testIds })
// start sniffing console.log
testController.tests = testIds
// start running
await vscode.commands.executeCommand(
`extension.devicescript.editor.${shouldDebug ? "debug" : "run"}`
)
if (token.isCancellationRequested) return
} finally {
// stop sniffing console.log
testController.tests = []
// fail remaining tests
tests.forEach(test => run.errored(test, { message: "cancelled" }))
// and done
run.end()
}
}
}