Files
redocly-cli/packages/core/__tests__/utils.ts
2020-11-13 21:13:47 +02:00

67 lines
1.7 KiB
TypeScript

import * as yaml from 'js-yaml';
import * as path from 'path';
import { Document, Source } from '../src/resolve';
import { NormalizedProblem } from '../src/walk';
import { RuleConfig, LintConfig, Plugin } from '../src/config/config';
import { Oas3RuleSet } from '../src/validate';
export function parseYamlToDocument(body: string, absoluteRef: string = ''): Document {
return {
source: new Source(absoluteRef, body),
parsed: yaml.safeLoad(body, { filename: absoluteRef }),
};
}
export function replaceSourceWithRef(results: NormalizedProblem[], cwd?: string) {
const cwdRegexp = cwd ? new RegExp(cwd + path.sep, 'g') : /$^/;
return results.map((r) => {
const mapped = {
...r,
message: r.message.replace(cwdRegexp, ''),
location: r.location.map((l) => ({
...l,
source: cwd ? path.relative(cwd, l.source.absoluteRef) : l.source.absoluteRef,
})),
};
if (mapped.from) {
mapped.from = {
...mapped.from,
source: cwd
? path.relative(cwd, mapped.from.source.absoluteRef)
: (mapped.from.source.absoluteRef as any),
};
}
return mapped;
});
}
export const yamlSerializer = {
test: () => {
return true;
},
print: (val: any) => {
return yaml.safeDump(val);
},
};
export function makeConfigForRuleset(rules: Oas3RuleSet, plugin?: Partial<Plugin>) {
const rulesConf: Record<string, RuleConfig> = {};
const ruleId = 'test';
Object.keys(rules).forEach((name) => {
rulesConf[`${ruleId}/${name}`] = 'error';
});
return new LintConfig({
plugins: [
{
...plugin,
id: ruleId,
rules: { oas3: rules },
},
],
extends: [],
rules: rulesConf,
});
}