All: Change function paths to use this convention: module/function#subfunction@endpoint

FunctionRun: Write tests for specifying funciton path and function name, improve CLI support
This commit is contained in:
Austen Collins 2015-12-08 17:24:53 -08:00
parent feb5423275
commit 75f6737dcb
8 changed files with 80 additions and 33 deletions

View File

@ -41,7 +41,7 @@ class FunctionRun extends SPlugin {
description: 'Optional - Run a specific function by name in the current working directory'
}, {
option: 'path',
shortcut: 'n',
shortcut: 'p',
description: 'Optional - Run a specific function by path in the current working directory'
}
],
@ -57,11 +57,21 @@ class FunctionRun extends SPlugin {
let _this = this;
// If CLI, parse options
if (_this.S.cli) {
// Options
evt = this.S.cli.options;
// Option - Non-interactive
if (_this.S.cli.options.nonInteractive) _this.S._interactive = false
}
return _this._getFunction(evt)
.bind(_this)
.then(_this._runFunction)
.then(function(evt) {
return evt;
})
}
@ -112,9 +122,13 @@ class FunctionRun extends SPlugin {
}
// Load event object
evt.function = functions[0];
evt.event = evt.function.functionPath.replace('s-function.json', '');
evt.event = SUtils.readAndParseJsonSync(evt.event);
evt.function = functions[0];
let eventPath = evt.function.pathFunction.replace('s-function.json', '');
if (fs.existsSync(path.join(eventPath, 'event.json'))) {
evt.event = SUtils.readAndParseJsonSync(path.join(eventPath, 'event.json'));
} else {
evt.event = {};
}
// Return
return evt;
@ -135,6 +149,8 @@ class FunctionRun extends SPlugin {
_runFunction(evt) {
let _this = this;
// Look for a property named after the function in the event object
if (evt.event[evt.function.name]) evt.event = evt.event[evt.function.name];

View File

@ -122,7 +122,7 @@ exports.returnPartial = function(string, symbol, number, defaultResponse) {
* Get Functions
* - Resolve paths and return function JSONs
* - If no paths specified, finds all functions in baseDir
* - Each function must contain !# in path: module/function!#get
* - Each function must contain # in path: module/function#get
*/
exports.getFunctions = function(baseDir, functionPaths) {
@ -139,11 +139,12 @@ exports.getFunctions = function(baseDir, functionPaths) {
// If functionPaths, validate and return them
if (functionPaths) {
// Validate - ensure functionpath contains !#
// Validate - ensure functionPath contains #
for (let i = 0; i < functionPaths.length; i++) {
let path = functionPaths[i];
if (path.indexOf('!#') === -1) {
throw new SError(`Function path is missing '!#' : ${path}`,
if (path.indexOf('#') === -1) {
throw new SError(`Function path is missing '#' : ${path}`,
SError.errorCodes.INVALID_RESOURCE_NAME);
}
}
@ -166,7 +167,7 @@ exports.getFunctions = function(baseDir, functionPaths) {
// Create paths for each function in s-function.json
for (let j = 0; j < Object.keys(functionsObject).length; j++ ) {
functionPaths.push(filePath + '!#' + Object.keys(functionsObject)[j]);
functionPaths.push(filePath + '#' + Object.keys(functionsObject)[j]);
}
}
@ -182,8 +183,8 @@ exports.getFunctions = function(baseDir, functionPaths) {
let functionKey = null;
// Strip functionPath, functionKey & endpointKeys
functionKey = _this.returnPartial(functionPath, '!#', 1, null);
functionPath = _this.returnPartial(functionPath, '!#', 0, functionPath); // Strip from functionPath
functionKey = _this.returnPartial(functionPath, '#', 1, null);
functionPath = _this.returnPartial(functionPath, '#', 0, functionPath); // Strip from functionPath
// Check functionPath exists
if (!functionKey) {
@ -256,7 +257,7 @@ exports.getFunctions = function(baseDir, functionPaths) {
* Get Endpoints
* - Resolve paths and return endpoint JSONs
* - If no paths specified, finds all endpoints in baseDir
* - Each endpoint must contain !# and !@ in path: module/function!#get!@get-data
* - Each endpoint must contain # and @ in path: module/function#get@get-data
*/
exports.getEndpoints = function(baseDir, endpointPaths) {
@ -273,11 +274,11 @@ exports.getEndpoints = function(baseDir, endpointPaths) {
// If endpointPaths, validate and return them
if (endpointPaths) {
// Validate - ensure endpoint path contains !#
// Validate - ensure endpoint path contains #
for (let i = 0; i < endpointPaths.length; i++) {
let path = endpointPaths[i];
if (path.indexOf('!#') === -1 || path.indexOf('!@') === -1) {
throw new SError(`Endpoint path is missing '!#' or '!@' : ${path}`,
if (path.indexOf('#') === -1 || path.indexOf('@') === -1) {
throw new SError(`Endpoint path is missing '#' or '@' : ${path}`,
SError.errorCodes.INVALID_RESOURCE_NAME);
}
}
@ -301,10 +302,10 @@ exports.getEndpoints = function(baseDir, endpointPaths) {
// Create paths for each function in s-function.json
for (let j = 0; j < Object.keys(functionsObject).length; j++ ) {
let functionPath = filePath + '!#' + Object.keys(functionsObject)[j];
let functionPath = filePath + '#' + Object.keys(functionsObject)[j];
let funcObject = functionsObject[Object.keys(functionsObject)[j]];
for (let k = 0; k < Object.keys(funcObject.endpoints).length; k++) {
let endpointPath = functionPath + '!@' + Object.keys(funcObject.endpoints)[k];
let endpointPath = functionPath + '@' + Object.keys(funcObject.endpoints)[k];
endpointPaths.push(endpointPath);
}
}
@ -323,10 +324,10 @@ exports.getEndpoints = function(baseDir, endpointPaths) {
endpointKey = null;
// Strip endpointPath, functionKey & endpointKeys
functionKey = _this.returnPartial(endpointPath, '!#', 1, null);
endpointKey = _this.returnPartial(functionKey, '!@', 1, null);
functionKey = _this.returnPartial(functionKey, '!@', 0, null); // String !@ from functionKey
endpointPath = _this.returnPartial(endpointPath, '!#', 0); // Strip from functionPath
functionKey = _this.returnPartial(endpointPath, '#', 1, null);
endpointKey = _this.returnPartial(functionKey, '@', 1, null);
functionKey = _this.returnPartial(functionKey, '@', 0, null); // String @ from functionKey
endpointPath = _this.returnPartial(endpointPath, '#', 0); // Strip from functionPath
// Check endpointKey exists
if (!endpointKey) {

View File

@ -19,12 +19,12 @@ describe('All Tests', function() {
//require('./tests/actions/ModuleInstall');
//require('./tests/actions/ModuleCreate');
//require('./tests/actions/FunctionCreate');
require('./tests/actions/EnvList');
//require('./tests/actions/EnvList');
//require('./tests/actions/EnvGet');
//require('./tests/actions/EnvSetUnset');
//require('./tests/actions/ResourcesDeploy');
require('./tests/actions/FunctionRun');
//require('./tests/actions/FunctionDeploy');
//require('./tests/actions/EndpointDeploy');
require('./tests/actions/FunctionDeploy');
require('./tests/actions/EndpointDeploy');
});

View File

@ -3,7 +3,6 @@
// Load ENV
var ServerlessHelpers = require('serverless-helpers-js');
ServerlessHelpers.loadEnv();
console.log(process.env);
// Lambda Handler
module.exports.one = function(event, context) {

View File

@ -3,7 +3,6 @@
// Load ENV
var ServerlessHelpers = require('serverless-helpers-js');
ServerlessHelpers.loadEnv();
console.log(process.env);
// Lambda Handler
module.exports.one = function(event, context) {

View File

@ -76,7 +76,7 @@ describe('Test Action: Endpoint Deploy', function() {
stage: config.stage,
region: config.region,
paths: [
'moduleone/simple!#simpleOne!@simple/one'
'moduleone/simple#simpleOne@simple/one'
]
};

View File

@ -77,7 +77,7 @@ describe('Test Action: Function Deploy', function() {
stage: config.stage,
region: config.region,
paths: [
'moduleone/simple!#simpleOne'
'moduleone/simple#simpleOne'
]
};

View File

@ -13,12 +13,24 @@ let Serverless = require('../../../lib/Serverless.js'),
let serverless;
/**
* Validate Event
* - Validate an event object's properties
*/
let validateEvent = function(evt) {
assert.equal(true, typeof evt.function != 'undefined');
assert.equal(true, typeof evt.event != 'undefined');
assert.equal(true, typeof evt.result != 'undefined');
assert.equal(true, typeof evt.handler != 'undefined');
};
describe('Test Action: Function Run', function() {
before(function(done) {
this.timeout(0);
testUtils.createTestProject(config ['moduleone/simple'])
testUtils.createTestProject(config, ['moduleone/simple'])
.then(projPath => {
this.timeout(0);
@ -42,16 +54,36 @@ describe('Test Action: Function Run', function() {
done();
});
describe('Function Run', function() {
describe('Function Run w/ Path', function() {
it('should run the function with no errors', function(done) {
this.timeout(0);
serverless.actions.functionRun({
path: 'moduleone/simple!#simpleOne'
path: 'moduleone/simple#simpleOne'
})
.then(function(evt) {
console.log(evt);
validateEvent(evt);
assert.equal(true, evt.result.status == 'success');
done();
})
.catch(e => {
done(e);
});
});
});
describe('Function Run w/ Name', function() {
it('should run the function with no errors', function(done) {
this.timeout(0);
serverless.actions.functionRun({
name: 'simpleOne'
})
.then(function(evt) {
validateEvent(evt);
assert.equal(true, evt.result.status == 'success');
done();
})
.catch(e => {