Skip to content

Commit

Permalink
test: add test format and diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
action-hong committed Nov 3, 2023
1 parent 29bbaa1 commit 951fda1
Show file tree
Hide file tree
Showing 25 changed files with 1,545 additions and 65 deletions.
53 changes: 53 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: CI

on:
push:
branches:
- main

pull_request:
branches:
- main

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set node
uses: actions/setup-node@v3
with:
node-version: lts/*

- name: Install
run: npm install

- name: Lint
run: npm run lint

test:
runs-on: ${{ matrix.os }}

strategy:
matrix:
node: [lts/*]
os: [ubuntu-latest, windows-latest, macos-latest]
fail-fast: false

steps:
- uses: actions/checkout@v3

- name: Set node version to ${{ matrix.node }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}

- name: Install
run: npm install

- name: Build
run: npm run compile

- name: Test
run: npm run test
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
"javascript",
"typescript",
],
"[markdown]": {
"editor.formatOnSave": false,
"editor.defaultFormatter": "vscode.markdown-language-features"
}
}
11 changes: 7 additions & 4 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ export function activate(context: ExtensionContext) {
debug: {
module: serverModule,
transport: TransportKind.ipc,
options: {
execArgv: ['--nolazy', '--inspect=6009']
}
}
};

// Options to control the language client
const clientOptions: LanguageClientOptions = {
// Register the server for plain text documents
documentSelector: [{ scheme: 'file', language: 'markdown' }],
synchronize: {
// Notify the server about file changes to '.clientrc files contained in the workspace
fileEvents: workspace.createFileSystemWatcher('**/.clientrc')
}
// synchronize: {
// // Notify the server about file changes to '.clientrc files contained in the workspace
// fileEvents: workspace.createFileSystemWatcher('**/.clientrc')
// }
};

// Create the language client and start the client.
Expand Down
43 changes: 0 additions & 43 deletions client/src/test/completion.test.ts

This file was deleted.

46 changes: 37 additions & 9 deletions client/src/test/diagnostics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,56 @@
import * as vscode from 'vscode';
import * as assert from 'assert';
import { getDocUri, activate } from './helper';
import { readFile } from 'fs/promises';

