add basic and local executors with minimal tests

This commit is contained in:
oleg 2015-03-18 00:13:37 +03:00
parent fd30c1d94c
commit cd0b335844
6 changed files with 162 additions and 1 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
node_modules/
node_modules
test/workspace

35
lib/executor/base.js Normal file
View File

@ -0,0 +1,35 @@
'use strict';
var Steppy = require('twostep').Steppy;
function Executor(params) {
this.cwd = params.cwd;
}
exports.Executor = Executor;
Executor.prototype._getSources = function(params, callback) {
};
Executor.prototype._runStep = function(params, callback) {
};
Executor.prototype.run = function(params, callback) {
var self = this;
Steppy(
function() {
self._getSources(params.scm, this.slot());
},
function() {
var funcs = params.steps.map(function(step) {
return function() {
self._runStep(step, this.slot());
};
});
funcs.push(this.slot());
Steppy.apply(this, funcs);
},
callback
);
};

6
lib/executor/index.js Normal file
View File

@ -0,0 +1,6 @@
'use strict';
exports.createExecutor = function(params) {
var Constructor = require('./' + params.type).Executor;
return new Constructor(params);
};

67
lib/executor/local.js Normal file
View File

@ -0,0 +1,67 @@
'use strict';
var Steppy = require('twostep').Steppy,
inherits = require('util').inherits,
ParentExecutor = require('./base').Executor,
createScm = require('../scm').createScm,
createCommand = require('../command').createCommand,
fs = require('fs');
function Executor(params) {
ParentExecutor.call(this, params);
}
inherits(Executor, ParentExecutor);
exports.Executor = Executor;
Executor.prototype._getSources = function(params, callback) {
var self = this,
scm, isFirstRun;
Steppy(
function() {
var slot = this.slot();
fs.exists(self.cwd, function(exists) {
slot(null, exists);
});
},
function(err, exists) {
var scmParams = {type: params.type};
if (exists) {
scmParams.cwd = self.cwd;
isFirstRun = false;
} else {
scmParams.repository = params.repository;
isFirstRun = true;
}
scm = createScm(scmParams);
if (isFirstRun) {
scm.clone(self.cwd, params.rev, this.slot());
} else {
scm.pull(params.rev, this.slot())
}
},
function() {
if (!isFirstRun) {
scm.update(params.rev, this.slot());
} else {
this.pass(null);
}
},
callback
);
};
Executor.prototype._runStep = function(params, callback) {
Steppy(
function() {
if (params.type !== 'shell') {
throw new Error('Unknown step type: ' + params.type);
}
var command = createCommand(params);
command.run(params, this.slot())
},
callback
);
};

51
test/executor.js Normal file
View File

@ -0,0 +1,51 @@
'use strict';
var expect = require('expect.js'),
path = require('path'),
fs = require('fs'),
createExecutor = require('../lib/executor').createExecutor,
SpawnCommand = require('../lib/command/spawn').Command;
['local'].forEach(function(type) {
describe(type + ' executor', function() {
var workspacePath = path.join(__dirname, 'workspace');
function rmdir(dir, callback) {
new SpawnCommand().run({cmd: 'rm', args: ['-R', dir]}, callback);
}
it('remove test workspace dir if it exists', function(done) {
if (fs.exists(workspacePath, function(isExists) {
if (isExists) {
rmdir(workspacePath, done);
} else {
done();
}
}));
});
var executor;
it('instance should be created without errors', function() {
executor = createExecutor({
type: type,
cwd: workspacePath
});
});
it('should run', function(done) {
executor.run({
scm: {
type: 'mercurial',
repository: path.join(__dirname, 'repos', 'mercurial'),
rev: 'default'
},
steps: [
{type: 'shell', cmd: 'echo 1'},
{type: 'shell', cmd: 'echo 2'}
]
}, done);
});
});
});

View File

@ -2,3 +2,4 @@
require('./commands');
require('./scm')
require('./executor')