-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
Adding semantic and model validator to CI #910
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,58 +2,17 @@ | |
// Licensed under the MIT License. See License in the project root for license information. | ||
|
||
'use strict'; | ||
var glob = require('glob'), | ||
path = require('path'), | ||
_ = require('lodash'); | ||
|
||
var globPath, swaggers; | ||
var execSync = require('child_process').execSync; | ||
var isWindows = (process.platform.lastIndexOf('win') === 0); | ||
var prOnly = undefined !== process.env['PR_ONLY'] ? process.env['PR_ONLY'] : 'false'; | ||
globPath = path.join(__dirname, '../', '/**/swagger/*.json'); | ||
swaggers = _(glob.sync(globPath)); | ||
|
||
/** | ||
* Converts command to OS specific command by prepending `mono` for non-windows prOnlySwaggers | ||
* @returns {string} clr command | ||
*/ | ||
function clrCmd(cmd) { | ||
return isWindows ? cmd : ('mono ' + cmd); | ||
}; | ||
|
||
/** | ||
* Retrieves list of swagger files to be processed for linting | ||
* @returns {Array} list of files to be processed for linting | ||
*/ | ||
function getFilesForLinter() { | ||
if (prOnly === 'true') { | ||
// TODO: Currently works for PR into master branch only | ||
var cmd = 'git diff --name-only HEAD $(git merge-base HEAD master)'; | ||
let result; | ||
try { | ||
result = execSync(cmd, { encoding: 'utf8' }); | ||
console.log(result); | ||
var swaggerFileInPR = result.split('\n').filter(function (item) { | ||
return (item.match(/.*\/swagger\/*/ig) !== null); | ||
}); | ||
console.log(`>>>> Number of swaggers found in this PR: ${swaggerFileInPR.length}`); | ||
return swaggerFileInPR; | ||
} catch (err) { | ||
throw err; | ||
} | ||
} else { | ||
// Return all the swagger files for linter processing | ||
return swaggers; | ||
} | ||
} | ||
var _ = require('lodash'), | ||
execSync = require('child_process').execSync, | ||
utils = require('./util/utils'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool. Thanks for refactoring the code into utils 👍 |
||
|
||
describe('AutoRest Linter validation:', function () { | ||
var autoRestLocation = './AutoRest.*/tools/AutoRest.exe'; | ||
let swaggersToProcess = getFilesForLinter(); | ||
let swaggersToProcess = utils.getFilesChangedInPR(); | ||
_(swaggersToProcess).each(function (swagger) { | ||
it(swagger + ' should follow linter rules.', function (done) { | ||
var cmd = clrCmd(autoRestLocation + ' -CodeGenerator None -I ' + swagger); | ||
console.log(cmd); | ||
it(swagger + ' should honor linter validation rules.', function (done) { | ||
var cmd = utils.clrCmd(autoRestLocation + ' -CodeGenerator None -I ' + swagger); | ||
console.log(`Executing: ${cmd}`); | ||
let result; | ||
try { | ||
result = execSync(cmd, { encoding: 'utf8' }); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License in the project root for license information. | ||
|
||
'use strict'; | ||
var _ = require('lodash'), | ||
utils = require('./util/utils'), | ||
oav = require('openapi-validation-tools'); | ||
|
||
describe('Azure swagger model validation using x-ms-examples and examples in spec', function () { | ||
let swaggersToProcess = utils.getFilesChangedInPR(); | ||
_(swaggersToProcess).each(function (swagger) { | ||
it(swagger + ' should have valid examples.', function (done) { | ||
oav.validateExamples(swagger, null, false, 'error').catch(function (err) { | ||
console.log(err); | ||
}); | ||
done(); | ||
}); | ||
}).value(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License in the project root for license information. | ||
|
||
'use strict'; | ||
var _ = require('lodash'), | ||
utils = require('./util/utils'), | ||
oav = require('openapi-validation-tools'); | ||
|
||
describe('Azure swagger semantic validation:', function () { | ||
let swaggersToProcess = utils.getFilesChangedInPR(); | ||
_(swaggersToProcess).each(function (swagger) { | ||
it(swagger + ' should be semantically valid.', function (done) { | ||
oav.validateSpec(swagger, false, 'error').catch(function (err) { | ||
console.log(err); | ||
}); | ||
done(); | ||
}); | ||
}).value(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License in the project root for license information. | ||
|
||
'use strict'; | ||
var assert = require("assert"), | ||
fs = require('fs'), | ||
path = require('path'), | ||
_ = require('lodash'), | ||
async = require('async'), | ||
RefParser = require('json-schema-ref-parser'), | ||
util = require('util'), | ||
utils = require('./util/utils'); | ||
|
||
var context; | ||
|
||
|
||
// Useful when debugging a test for a particular swagger. | ||
// Just update the regex. That will return an array of filtered items. | ||
// swaggers = swaggers.filter(function(item) { | ||
// return (item.match(/.*arm-redis.*/ig) !== null); | ||
// }); | ||
|
||
|
||
describe('Azure swagger schema validation:', function () { | ||
before(function (done) { | ||
utils.initializeValidator(function (err, result) { | ||
if (err) { | ||
done(err); | ||
} | ||
context = result; | ||
done(); | ||
}); | ||
}); | ||
|
||
_(utils.swaggers).each(function (swagger) { | ||
it(swagger + ' should be a valid Swagger document.', function (done) { | ||
utils.parseJsonFromFile(swagger, function (err, parsedData) { | ||
if (err) { done(err); } | ||
if (parsedData.documents && util.isArray(parsedData.documents)) { | ||
console.log(util.format('Skipping the test for \'%s\' document as it seems to be a composite swagger doc.', swagger)); | ||
done(); | ||
} | ||
var valid = context.validator.validate(parsedData, context.extensionSwaggerSchema); | ||
if (!valid) { | ||
var error = context.validator.getLastErrors(); | ||
throw new Error("Schema validation failed: " + util.inspect(error, { depth: null })); | ||
} | ||
assert(valid === true); | ||
done(); | ||
}); | ||
}); | ||
}).value(); | ||
|
||
describe('Azure composite swagger schema validation:', function () { | ||
_(utils.compositeSwaggers).each(function (compositeSwagger) { | ||
it('composite: ' + compositeSwagger + ' should be a valid Composite Swagger document.', function (done) { | ||
utils.parseJsonFromFile(compositeSwagger, function (err, parsedData) { | ||
if (err) { done(err); } | ||
var valid = context.validator.validate(parsedData, context.compositeSchema); | ||
if (!valid) { | ||
var error = context.validator.getLastErrors(); | ||
throw new Error("Schema validation for Composite Swagger failed: " + util.inspect(error, { depth: null })); | ||
} | ||
assert(valid === true); | ||
var compositeSwaggerDir = path.dirname(compositeSwagger); | ||
var messages = []; | ||
if (parsedData.documents && parsedData.documents.length > 0) { | ||
async.eachSeries(parsedData.documents, function (docUrl, loopCallback) { | ||
//construct the absolue path if the item in the documents array is a relative path | ||
if (!path.isAbsolute(docUrl) && !docUrl.startsWith('http')) { | ||
docUrl = path.join(compositeSwaggerDir, docUrl); | ||
} | ||
//make a request if it is a url | ||
if (docUrl.startsWith('http')) { | ||
request({ url: docUrl, json: true }, function (error, response, responseBody) { | ||
if (error) { | ||
messages.push('An error occurred while accessing the swagger doc ' + | ||
docUrl + ' from the documents list. The error is ' + util.inspect(error, { depth: null })); | ||
} | ||
if (response.statusCode !== 200) { | ||
messages.push('\'' + response.statusCode + '\': \'File Not Found\'- error occurred while accessing the swagger doc ' + | ||
docUrl + ' from the documents list.'); | ||
} | ||
loopCallback(); | ||
}); | ||
} else { | ||
//check whether the file exists | ||
if (!fs.existsSync(docUrl)) { | ||
messages.push('\'File Not Found\': error occurred while accessing the swagger doc ' + | ||
docUrl + ' from the documents list on the host filesystem.'); | ||
} | ||
loopCallback(); | ||
} | ||
}, function (err) { | ||
if (err) { | ||
throw err; | ||
} | ||
if (messages.length > 0) { | ||
throw new Error(JSON.stringify(messages)); | ||
} | ||
done(); | ||
}); | ||
} else { | ||
done(); | ||
} | ||
}); | ||
}); | ||
}).value(); | ||
}); | ||
|
||
describe('Azure x-ms-example schema validation:', function () { | ||
_(utils.examples).each(function (example) { | ||
it('x-ms-examples: ' + example + ' should be a valid x-ms-example.', function (done) { | ||
utils.parseJsonFromFile(example, function (err, parsedData) { | ||
if (err) { done(err); } | ||
var valid = context.validator.validate(parsedData, context.exampleSchema); | ||
if (!valid) { | ||
var error = context.validator.getLastErrors(); | ||
throw new Error("Schema validation failed: " + util.inspect(error, { depth: null })); | ||
} | ||
assert(valid === true); | ||
done(); | ||
}); | ||
}); | ||
}).value(); | ||
}); | ||
}); | ||
|
||
describe('External file or url references ("$ref") in a swagger spec:', function () { | ||
_(utils.swaggers).each(function (swagger) { | ||
it(swagger + ' should be completely resolvable.', function (done) { | ||
RefParser.bundle(swagger, function (bundleErr, bundleResult) { | ||
if (bundleErr) { | ||
var msg = swagger + ' has references that cannot be resolved. They are as follows: \n' + util.inspect(bundleErr.message, { depth: null }); | ||
console.log(msg); | ||
throw new Error(msg); | ||
} | ||
done(); | ||
}); | ||
}); | ||
}).value(); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: I am not sure what is the general rule for line ending in JavaScript world?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no rule as such.