Skip to content
This repository has been archived by the owner on Oct 5, 2023. It is now read-only.

fix: handle empty files #246

Merged
merged 4 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
8 changes: 8 additions & 0 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ describe('#validate', () => {
await expect(o.validate()).rejects.toMatchSnapshot();
});

it('should error out for empty file', async () => {
const o = new OASNormalize(require.resolve('./__fixtures__/invalid/empty.json'), {
enablePaths: true,
});

await expect(o.validate()).rejects.toStrictEqual(new Error('No file contents found.'));
});

// Skipping because the `chalk` dependency of `better-ajv-errors` within `openapi-parser` has
// issues in CI. Test works fine locally though!
it.skip('should colorize errors when `opts.colorizeErrors` is present', async () => {
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ export default class OASNormalize {
}

const contents = fs.readFileSync(this.file).toString();
if (!contents) {
return Promise.reject(new Error('No file contents found.'));
}
return resolve(contents);

default:
Expand Down