Skip to content

Commit

Permalink
Merge pull request #4 from snyk/fix/use-wrapper
Browse files Browse the repository at this point in the history
fix: use gradle wrapper script if present
  • Loading branch information
darscan authored Jul 31, 2017
2 parents 022d93a + 060d568 commit 10da9bc
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 9 deletions.
22 changes: 21 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var os = require('os');
var fs = require('fs');
var path = require('path');
var subProcess = require('./sub-process');
Expand All @@ -10,7 +11,9 @@ module.exports = {

function inspect(root, targetFile, options) {
if (!options) { options = { dev: false }; }
return subProcess.execute('gradle',

return subProcess.execute(
getCommand(root, targetFile),
buildArgs(root, targetFile, options.args),
{ cwd: root })
.then(function (result) {
Expand All @@ -35,6 +38,23 @@ function inspect(root, targetFile, options) {
});
}

function getCommand(root, targetFile) {
var isWin = /^win/.test(os.platform());
var wrapperScript = isWin ? 'gradlew.bat' : './gradlew';
// try to find a sibling wrapper script first
var pathToWrapper = path.resolve(
root, path.dirname(targetFile), wrapperScript);
if (fs.existsSync(pathToWrapper)) {
return pathToWrapper;
}
// now try to find a wrapper in the root
pathToWrapper = path.resolve(root, wrapperScript);
if (fs.existsSync(pathToWrapper)) {
return pathToWrapper;
}
return 'gradle';
}

function buildArgs(root, targetFile, gradleArgs) {
var args = ['dependencies', '-q'];
if (targetFile) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"author": "snyk.io",
"license": "Apache-2.0",
"devDependencies": {
"fs": "0.0.1-security",
"jscs": "^3.0.7",
"semantic-release": "^6.3.6",
"sinon": "^2.4.1",
"tap": "^10.3.2",
"tap-only": "0.0.5"
},
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions test/fixtures/with-wrapper-in-root/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// not a real buildfile
1 change: 1 addition & 0 deletions test/fixtures/with-wrapper-in-root/gradlew
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo "not an actual wrapper script"
1 change: 1 addition & 0 deletions test/fixtures/with-wrapper-in-root/gradlew.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
not an actual wrapper script
1 change: 1 addition & 0 deletions test/fixtures/with-wrapper/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// not a real buildfile
1 change: 1 addition & 0 deletions test/fixtures/with-wrapper/gradlew
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo "not an actual wrapper script"
1 change: 1 addition & 0 deletions test/fixtures/with-wrapper/gradlew.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
not an actual wrapper script
11 changes: 6 additions & 5 deletions test/functional/parse-gradle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@ var fs = require('fs');
var path = require('path');
var test = require('tap-only');
var parse = require('../../lib/parse-gradle');
var fixturePath = path.join(__dirname, '..', 'fixtures', 'no-wrapper');

test('compare full results', function (t) {
t.plan(1);
var gradleOutput = fs.readFileSync(path.join(
__dirname, '..', 'fixtures', 'gradle-dependencies-output.txt'), 'utf8');
var gradleOutput = fs.readFileSync(
path.join(fixturePath, 'gradle-dependencies-output.txt'), 'utf8');
var depTree = parse(gradleOutput, '[email protected]');
var results = require(path.join(
__dirname, '..','fixtures','gradle-dependencies-results.json'));
var results = require(
path.join(fixturePath,'gradle-dependencies-results.json'));

t.same(depTree, results);
});

test('parse a `gradle dependencies` output', function (t) {
t.plan(7);
var gradleOutput = fs.readFileSync(path.join(
__dirname, '..', 'fixtures', 'gradle-dependencies-output.txt'), 'utf8');
fixturePath, 'gradle-dependencies-output.txt'), 'utf8');
var depTree = parse(gradleOutput, '[email protected]');

t.equal(
Expand Down
124 changes: 122 additions & 2 deletions test/system/plugin.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
var fs = require('fs');
var os = require('os');
var path = require('path');
var test = require('tap-only');
var sinon = require('sinon');
var plugin = require('../../lib');
var subProcess = require('../../lib/sub-process');

var rootNoWrapper = path.join(
__dirname, '..', 'fixtures', 'no-wrapper');

var rootWithWrapper = path.join(
__dirname, '..', 'fixtures', 'with-wrapper');

var subWithWrapper = path.join(
__dirname, '..', 'fixtures', 'with-wrapper-in-root');

test('run inspect()', function (t) {
t.plan(1);
return plugin.inspect('.', path.join(
__dirname, '..', 'fixtures', 'build.gradle'))
__dirname, '..', 'fixtures', 'no-wrapper', 'build.gradle'))
.then(function (result) {
t.equal(result.package
.dependencies['com.android.tools.build:builder']
Expand All @@ -16,5 +27,114 @@ test('run inspect()', function (t) {
.dependencies['com.android.tools:annotations'].version,
'25.3.0',
'correct version found');
})
.catch(t.fail);
});

test('windows without wrapper', function (t) {
t.plan(1);

stubPlatform('win32', t);
stubSubProcessExec(t);

return plugin.inspect(rootNoWrapper, 'build.gradle')
.then(t.fail)
.catch(function () {
var cmd = subProcess.execute.getCall(0).args[0];
t.same(cmd, 'gradle', 'invokes gradle directly');
});
});

test('darwin without wrapper', function (t) {
t.plan(1);

stubPlatform('darwin', t);
stubSubProcessExec(t);

return plugin.inspect(rootNoWrapper, 'build.gradle')
.then(t.fail)
.catch(function () {
var cmd = subProcess.execute.getCall(0).args[0];
t.same(cmd, 'gradle', 'invokes gradle directly');
});
});

test('windows with wrapper', function (t) {
t.plan(1);

stubPlatform('win32', t);
stubSubProcessExec(t);

return plugin.inspect(rootWithWrapper, 'build.gradle')
.then(t.fail)
.catch(function () {
var cmd = subProcess.execute.getCall(0).args[0];
var expectedCmd = path.join(
__dirname, '..', 'fixtures', 'with-wrapper', 'gradlew.bat');
t.same(cmd, expectedCmd, 'invokes wrapper bat');
});
});

test('darwin with wrapper', function (t) {
t.plan(1);

stubPlatform('darwin', t);
stubSubProcessExec(t);

return plugin.inspect(rootWithWrapper, 'build.gradle')
.then(t.fail)
.catch(function () {
var cmd = subProcess.execute.getCall(0).args[0];
var expectedCmd = path.join(
__dirname, '..', 'fixtures', 'with-wrapper', 'gradlew');
t.same(cmd, expectedCmd, 'invokes wrapper script');
});
});

test('windows with wrapper in root', function (t) {
t.plan(1);

stubPlatform('win32', t);
stubSubProcessExec(t);

return plugin.inspect(subWithWrapper, path.join('app', 'build.gradle'))
.then(t.fail)
.catch(function () {
var cmd = subProcess.execute.getCall(0).args[0];
var expectedCmd = path.join(
__dirname, '..', 'fixtures', 'with-wrapper-in-root', 'gradlew.bat');
t.same(cmd, expectedCmd, 'invokes wrapper bat');
});
});

test('darwin with wrapper in root', function (t) {
t.plan(1);

stubPlatform('darwin', t);
stubSubProcessExec(t);

return plugin.inspect(subWithWrapper, path.join('app', 'build.gradle'))
.then(t.fail)
.catch(function () {
var cmd = subProcess.execute.getCall(0).args[0];
var expectedCmd = path.join(
__dirname, '..', 'fixtures', 'with-wrapper-in-root', 'gradlew');
t.same(cmd, expectedCmd, 'invokes wrapper script');
});
});

function stubPlatform(platform, t) {
sinon.stub(os, 'platform')
.callsFake(function () {
return platform;
});
t.teardown(os.platform.restore);
}

function stubSubProcessExec(t) {
sinon.stub(subProcess, 'execute')
.callsFake(function () {
return Promise.reject('abort');
});
t.teardown(subProcess.execute.restore);
}

0 comments on commit 10da9bc

Please sign in to comment.