|
| 1 | +/* |
| 2 | + * Copyright 2023-Present The Serverless Workflow Specification Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { SWSchemaValidator } from "./index"; |
| 18 | +import fs from "node:fs"; |
| 19 | +import path from "node:path"; |
| 20 | + |
| 21 | +SWSchemaValidator.prepareSchemas(); |
| 22 | + |
| 23 | +const ctkDir = path.join(__dirname, "..", "..", "..", "ctk", "features"); |
| 24 | + |
| 25 | +function extractYamlBlocks(content: string): string[] { |
| 26 | + const yamlBlockRegex = /"""yaml\s([\s\S]*?)\s"""/gm; // Match YAML blocks |
| 27 | + let match; |
| 28 | + const yamlBlocks: string[] = []; |
| 29 | + |
| 30 | + while ((match = yamlBlockRegex.exec(content)) !== null) { |
| 31 | + yamlBlocks.push(match[1]); |
| 32 | + } |
| 33 | + |
| 34 | + return yamlBlocks; |
| 35 | +} |
| 36 | + |
| 37 | +const workflows = fs.readdirSync(ctkDir) |
| 38 | + .filter((file) => file.endsWith(".feature")) |
| 39 | + .flatMap((file) => { |
| 40 | + const filePath = path.join(ctkDir, file); |
| 41 | + const fileContent = fs.readFileSync(filePath, SWSchemaValidator.defaultEncoding); |
| 42 | + |
| 43 | + const yamlBlocks = extractYamlBlocks(fileContent); |
| 44 | + |
| 45 | + return yamlBlocks |
| 46 | + .map((yamlText) => SWSchemaValidator.yamlToJSON(yamlText)) |
| 47 | + .filter((workflow) => typeof workflow === "object") |
| 48 | + .filter((workflow) => "document" in workflow) |
| 49 | + .filter((workflow) => "dsl" in workflow.document) |
| 50 | + .map((workflow) => ({ workflow, file })); |
| 51 | + }); |
| 52 | + |
| 53 | +describe(`Validate workflows from .feature files`, () => { |
| 54 | + test.each(workflows)('$workflow.document.name (from $file)', ({ workflow, file }) => { |
| 55 | + const results = SWSchemaValidator.validateSchema(workflow); |
| 56 | + |
| 57 | + if (results?.errors) { |
| 58 | + console.warn( |
| 59 | + `Schema validation failed for workflow "${workflow.document.name}" in file "${file}" with:`, |
| 60 | + JSON.stringify(results.errors, null, 2) |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + expect(results?.valid).toBeTruthy(); |
| 65 | + }); |
| 66 | +}); |
0 commit comments