mirror of https://github.com/Seich/Beau.git
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:
parent
450d53e9f9
commit
14704c09d1
|
|
@ -45,25 +45,6 @@ Config {
|
||||||
"HOSTS",
|
"HOSTS",
|
||||||
"COOKIEJAR",
|
"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,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
const ListCommand = require('../commands/list');
|
const ListCommand = require('../commands/list');
|
||||||
|
|
||||||
|
jest.mock('../../../src/shared');
|
||||||
|
|
||||||
jest.mock('../utils');
|
jest.mock('../utils');
|
||||||
|
|
||||||
describe('List Command', () => {
|
describe('List Command', () => {
|
||||||
|
|
@ -7,9 +9,7 @@ describe('List Command', () => {
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
result = [];
|
result = [];
|
||||||
jest
|
jest.spyOn(process.stdout, 'write').mockImplementation(val =>
|
||||||
.spyOn(process.stdout, 'write')
|
|
||||||
.mockImplementation(val =>
|
|
||||||
result.push(require('strip-ansi')(val.toString('utf8')))
|
result.push(require('strip-ansi')(val.toString('utf8')))
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
const RequestCommand = require('../commands/request');
|
const RequestCommand = require('../commands/request');
|
||||||
const requestPromiseNativeMock = require('request-promise-native');
|
const requestPromiseNativeMock = require('request-promise-native');
|
||||||
|
|
||||||
|
jest.mock('../../../src/shared');
|
||||||
|
|
||||||
jest.mock('../utils');
|
jest.mock('../utils');
|
||||||
|
|
||||||
describe('Request Command', () => {
|
describe('Request Command', () => {
|
||||||
|
|
@ -9,9 +11,7 @@ describe('Request Command', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
requestPromiseNativeMock.fail = false;
|
requestPromiseNativeMock.fail = false;
|
||||||
result = [];
|
result = [];
|
||||||
jest
|
jest.spyOn(process.stdout, 'write').mockImplementation(val =>
|
||||||
.spyOn(process.stdout, 'write')
|
|
||||||
.mockImplementation(val =>
|
|
||||||
result.push(require('strip-ansi')(val.toString('utf8')))
|
result.push(require('strip-ansi')(val.toString('utf8')))
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
const utils = require('../utils.js');
|
const utils = require('../utils.js');
|
||||||
|
|
||||||
|
jest.mock('../../../src/shared');
|
||||||
|
|
||||||
jest.mock('fs');
|
jest.mock('fs');
|
||||||
|
|
||||||
describe('utils', () => {
|
describe('utils', () => {
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,7 @@ describe('Validate Command', () => {
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
result = [];
|
result = [];
|
||||||
jest
|
jest.spyOn(process.stdout, 'write').mockImplementation(val =>
|
||||||
.spyOn(process.stdout, 'write')
|
|
||||||
.mockImplementation(val =>
|
|
||||||
result.push(require('strip-ansi')(val.toString('utf8')))
|
result.push(require('strip-ansi')(val.toString('utf8')))
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
module.exports = {
|
||||||
|
...require.requireActual('../shared'),
|
||||||
|
moduleVersion: jest.fn().mockReturnValue(1)
|
||||||
|
};
|
||||||
|
|
@ -51,31 +51,6 @@ Beau {
|
||||||
"HOSTS",
|
"HOSTS",
|
||||||
"COOKIEJAR",
|
"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": RequestList {
|
||||||
"REQUESTS": Array [
|
"REQUESTS": Array [
|
||||||
|
|
@ -153,31 +128,6 @@ Beau {
|
||||||
"HOSTS",
|
"HOSTS",
|
||||||
"COOKIEJAR",
|
"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 [
|
"list": Array [
|
||||||
Request {
|
Request {
|
||||||
|
|
|
||||||
|
|
@ -119,55 +119,6 @@ Config {
|
||||||
"HOSTS",
|
"HOSTS",
|
||||||
"COOKIEJAR",
|
"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",
|
"HOSTS",
|
||||||
"COOKIEJAR",
|
"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,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ Object {
|
||||||
"body": Object {
|
"body": Object {
|
||||||
"username": "seich",
|
"username": "seich",
|
||||||
},
|
},
|
||||||
"endpoint": "http://martianwabbit.com/user",
|
"endpoint": "http://example.com/user",
|
||||||
"headers": Object {
|
"headers": Object {
|
||||||
"authentication": "BEARER abc123",
|
"authentication": "BEARER abc123",
|
||||||
},
|
},
|
||||||
|
|
@ -25,7 +25,7 @@ Object {
|
||||||
"body": "{\\"hello\\": \\"world\\"}",
|
"body": "{\\"hello\\": \\"world\\"}",
|
||||||
"request": Object {
|
"request": Object {
|
||||||
"body": undefined,
|
"body": undefined,
|
||||||
"endpoint": "http://martianwabbit.com/user",
|
"endpoint": "http://example.com/user",
|
||||||
"headers": undefined,
|
"headers": undefined,
|
||||||
},
|
},
|
||||||
"response": Object {
|
"response": Object {
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,16 @@
|
||||||
const yaml = require('js-yaml');
|
const yaml = require('js-yaml');
|
||||||
const Beau = require('../beau');
|
const Beau = require('../beau');
|
||||||
|
const { moduleVersion } = require('../shared');
|
||||||
|
|
||||||
|
jest.mock('../shared');
|
||||||
|
|
||||||
const requireg = require('requireg');
|
const requireg = require('requireg');
|
||||||
requireg.resolving = false;
|
requireg.resolving = false;
|
||||||
|
|
||||||
describe(`Beau's config Loader.`, () => {
|
describe(`Beau's config Loader.`, () => {
|
||||||
it('should create a request list', () => {
|
it('should create a request list', () => {
|
||||||
|
moduleVersion.mockReturnValue(1);
|
||||||
|
|
||||||
const doc = yaml.safeLoad(`
|
const doc = yaml.safeLoad(`
|
||||||
version: 1
|
version: 1
|
||||||
endpoint: 'http://jsonplaceholder.typicode.com'
|
endpoint: 'http://jsonplaceholder.typicode.com'
|
||||||
|
|
@ -23,7 +28,21 @@ describe(`Beau's config Loader.`, () => {
|
||||||
|
|
||||||
const beau = new Beau(doc);
|
const beau = new Beau(doc);
|
||||||
|
|
||||||
expect(beau.requests).toBeDefined();
|
|
||||||
expect(beau).toMatchSnapshot();
|
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();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -61,6 +61,7 @@ describe('Config', () => {
|
||||||
|
|
||||||
it('should load multiple hosts', () => {
|
it('should load multiple hosts', () => {
|
||||||
const doc = yaml.safeLoad(`
|
const doc = yaml.safeLoad(`
|
||||||
|
version: 1
|
||||||
endpoint: http://example.org
|
endpoint: http://example.org
|
||||||
|
|
||||||
defaults:
|
defaults:
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ describe('Request', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
validRequestConfig = {
|
validRequestConfig = {
|
||||||
request: 'POST /user',
|
request: 'POST /user',
|
||||||
endpoint: 'http://martianwabbit.com',
|
endpoint: 'http://example.com',
|
||||||
alias: 'update',
|
alias: 'update',
|
||||||
params: {
|
params: {
|
||||||
userId: '$profile.UserId'
|
userId: '$profile.UserId'
|
||||||
|
|
@ -27,7 +27,7 @@ describe('Request', () => {
|
||||||
|
|
||||||
invalidRequestConfig = {
|
invalidRequestConfig = {
|
||||||
request: `POST /session`,
|
request: `POST /session`,
|
||||||
endpoint: 'http://martianwabbit.com'
|
endpoint: 'http://example.com'
|
||||||
};
|
};
|
||||||
|
|
||||||
cache = new RequestCache();
|
cache = new RequestCache();
|
||||||
|
|
@ -36,7 +36,7 @@ describe('Request', () => {
|
||||||
|
|
||||||
request = new Request(validRequestConfig);
|
request = new Request(validRequestConfig);
|
||||||
requestWithoutDependencies = new Request({
|
requestWithoutDependencies = new Request({
|
||||||
endpoint: 'http://martianwabbit.com',
|
endpoint: 'http://example.com',
|
||||||
request: 'GET /user',
|
request: 'GET /user',
|
||||||
alias: 'show'
|
alias: 'show'
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,19 @@
|
||||||
const RequestList = require('./requestList');
|
const RequestList = require('./requestList');
|
||||||
const Config = require('./config');
|
const Config = require('./config');
|
||||||
|
const { moduleVersion } = require('./shared');
|
||||||
|
|
||||||
class Beau {
|
class Beau {
|
||||||
constructor(doc, env = {}) {
|
constructor(doc, env = {}) {
|
||||||
this.config = new Config(doc, env);
|
this.config = new Config(doc, env);
|
||||||
this.requests = new RequestList(this.config);
|
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()}.`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
const deepMerge = require('deepmerge');
|
const deepMerge = require('deepmerge');
|
||||||
const { requestRegex, UpperCaseKeys } = require('./shared');
|
const { requestRegex, UpperCaseKeys, moduleVersion } = require('./shared');
|
||||||
const Plugins = require('./plugins');
|
const Plugins = require('./plugins');
|
||||||
|
|
||||||
class Config {
|
class Config {
|
||||||
constructor(doc, env = {}) {
|
constructor(doc, env = {}) {
|
||||||
this.defaultConfigValues = {
|
const defaultConfigValues = {
|
||||||
VERSION: 1,
|
VERSION: moduleVersion(),
|
||||||
ENDPOINT: '',
|
ENDPOINT: '',
|
||||||
PLUGINS: [],
|
PLUGINS: [],
|
||||||
DEFAULTS: {},
|
DEFAULTS: {},
|
||||||
|
|
@ -14,11 +14,10 @@ class Config {
|
||||||
COOKIEJAR: false
|
COOKIEJAR: false
|
||||||
};
|
};
|
||||||
|
|
||||||
this.configKeys = Object.keys(this.defaultConfigValues);
|
this.configKeys = Object.keys(defaultConfigValues);
|
||||||
this.doc = doc;
|
|
||||||
|
|
||||||
let config = this.loadConfig(doc);
|
let config = this.loadConfig(doc);
|
||||||
Object.assign(this, this.defaultConfigValues, config);
|
Object.assign(this, defaultConfigValues, config);
|
||||||
|
|
||||||
this.ENVIRONMENT = deepMerge(this.ENVIRONMENT, env);
|
this.ENVIRONMENT = deepMerge(this.ENVIRONMENT, env);
|
||||||
|
|
||||||
|
|
@ -29,21 +28,18 @@ class Config {
|
||||||
ENDPOINT: this.ENDPOINT
|
ENDPOINT: this.ENDPOINT
|
||||||
});
|
});
|
||||||
|
|
||||||
this.loadHosts(this.HOSTS, config);
|
this.loadHosts(this.HOSTS, config, defaultConfigValues);
|
||||||
|
|
||||||
this.PLUGINS = new Plugins(this.PLUGINS);
|
this.PLUGINS = new Plugins(this.PLUGINS);
|
||||||
}
|
}
|
||||||
|
|
||||||
loadHosts(hosts, rootConfig) {
|
loadHosts(hosts, rootConfig, defaultConfigValues) {
|
||||||
hosts.forEach(host => {
|
hosts.forEach(host => {
|
||||||
if (typeof host.host === 'undefined') {
|
if (typeof host.host === 'undefined') {
|
||||||
throw new Error(`Host doesn't indicate it's host name.`);
|
throw new Error(`Host doesn't indicate it's host name.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
let config = deepMerge(
|
let config = deepMerge(defaultConfigValues, this.loadConfig(host));
|
||||||
this.defaultConfigValues,
|
|
||||||
this.loadConfig(host)
|
|
||||||
);
|
|
||||||
|
|
||||||
config.DEFAULTS = deepMerge(rootConfig.DEFAULTS, config.DEFAULTS);
|
config.DEFAULTS = deepMerge(rootConfig.DEFAULTS, config.DEFAULTS);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -67,6 +67,8 @@ const replaceInObject = function(obj, fn) {
|
||||||
return obj;
|
return obj;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const moduleVersion = () => parseInt(require('../package.json').version, 10);
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
requestRegex,
|
requestRegex,
|
||||||
replacementRegex,
|
replacementRegex,
|
||||||
|
|
@ -74,5 +76,6 @@ module.exports = {
|
||||||
UpperCaseKeys,
|
UpperCaseKeys,
|
||||||
removeOptionalKeys,
|
removeOptionalKeys,
|
||||||
toKebabCase,
|
toKebabCase,
|
||||||
replaceInObject
|
replaceInObject,
|
||||||
|
moduleVersion
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue