Skip to content

Commit fcfdff6

Browse files
committed
chore: update CI workflow and testing setup, add unit tests for erbBlock
1 parent 7a47ff2 commit fcfdff6

File tree

8 files changed

+100
-49
lines changed

8 files changed

+100
-49
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ jobs:
1919
- run: gem install ruby-lsp
2020
- run: bun install
2121
- run: bun run build
22+
- run: bun run test
2223
- run: xvfb-run -a bun run test:e2e

bun.lock

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@
5151
"scripts": {
5252
"build": "bun x tsc -p tsconfig.json",
5353
"watch": "bun x tsc -w -p tsconfig.json",
54-
"pretest": "bun x tsc -p tsconfig.json",
55-
"test": "bun run test:e2e",
54+
"test": "NODE_OPTIONS=--disable-warning=MODULE_TYPELESS_PACKAGE_JSON TS_NODE_PROJECT=tsconfig.test.json bun x mocha --ui tdd --require ts-node/register --extension ts \"src/test/unit/**/*.test.ts\"",
55+
"test:watch": "NODE_OPTIONS=--disable-warning=MODULE_TYPELESS_PACKAGE_JSON TS_NODE_PROJECT=tsconfig.test.json bun x mocha --ui tdd --require ts-node/register --extension ts --watch --watch-files \"src/**/*.ts\" \"src/test/unit/**/*.test.ts\"",
56+
"pretest:e2e": "bun x tsc -p tsconfig.json",
5657
"test:e2e": "node ./dist/test/runTest.js"
5758
},
5859
"packageManager": "bun@1.3.5",
@@ -65,6 +66,7 @@
6566
"@types/node": "^25.0.3",
6667
"@types/vscode": "^1.107.0",
6768
"@vscode/test-electron": "^2.4.3",
68-
"mocha": "^10.8.2"
69+
"mocha": "^10.8.2",
70+
"ts-node": "^10.9.2"
6971
}
7072
}

src/test/runTest.ts

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,22 @@
1-
import { execFile } from 'child_process';
2-
import * as path from 'path';
3-
import { promisify } from 'util';
1+
import { execFile } from 'node:child_process';
2+
import * as path from 'node:path';
3+
import { promisify } from 'node:util';
44

5-
import {
6-
downloadAndUnzipVSCode,
7-
resolveCliPathFromVSCodeExecutablePath,
8-
runTests,
9-
} from '@vscode/test-electron';
5+
import { downloadAndUnzipVSCode, resolveCliPathFromVSCodeExecutablePath, runTests } from '@vscode/test-electron';
106

117
const execFileAsync = promisify(execFile);
128
const DEPENDENCY_EXTENSIONS = ['Shopify.ruby-lsp'];
139

1410
async function installExtension(cliPath: string, extensionsDir: string, extensionId: string) {
15-
await execFileAsync(cliPath, [
16-
'--extensions-dir',
17-
extensionsDir,
18-
'--install-extension',
19-
extensionId,
20-
]);
11+
await execFileAsync(cliPath, ['--extensions-dir', extensionsDir, '--install-extension', extensionId]);
2112
}
2213

2314
async function main() {
2415
try {
2516
const extensionDevelopmentPath = path.resolve(__dirname, '../..');
2617
const extensionTestsPath = path.resolve(__dirname, './suite/index');
27-
const workspacePath = path.resolve(
28-
extensionDevelopmentPath,
29-
'test-fixtures/ruby-workspace'
30-
);
31-
const extensionsDir = path.resolve(
32-
extensionDevelopmentPath,
33-
'.vscode-test/extensions'
34-
);
18+
const workspacePath = path.resolve(extensionDevelopmentPath, 'test-fixtures/ruby-workspace');
19+
const extensionsDir = path.resolve(extensionDevelopmentPath, '.vscode-test/extensions');
3520
const vscodeExecutablePath = await downloadAndUnzipVSCode('stable');
3621
const cliPath = resolveCliPathFromVSCodeExecutablePath(vscodeExecutablePath);
3722

@@ -43,13 +28,12 @@ async function main() {
4328
extensionDevelopmentPath,
4429
extensionTestsPath,
4530
vscodeExecutablePath,
46-
launchArgs: [workspacePath, '--extensions-dir', extensionsDir],
31+
launchArgs: [workspacePath, '--extensions-dir', extensionsDir]
4732
});
4833
} catch (error) {
49-
console.error('Failed to run extension tests.');
50-
console.error(error);
34+
console.error('Failed to run extension tests.', error);
5135
process.exit(1);
5236
}
5337
}
5438

55-
void main();
39+
main();

src/test/suite/extension.test.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
import * as assert from 'assert';
1+
import { ok } from 'node:assert';
22
import * as vscode from 'vscode';
33

44
const FIXTURE_RELATIVE_PATH = 'app/views/example.html.erb';
55

