Added a version mismatch warning. (#37)

* I need to stop rewriting builtin methods with reduce.

* Removed redundant fields from the config class.

* Added a warning when module version and beau.yml version differ.
This commit is contained in:
Sergio Díaz 2018-06-14 23:12:16 -06:00 committed by GitHub
parent 450d53e9f9
commit 14704c09d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 128 additions and 239 deletions

View File

@ -45,25 +45,6 @@ Config {
"HOSTS",
"COOKIEJAR",
],
"defaultConfigValues": Object {
"COOKIEJAR": false,
"DEFAULTS": Object {},
"ENDPOINT": "",
"ENVIRONMENT": Object {},
"HOSTS": Array [],
"PLUGINS": Array [],
"VERSION": 1,
},
"doc": Object {
"GET /anything": Object {
"alias": "anything",
"payload": Object {
"name": "$env.params.name",
},
},
"endpoint": "https://example.org/",
"version": 1,
},
}
`;

View File

@ -1,5 +1,7 @@
const ListCommand = require('../commands/list');
jest.mock('../../../src/shared');
jest.mock('../utils');
describe('List Command', () => {
@ -7,9 +9,7 @@ describe('List Command', () => {
beforeEach(() => {
result = [];
jest
.spyOn(process.stdout, 'write')
.mockImplementation(val =>
jest.spyOn(process.stdout, 'write').mockImplementation(val =>
result.push(require('strip-ansi')(val.toString('utf8')))
);
});

View File

@ -1,6 +1,8 @@
const RequestCommand = require('../commands/request');
const requestPromiseNativeMock = require('request-promise-native');
jest.mock('../../../src/shared');
jest.mock('../utils');
describe('Request Command', () => {
@ -9,9 +11,7 @@ describe('Request Command', () => {
beforeEach(() => {
requestPromiseNativeMock.fail = false;
result = [];
jest
.spyOn(process.stdout, 'write')
.mockImplementation(val =>
jest.spyOn(process.stdout, 'write').mockImplementation(val =>
result.push(require('strip-ansi')(val.toString('utf8')))
);
});

View File

@ -1,5 +1,7 @@
const utils = require('../utils.js');
jest.mock('../../../src/shared');
jest.mock('fs');
describe('utils', () => {

View File

@ -7,9 +7,7 @@ describe('Validate Command', () => {
beforeEach(() => {
result = [];
jest
.spyOn(process.stdout, 'write')
.mockImplementation(val =>
jest.spyOn(process.stdout, 'write').mockImplementation(val =>
result.push(require('strip-ansi')(val.toString('utf8')))
);
});

4
src/__mocks__/shared.js Normal file
View File

@ -0,0 +1,4 @@
module.exports = {
...require.requireActual('../shared'),
moduleVersion: jest.fn().mockReturnValue(1)
};

View File

@ -51,31 +51,6 @@ Beau {
"HOSTS",
"COOKIEJAR",
],
"defaultConfigValues": Object {
"COOKIEJAR": false,
"DEFAULTS": Object {},
"ENDPOINT": "",
"ENVIRONMENT": Object {},
"HOSTS": Array [],
"PLUGINS": Array [],
"VERSION": 1,
},
"doc": Object {
"GET /posts/1": "get-post",
"GET /user": Object {
"alias": "user",
"headers": Object {
"hello": "world",
},
},
"defaults": Object {
"headers": Object {
"authentication": "hello",
},
},
"endpoint": "http://jsonplaceholder.typicode.com",
"version": 1,
},
},
"requests": RequestList {
"REQUESTS": Array [
@ -153,31 +128,6 @@ Beau {
"HOSTS",
"COOKIEJAR",
],
"defaultConfigValues": Object {
"COOKIEJAR": false,
"DEFAULTS": Object {},
"ENDPOINT": "",
"ENVIRONMENT": Object {},
"HOSTS": Array [],
"PLUGINS": Array [],
"VERSION": 1,
},
"doc": Object {
"GET /posts/1": "get-post",
"GET /user": Object {
"alias": "user",
"headers": Object {
"hello": "world",
},
},
"defaults": Object {
"headers": Object {
"authentication": "hello",
},
},
"endpoint": "http://jsonplaceholder.typicode.com",
"version": 1,
},
},
"list": Array [
Request {

View File

@ -119,55 +119,6 @@ Config {
"HOSTS",
"COOKIEJAR",
],
"defaultConfigValues": Object {
"COOKIEJAR": false,
"DEFAULTS": Object {},
"ENDPOINT": "",
"ENVIRONMENT": Object {},
"HOSTS": Array [],
"PLUGINS": Array [],
"VERSION": 1,
},
"doc": Object {
"GET /e1": "e1",
"defaults": Object {
"HEADERS": Object {
"hello": "mars",
},
},
"endpoint": "http://example.org",
"hosts": Array [
Object {
"GET /e2": "e2",
"GET /posts": "posts",
"defaults": Object {
"HEADERS": Object {
"hello": "world",
"world": "hello",
},
},
"endpoint": "http://example.com",
"host": "com",
},
Object {
"GET /e3": "e3",
"GET /posts": "posts",
"defaults": Object {
"HEADERS": Object {
"hello": "world",
"world": "bye",
},
},
"endpoint": "http://example.net",
"host": "net",
},
Object {
"GET /posts": "posts",
"endpoint": "http://example.info",
"host": "info",
},
],
},
}
`;
@ -221,30 +172,5 @@ Config {
"HOSTS",
"COOKIEJAR",
],
"defaultConfigValues": Object {
"COOKIEJAR": false,
"DEFAULTS": Object {},
"ENDPOINT": "",
"ENVIRONMENT": Object {},
"HOSTS": Array [],
"PLUGINS": Array [],
"VERSION": 1,
},
"doc": Object {
"GET /posts/1": "get-post",
"GET /user": Object {
"alias": "user",
"headers": Object {
"hello": "world",
},
},
"defaults": Object {
"HEADERS": Object {
"authentication": "hello",
},
},
"endpoint": "http://jsonplaceholder.typicode.com",
"version": 1,
},
}
`;

View File

@ -7,7 +7,7 @@ Object {
"body": Object {
"username": "seich",
},
"endpoint": "http://martianwabbit.com/user",
"endpoint": "http://example.com/user",
"headers": Object {
"authentication": "BEARER abc123",
},
@ -25,7 +25,7 @@ Object {
"body": "{\\"hello\\": \\"world\\"}",
"request": Object {
"body": undefined,
"endpoint": "http://martianwabbit.com/user",
"endpoint": "http://example.com/user",
"headers": undefined,
},
"response": Object {

View File

@ -1,11 +1,16 @@
const yaml = require('js-yaml');
const Beau = require('../beau');
const { moduleVersion } = require('../shared');
jest.mock('../shared');
const requireg = require('requireg');
requireg.resolving = false;
describe(`Beau's config Loader.`, () => {
it('should create a request list', () => {
moduleVersion.mockReturnValue(1);
const doc = yaml.safeLoad(`
version: 1
endpoint: 'http://jsonplaceholder.typicode.com'
@ -23,7 +28,21 @@ describe(`Beau's config Loader.`, () => {
const beau = new Beau(doc);
expect(beau.requests).toBeDefined();
expect(beau).toMatchSnapshot();
});
it('should display a warning if the module version and the beau file version are different', () => {
let stdout;
let spy = jest
.spyOn(console, 'warn')
.mockImplementation(val => (stdout = val));
moduleVersion.mockReturnValue(2);
const beau = new Beau({ version: 1 });
expect(stdout).toEqual('This Beau file expects v1. You are using v2.');
spy.mockReset();
spy.mockRestore();
});
});

