From 4ccfa86271d66791eec7821d21e1cf88fdabc13c Mon Sep 17 00:00:00 2001 From: Luis Tejeda <46000487+LuisTejedaS@users.noreply.github.com> Date: Mon, 6 Jun 2022 15:58:09 -0500 Subject: [PATCH] Throw errors instead of returning reason Throw errors instead of returning reason and throw errors if the entry has no root files --- lib/schemaUtils.js | 19 +++---- test/unit/bundle.test.js | 84 ++++++++++++++++++++++++++-- test/unit/detectRelatedFiles.test.js | 12 ++-- 3 files changed, 93 insertions(+), 22 deletions(-) diff --git a/lib/schemaUtils.js b/lib/schemaUtils.js index 7239f54..01cdc37 100644 --- a/lib/schemaUtils.js +++ b/lib/schemaUtils.js @@ -4921,26 +4921,23 @@ module.exports = { try { res.output.data = this.mapProcessRelatedFiles(inputRelatedFiles.rootFiles, inputRelatedFiles.data, inputRelatedFiles.origin, version, inputRelatedFiles.bundleFormat, toBundle); + if (res.output.data === undefined || res.output.data.result === false || + res.output.data.length === 0) { + res.result = false; + } } catch (error) { if (error instanceof ParseError) { - return { - result: false, - reason: error.message - }; - } - else { throw (error); } - } - if (res.output.data.result === false) { - res.result = res.output.data.result; - res.error = res.output.data.error; + let newError = new Error('There was an error during the process'); + newError.stack = error.stack; + throw (newError); } return res; } else { - return res; + throw new Error('Input should have at least one root file'); } }, diff --git a/test/unit/bundle.test.js b/test/unit/bundle.test.js index d7a9f46..295a0d0 100644 --- a/test/unit/bundle.test.js +++ b/test/unit/bundle.test.js @@ -401,12 +401,14 @@ describe('bundle files method - 3.0', function () { } ] }; - const res = await Converter.bundle(input); - expect(res).to.not.be.empty; - expect(res.result).to.be.false; - expect(res.reason).to.equal('Invalid format. Input must be in YAML or JSON ' + - 'format. Specification is not a valid YAML. YAMLException: duplicated mapping' + - ' key at line 30, column -54:\n components:\n ^'); + try { + await Converter.bundle(input); + } + catch (error) { + expect(error.message).to.equal('Invalid format. Input must be in YAML or JSON ' + + 'format. Specification is not a valid YAML. YAMLException: duplicated mapping' + + ' key at line 30, column -54:\n components:\n ^'); + } }); it('Should return bundled file from same_ref_different_source', async function () { @@ -746,6 +748,76 @@ describe('bundle files method - 3.0', function () { expect(res.result).to.be.true; expect(res.output.data.bundledContent).to.be.equal(expected); }); + + it('Should take the root file from data array', async function () { + let contentRootFile = fs.readFileSync(sameRefDiffSource + '/root.yaml', 'utf8'), + user = fs.readFileSync(sameRefDiffSource + '/schemas/user/user.yaml', 'utf8'), + client = fs.readFileSync(sameRefDiffSource + '/schemas/client/client.yaml', 'utf8'), + specialUser = fs.readFileSync(sameRefDiffSource + '/schemas/user/special.yaml', 'utf8'), + specialClient = fs.readFileSync(sameRefDiffSource + '/schemas/client/special.yaml', 'utf8'), + magic = fs.readFileSync(sameRefDiffSource + '/schemas/client/magic.yaml', 'utf8'), + expected = fs.readFileSync(sameRefDiffSource + '/expected.json', 'utf8'), + input = { + type: 'folder', + specificationVersion: '3.0', + data: [ + { + path: '/root.yaml', + content: contentRootFile + }, + { + path: '/schemas/user/user.yaml', + content: user + }, + { + path: '/schemas/user/special.yaml', + content: specialUser + }, + { + path: '/schemas/client/client.yaml', + content: client + }, + { + path: '/schemas/client/special.yaml', + content: specialClient + }, + { + path: '/schemas/client/magic.yaml', + content: magic + } + ], + options: {}, + bundleFormat: 'JSON' + }; + const res = await Converter.bundle(input); + expect(res).to.not.be.empty; + expect(res.result).to.be.true; + expect(res.output.data.bundledContent).to.be.equal(expected); + + }); + + it('Should throw error when input has not root files', async function () { + let user = fs.readFileSync(schemaFromResponse + '/schemas/user.yaml', 'utf8'), + input = { + type: 'folder', + specificationVersion: '3.0', + rootFiles: [], + data: [ + { + path: '/schemas/user.yaml', + content: user + } + ], + options: {}, + bundleFormat: 'JSON' + }; + try { + await Converter.bundle(input); + } + catch (error) { + expect(error.message).to.equal('Input should have at least one root file'); + } + }); }); diff --git a/test/unit/detectRelatedFiles.test.js b/test/unit/detectRelatedFiles.test.js index 5e34a20..d1be8e0 100644 --- a/test/unit/detectRelatedFiles.test.js +++ b/test/unit/detectRelatedFiles.test.js @@ -30,7 +30,7 @@ let expect = require('chai').expect, describe('detectRelatedFiles method', function () { - it('should return empty data when there is no root in the entry', async function () { + it('should return error when there is no root in the entry', async function () { let contentFile = fs.readFileSync(petstoreSeparatedPet, 'utf8'), input = { type: 'folder', @@ -44,10 +44,12 @@ describe('detectRelatedFiles method', function () { } ] }; - const res = await Converter.detectRelatedFiles(input); - expect(res).to.not.be.empty; - expect(res.result).to.be.true; - expect(res.output.data.length).to.equal(0); + try { + await Converter.detectRelatedFiles(input); + } + catch (error) { + expect(error.message).to.equal('Input should have at least one root file'); + } }); it('should locate root and return empty data when there is no ref', async function () {