Skip to content

Commit 3cfee9e

Browse files
authored
refactor: modularize syntax grammar tests (#510)
* refactor: modularize syntax grammar tests As we add more TextMate grammars and tests, the bash script we currently have would not suffice because for an singular snapshot or unit tests, the TextMate grammar testing software [we use](https://github.com/PanAeon/vscode-tmgrammar-test) requires 1. The scope of the grammar to test 2. The grammar specification files 3. The test file itself To conviniently express these requirements for each test, this commit adds a test runner written in JavaScript with an array specifying the layout of each test. * fixup! refactor: modularize syntax grammar tests * fixup! refactor: modularize syntax grammar tests * fixup! refactor: modularize syntax grammar tests
1 parent a943874 commit 3cfee9e

File tree

11 files changed

+79
-24
lines changed

11 files changed

+79
-24
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ dist/
1212
.vscode/settings.json
1313
.vimrc
1414
.nvimrc
15+
syntaxes/test/*.js
16+
syntaxes/test/*.map

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ script:
1717
- ./scripts/build.sh
1818
- ./scripts/test.sh
1919
- ./scripts/format.sh
20-
- ./scripts/syntax.sh
2120

2221
after_script:
2322
- ./scripts/cleanup.sh

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,15 @@
7171
"compile:banner": "tsc -p server/banner.tsconfig.json && rollup -c banner.rollup.config.js",
7272
"compile:test": "tsc -b server/src/tests",
7373
"compile:integration": "tsc -b integration",
74+
"compile:syntaxes-test": "tsc -b syntaxes/test",
7475
"format": "scripts/format.sh",
7576
"watch": "tsc -b -w",
7677
"postinstall": "vscode-install && cd client && yarn && cd ../server && yarn && cd ..",
7778
"package": "rm -rf dist && node scripts/package.js",
7879
"test": "yarn compile:test && jasmine server/out/tests/*_spec.js",
7980
"test:lsp": "yarn compile:integration && jasmine integration/out/lsp/*_spec.js",
8081
"test:e2e": "yarn compile:integration && ./scripts/e2e.sh",
81-
"test:syntax": "./scripts/syntax.sh"
82+
"test:syntaxes": "yarn compile:syntaxes-test && jasmine syntaxes/test/driver.js"
8283
},
8384
"dependencies": {
8485
"typescript": "~3.6.4"

scripts/syntax.sh

Lines changed: 0 additions & 19 deletions
This file was deleted.

scripts/test.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ yarn run test:lsp
1717
# E2E test that brings up full vscode
1818
# TODO: Disabled for now because it cannot be run on Travis
1919
# yarn run test:e2e
20+
21+
# Syntaxes tests
22+
yarn run test:syntaxes

syntaxes/test/cases.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"scopeName": "inline-template.ng",
4+
"grammarFiles": ["syntaxes/inline-template.json"],
5+
"testFile": "syntaxes/test/data/inline-template.ts"
6+
}
7+
]

syntaxes/test/inline_template.ts renamed to syntaxes/test/data/inline-template.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// SYNTAX TEST "template.ng"
21
/* clang-format off */
32

43
@Component({

syntaxes/test/inline_template.ts.snap renamed to syntaxes/test/data/inline-template.ts.snap

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
>// SYNTAX TEST "template.ng"
2-
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ inline-template.ng
31
>/* clang-format off */
42
#^^^^^^^^^^^^^^^^^^^^^^^ inline-template.ng
53
>

syntaxes/test/driver.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env node
2+
// Usage:
3+
// yarn test:syntaxes [options]
4+
//
5+
// Options:
6+
// -u, --update update snapshot files (always passes)
7+
8+
import 'jasmine';
9+
10+
import * as cp from 'child_process';
11+
import * as fs from 'fs';
12+
import * as path from 'path';
13+
import * as util from 'util';
14+
import * as SNAPSHOT_TEST_CASES from './cases.json';
15+
16+
interface TestCase {
17+
scopeName: string;
18+
grammarFiles: string[];
19+
testFile: string;
20+
}
21+
22+
const dummyGrammarDir = path.join(__dirname, 'dummy');
23+
const DUMMY_GRAMMARS =
24+
fs.readdirSync(dummyGrammarDir).map((file: string) => path.join(dummyGrammarDir, file));
25+
26+
/** Wraps node's spawn in a Promise. */
27+
function spawn(...args: Parameters<typeof cp.spawn>): Promise<number> {
28+
const child = cp.spawn(...args);
29+
30+
return new Promise((resolve, reject) => {
31+
child.on('exit', (code: number) => {
32+
if (code === 0)
33+
resolve(0);
34+
else
35+
reject(code);
36+
});
37+
});
38+
}
39+
40+
async function snapshotTest({scopeName, grammarFiles, testFile}: TestCase): Promise<number> {
41+
grammarFiles.push(...DUMMY_GRAMMARS);
42+
const grammarOptions = grammarFiles.reduce((acc, file) => [...acc, '-g', file], [] as string[]);
43+
const extraArgs = process.argv.slice(3);
44+
const options =
45+
['vscode-tmgrammar-snap', '-s', scopeName, ...grammarOptions, '-t', testFile, ...extraArgs];
46+
47+
return spawn('yarn', options, {stdio: 'inherit' /* use parent process IO */}).catch(code => code);
48+
}
49+
50+
describe('snapshot tests', async () => {
51+
it('should pass all cases', async () => {
52+
for (let tc of SNAPSHOT_TEST_CASES) {
53+
const ec = await snapshotTest(tc);
54+
expect(ec).toBe(0);
55+
}
56+
});
57+
});

0 commit comments

Comments
 (0)