Path: blob/main/tests/unit/schema-validation/utils.ts
6451 views
/*1* utils.ts2*3* Copyright (C) 2021-2022 Posit Software, PBC4*5*/67import { fileLoader } from "../../utils.ts";8import { unitTest } from "../../test.ts";9import { initYamlIntelligenceResourcesFromFilesystem } from "../../../src/core/schema/utils.ts";10import {11initState,12setInitializer,13} from "../../../src/core/lib/yaml-validation/state.ts";14import { ensureSchemaResources } from "../../../src/core/schema/yaml-schema.ts";15import { convertFromYaml } from "../../../src/core/lib/yaml-schema/from-yaml.ts";16import { setSchemaDefinition } from "../../../src/core/lib/yaml-validation/schema.ts";17import { ValidationError } from "../../../src/core/lib/yaml-schema/validated-yaml.ts";18import { isEqual } from "../../../src/core/lodash.ts";19import { assertRejects } from "testing/asserts";20import { readYamlFromString } from "../../../src/core/yaml.ts";21import { readAnnotatedYamlFromMappedString } from "../../../src/core/schema/annotated-yaml.ts";22import {23asMappedString,24MappedString,25} from "../../../src/core/lib/mapped-text.ts";26import {27AnnotatedParse,28Schema,29} from "../../../src/core/lib/yaml-schema/types.ts";30import { navigate } from "../../../src/core/lib/yaml-intelligence/annotated-yaml.ts";31import { idSchema } from "../../../src/core/lib/yaml-schema/common.ts";3233export const schemaTestFile = fileLoader("schema-validation");3435export async function fullInit() {36await initYamlIntelligenceResourcesFromFilesystem();37await ensureSchemaResources();38}3940export function readSelfValidatingSchemaTestFile(file: string): {41annotation: AnnotatedParse;42mappedYaml: MappedString;43schema: Schema;44} {45const mappedYaml = asMappedString(Deno.readTextFileSync(file));46const annotatedYaml = readAnnotatedYamlFromMappedString(47mappedYaml,48);49const annotation = navigate(["value"], annotatedYaml)!;5051// deno-lint-ignore no-explicit-any52let schema = convertFromYaml((annotatedYaml.result as any).schema);53if (schema.$id === undefined) {54schema = idSchema(schema, String(Math.random()).slice(2));55}56setSchemaDefinition(schema);5758return { annotation, schema, mappedYaml };59}6061export function schemaFromString(schemaStr: string) {62const yaml = readYamlFromString(schemaStr);63const schema = convertFromYaml(yaml);64setSchemaDefinition(schema);65return schema;66}6768export function expectValidationError(e: ValidationError) {69let willThrow = e instanceof ValidationError;70const goodE = e as ValidationError;71const result = {72toHaveLength(l: number) {73if (!willThrow) return result;74if (goodE.validationErrors.length !== l) {75willThrow = false;76}77return result;78},79toHaveInfo(key: string, which?: number) {80if (!willThrow) return result;81if (which === undefined) {82which = 0;83}84if (goodE.validationErrors[which] === undefined) {85willThrow = false;86return result;87}88if (goodE.validationErrors[which].niceError.info[key] === undefined) {89willThrow = false;90return result;91}92return result;93},94forSchemaPathToEndWith(spec: string | string[], which?: number) {95if (!willThrow) return result;96if (typeof spec === "string") {97spec = [spec];98}99if (which === undefined) {100which = 0;101}102if (goodE.validationErrors[which] === undefined) {103willThrow = false;104return result;105}106if (107!isEqual(108spec,109goodE.validationErrors[which].schemaPath.slice(-spec.length),110)111) {112willThrow = false;113return result;114}115return result;116},117go() {118if (willThrow) {119throw e;120}121},122};123return result;124}125126export type ValidationChecker = ReturnType<typeof expectValidationError>;127128export async function assertYamlValidationFails(129fun: () => Promise<unknown>,130checker: (e: ValidationError) => ValidationChecker,131) {132await assertRejects(async () => {133try {134await fun();135} catch (e) {136if (e instanceof ValidationError) {137checker(e).go();138}139}140});141}142143// deno-lint-ignore require-await144export async function yamlValidationUnitTest(145name: string,146fun: () => Promise<unknown>,147) {148unitTest(name, async () => {149setInitializer(fullInit);150await initState();151await fun();152});153}154155156