View File

@ -61,6 +61,7 @@ describe('Config', () => {
it('should load multiple hosts', () => {
const doc = yaml.safeLoad(`
version: 1
endpoint: http://example.org
defaults:

View File

@ -12,7 +12,7 @@ describe('Request', () => {
beforeEach(() => {
validRequestConfig = {
request: 'POST /user',
endpoint: 'http://martianwabbit.com',
endpoint: 'http://example.com',
alias: 'update',
params: {
userId: '$profile.UserId'
@ -27,7 +27,7 @@ describe('Request', () => {
invalidRequestConfig = {
request: `POST /session`,
endpoint: 'http://martianwabbit.com'
endpoint: 'http://example.com'
};
cache = new RequestCache();
@ -36,7 +36,7 @@ describe('Request', () => {
request = new Request(validRequestConfig);
requestWithoutDependencies = new Request({
endpoint: 'http://martianwabbit.com',
endpoint: 'http://example.com',
request: 'GET /user',
alias: 'show'
});

View File

@ -1,10 +1,19 @@
const RequestList = require('./requestList');
const Config = require('./config');
const { moduleVersion } = require('./shared');
class Beau {
constructor(doc, env = {}) {
this.config = new Config(doc, env);
this.requests = new RequestList(this.config);
if (this.config.VERSION !== moduleVersion()) {
console.warn(
`This Beau file expects v${
this.config.VERSION
}. You are using v${moduleVersion()}.`
);
}
}
}

View File

@ -1,11 +1,11 @@
const deepMerge = require('deepmerge');
const { requestRegex, UpperCaseKeys } = require('./shared');
const { requestRegex, UpperCaseKeys, moduleVersion } = require('./shared');
const Plugins = require('./plugins');
class Config {
constructor(doc, env = {}) {
this.defaultConfigValues = {
VERSION: 1,
const defaultConfigValues = {
VERSION: moduleVersion(),
ENDPOINT: '',
PLUGINS: [],
DEFAULTS: {},
@ -14,11 +14,10 @@ class Config {
COOKIEJAR: false
};
this.configKeys = Object.keys(this.defaultConfigValues);
this.doc = doc;
this.configKeys = Object.keys(defaultConfigValues);
let config = this.loadConfig(doc);
Object.assign(this, this.defaultConfigValues, config);
Object.assign(this, defaultConfigValues, config);
this.ENVIRONMENT = deepMerge(this.ENVIRONMENT, env);
@ -29,21 +28,18 @@ class Config {
ENDPOINT: this.ENDPOINT
});
this.loadHosts(this.HOSTS, config);
this.loadHosts(this.HOSTS, config, defaultConfigValues);
this.PLUGINS = new Plugins(this.PLUGINS);
}
loadHosts(hosts, rootConfig) {
loadHosts(hosts, rootConfig, defaultConfigValues) {
hosts.forEach(host => {
if (typeof host.host === 'undefined') {
throw new Error(`Host doesn't indicate it's host name.`);
}
let config = deepMerge(
this.defaultConfigValues,
this.loadConfig(host)
);
let config = deepMerge(defaultConfigValues, this.loadConfig(host));
config.DEFAULTS = deepMerge(rootConfig.DEFAULTS, config.DEFAULTS);

View File

@ -67,6 +67,8 @@ const replaceInObject = function(obj, fn) {
return obj;
};
const moduleVersion = () => parseInt(require('../package.json').version, 10);
module.exports = {
requestRegex,
replacementRegex,
@ -74,5 +76,6 @@ module.exports = {
UpperCaseKeys,
removeOptionalKeys,
toKebabCase,
replaceInObject
replaceInObject,
moduleVersion
};