Skip to content

Commit

Permalink
feat: allow the command to be overridden via options
Browse files Browse the repository at this point in the history
  • Loading branch information
darscan committed Jun 12, 2017
1 parent 2706e1a commit 1ae1bbe
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
20 changes: 11 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ module.exports = {
inspect: inspect,
};

function inspect(root, targetFile, args) {
function inspect(root, targetFile, args, options) {
if (!options) { options = {}; }
var command = options.command || 'python';
return Promise.all([
getMetaData(),
getDependencies(root, targetFile, args),
getMetaData(command, root),
getDependencies(command, root, targetFile, args),
])
.then(function (result) {
return {
Expand All @@ -18,8 +20,8 @@ function inspect(root, targetFile, args) {
});
}

function getMetaData() {
return subProcess.execute('python', ['--version'])
function getMetaData(command, root) {
return subProcess.execute(command, ['--version'], { cwd: root })
.then(function (output) {
return {
name: 'snyk-python-plugin',
Expand All @@ -28,10 +30,10 @@ function getMetaData() {
});
}

function getDependencies(root, targetFile, args) {
function getDependencies(command, root, targetFile, args) {
return subProcess.execute(
'python',
buildArgs(root, targetFile, args),
command,
buildArgs(targetFile, args),
{ cwd: root }
)
.then(function (output) {
Expand All @@ -48,7 +50,7 @@ function getDependencies(root, targetFile, args) {
});
}

function buildArgs(root, targetFile, extraArgs) {
function buildArgs(targetFile, extraArgs) {
var args = [path.resolve(__dirname, '../plug/pip_resolve.py')];
if (targetFile) { args.push(targetFile); }
if (extraArgs) { args.push(extraArgs); }
Expand Down
23 changes: 21 additions & 2 deletions test/inspect.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var test = require('tap').test;
var path = require('path');
var sinon = require('sinon');

var plugin = require('../lib');
var subProcess = require('../lib/sub-process');

Expand Down Expand Up @@ -86,6 +88,23 @@ test('deps not installed', function (t) {
});
});

function chdirWorkspaces(subdir) {
process.chdir(path.resolve(__dirname, 'workspaces', subdir));
test('uses provided exec command', function (t) {
var command = 'echo';
var execute = sinon.stub(subProcess, 'execute');
execute.onFirstCall().returns(Promise.resolve('abc'));
execute.onSecondCall().returns(Promise.resolve('{}'));
t.teardown(execute.restore);

return plugin.inspect('.', 'requirements.txt', null, {
command: command,
})
.then(function () {
t.ok(execute.calledTwice, 'execute called twice');
t.equal(execute.firstCall.args[0], command, 'uses command');
t.equal(execute.secondCall.args[0], command, 'uses command');
});
});

function chdirWorkspaces(dir) {
process.chdir(path.resolve(__dirname, 'workspaces', dir));
}

0 comments on commit 1ae1bbe

Please sign in to comment.