2019-11-10 03:56:49 +00:00
|
|
|
import { expect } from 'chai';
|
2021-07-14 05:13:21 +00:00
|
|
|
import { vol } from 'memfs';
|
2019-11-10 03:56:49 +00:00
|
|
|
|
2022-02-02 05:45:57 +00:00
|
|
|
import { main, STATUS_ERROR, STATUS_SUCCESS } from '../src/app.js';
|
2022-02-02 13:46:24 +00:00
|
|
|
import { Filesystem, readSource, resetFs, setFs } from '../src/source.js';
|
2019-11-10 03:56:49 +00:00
|
|
|
|
2019-11-16 00:43:46 +00:00
|
|
|
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 = {
|
2021-07-14 05:13:21 +00:00
|
|
|
'./docs/config.yml': 'data: {logger: {level: debug, name: test, stream: !stream stderr}}',
|
|
|
|
'./docs/partial.yml': 'data: {logger: {name: test}}',
|
|
|
|
'./rules.yml': `{
|
2019-11-16 12:31:37 +00:00
|
|
|
name: test,
|
|
|
|
rules: [{
|
|
|
|
name: test,
|
2019-11-17 17:55:33 +00:00
|
|
|
desc: test-rule,
|
2019-11-16 12:31:37 +00:00
|
|
|
level: info,
|
|
|
|
tags: [test],
|
|
|
|
check: {
|
|
|
|
type: object,
|
|
|
|
required: [foo],
|
|
|
|
properties: {
|
|
|
|
foo: {
|
|
|
|
type: number,
|
|
|
|
default: 4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}`,
|
2021-07-14 05:13:21 +00:00
|
|
|
'./test.yml': 'hello world',
|
2019-11-16 00:43:46 +00:00
|
|
|
};
|
|
|
|
|
2020-04-02 23:58:57 +00:00
|
|
|
describe('main app', async () => {
|
2021-07-14 05:13:21 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
vol.reset();
|
|
|
|
});
|
|
|
|
|
2022-02-02 13:46:24 +00:00
|
|
|
afterEach(() => {
|
|
|
|
resetFs();
|
|
|
|
});
|
|
|
|
|
2020-04-02 23:58:57 +00:00
|
|
|
it('completion should succeed', async () => {
|
2019-11-10 03:56:49 +00:00
|
|
|
const status = await main(['node', 'test', 'complete']);
|
|
|
|
expect(status).to.equal(STATUS_SUCCESS);
|
|
|
|
});
|
2019-11-14 05:07:54 +00:00
|
|
|
|
2020-04-02 23:58:57 +00:00
|
|
|
it('should list rules and exit', async () => {
|
2021-07-14 05:13:21 +00:00
|
|
|
vol.fromJSON(TEST_FILES);
|
|
|
|
const restore = setFs(vol.promises as Filesystem);
|
2019-11-14 05:27:16 +00:00
|
|
|
|
|
|
|
const status = await main([
|
2019-11-16 00:43:46 +00:00
|
|
|
...TEST_ARGS_PRE,
|
|
|
|
'list',
|
|
|
|
...TEST_ARGS_CONFIG,
|
2019-11-14 05:27:16 +00:00
|
|
|
]);
|
|
|
|
|
2021-07-14 05:13:21 +00:00
|
|
|
restore();
|
2019-11-14 05:27:16 +00:00
|
|
|
|
2019-11-14 05:07:54 +00:00
|
|
|
expect(status).to.equal(STATUS_SUCCESS);
|
|
|
|
});
|
|
|
|
|
2020-04-02 23:58:57 +00:00
|
|
|
it('should load the source', async () => {
|
2021-07-14 05:13:21 +00:00
|
|
|
vol.fromJSON(TEST_FILES);
|
|
|
|
const restore = setFs(vol.promises as Filesystem);
|
2019-11-14 05:07:54 +00:00
|
|
|
|
|
|
|
const status = await main([
|
2019-11-16 00:43:46 +00:00
|
|
|
...TEST_ARGS_PRE,
|
|
|
|
...TEST_ARGS_CONFIG,
|
|
|
|
...TEST_ARGS_SOURCE,
|
2019-11-14 05:07:54 +00:00
|
|
|
]);
|
|
|
|
|
2021-07-14 05:13:21 +00:00
|
|
|
restore();
|
2019-11-14 05:07:54 +00:00
|
|
|
|
|
|
|
expect(status).to.equal(STATUS_SUCCESS);
|
|
|
|
});
|
2019-11-16 00:43:46 +00:00
|
|
|
|
2020-04-02 23:58:57 +00:00
|
|
|
it('should exit with rule errors', async () => {
|
2021-07-14 05:13:21 +00:00
|
|
|
vol.fromJSON(TEST_FILES);
|
|
|
|
const restore = setFs(vol.promises as Filesystem);
|
2019-11-16 00:43:46 +00:00
|
|
|
|
|
|
|
const status = await main([
|
|
|
|
...TEST_ARGS_PRE,
|
|
|
|
...TEST_ARGS_CONFIG,
|
|
|
|
...TEST_ARGS_SOURCE,
|
|
|
|
...TEST_ARGS_RULES,
|
|
|
|
]);
|
|
|
|
|
2021-07-14 05:13:21 +00:00
|
|
|
restore();
|
2019-11-16 00:43:46 +00:00
|
|
|
|
|
|
|
expect(status).to.equal(STATUS_ERROR);
|
|
|
|
});
|
|
|
|
|
2020-04-02 23:58:57 +00:00
|
|
|
it('should exit with error count', async () => {
|
2021-07-14 05:13:21 +00:00
|
|
|
vol.fromJSON(TEST_FILES);
|
|
|
|
const restore = setFs(vol.promises as Filesystem);
|
2019-11-16 00:43:46 +00:00
|
|
|
|
|
|
|
const status = await main([
|
|
|
|
...TEST_ARGS_PRE,
|
|
|
|
...TEST_ARGS_CONFIG,
|
|
|
|
...TEST_ARGS_SOURCE,
|
|
|
|
...TEST_ARGS_RULES,
|
|
|
|
'--count',
|
|
|
|
]);
|
|
|
|
|
2021-07-14 05:13:21 +00:00
|
|
|
restore();
|
2019-11-16 00:43:46 +00:00
|
|
|
|
|
|
|
expect(status).to.equal(STATUS_ERROR);
|
|
|
|
});
|
2019-11-16 00:56:08 +00:00
|
|
|
|
2019-11-16 12:31:37 +00:00
|
|
|
it('should fix up partial documents', async () => {
|
2021-07-14 05:13:21 +00:00
|
|
|
vol.fromJSON({
|
2019-11-16 12:31:37 +00:00
|
|
|
...TEST_FILES,
|
|
|
|
'test.yml': '{}',
|
|
|
|
});
|
2021-07-14 05:13:21 +00:00
|
|
|
const restore = setFs(vol.promises as Filesystem);
|
2019-11-16 12:31:37 +00:00
|
|
|
|
|
|
|
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');
|
|
|
|
|
2021-07-14 05:13:21 +00:00
|
|
|
restore();
|
2019-11-16 12:31:37 +00:00
|
|
|
|
|
|
|
expect(status).to.equal(STATUS_SUCCESS);
|
|
|
|
expect(result).to.equal('foo: 4\n');
|
|
|
|
});
|
2019-11-17 22:56:59 +00:00
|
|
|
|
|
|
|
it('should validate config before running', async () => {
|
2021-07-14 05:13:21 +00:00
|
|
|
vol.fromJSON(TEST_FILES);
|
|
|
|
const restore = setFs(vol.promises as Filesystem);
|
2019-11-17 22:56:59 +00:00
|
|
|
|
2019-11-19 12:03:48 +00:00
|
|
|
const [configPath, configDocs, configName] = TEST_ARGS_CONFIG;
|
2019-11-17 22:56:59 +00:00
|
|
|
const status = await main([
|
|
|
|
...TEST_ARGS_PRE,
|
2019-11-19 12:03:48 +00:00
|
|
|
configPath,
|
|
|
|
configDocs,
|
|
|
|
configName,
|
2019-11-17 22:56:59 +00:00
|
|
|
'partial.yml',
|
|
|
|
...TEST_ARGS_RULES,
|
|
|
|
...TEST_ARGS_SOURCE,
|
|
|
|
]);
|
|
|
|
|
2021-07-14 05:13:21 +00:00
|
|
|
restore();
|
2019-11-17 22:56:59 +00:00
|
|
|
|
|
|
|
expect(status).to.equal(STATUS_ERROR);
|
|
|
|
});
|
2019-11-10 03:56:49 +00:00
|
|
|
});
|