Skip to content

Commit

Permalink
refactor: migrate to vscode native Test API
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyBuisset committed May 23, 2022
1 parent 13295ec commit 383d80a
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 379 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
"type": "extensionHost",
"request": "launch",
"name": "Example adapter",
"name": "Protostar adapter",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}"
Expand All @@ -14,4 +14,4 @@
]
}
]
}
}
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@
## 🎟️ Description

vscode extension to view protostar tests in the Test Explorer.
This repository contains a `TestAdapter` extension for [protostar](https://docs.swmansion.com/protostar/) that works with the
[Test Explorer](https://marketplace.visualstudio.com/items?itemName=hbenl.vscode-test-explorer) extension.

More documentation can be found in the [Test Adapter API repository](https://github.com/hbenl/vscode-test-adapter-api).
This extension uses vscode native TEst API

## 🎗️ Prerequisites

* Install the [Test Explorer](https://marketplace.visualstudio.com/items?itemName=hbenl.vscode-test-explorer) extension
_No pre-requisite_

## 📦 Installation

Expand Down
94 changes: 22 additions & 72 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 4 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,17 @@
"publish": "vsce publish"
},
"dependencies": {
"tslib": "^1.9.3",
"vscode-test-adapter-api": "^1.7.0",
"vscode-test-adapter-util": "^0.7.0"
"tslib": "^2.3.1"
},
"devDependencies": {
"@types/node": "^17.0.33",
"@types/vscode": "~1.23.0",
"typescript": "^4.0.5",
"vsce": "^1.95.1"
"@types/vscode": "~1.59.0",
"typescript": "^4.3.5",
"vsce": "^1.96.1"
},
"engines": {
"vscode": "^1.23.0"
},
"extensionDependencies": [
"hbenl.vscode-test-explorer"
],
"activationEvents": [
"*"
],
Expand Down
77 changes: 0 additions & 77 deletions src/adapter.ts

This file was deleted.

41 changes: 19 additions & 22 deletions src/main.ts
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);
}
);
}
36 changes: 36 additions & 0 deletions src/parser.ts
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);
}
};
Loading

0 comments on commit 383d80a

Please sign in to comment.