66
suite('ERB Inline JS Helper - Happy Path', () => {
77
let document: vscode.TextDocument;
88

9-
suiteSetup(async function () {
9+
suiteSetup(async () => {
1010
const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath;
11-
assert.ok(workspaceRoot, 'Workspace is not open');
11+
ok(workspaceRoot, 'Workspace is not open');
1212

1313
document = await vscode.workspace.openTextDocument(vscode.Uri.file(`${workspaceRoot}/${FIXTURE_RELATIVE_PATH}`));
1414

1515
const extension = vscode.extensions.getExtension('kudoas.erb-inline-js-helper');
16-
assert.ok(extension, 'Extension is not found');
16+
ok(extension, 'Extension is not found');
1717
await extension.activate();
18-
assert.ok(extension.isActive, 'Extension did not activate');
18+
ok(extension.isActive, 'Extension did not activate');
1919
});
2020

21-
test('extension activates', async function () {
21+
test('extension activates', async () => {
2222
const extension = vscode.extensions.getExtension('kudoas.erb-inline-js-helper');
2323

24-
assert.ok(extension, 'Extension is not found');
24+
ok(extension, 'Extension is not found');
2525
await extension.activate();
26-
assert.ok(extension.isActive, 'Extension did not activate');
26+
ok(extension.isActive, 'Extension did not activate');
2727
});
2828

29-
test('completion provides console.log', async function () {
29+
test('completion provides console.log', async () => {
3030
const position = positionAtSubstring(document, 'console.', 0, 'completion target', 'after');
3131

3232
const list = await vscode.commands.executeCommand<vscode.CompletionList>(
@@ -36,29 +36,29 @@ suite('ERB Inline JS Helper - Happy Path', () => {
3636
'.'
3737
);
3838

39-
assert.ok(list, 'Completion list is undefined');
39+
ok(list, 'Completion list is undefined');
4040
const labels = list.items.map((item) => (typeof item.label === 'string' ? item.label : item.label.label));
41-
assert.ok(labels.includes('log'), 'Expected "log" in completion items');
41+
ok(labels.includes('log'), 'Expected "log" in completion items');
4242
});
4343

44-
test('hover shows info for message', async function () {
44+
test('hover shows info for message', async () => {
4545
const position = positionAtSubstring(document, 'message', 0, 'hover target');
4646
const hovers = await vscode.commands.executeCommand<vscode.Hover[]>(
4747
'vscode.executeHoverProvider',
4848
document.uri,
4949
position
5050
);
5151

52-
assert.ok(hovers && hovers.length > 0, 'Hover is empty');
52+
ok(hovers && hovers.length > 0, 'Hover is empty');
5353
const text = hovers
5454
.flatMap((hover) => hover.contents)
5555
.map((content) => (typeof content === 'string' ? content : content.value))
5656
.join(' ');
5757

58-
assert.ok(text.includes('message'), 'Hover does not mention "message"');
58+
ok(text.includes('message'), 'Hover does not mention "message"');
5959
});
6060

61-
test('definition points to message declaration', async function () {
61+
test('definition points to message declaration', async () => {
6262
const usageLineIndex = indexOfSubstring(document, 'console.log(message);', 0, 'definition usage line');
6363
const usageNameIndex = indexOfSubstring(document, 'message', usageLineIndex, 'definition usage');
6464
const usagePosition = document.positionAt(usageNameIndex);
@@ -67,7 +67,7 @@ suite('ERB Inline JS Helper - Happy Path', () => {
6767
>('vscode.executeDefinitionProvider', document.uri, usagePosition);
6868

6969
const locations = normalizeLocations(definitions);
70-
assert.ok(locations.length > 0, 'Definition result is empty');
70+
ok(locations.length > 0, 'Definition result is empty');
7171

7272
const declarationLineIndex = indexOfSubstring(document, 'const message', 0, 'definition declaration');
7373
const declarationNameIndex = indexOfSubstring(
@@ -83,7 +83,7 @@ suite('ERB Inline JS Helper - Happy Path', () => {
8383
location.uri.toString() === document.uri.toString() && location.range.start.isEqual(declarationNamePosition)
8484
);
8585

86-
assert.ok(match, 'Definition did not resolve to the message declaration');
86+
ok(match, 'Definition did not resolve to the message declaration');
8787
});
8888
});
8989

@@ -102,7 +102,7 @@ function positionAtSubstring(
102102
function indexOfSubstring(document: vscode.TextDocument, substring: string, fromIndex: number, label: string): number {
103103
const text = document.getText();
104104
const index = text.indexOf(substring, fromIndex);
105-
assert.ok(index !== -1, `Missing substring for ${label}: ${substring}`);
105+
ok(index !== -1, `Missing substring for ${label}: ${substring}`);
106106
return index;
107107
}
108108

src/test/suite/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as fs from 'fs';
2-
import * as path from 'path';
1+
import * as fs from 'node:fs';
2+
import * as path from 'node:path';
33

44
import Mocha from 'mocha';
55

src/test/unit/erbBlocks.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { ok, strictEqual } from 'node:assert';
2+
3+
import { findJavascriptTagBlock } from '../../erbBlock';
4+
5+
suite('erbBlock', () => {
6+
test('findJavascriptTagBlock returns expected range', () => {
7+
const erb = `
8+
<div>
9+
<%= javascript_tag do %>
10+
const answer = 42;
11+
<% end %>
12+
</div>`;
13+
const offset = erb.indexOf('answer');
14+
const block = findJavascriptTagBlock(erb, offset);
15+
16+
ok(block);
17+
const expectedStart = erb.indexOf('%>') + 2;
18+
const expectedEnd = erb.indexOf('<% end %>');
19+
strictEqual(block!.start, expectedStart);
20+
strictEqual(block!.end, expectedEnd);
21+
});
22+
});

tsconfig.test.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"types": ["node", "mocha", "vscode"]
5+
},
6+
"include": ["src/**/*.ts"]
7+
}

0 commit comments

Comments
 (0)