Skip to content

Commit

Permalink
refactor: update validation of options.serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
waitingsong committed Feb 13, 2020
1 parent 23fc7b3 commit 9d9f33a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/utils/options.js
Expand Up @@ -65,10 +65,18 @@ module.exports = function(options) {
const isDebug = process.execArgv.some(argv => argv.includes('--debug') || argv.includes('--inspect'));
if (isDebug) options.isDebug = isDebug;

/* istanbul ignore next */
if (options.serialization && !semver.gte(process.version, '12.16.0')) {
const err = new Error('[master] agent_worker options.serialization requires Node.js >= v12.16.0');
this.logger.error(err);
if (semver.gte(process.version, '12.16.0')) {
/* istanbul ignore else */
if (typeof options.serialization !== 'undefined'
&& options.serialization !== 'json'
&& options.serialization !== 'advanced') {
throw new Error(`[egg-cluster] options.serialization must be "json" or "advanced", value is ${options.serialization}`);
}
} else {
/* istanbul ignore else */
if (options.serialization) {
throw new Error('[egg-cluster] options.serialization requires Node.js >= v12.16.0');
}
}

return options;
Expand Down
61 changes: 61 additions & 0 deletions test/options.test.js
Expand Up @@ -4,6 +4,7 @@ const path = require('path');
const assert = require('assert');
const os = require('os');
const mm = require('egg-mock');
const semver = require('semver');
const parseOptions = require('../lib/utils/options');
const utils = require('./utils');

Expand Down Expand Up @@ -202,4 +203,64 @@ describe('test/options.test.js', () => {
}
});
});

describe('should options.serialization', () => {
it('should with "json" value when Node.js >= v12.16.0', () => {
if (semver.gte(process.version, '12.16.0')) {
const options = parseOptions({
serialization: 'json',
});
assert(options.serialization === 'json');
}
});

it('should with "advanced" value when Node.js >= v12.16.0', () => {
if (semver.gte(process.version, '12.16.0')) {
const options = parseOptions({
serialization: 'advanced',
});
assert(options.serialization === 'advanced');
}
});

it('should error with invalid value when Node.js >= v12.16.0', () => {
if (semver.gte(process.version, '12.16.0')) {
try {
parseOptions({
serialization: 'fake',
});
assert(false);
} catch (ex) {
assert(ex.message.includes('must be "json" or "advanced"'));
}
}
});

it('should error with empty value when Node.js >= v12.16.0', () => {
if (semver.gte(process.version, '12.16.0')) {
try {
parseOptions({
serialization: '',
});
assert(false);
} catch (ex) {
assert(ex.message.includes('must be "json" or "advanced"'));
}
}
});

it('should error with value when Node.js < v12.16.0', () => {
if (!semver.gte(process.version, '12.16.0')) {
try {
parseOptions({
serialization: 'json',
});
assert(false);
} catch (ex) {
assert(ex.message.includes('requires Node.js >= v12.16.0'));
}
}
});
});

});

0 comments on commit 9d9f33a

Please sign in to comment.