-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: migrate to vscode native Test API
- Loading branch information
1 parent
13295ec
commit 383d80a
Showing
10 changed files
with
257 additions
and
379 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,27 @@ | ||
import * as vscode from 'vscode'; | ||
import { TestHub, testExplorerExtensionId } from 'vscode-test-adapter-api'; | ||
import { Log, TestAdapterRegistrar } from 'vscode-test-adapter-util'; | ||
import { ProtostarAdapter } from './adapter'; | ||
import { RunHandler } from './runner'; | ||
import { ResolveHandler } from './resolver'; | ||
|
||
export async function activate(context: vscode.ExtensionContext) { | ||
|
||
const workspaceFolder = (vscode.workspace.workspaceFolders || [])[0]; | ||
|
||
// create a simple logger that can be configured with the configuration variables | ||
// `protostarTestExplorer.logpanel` and `protostarTestExplorer.logfile` | ||
const log = new Log('protostarTestExplorer', workspaceFolder, 'Protostar Test Explorer Log'); | ||
context.subscriptions.push(log); | ||
export async function activate(context: vscode.ExtensionContext) { | ||
const controller = vscode.tests.createTestController('protostar-test-controller', 'Protostar Test Controller'); | ||
context.subscriptions.push(controller); | ||
|
||
// get the Test Explorer extension | ||
const testExplorerExtension = vscode.extensions.getExtension<TestHub>(testExplorerExtensionId); | ||
if (log.enabled) log.info(`Test Explorer ${testExplorerExtension ? '' : 'not '}found`); | ||
const outputChannel = vscode.window.createOutputChannel('Protostar Tests'); | ||
|
||
if (testExplorerExtension) { | ||
const resolveHandler = new ResolveHandler(controller, outputChannel); | ||
controller.resolveHandler = async test => { | ||
await resolveHandler.resolve(test); | ||
}; | ||
|
||
const testHub = testExplorerExtension.exports; | ||
vscode.workspace.onDidOpenTextDocument(resolveHandler.parseTestsInDocument); | ||
vscode.workspace.onDidChangeTextDocument(e => resolveHandler.parseTestsInDocument(e.document)); | ||
|
||
// this will register an ExampleTestAdapter for each WorkspaceFolder | ||
context.subscriptions.push(new TestAdapterRegistrar( | ||
testHub, | ||
workspaceFolder => new ProtostarAdapter(workspaceFolder, log), | ||
log | ||
)); | ||
} | ||
controller.createRunProfile( | ||
'Run', | ||
vscode.TestRunProfileKind.Run, | ||
(request, token) => { | ||
new RunHandler(controller, outputChannel).hanleRequest(request, token); | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { TextDecoder } from 'util'; | ||
import { TestController, OutputChannel, TestItem, workspace } from 'vscode'; | ||
|
||
export class Parser { | ||
controller: TestController; | ||
outputChannel: OutputChannel; | ||
|
||
constructor(controller: TestController, outputChannel: OutputChannel) { | ||
this.controller = controller; | ||
this.outputChannel = outputChannel; | ||
} | ||
|
||
parseTestsInFileContents = async (file: TestItem, contents?: string) => { | ||
// If a document is open, VS Code already knows its contents. If this is being | ||
// called from the resolveHandler when a document isn't open, we'll need to | ||
// read them from disk ourselves. | ||
if (contents === undefined) { | ||
const rawContent = await workspace.fs.readFile(file.uri!); | ||
contents = new TextDecoder().decode(rawContent); | ||
} | ||
|
||
const testRegex = /^func (test_[A-Za-z0-9_]*).*$/; | ||
|
||
const children = contents | ||
.split('\n') | ||
.filter(line => line.match(testRegex)) | ||
.map(line => { | ||
const label = line.match(testRegex)?.at(1) | ||
const id = `${file.id}::${label}`; | ||
this.outputChannel.appendLine(`[${workspace.name}] Found new test: ${id}`); | ||
return this.controller.createTestItem(id, label!, file.uri); | ||
}); | ||
|
||
file.children.replace(children); | ||
} | ||
}; |
Oops, something went wrong.