Skip to content

Commit 2709fb5

Browse files
refactor: update test file to use .each
Signed-off-by: Matthias Pichler <[email protected]>
1 parent 36bb47d commit 2709fb5

File tree

2 files changed

+37
-31
lines changed

2 files changed

+37
-31
lines changed

.ci/validation/src/index.test.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,27 @@ SWSchemaValidator.prepareSchemas();
2323
const examplePath = "../../../examples";
2424

2525
describe(`Verify every example in the repository`, () => {
26-
fs.readdirSync(path.join(__dirname, examplePath), {
27-
encoding: SWSchemaValidator.defaultEncoding,
28-
recursive: false,
29-
withFileTypes: true,
30-
}).forEach((file) => {
31-
if (file.isFile() && file.name.endsWith(".yaml")) {
32-
test(`Example ${file.name}`, () => {
33-
const workflow = SWSchemaValidator.toJSON(
34-
path.join(__dirname, `${examplePath}/${file.name}`)
35-
);
36-
const results = SWSchemaValidator.validateSchema(workflow);
37-
if (results?.errors != null) {
38-
console.warn(
39-
`Schema validation on ${file.name} failed with: `,
40-
JSON.stringify(results.errors, null, 2)
41-
);
42-
}
43-
expect(results?.valid).toBeTruthy();
44-
});
26+
const examples = fs
27+
.readdirSync(path.join(__dirname, examplePath), {
28+
encoding: SWSchemaValidator.defaultEncoding,
29+
recursive: false,
30+
withFileTypes: true,
31+
})
32+
.filter((file) => file.isFile())
33+
.filter((file) => file.name.endsWith(".yaml"))
34+
.map((file) => file.name);
35+
36+
test.each(examples)("Example %s", (file) => {
37+
const workflow = SWSchemaValidator.toJSON(
38+
path.join(__dirname, `${examplePath}/${file}`)
39+
);
40+
const results = SWSchemaValidator.validateSchema(workflow);
41+
if (results?.errors != null) {
42+
console.warn(
43+
`Schema validation on ${file} failed with: `,
44+
JSON.stringify(results.errors, null, 2)
45+
);
4546
}
47+
expect(results?.valid).toBeTruthy();
4648
});
4749
});

.ci/validation/src/index.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@ export module SWSchemaValidator {
3030
export const defaultEncoding = "utf-8";
3131

3232
export function prepareSchemas() {
33-
fs.readdirSync(path.join(__dirname, schemaPath), {
33+
const files = fs.readdirSync(path.join(__dirname, schemaPath), {
3434
encoding: defaultEncoding,
3535
recursive: false,
3636
withFileTypes: true,
37-
}).forEach((file) => {
38-
if (file.isFile()) {
39-
ajv.addSchema(syncReadSchema(file.name));
40-
}
4137
});
38+
39+
files
40+
.filter((file) => file.isFile())
41+
.forEach((file) => {
42+
ajv.addSchema(syncReadSchema(file.name));
43+
});
4244
}
4345

4446
function syncReadSchema(filename: string): any {
@@ -54,14 +56,16 @@ export module SWSchemaValidator {
5456

5557
export function validateSchema(workflow: Record<string, unknown>) {
5658
const validate = ajv.getSchema(workflowSchemaId);
57-
if (validate) {
58-
const isValid = validate(workflow);
59-
return {
60-
valid: isValid,
61-
errors: validate.errors,
62-
};
59+
60+
if (!validate) {
61+
throw new Error(`Failed to validate schema on workflow`);
6362
}
64-
throw new Error(`Failed to validate schema on workflow`);
63+
64+
const isValid = validate(workflow);
65+
return {
66+
valid: isValid,
67+
errors: validate.errors,
68+
};
6569
}
6670
}
6771

0 commit comments

Comments
 (0)