fix(tests): correct paths to schema, test config
This commit is contained in:
parent
3b7e48a494
commit
ed26185e31
|
@ -19,7 +19,7 @@ clean-deps: ## clean up the node_modules directory
|
|||
rm -rf node_modules/
|
||||
|
||||
COVER_ARGS := --all \
|
||||
--100 \
|
||||
--check-coverage \
|
||||
--exclude ".eslintrc.js" \
|
||||
--exclude "bundle/**" \
|
||||
--exclude "config/**" \
|
||||
|
|
|
@ -24,7 +24,7 @@ do
|
|||
STDOUT_PATH=./out/${example}-stdout.log
|
||||
STDERR_PATH=./out/${example}-stderr.log
|
||||
|
||||
node out/index.js \
|
||||
node out/src/index.js \
|
||||
--config-path ./docs \
|
||||
--config-name config-stderr.yml \
|
||||
--count \
|
||||
|
|
|
@ -19,8 +19,11 @@ export interface ConfigData {
|
|||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Path to project root directory.
|
||||
*/
|
||||
export function dirName(): string {
|
||||
return dirname(fileURLToPath(import.meta.url));
|
||||
return join(dirname(fileURLToPath(import.meta.url)), '..', '..', '..');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -63,7 +66,7 @@ export async function loadConfig(name: string, ...extras: Array<string>): Promis
|
|||
const parser = new YamlParser();
|
||||
const [head] = parser.parse(data);
|
||||
|
||||
/* eslint-disable-next-line sonarjs/prefer-immediate-return */
|
||||
/* eslint-disable-next-line sonarjs/prefer-immediate-return,@typescript-eslint/no-explicit-any */
|
||||
return head as any; // TODO: validate config
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
import { hasItems } from '@apextoaster/js-utils';
|
||||
import deepDiff from 'deep-diff';
|
||||
const { applyDiff, diff } = deepDiff;
|
||||
import lodash from 'lodash';
|
||||
const { cloneDeep } = lodash;
|
||||
|
||||
import { Rule } from './index.js';
|
||||
import { Visitor } from '../visitor/index.js';
|
||||
import { VisitorContext } from '../visitor/VisitorContext.js';
|
||||
|
||||
const { applyDiff, diff } = deepDiff;
|
||||
/* eslint-disable-next-line @typescript-eslint/unbound-method */
|
||||
const { cloneDeep } = lodash;
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
|
||||
export interface RuleVisitorOptions {
|
||||
|
|
|
@ -7,9 +7,9 @@ import { Visitor, VisitorError, VisitorResult } from '../visitor/index.js';
|
|||
import { VisitorContext } from '../visitor/VisitorContext.js';
|
||||
import { Rule, RuleData } from './index.js';
|
||||
|
||||
/* eslint-disable-next-line @typescript-eslint/unbound-method */
|
||||
const { cloneDeep, defaultTo } = lodash;
|
||||
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/strict-boolean-expressions */
|
||||
|
||||
const DEFAULT_FILTER = () => true;
|
||||
|
|
|
@ -1,18 +1,24 @@
|
|||
import { doesExist, ensureArray } from '@apextoaster/js-utils';
|
||||
import { doesExist, ensureArray, NotFoundError } from '@apextoaster/js-utils';
|
||||
import { ValidateFunction } from 'ajv';
|
||||
import { readFileSync } from 'fs';
|
||||
import lodash from 'lodash';
|
||||
const { intersection } = lodash;
|
||||
import minimatch from 'minimatch';
|
||||
const { Minimatch } = minimatch;
|
||||
import { LogLevel } from 'noicejs';
|
||||
import { join } from 'path';
|
||||
|
||||
// import ruleSchemaData from '../../rules/salty-dog.yml';
|
||||
import { dirName } from '../config/index.js';
|
||||
import { YamlParser } from '../parser/YamlParser.js';
|
||||
import { listFiles, readSource } from '../source.js';
|
||||
import { VisitorResult } from '../visitor/index.js';
|
||||
import { VisitorContext } from '../visitor/VisitorContext.js';
|
||||
import { SchemaRule } from './SchemaRule.js';
|
||||
|
||||
/* eslint-disable-next-line @typescript-eslint/unbound-method */
|
||||
const { intersection } = lodash;
|
||||
const { Minimatch } = minimatch;
|
||||
|
||||
// import ruleSchemaData from '../../rules/salty-dog.yml';
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
export interface RuleData {
|
||||
// metadata
|
||||
|
@ -227,8 +233,23 @@ export async function resolveRules(rules: Array<Rule>, selector: RuleSelector):
|
|||
return Array.from(activeRules);
|
||||
}
|
||||
|
||||
export function loadSchema(): any {
|
||||
const path = join(dirName(), 'rules', 'salty-dog.yml');
|
||||
const data = readFileSync(path, { encoding: 'utf-8' });
|
||||
|
||||
if (doesExist(data)) {
|
||||
const parser = new YamlParser();
|
||||
const [schema] = parser.parse(data);
|
||||
|
||||
// TODO: parse schema data before returning
|
||||
return schema;
|
||||
}
|
||||
|
||||
throw new NotFoundError('');
|
||||
}
|
||||
|
||||
export function validateRules(ctx: VisitorContext, root: any): boolean {
|
||||
const { definitions, name } = { definitions: { config: {} } } as any; // ruleSchemaData as any;
|
||||
const { definitions, name } = loadSchema();
|
||||
|
||||
const validCtx = new VisitorContext(ctx);
|
||||
validCtx.addSchema(name, definitions);
|
||||
|
@ -243,7 +264,7 @@ export function validateRules(ctx: VisitorContext, root: any): boolean {
|
|||
}
|
||||
|
||||
export function validateConfig(ctx: VisitorContext, root: any): boolean {
|
||||
const { definitions, name } = { definitions: { config: {} } } as any; // ruleSchemaData as any;
|
||||
const { definitions, name } = loadSchema();
|
||||
|
||||
const validCtx = new VisitorContext(ctx);
|
||||
validCtx.addSchema(name, definitions);
|
||||
|
|
|
@ -8,7 +8,7 @@ const __dirname = dirName();
|
|||
|
||||
describe('load config helper', async () => {
|
||||
it('should load an existing config', async () =>
|
||||
expect(loadConfig('config-stderr.yml', join(__dirname, '..', 'docs'))).to.eventually.deep.include({
|
||||
expect(loadConfig('config-stderr.yml', join(__dirname, 'docs'))).to.eventually.deep.include({
|
||||
data: {
|
||||
logger: {
|
||||
level: 'debug',
|
||||
|
@ -20,11 +20,11 @@ describe('load config helper', async () => {
|
|||
);
|
||||
|
||||
it('should throw when config is missing', async () =>
|
||||
expect(loadConfig('missing.yml', join(__dirname, '..', 'docs'))).to.eventually.be.rejectedWith(NotFoundError)
|
||||
expect(loadConfig('missing.yml', join(__dirname, 'docs'))).to.eventually.be.rejectedWith(NotFoundError)
|
||||
);
|
||||
|
||||
it('should load included config', async () =>
|
||||
expect(loadConfig('config-include.yml', join(__dirname, '..', 'docs'))).to.eventually.deep.include({
|
||||
expect(loadConfig('config-include.yml', join(__dirname, 'docs'))).to.eventually.deep.include({
|
||||
data: {
|
||||
include: {
|
||||
foo: 'bar',
|
||||
|
|
Loading…
Reference in New Issue