Skip to content

Commit

Permalink
added model validation and more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
amarzavery committed Feb 6, 2017
1 parent e23cebe commit 20c10be
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 17 deletions.
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ env:
- MODE=linter PR_ONLY=true
- MODE=semantic PR_ONLY=false
- MODE=semantic PR_ONLY=true
- MODE=model PR_ONLY=false
- MODE=model PR_ONLY=true
global:
secure: IwPVExGJQfL6QpTd6Utv3R/qBz7yApgMspzMLSkrq9cMP6t+NHk1vYg0n2evcWaq2rqNZE9xBMfRZi7Yy/RCUT++qALE89xlNvZ/A94PaDUiK3tYYNj1XxsgRauCHshXz6smmLwQMpTKsEl6lyjXeXpwQMDNRehhTu0Hg2tE7ZikfQVb+jjRNcP41RfzzGQyxEhd3fxF2G1G21WXH0cXtZssOd1MYcXhmXk8dR3DyEEMbPzDV1Bk3auXOvbqS79bnZ/Pi4p/7P/aTOm8O2ACKM0XAvww95vAQ0LcPzKnNe3sNjFVAssBiZbIk0Zs30wULBphlTxVoufHKSZuTf+QEBTKpH99v/+SBDDPu9+0q2esq7TKgf8bvzkeXjh54fECdJUqEo9E2gW08+RQxWCqryMJouOPcY2OMs4lZwSMOXQY68a/CYVRWFaFg5s6jntC8sLHtDxV0qem3xyjc+852v8rUfkyvMhOBoZJjWmnqYVqdamHOOfrzjc7AzPUEzSeiN6OPPVli+SzwLHdip0GxdK46pAISCOcbdyYn11VvTIn1QosE66eWhF6SViVH6lNWgSfVTpcQ2zq/qSKh0/zpwn82Ys+wKDOf3EwQAanndgk26npi7Ik4nIuexZ/TE56rQ7qjZqmHoxFz7QMeTZDiVmLFxtR19cTT3GLxDz8nBQ=
matrix:
Expand All @@ -22,6 +24,8 @@ matrix:
- env: MODE=linter PR_ONLY=true
- env: MODE=semantic PR_ONLY=false
- env: MODE=semantic PR_ONLY=true
- env: MODE=model PR_ONLY=false
- env: MODE=model PR_ONLY=true
before_install:
- docker pull lmazuel/swagger-to-sdk
- python -c "import os; print('\n'.join(v for v in os.environ.keys() if v.startswith('TRAVIS')))" > /tmp/env_file
Expand All @@ -42,6 +46,7 @@ script:
- if [[ $MODE == 'python' ]]; then $DOCKER_CMD AutorestCI/azure-sdk-for-python --pr-repo-id Azure/azure-sdk-for-python -o master -v; fi
- if [[ $MODE == 'node' ]]; then $DOCKER_CMD AutorestCI/azure-sdk-for-node --pr-repo-id Azure/azure-sdk-for-node -o master -v; fi
- if [[ $MODE == 'ruby' ]]; then $DOCKER_CMD AutorestCI/azure-sdk-for-ruby --pr-repo-id Azure/azure-sdk-for-ruby -o master -v; fi
- if [[ $MODE == 'syntax' ]]; then npm test -- test/test.js; fi
- if [[ $MODE == 'syntax' ]]; then npm test -- test/syntax.js; fi
- if [[ $MODE == 'linter' ]]; then npm test -- test/linter.js; fi
- if [[ $MODE == 'semantic' ]]; then npm test -- test/semantic.js; fi
- if [[ $MODE == 'model' ]]; then npm test -- test/model.js; fi
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016 Microsoft
Copyright (c) 2017 Microsoft

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
19 changes: 19 additions & 0 deletions test/model.js
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();
});
4 changes: 2 additions & 2 deletions test/semantic.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var _ = require('lodash'),
utils = require('./util/utils'),
oav = require('openapi-validation-tools');

describe('Azure swagger semantic validation:', function () {
describe('Azure swagger semantic validation:', function () {
let swaggersToProcess = utils.getFilesChangedInPR();
_(swaggersToProcess).each(function (swagger) {
it(swagger + ' should be semantically valid.', function (done) {
Expand All @@ -16,4 +16,4 @@ describe('Azure swagger semantic validation:', function () {
done();
});
}).value();
});
});
File renamed without changes.
45 changes: 32 additions & 13 deletions test/util/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,37 @@ exports.clrCmd = function clrCmd(cmd) {
};

/**
* Gets the name of the current branch to which the PR is sent.
* @returns {string} branchName The current branch name.
* Gets the name of the target branch to which the PR is sent. We are using the environment
* variable provided by travis-ci. It is called TRAVIS_BRANCH. More info can be found here:
* https://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables
* If the environment variable is undefined then the method returns 'master' as the default value.
* @returns {string} branchName The target branch name.
*/
exports.getCurrentBranch = function getCurrentBranch() {
exports.getTargetBranch = function getTargetBranch() {
console.log(`@@@@@ process.env['TRAVIS_BRANCH'] - ${process.env['TRAVIS_BRANCH']}`);
let result = process.env['TRAVIS_BRANCH'] || 'master';
result = result.trim();
console.log(`>>>>> The target branch is: "${result}".`);
return result;
};

/**
* Gets the name of the source branch from which the PR is sent.
* @returns {string} branchName The source branch name.
*/
exports.getSourceBranch = function getSourceBranch() {
let cmd = 'git rev-parse --abbrev-ref HEAD';
let result = 'master';
try {
result = execSync(cmd, { encoding: 'utf8' });
} catch (err) {
console.log(`An error occurred while getting the current branch ${util.inspect(err, { depth: null })}.`);
let result = process.env['TRAVIS_PULL_REQUEST_BRANCH'];
console.log(`@@@@@ process.env['TRAVIS_PULL_REQUEST_BRANCH'] - ${process.env['TRAVIS_PULL_REQUEST_BRANCH']}`);
if (!result) {
try {
result = execSync(cmd, { encoding: 'utf8' });
} catch (err) {
console.log(`An error occurred while getting the current branch ${util.inspect(err, { depth: null })}.`);
}
}
console.log(`>>>>> The current branch is: "${result}".`);
result = result.trim();
console.log(`>>>>> The source branch is: "${result}".`);
return result;
};

Expand All @@ -91,12 +110,12 @@ exports.getCurrentBranch = function getCurrentBranch() {
exports.getFilesChangedInPR = function getFilesChangedInPR() {
let result = exports.swaggers;
if (exports.prOnly === 'true') {
let currentBranch, cmd, filesChanged, swaggerFilesInPR;
let targetBranch, cmd, filesChanged, swaggerFilesInPR;
try {
currentBranch = exports.getCurrentBranch();
cmd = `git diff --name-only HEAD $(git merge-base HEAD ${currentBranch})`;
targetBranch = exports.getTargetBranch();
cmd = `git diff --name-only HEAD $(git merge-base HEAD ${targetBranch})`;
filesChanged = execSync(cmd, { encoding: 'utf8' });
console.log('>>>>> Files changed in this PR are as follows:');
console.log('>>>>> Files changed in this PR are as follows:')
console.log(filesChanged);
swaggerFilesInPR = filesChanged.split('\n').filter(function (item) {
return (item.match(/.*\/swagger\/*/ig) !== null);
Expand Down

0 comments on commit 20c10be

Please sign in to comment.