From 1ae1bbe5d5b169552a4ea6b4188df11a221c9d27 Mon Sep 17 00:00:00 2001 From: Shaun Smith Date: Fri, 9 Jun 2017 17:49:38 +0100 Subject: [PATCH] feat: allow the command to be overridden via options --- lib/index.js | 20 +++++++++++--------- test/inspect.test.js | 23 +++++++++++++++++++++-- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/lib/index.js b/lib/index.js index 6e740b54..50608d86 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 { @@ -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', @@ -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) { @@ -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); } diff --git a/test/inspect.test.js b/test/inspect.test.js index 591dfd4e..090a7985 100644 --- a/test/inspect.test.js +++ b/test/inspect.test.js @@ -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'); @@ -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)); }