diff --git a/src/cli-validator/runValidator.js b/src/cli-validator/runValidator.js index c19cba720..e09e62d54 100644 --- a/src/cli-validator/runValidator.js +++ b/src/cli-validator/runValidator.js @@ -242,11 +242,27 @@ const processInput = async function(program) { process.chdir(originalWorkingDirectory); } - // run validator & spectral, print the results, and determine if validator passed - let results; + // run spectral and save the results + let spectralResults; try { + process.chdir(path.dirname(validFile)); // let spectral handle the parsing of the original swagger/oa3 document - const spectralResults = await spectral.run(originalFile); + spectralResults = await spectral.run(originalFile); + } catch (err) { + printError(chalk, 'There was a problem with spectral.', getError(err)); + if (debug) { + console.log(err.stack); + } + exitCode = 1; + continue; + } finally { + // return the working directory to its original location + process.chdir(originalWorkingDirectory); + } + + // run validator, print the results, and determine if validator passed + let results; + try { results = validator(swagger, configObject, spectralResults, debug); } catch (err) { printError(chalk, 'There was a problem with a validator.', getError(err)); diff --git a/test/spectral/mockFiles/oas3/file-resolver/subdir/main.yaml b/test/spectral/mockFiles/oas3/file-resolver/subdir/main.yaml new file mode 100644 index 000000000..b2074027f --- /dev/null +++ b/test/spectral/mockFiles/oas3/file-resolver/subdir/main.yaml @@ -0,0 +1,15 @@ +openapi: 3.0.0 +info: + version: 1.0.0 + title: main +paths: + /example: + get: + summary: Summary + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "./schema.yaml#/components/schemas/SchemaDef" \ No newline at end of file diff --git a/test/spectral/mockFiles/oas3/file-resolver/subdir/schema.yaml b/test/spectral/mockFiles/oas3/file-resolver/subdir/schema.yaml new file mode 100644 index 000000000..7f8cdcd82 --- /dev/null +++ b/test/spectral/mockFiles/oas3/file-resolver/subdir/schema.yaml @@ -0,0 +1,6 @@ +openapi: 3.0.0 +paths: {} +components: + schemas: + SchemaDef: + type: object \ No newline at end of file diff --git a/test/spectral/mockFiles/oas3/url-reference-resolver/main.yaml b/test/spectral/mockFiles/oas3/url-reference-resolver/main.yaml new file mode 100644 index 000000000..0558300a7 --- /dev/null +++ b/test/spectral/mockFiles/oas3/url-reference-resolver/main.yaml @@ -0,0 +1,15 @@ +openapi: 3.0.0 +info: + version: 1.0.0 + title: main +paths: + /example: + get: + summary: Summary + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: 'http://json-schema.org/draft-04/schema#/properties/title' \ No newline at end of file diff --git a/test/spectral/mockFiles/swagger/file-resolver/subdir/main.yaml b/test/spectral/mockFiles/swagger/file-resolver/subdir/main.yaml new file mode 100644 index 000000000..7179f3fe4 --- /dev/null +++ b/test/spectral/mockFiles/swagger/file-resolver/subdir/main.yaml @@ -0,0 +1,17 @@ +--- +swagger: '2.0' +info: + title: main + version: 1.0.0 +paths: + "/example": + get: + produces: + - application/json + parameters: [] + responses: + '200': + description: OK + schema: + $ref: "./schema.yaml#/definitions/SchemaDef" + summary: Summary diff --git a/test/spectral/mockFiles/swagger/file-resolver/subdir/schema.yaml b/test/spectral/mockFiles/swagger/file-resolver/subdir/schema.yaml new file mode 100644 index 000000000..598509643 --- /dev/null +++ b/test/spectral/mockFiles/swagger/file-resolver/subdir/schema.yaml @@ -0,0 +1,7 @@ +--- +swagger: '2.0' +paths: {} +definitions: + SchemaDef: + type: object +x-components: {} diff --git a/test/spectral/mockFiles/swagger/url-reference-resolver/main.yaml b/test/spectral/mockFiles/swagger/url-reference-resolver/main.yaml new file mode 100644 index 000000000..f9021d2c8 --- /dev/null +++ b/test/spectral/mockFiles/swagger/url-reference-resolver/main.yaml @@ -0,0 +1,17 @@ +--- +swagger: '2.0' +info: + title: main + version: 1.0.0 +paths: + "/example": + get: + produces: + - application/json + parameters: [] + responses: + '200': + description: OK + schema: + $ref: 'http://json-schema.org/draft-04/schema#/properties/title' + summary: Summary diff --git a/test/spectral/tests/path-resolve.test.js b/test/spectral/tests/path-resolve.test.js new file mode 100644 index 000000000..bd23a444b --- /dev/null +++ b/test/spectral/tests/path-resolve.test.js @@ -0,0 +1,122 @@ +const commandLineValidator = require('../../../src/cli-validator/runValidator'); +const { getCapturedText } = require('../../test-utils'); + +describe('spectral - test file resolve - OAS3', function() { + let consoleSpy; + + beforeEach(() => { + consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); + }); + + afterEach(() => { + consoleSpy.mockRestore(); + }); + + it('test no file read error using mockFiles/oas3/file-resolver/subdir/main.yaml', async function() { + // set up mock user input + const program = {}; + program.default_mode = true; + program.print_validator_modules = true; + program.args = [ + './test/spectral/mockFiles/oas3/file-resolver/subdir/main.yaml' + ]; + + const exitCode = await commandLineValidator(program); + const capturedText = getCapturedText(consoleSpy.mock.calls); + const allOutput = capturedText.join(''); + + // No errors should be triggered + expect(exitCode).toEqual(0); + expect(allOutput).not.toContain('ENOENT: no such file or directory'); + }); +}); + +describe('spectral - test file resolve - Swagger', function() { + let consoleSpy; + + beforeEach(() => { + consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); + }); + + afterEach(() => { + consoleSpy.mockRestore(); + }); + + it('test no file read error using mockFiles/swagger/file-resolver/subdir/main.yaml', async function() { + // set up mock user input + const program = {}; + program.default_mode = true; + program.print_validator_modules = true; + program.args = [ + './test/spectral/mockFiles/swagger/file-resolver/subdir/main.yaml' + ]; + + const exitCode = await commandLineValidator(program); + const capturedText = getCapturedText(consoleSpy.mock.calls); + const allOutput = capturedText.join(''); + + // No errors should be triggered + expect(exitCode).toEqual(0); + expect(allOutput).not.toContain('ENOENT: no such file or directory'); + }); +}); + +describe('spectral - test url resolve - OAS3', function() { + let consoleSpy; + + beforeEach(() => { + consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); + }); + + afterEach(() => { + consoleSpy.mockRestore(); + }); + + it('test no file read error using mockFiles/oas3/url-reference-resolver/main.yaml', async function() { + // set up mock user input + const program = {}; + program.default_mode = true; + program.print_validator_modules = true; + program.args = [ + './test/spectral/mockFiles/oas3/url-reference-resolver/main.yaml' + ]; + + const exitCode = await commandLineValidator(program); + const capturedText = getCapturedText(consoleSpy.mock.calls); + const allOutput = capturedText.join(''); + + // No errors should be triggered + expect(exitCode).toEqual(0); + expect(allOutput).not.toContain('Error resolving $ref pointer'); + }); +}); + +describe('spectral - test url resolve - Swagger', function() { + let consoleSpy; + + beforeEach(() => { + consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); + }); + + afterEach(() => { + consoleSpy.mockRestore(); + }); + + it('test no file read error using mockFiles/swagger/url-reference-resolver/main.yaml', async function() { + // set up mock user input + const program = {}; + program.default_mode = true; + program.print_validator_modules = true; + program.args = [ + './test/spectral/mockFiles/swagger/url-reference-resolver/main.yaml' + ]; + + const exitCode = await commandLineValidator(program); + const capturedText = getCapturedText(consoleSpy.mock.calls); + const allOutput = capturedText.join(''); + + // No errors should be triggered + expect(exitCode).toEqual(0); + expect(allOutput).not.toContain('Error resolving $ref pointer'); + }); +});