Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: spectral file path resolving #201

Merged
merged 1 commit into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 19 additions & 3 deletions src/cli-validator/runValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
15 changes: 15 additions & 0 deletions test/spectral/mockFiles/oas3/file-resolver/subdir/main.yaml
Original file line number Diff line number Diff line change
@@ -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"
6 changes: 6 additions & 0 deletions test/spectral/mockFiles/oas3/file-resolver/subdir/schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
openapi: 3.0.0
paths: {}
components:
schemas:
SchemaDef:
type: object
15 changes: 15 additions & 0 deletions test/spectral/mockFiles/oas3/url-reference-resolver/main.yaml
Original file line number Diff line number Diff line change
@@ -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'
17 changes: 17 additions & 0 deletions test/spectral/mockFiles/swagger/file-resolver/subdir/main.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
swagger: '2.0'
paths: {}
definitions:
SchemaDef:
type: object
x-components: {}
17 changes: 17 additions & 0 deletions test/spectral/mockFiles/swagger/url-reference-resolver/main.yaml
Original file line number Diff line number Diff line change
@@ -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
122 changes: 122 additions & 0 deletions test/spectral/tests/path-resolve.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});