Skip to content

Commit 50b93c2

Browse files
test: add tests for examples in dsl-reference
Signed-off-by: Matthias Pichler <[email protected]>
1 parent f658c7d commit 50b93c2

File tree

8 files changed

+271
-120
lines changed

8 files changed

+271
-120
lines changed

.ci/validation/package-lock.json

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.ci/validation/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"dependencies": {
2626
"ajv": "^8.12.0",
2727
"ajv-formats": "^2.1.1",
28-
"js-yaml": "^4.1.0"
28+
"js-yaml": "^4.1.0",
29+
"marked": "^13.0.0"
2930
}
3031
}

.ci/validation/src/dsl.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
import marked from "marked";
21+
22+
SWSchemaValidator.prepareSchemas();
23+
24+
const dslReferencePath = path.join(
25+
__dirname,
26+
"..",
27+
"..",
28+
"..",
29+
"dsl-reference.md"
30+
);
31+
32+
describe(`Verify every example in the dsl docs`, () => {
33+
const workflows = marked
34+
.lexer(fs.readFileSync(dslReferencePath, SWSchemaValidator.defaultEncoding))
35+
.filter((item): item is marked.Tokens.Code => item.type === "code")
36+
.filter((item) => item.lang === "yaml")
37+
.map((item) => item.text)
38+
.map((text) => SWSchemaValidator.yamlToJSON(text))
39+
.filter((workflow) => typeof workflow === "object")
40+
.filter((workflow) => "document" in workflow)
41+
.filter((workflow) => "dsl" in workflow.document);
42+
43+
test.each(workflows)("$document.name", (workflow) => {
44+
const results = SWSchemaValidator.validateSchema(workflow);
45+
if (results?.errors) {
46+
console.warn(
47+
`Schema validation on workflow ${workflow.document.name} failed with: `,
48+
JSON.stringify(results.errors, null, 2)
49+
);
50+
}
51+
expect(results?.valid).toBeTruthy();
52+
});
53+
});

.ci/validation/src/examples.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe(`Verify every example in the repository`, () => {
3434
.map((file) => file.name);
3535

3636
test.each(examples)("Example %s", (file) => {
37-
const workflow = SWSchemaValidator.toJSON(
37+
const workflow = SWSchemaValidator.loadAsJSON(
3838
path.join(__dirname, `${examplePath}/${file}`)
3939
);
4040
const results = SWSchemaValidator.validateSchema(workflow);

.ci/validation/src/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,15 @@ export module SWSchemaValidator {
4444
}
4545

4646
function syncReadSchema(filename: string): any {
47-
return toJSON(path.join(__dirname, `${schemaPath}/${filename}`));
47+
return loadAsJSON(path.join(__dirname, `${schemaPath}/${filename}`));
4848
}
4949

50-
export function toJSON(filename: string): any {
51-
const yamlObj = yaml.load(fs.readFileSync(filename, defaultEncoding), {
50+
export function loadAsJSON(filename: string): any {
51+
return yamlToJSON(fs.readFileSync(filename, defaultEncoding));
52+
}
53+
54+
export function yamlToJSON(yamlStr: string): any {
55+
const yamlObj = yaml.load(yamlStr, {
5256
json: true,
5357
});
5458
return structuredClone(yamlObj);

.ci/validation/src/invalid.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe(`Check that invalid workflows are rejected`, () => {
3434
.map((file) => file.name);
3535

3636
test.each(examples)("Example %s", (file) => {
37-
const workflow = SWSchemaValidator.toJSON(
37+
const workflow = SWSchemaValidator.loadAsJSON(
3838
path.join(__dirname, `${invalidPath}/${file}`)
3939
);
4040
const results = SWSchemaValidator.validateSchema(workflow);

0 commit comments

Comments
 (0)