Skip to content

Commit

Permalink
fix: include filename in error output (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsanders11 authored Jun 18, 2024
1 parent a794371 commit ffeb0ff
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 15 deletions.
71 changes: 62 additions & 9 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,14 @@ describe('action', () => {

await main.run();
expect(runSpy).toHaveReturned();
expect(process.exitCode).not.toBeDefined();
expect(process.exitCode).toEqual(1);

expect(core.setFailed).toHaveBeenLastCalledWith(
'JSON schema missing $schema key'
expect(core.error).toHaveBeenLastCalledWith(
'JSON schema missing $schema key',
{
title: 'JSON Schema Validation Error',
file: '/foo/bar'
}
);
});

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

expect(core.error).toHaveBeenCalledWith(
'Error while validating file: /foo/bar/baz/config.yml'
);
expect(core.error).toHaveBeenLastCalledWith(
JSON.stringify(
{
instancePath: '',
schemaPath: '#/oneOf',
keyword: 'oneOf',
params: {
passingSchemas: [0, 1]
},
message: 'must match exactly one schema in oneOf'
},
null,
4
),
{
title: 'JSON Schema Validation Error',
file: '/foo/bar/baz/config.yml'
}
);

expect(core.setOutput).toHaveBeenCalledTimes(1);
expect(core.setOutput).toHaveBeenLastCalledWith('valid', false);
});
Expand Down Expand Up @@ -447,14 +474,36 @@ describe('action', () => {
});

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

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

await main.run();
expect(runSpy).toHaveReturned();
expect(process.exitCode).not.toBeDefined();
expect(process.exitCode).toEqual(1);

expect(core.error).toHaveBeenCalledWith(
'Error while validating file: /foo/bar/baz/config.yml'
);
expect(core.error).toHaveBeenLastCalledWith(
JSON.stringify(
{
instancePath: '/properties/foobar/minLength',
schemaPath: '#/definitions/nonNegativeInteger/type',
keyword: 'type',
params: {
type: 'integer'
},
message: 'must be integer'
},
null,
4
),
{
title: 'JSON Schema Validation Error',
file: '/foo/bar/baz/config.yml'
}
);
expect(core.setOutput).toHaveBeenCalledTimes(1);
expect(core.setOutput).toHaveBeenLastCalledWith('valid', false);
});
Expand Down Expand Up @@ -482,10 +531,14 @@ describe('action', () => {

await main.run();
expect(runSpy).toHaveReturned();
expect(process.exitCode).not.toBeDefined();

expect(core.setFailed).toHaveBeenLastCalledWith(
'JSON schema missing $schema key'
expect(process.exitCode).toEqual(1);

expect(core.error).toHaveBeenLastCalledWith(
'JSON schema missing $schema key',
{
title: 'JSON Schema Validation Error',
file: '/foo/bar/baz/config.yml'
}
);
});
});
Expand Down
20 changes: 17 additions & 3 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 17 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ export async function run(): Promise<void> {
);

if (typeof schema.$schema !== 'string') {
core.setFailed('JSON schema missing $schema key');
core.error(`Error while validating schema: ${schemaPath}`);
core.error('JSON schema missing $schema key', {
title: 'JSON Schema Validation Error',
file: schemaPath
});
process.exitCode = 1;
return;
}

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

if (validatingSchema && typeof instance.$schema !== 'string') {
core.setFailed('JSON schema missing $schema key');
core.error(`Error while validating schema: ${file}`);
core.error('JSON schema missing $schema key', {
title: 'JSON Schema Validation Error',
file
});
process.exitCode = 1;
return;
}

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

for (const error of errors) {
core.error(JSON.stringify(error, null, 4));
core.error(`Error while validating file: ${file}`);
core.error(JSON.stringify(error, null, 4), {
title: 'JSON Schema Validation Error',
file
});
}
} else {
core.debug(`✓ ${file} is valid`);
Expand Down

0 comments on commit ffeb0ff

Please sign in to comment.