-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTestApp.ts
156 lines (126 loc) · 3.52 KB
/
TestApp.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { expect } from 'chai';
import { vol } from 'memfs';
import { main, STATUS_ERROR, STATUS_SUCCESS } from '../src/app.js';
import { Filesystem, readSource, resetFs, setFs } from '../src/source.js';
const TEST_ARGS_PRE = ['node', 'test'];
const TEST_ARGS_CONFIG = ['--config-path', 'docs', '--config-name', 'config.yml'];
const TEST_ARGS_RULES = ['--rule-file', 'rules.yml', '--tag', 'test'];
const TEST_ARGS_SOURCE = ['--source', 'test.yml'];
const TEST_FILES = {
'./docs/config.yml': 'data: {logger: {level: debug, name: test, stream: !stream stderr}}',
'./docs/partial.yml': 'data: {logger: {name: test}}',
'./rules.yml': `{
name: test,
rules: [{
name: test,
desc: test-rule,
level: info,
tags: [test],
check: {
type: object,
required: [foo],
properties: {
foo: {
type: number,
default: 4
}
}
}
}]
}`,
'./test.yml': 'hello world',
};
describe('main app', async () => {
beforeEach(() => {
vol.reset();
});
afterEach(() => {
resetFs();
});
it('completion should succeed', async () => {
const status = await main(['node', 'test', 'complete']);
expect(status).to.equal(STATUS_SUCCESS);
});
it('should list rules and exit', async () => {
vol.fromJSON(TEST_FILES);
const restore = setFs(vol.promises as Filesystem);
const status = await main([
...TEST_ARGS_PRE,
'list',
...TEST_ARGS_CONFIG,
]);
restore();
expect(status).to.equal(STATUS_SUCCESS);
});
it('should load the source', async () => {
vol.fromJSON(TEST_FILES);
const restore = setFs(vol.promises as Filesystem);
const status = await main([
...TEST_ARGS_PRE,
...TEST_ARGS_CONFIG,
...TEST_ARGS_SOURCE,
]);
restore();
expect(status).to.equal(STATUS_SUCCESS);
});
it('should exit with rule errors', async () => {
vol.fromJSON(TEST_FILES);
const restore = setFs(vol.promises as Filesystem);
const status = await main([
...TEST_ARGS_PRE,
...TEST_ARGS_CONFIG,
...TEST_ARGS_SOURCE,
...TEST_ARGS_RULES,
]);
restore();
expect(status).to.equal(STATUS_ERROR);
});
it('should exit with error count', async () => {
vol.fromJSON(TEST_FILES);
const restore = setFs(vol.promises as Filesystem);
const status = await main([
...TEST_ARGS_PRE,
...TEST_ARGS_CONFIG,
...TEST_ARGS_SOURCE,
...TEST_ARGS_RULES,
'--count',
]);
restore();
expect(status).to.equal(STATUS_ERROR);
});
it('should fix up partial documents', async () => {
vol.fromJSON({
...TEST_FILES,
'test.yml': '{}',
});
const restore = setFs(vol.promises as Filesystem);
const status = await main([
...TEST_ARGS_PRE,
'fix',
...TEST_ARGS_CONFIG,
...TEST_ARGS_SOURCE,
...TEST_ARGS_RULES,
'--dest', 'test-dest',
]);
const result = await readSource('test-dest');
restore();
expect(status).to.equal(STATUS_SUCCESS);
expect(result).to.equal('foo: 4\n');
});
it('should validate config before running', async () => {
vol.fromJSON(TEST_FILES);
const restore = setFs(vol.promises as Filesystem);
const [configPath, configDocs, configName] = TEST_ARGS_CONFIG;
const status = await main([
...TEST_ARGS_PRE,
configPath,
configDocs,
configName,
'partial.yml',
...TEST_ARGS_RULES,
...TEST_ARGS_SOURCE,
]);
restore();
expect(status).to.equal(STATUS_ERROR);
});
});