Skip to content

Commit ffeb0ff

Browse files
authored
fix: include filename in error output (#12)
1 parent a794371 commit ffeb0ff

File tree

3 files changed

+96
-15
lines changed

3 files changed

+96
-15
lines changed

__tests__/main.test.ts

+62-9
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,14 @@ describe('action', () => {
305305

306306
await main.run();
307307
expect(runSpy).toHaveReturned();
308-
expect(process.exitCode).not.toBeDefined();
308+
expect(process.exitCode).toEqual(1);
309309

310-
expect(core.setFailed).toHaveBeenLastCalledWith(
311-
'JSON schema missing $schema key'
310+
expect(core.error).toHaveBeenLastCalledWith(
311+
'JSON schema missing $schema key',
312+
{
313+
title: 'JSON Schema Validation Error',
314+
file: '/foo/bar'
315+
}
312316
);
313317
});
314318

@@ -359,6 +363,29 @@ describe('action', () => {
359363
expect(runSpy).toHaveReturned();
360364
expect(process.exitCode).toEqual(1);
361365

366+
expect(core.error).toHaveBeenCalledWith(
367+
'Error while validating file: /foo/bar/baz/config.yml'
368+
);
369+
expect(core.error).toHaveBeenLastCalledWith(
370+
JSON.stringify(
371+
{
372+
instancePath: '',
373+
schemaPath: '#/oneOf',
374+
keyword: 'oneOf',
375+
params: {
376+
passingSchemas: [0, 1]
377+
},
378+
message: 'must match exactly one schema in oneOf'
379+
},
380+
null,
381+
4
382+
),
383+
{
384+
title: 'JSON Schema Validation Error',
385+
file: '/foo/bar/baz/config.yml'
386+
}
387+
);
388+
362389
expect(core.setOutput).toHaveBeenCalledTimes(1);
363390
expect(core.setOutput).toHaveBeenLastCalledWith('valid', false);
364391
});
@@ -447,14 +474,36 @@ describe('action', () => {
447474
});
448475

449476
it('which are invalid', async () => {
450-
mockGetBooleanInput({ 'fail-on-invalid': false });
477+
mockGetBooleanInput({ 'fail-on-invalid': true });
451478

452479
vi.mocked(fs.readFile).mockResolvedValueOnce(invalidSchemaContents);
453480

454481
await main.run();
455482
expect(runSpy).toHaveReturned();
456-
expect(process.exitCode).not.toBeDefined();
483+
expect(process.exitCode).toEqual(1);
457484

485+
expect(core.error).toHaveBeenCalledWith(
486+
'Error while validating file: /foo/bar/baz/config.yml'
487+
);
488+
expect(core.error).toHaveBeenLastCalledWith(
489+
JSON.stringify(
490+
{
491+
instancePath: '/properties/foobar/minLength',
492+
schemaPath: '#/definitions/nonNegativeInteger/type',
493+
keyword: 'type',
494+
params: {
495+
type: 'integer'
496+
},
497+
message: 'must be integer'
498+
},
499+
null,
500+
4
501+
),
502+
{
503+
title: 'JSON Schema Validation Error',
504+
file: '/foo/bar/baz/config.yml'
505+
}
506+
);
458507
expect(core.setOutput).toHaveBeenCalledTimes(1);
459508
expect(core.setOutput).toHaveBeenLastCalledWith('valid', false);
460509
});
@@ -482,10 +531,14 @@ describe('action', () => {
482531

483532
await main.run();
484533
expect(runSpy).toHaveReturned();
485-
expect(process.exitCode).not.toBeDefined();
486-
487-
expect(core.setFailed).toHaveBeenLastCalledWith(
488-
'JSON schema missing $schema key'
534+
expect(process.exitCode).toEqual(1);
535+
536+
expect(core.error).toHaveBeenLastCalledWith(
537+
'JSON schema missing $schema key',
538+
{
539+
title: 'JSON Schema Validation Error',
540+
file: '/foo/bar/baz/config.yml'
541+
}
489542
);
490543
});
491544
});

dist/index.js

+17-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,12 @@ export async function run(): Promise<void> {
111111
);
112112

113113
if (typeof schema.$schema !== 'string') {
114-
core.setFailed('JSON schema missing $schema key');
114+
core.error(`Error while validating schema: ${schemaPath}`);
115+
core.error('JSON schema missing $schema key', {
116+
title: 'JSON Schema Validation Error',
117+
file: schemaPath
118+
});
119+
process.exitCode = 1;
115120
return;
116121
}
117122

@@ -134,7 +139,12 @@ export async function run(): Promise<void> {
134139
const instance = yaml.parse(await fs.readFile(file, 'utf-8'));
135140

136141
if (validatingSchema && typeof instance.$schema !== 'string') {
137-
core.setFailed('JSON schema missing $schema key');
142+
core.error(`Error while validating schema: ${file}`);
143+
core.error('JSON schema missing $schema key', {
144+
title: 'JSON Schema Validation Error',
145+
file
146+
});
147+
process.exitCode = 1;
138148
return;
139149
}
140150

@@ -145,7 +155,11 @@ export async function run(): Promise<void> {
145155
core.debug(`𐄂 ${file} is not valid`);
146156

147157
for (const error of errors) {
148-
core.error(JSON.stringify(error, null, 4));
158+
core.error(`Error while validating file: ${file}`);
159+
core.error(JSON.stringify(error, null, 4), {
160+
title: 'JSON Schema Validation Error',
161+
file
162+
});
149163
}
150164
} else {
151165
core.debug(`✓ ${file} is valid`);

0 commit comments

Comments
 (0)