-
Notifications
You must be signed in to change notification settings - Fork 62
/
yeoman-generator.test.ts
118 lines (108 loc) · 4.62 KB
/
yeoman-generator.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/******************************************************************************
* Copyright 2022 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/
import { describe, test } from 'vitest';
import { normalizeEOL } from 'langium';
import { createHelpers } from 'yeoman-test';
import * as path from 'node:path';
import * as url from 'node:url';
const __dirname = url.fileURLToPath(new URL('../', import.meta.url));
const answersForCore = {
extensionName: 'hello-world',
rawLanguageName: 'Hello World',
fileExtension: '.hello',
includeVSCode: false,
includeCLI: false,
includeWeb: false,
openWith: false
};
describe('Check yeoman generator works', () => {
const moduleRoot = path.join(__dirname, 'app');
test('Should produce files for Core', async () => {
const context = createHelpers({}).run(moduleRoot);
context.targetDirectory = path.join(__dirname, '../../../examples/hello-world'); // generate in examples
const targetRoot = path.join(__dirname, '../../../examples');
context.cleanTestDirectory(true); // clean-up examples/hello-world
await context
.onGenerator(async (generator) => {
// will generate into examples/hello-world instead of examples/hello-world/hello-world
generator.destinationRoot(targetRoot); // types are wrong, should be string
})
.withAnswers(answersForCore)
.withArguments('skip-install')
.then((result) => {
const files = [
targetRoot + '/hello-world/.eslintrc.json',
targetRoot + '/hello-world/.gitignore',
targetRoot + '/hello-world/langium-config.json',
targetRoot + '/hello-world/langium-quickstart.md',
targetRoot + '/hello-world/tsconfig.json',
targetRoot + '/hello-world/package.json',
targetRoot + '/hello-world/.vscode/extensions.json',
targetRoot + '/hello-world/.vscode/tasks.json',
targetRoot + '/hello-world/src/language/hello-world-module.ts',
targetRoot + '/hello-world/src/language/hello-world-validator.ts',
targetRoot + '/hello-world/src/language/hello-world.langium'
];
result.assertFile(files);
result.assertJsonFileContent(targetRoot + '/hello-world/package.json', PACKAGE_JSON_EXPECTATION);
result.assertFileContent(targetRoot + '/hello-world/.vscode/tasks.json', TASKS_JSON_EXPECTATION);
}).finally(() => {
context.cleanTestDirectory(true);
});
}, 120_000);
});
// eslint-disable-next-line @typescript-eslint/no-var-requires
const langiumVersion = `~${require('../../langium/package.json').version}`;
// eslint-disable-next-line @typescript-eslint/no-var-requires
const langiumCliVersion = `~${require('../../langium-cli/package.json').version}`;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const PACKAGE_JSON_EXPECTATION: Record<string, any> = {
name: 'hello-world',
description: 'Please enter a brief description here',
version: '0.0.1',
files: ['out', 'src'],
scripts: {
'build': 'tsc -b tsconfig.json',
'watch': 'tsc -b tsconfig.json --watch',
'lint': 'eslint src --ext ts',
'langium:generate': 'langium generate',
'langium:watch': 'langium generate --watch'
},
'dependencies': {
'langium': langiumVersion
},
'devDependencies': {
'@types/node': '~16.18.41',
'@typescript-eslint/eslint-plugin': '~6.4.1',
'@typescript-eslint/parser': '~6.4.1',
'eslint': '~8.47.0',
'langium-cli': langiumCliVersion,
'typescript': '~5.1.6'
}
};
const TASKS_JSON_EXPECTATION =
normalizeEOL(`{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Build hello-world",
"command": "npm run langium:generate && npm run build",
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Langium: Generate grammar and build the hello-world language",
"icon": {
"color": "terminal.ansiGreen",
"id": "server-process"
}
}
]
}
`);