suite('Should get diagnostics', () => {
const docUri = getDocUri('diagnostics.txt');

test('Diagnoses uppercase texts', async () => {
await testDiagnostics(docUri, [
{ message: 'ANY is all uppercase.', range: toRange(0, 0, 0, 3), severity: vscode.DiagnosticSeverity.Warning, source: 'ex' },
{ message: 'ANY is all uppercase.', range: toRange(0, 14, 0, 17), severity: vscode.DiagnosticSeverity.Warning, source: 'ex' },
{ message: 'OS is all uppercase.', range: toRange(0, 18, 0, 20), severity: vscode.DiagnosticSeverity.Warning, source: 'ex' }
]);

const names = [
'example-units',
'example-vuepress'
];

test('Diagnoses works', async () => {

for (const name of names) {
const docUri = getDocUri(`${name}.md`);
const expectDocUri = getDocUri(`${name}-expected.json`);
await testDiagnostics(docUri, expectDocUri);
}
});

test('should get empty diagnostics with disabled', async () => {
const docUri = getDocUri('example-disabled.md');
await activate(docUri);
const diagnostics = vscode.languages.getDiagnostics(docUri);
assert.equal(diagnostics.length, 0);
});
});

function toDiagnostic(message: string, target: string, range: [number, number, number, number]) {
return {
message,
range: toRange(...range),
severity: vscode.DiagnosticSeverity.Warning,
source: 'zhlint',
code: target,
codeDescription: {
href: 'https://zhlint-project.github.io/zhlint/#supported-rules',
},
};
}

function toRange(sLine: number, sChar: number, eLine: number, eChar: number) {
const start = new vscode.Position(sLine, sChar);
const end = new vscode.Position(eLine, eChar);
return new vscode.Range(start, end);
}

async function testDiagnostics(docUri: vscode.Uri, expectedDiagnostics: vscode.Diagnostic[]) {
async function testDiagnostics(docUri: vscode.Uri, exceptUri: vscode.Uri) {
await activate(docUri);

const expectedDiagnostics = JSON.parse(await readFile(exceptUri.fsPath, 'utf8')).map((d: any) => toDiagnostic(d.message, d.target, d.range));

const actualDiagnostics = vscode.languages.getDiagnostics(docUri);

assert.equal(actualDiagnostics.length, expectedDiagnostics.length);
Expand Down
67 changes: 67 additions & 0 deletions client/src/test/formatter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { readFile } from 'fs/promises';
import { activate, getDocUri } from './helper';
import * as vscode from 'vscode';
import * as assert from 'assert';

suite('formatter should work', () => {
test('formatting work', async () => {
const names = [
'example-units',
'example-vuepress'
];

for (const name of names) {
const docUri = getDocUri(`${name}.md`);
const fixDocUri = getDocUri(`${name}-fixed.md`);
const { doc } = await activate(docUri);

const fixed = await readFile(fixDocUri.fsPath, 'utf8');

// format docUri
await vscode.commands.executeCommand('editor.action.formatDocument');
const actual = doc.getText();

assert.equal(actual, fixed);
}
});

// FIXME: test format selection not work, still format whole text
// test('formatting range work', async() => {
// const lists = [
// {
// name: 'example-units',
// selections: [
// new vscode.Selection(0, 0, 5, 61),
// new vscode.Selection(18, 0, 20, 27)
// ]
// },
// {
// name: 'example-vuepress',
// selections: [
// new vscode.Selection(1, 0, 1, 15)
// ]
// }
// ];

// for (const item of lists) {
// const {
// name,
// selections
// } = item;

// const {
// doc,
// editor,
// } = await activate(getDocUri(`${name}.md`));

// const fixed = await readFile(getDocUri(`${name}-select-fixed.md`).fsPath, 'utf8');

// editor.selections = selections;
// await vscode.commands.executeCommand('editor.action.formatSelection');
// const actual = doc.getText();
// assert.equal(actual, fixed);
// }

// });

});
6 changes: 5 additions & 1 deletion client/src/test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export let platformEol: string;
*/
export async function activate(docUri: vscode.Uri) {
// The extensionId is `publisher.name` from package.json
const ext = vscode.extensions.getExtension('vscode-samples.lsp-sample')!;
const ext = vscode.extensions.getExtension('kkopite.zhlint')!;
await ext.activate();
try {
doc = await vscode.workspace.openTextDocument(docUri);
Expand All @@ -25,6 +25,10 @@ export async function activate(docUri: vscode.Uri) {
} catch (e) {
console.error(e);
}
return {
doc,
editor
};
}

async function sleep(ms: number) {
Expand Down
8 changes: 7 additions & 1 deletion client/src/test/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ async function main() {
// Passed to --extensionTestsPath
const extensionTestsPath = path.resolve(__dirname, './index');

const workspace = path.resolve(__dirname, '../../testFixture');

// Download VS Code, unzip it and run the integration test
await runTests({ extensionDevelopmentPath, extensionTestsPath });
await runTests({
extensionDevelopmentPath,
extensionTestsPath,
launchArgs: [workspace, '--disable-extensions'],
});
} catch (err) {
console.error('Failed to run tests');
process.exit(1);
Expand Down
10 changes: 10 additions & 0 deletions client/testFixture/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"zhlint.options": {
"rules": {
"preset": "default"
}
},
"[markdown]": {
"editor.defaultFormatter": "kkopite.zhlint"
}
}
Empty file removed client/testFixture/completion.txt
Empty file.
1 change: 0 additions & 1 deletion client/testFixture/diagnostics.txt

This file was deleted.

Loading

0 comments on commit 951fda1

Please sign in to comment.