Skip to content

Commit

Permalink
test: Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lili2311 committed Oct 5, 2018
1 parent 39976bc commit 6faa02b
Show file tree
Hide file tree
Showing 35 changed files with 3,874 additions and 336 deletions.
33 changes: 16 additions & 17 deletions npm-debug.log
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@
1 verbose cli [ '/Users/lili/.nvm/versions/node/v6.12.3/bin/node',
1 verbose cli '/Users/lili/.nvm/versions/node/v6.12.3/bin/npm',
1 verbose cli 'run',
1 verbose cli 'tap' ]
1 verbose cli 'lint' ]
2 info using npm@3.10.10
3 info using node@v6.12.3
4 verbose run-script [ 'pretap', 'tap', 'posttap' ]
5 info lifecycle snyk@~pretap: snyk@
6 silly lifecycle snyk@~pretap: no script for pretap, continuing
7 info lifecycle snyk@~tap: snyk@
8 verbose lifecycle snyk@~tap: unsafe-perm in lifecycle true
9 verbose lifecycle snyk@~tap: PATH: /Users/lili/.nvm/versions/node/v6.12.3/lib/node_modules/npm/bin/node-gyp-bin:/Users/lili/www/snyk/snyk/node_modules/.bin:/usr/local/opt/maven@3.3/bin:/Users/lili/Downloads/google-cloud-sdk/bin:/usr/local/heroku/bin:/Users/lili/.rbenv/shims:/Users/lili/.nvm/versions/node/v6.12.3/bin:/usr/local/Cellar/postgresql@9.6/9.6.6/bin/:/Users/lili/.dotnet:/usr/local/go/bin:/System//Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
10 verbose lifecycle snyk@~tap: CWD: /Users/lili/www/snyk/snyk
11 silly lifecycle snyk@~tap: Args: [ '-c',
11 silly lifecycle 'tap test/cli.acceptance.test.js -Rspec --timeout=180 --node-path ts-node --test-file-pattern \'/\\.[tj]s$/\'' ]
12 silly lifecycle snyk@~tap: Returned: code: 1 signal: null
13 info lifecycle snyk@~tap: Failed to exec tap script
14 verbose stack Error: snyk@ tap: `tap test/cli.acceptance.test.js -Rspec --timeout=180 --node-path ts-node --test-file-pattern '/\.[tj]s$/'`
4 verbose run-script [ 'prelint', 'lint', 'postlint' ]
5 info lifecycle snyk@~prelint: snyk@
6 silly lifecycle snyk@~prelint: no script for prelint, continuing
7 info lifecycle snyk@~lint: snyk@
8 verbose lifecycle snyk@~lint: unsafe-perm in lifecycle true
9 verbose lifecycle snyk@~lint: PATH: /Users/lili/.nvm/versions/node/v6.12.3/lib/node_modules/npm/bin/node-gyp-bin:/Users/lili/www/snyk/snyk/node_modules/.bin:/usr/local/opt/maven@3.3/bin:/Users/lili/Downloads/google-cloud-sdk/bin:/usr/local/heroku/bin:/Users/lili/.rbenv/shims:/Users/lili/.nvm/versions/node/v6.12.3/bin:/usr/local/Cellar/postgresql@9.6/9.6.6/bin/:/Users/lili/.dotnet:/usr/local/go/bin:/System//Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
10 verbose lifecycle snyk@~lint: CWD: /Users/lili/www/snyk/snyk
11 silly lifecycle snyk@~lint: Args: [ '-c', 'npm run eslint && npm run tslint' ]
12 silly lifecycle snyk@~lint: Returned: code: 1 signal: null
13 info lifecycle snyk@~lint: Failed to exec lint script
14 verbose stack Error: snyk@ lint: `npm run eslint && npm run tslint`
14 verbose stack Exit status 1
14 verbose stack at EventEmitter.<anonymous> (/Users/lili/.nvm/versions/node/v6.12.3/lib/node_modules/npm/lib/utils/lifecycle.js:255:16)
14 verbose stack at emitTwo (events.js:106:13)
Expand All @@ -29,18 +28,18 @@
15 verbose pkgid snyk@
16 verbose cwd /Users/lili/www/snyk/snyk
17 error Darwin 17.3.0
18 error argv "/Users/lili/.nvm/versions/node/v6.12.3/bin/node" "/Users/lili/.nvm/versions/node/v6.12.3/bin/npm" "run" "tap"
18 error argv "/Users/lili/.nvm/versions/node/v6.12.3/bin/node" "/Users/lili/.nvm/versions/node/v6.12.3/bin/npm" "run" "lint"
19 error node v6.12.3
20 error npm v3.10.10
21 error code ELIFECYCLE
22 error snyk@ tap: `tap test/cli.acceptance.test.js -Rspec --timeout=180 --node-path ts-node --test-file-pattern '/\.[tj]s$/'`
22 error snyk@ lint: `npm run eslint && npm run tslint`
22 error Exit status 1
23 error Failed at the snyk@ tap script 'tap test/cli.acceptance.test.js -Rspec --timeout=180 --node-path ts-node --test-file-pattern '/\.[tj]s$/''.
23 error Failed at the snyk@ lint script 'npm run eslint && npm run tslint'.
23 error Make sure you have the latest version of node.js and npm installed.
23 error If you do, this is most likely a problem with the snyk package,
23 error not with npm itself.
23 error Tell the author that this fails on your system:
23 error tap test/cli.acceptance.test.js -Rspec --timeout=180 --node-path ts-node --test-file-pattern '/\.[tj]s$/'
23 error npm run eslint && npm run tslint
23 error You can get information on how to open an issue for this project with:
23 error npm bugs snyk
23 error Or if that isn't available, you can get their info via:
Expand Down
7 changes: 4 additions & 3 deletions src/cli/commands/protect/wizard.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ function wizard(options = {}) {

async function processPackageManager(options) {
const packageManager = detect.detectPackageManager(cwd, options);
const unsupportedPackageManager = unsupportedPackageManagers[packageManager];
if (unsupportedPackageManager) {

const usupportedPackageManager = unsupportedPackageManagers[packageManager];
if (usupportedPackageManager) {
return Promise.reject(
`Snyk wizard for ${unsupportedPackageManager} projects is not currently supported`);
`Snyk wizard for ${usupportedPackageManager} projects is not currently supported`);
}

return fs.exists(path.join('.', 'node_modules'))
Expand Down
4 changes: 0 additions & 4 deletions src/lib/get-node-runtime-version.ts

This file was deleted.

4 changes: 3 additions & 1 deletion src/lib/plugins/yarn/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const fs = require('then-fs');
const path = require('path');
const lockFileParser = require('snyk-nodejs-lockfile-parser');
const debug = require('debug')('snyk');
const getRuntimeVersion = require('../../get-node-runtime-version');

module.exports = {
inspect,
Expand Down Expand Up @@ -80,3 +79,6 @@ async function generateDependenciesFromLockfile(root, options, targetFile) {
.buildDepTree(manifestFile, lockFile, options.dev, lockFileParser.LockfileType.yarn);
}

function getRuntimeVersion() {
parseInt(process.version.slice(1).split('.')[0], 10);
}
39 changes: 20 additions & 19 deletions src/lib/protect/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
const protect = module.exports = {
var protect = module.exports = {
ignore: require('./ignore'),
update: require('./update').update,
patch: require('./patch'),
patchesForPackage: require('./patches-for-package'),
generatePolicy,
generatePolicy: generatePolicy,
};

const debug = require('debug')('snyk');
const _ = require('lodash');
var debug = require('debug')('snyk');
var _ = require('lodash');

function generatePolicy(policy, tasks, live, packageManager) {
const promises = ['patch', 'ignore', 'update']
.filter((task) => tasks[task].length)
.map((task) => protect[task](tasks[task], live, packageManager));
var promises = ['ignore', 'update', 'patch'].filter(function (task) {
return tasks[task].length;
}).map(function (task) {
return protect[task](tasks[task], live, packageManager);
});

return Promise.all(promises)
.then((res) => {
// we're squashing the arrays of arrays into a flat structure
// with only non-false values
const results = _.flattenDeep(res).filter(Boolean);
return Promise.all(promises).then(function (res) {
// we're squashing the arrays of arrays into a flat structure
// with only non-false values
var results = _.flattenDeep(res).filter(Boolean);

// then we merge the configs together using the original config
// as the baseline (this lets us retain the user's existing config)
results.unshift(policy);
const newPolicy = _.merge.apply(_, results);
// then we merge the configs together using the original config
// as the baseline (this lets us retain the user's existing config)
results.unshift(policy);
var newPolicy = _.merge.apply(_, results);

debug(JSON.stringify(newPolicy, '', 2));
debug(JSON.stringify(newPolicy, '', 2));

return newPolicy;
});
return newPolicy;
});
}
1 change: 0 additions & 1 deletion src/lib/protect/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ function update(packages, live, pkgManager) {
pkgManager = pkgManager || 'npm';
var lbl = 'Applying updates using ' + pkgManager + '...';
var error = false;
debug("===== pkgManager", pkgManager);

return spinner(lbl).then(function () {
var upgrade = packages
Expand Down
2 changes: 2 additions & 0 deletions src/lib/snyk-test/npm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ function test(root, options) {
}
let policyLocations = [options['policy-path'] || root];
const targetFile = options.file || detect.detectPackageFile(root);
// this is used for Meta
options.file = targetFile;

return Promise.resolve()
.then(() => {
Expand Down
149 changes: 55 additions & 94 deletions test/acceptance/cli.acceptance.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,121 +568,82 @@ test('`test npm-package-with-subfolder --file=subfolder/package-lock.json ` pick
});
});

test('`test npm-package-missing-dep --file=package-lock.json ` with missing dep errors', function (t) {
t.plan(1);
test('`test npm-package --file=yarn.lock ` sends pkg info', function (t) {
chdirWorkspaces();
return cli.test('npm-package-missing-dep', {file: 'package-lock.json'})
.catch((e) => {
t.includes(e.message, 'out of sync', 'Contains enough info about error');
return cli.test('npm-package', {file: 'yarn.lock'})
.then(function () {
var req = server.popRequest();
var pkg = req.body;
t.equal(req.method, 'POST', 'makes POST request');
t.match(req.url, '/vuln/npm', 'posts to correct url');
t.ok(pkg.dependencies['debug'], 'dependency');
t.ok(pkg.dependencies['debug'].dependencies['ms'], 'transitive dependency');
t.notOk(pkg.dependencies['object-assign'],
'no dev dependency');
t.notOk(pkg.from, 'no "from" array on root');
t.notOk(pkg.dependencies['debug'].from,
'no "from" array on dep');
});
});

test('`test npm-package-missing-dep ` in package-lock works', function (t) {
test('`test npm-package --file=yarn.lock --dev` sends pkg info', function (t) {
chdirWorkspaces();
return cli.test('npm-package-missing-dep')
return cli.test('npm-package', {file: 'yarn.lock', dev: true})
.then(function () {
var req = server.popRequest();
var pkg = req.body;
t.equal(req.method, 'POST', 'makes POST request');
t.match(req.url, '/vuln/npm', 'posts to correct url');
t.ok(pkg.dependencies['debug'], 'dependency');
t.ok(pkg.dependencies['debug'].dependencies['ms'], 'transitive dependency');
t.notOk(pkg.dependencies['object-assign'],
'no dev dependency');
t.ok(pkg.dependencies['object-assign'],
'dev dependency included');
t.notOk(pkg.from, 'no "from" array on root');
t.notOk(pkg.dependencies['debug'].from,
'no "from" array on dep');
});
});

// Unfortunately, lockfile parser for yarn doesn't work on node < 6
// https://github.com/snyk/nodejs-lockfile-parser/blob/master/lib/parsers/yarn-lock-parse.ts#L32
if (parseInt(process.version.slice(1).split('.')[0], 10) < 6) {
test('Testing yarn.lock on node 4', async (t) => {
return cli.test('npm-package-missing-dep', {file: 'yarn.lock'})
.catch((e) => {
t.includes(e.message,
'less than 6. Please upgrade your Node.js',
'Information about non-supported environment is shown');
});
});
} else {
test('`test npm-package --file=yarn.lock ` sends pkg info', function (t) {
chdirWorkspaces();
return cli.test('npm-package', {file: 'yarn.lock'})
.then(function () {
var req = server.popRequest();
var pkg = req.body;
t.equal(req.method, 'POST', 'makes POST request');
t.match(req.url, '/vuln/npm', 'posts to correct url');
t.ok(pkg.dependencies['debug'], 'dependency');
t.ok(pkg.dependencies['debug'].dependencies['ms'], 'transitive dependency');
t.notOk(pkg.dependencies['object-assign'],
'no dev dependency');
t.notOk(pkg.from, 'no "from" array on root');
t.notOk(pkg.dependencies['debug'].from,
'no "from" array on dep');
});
});

test('`test npm-package --file=yarn.lock --dev` sends pkg info', function (t) {
chdirWorkspaces();
return cli.test('npm-package', {file: 'yarn.lock', dev: true})
.then(function () {
var req = server.popRequest();
var pkg = req.body;
t.equal(req.method, 'POST', 'makes POST request');
t.match(req.url, '/vuln/npm', 'posts to correct url');
t.ok(pkg.dependencies['debug'], 'dependency');
t.ok(pkg.dependencies['debug'].dependencies['ms'], 'transitive dependency');
t.ok(pkg.dependencies['object-assign'],
'dev dependency included');
t.notOk(pkg.from, 'no "from" array on root');
t.notOk(pkg.dependencies['debug'].from,
'no "from" array on dep');
});
});
test('`test npm-package-shrinkwrap --file=yarn.lock ` with npm-shrinkwrap errors', function (t) {
t.plan(1);
chdirWorkspaces();
return cli.test('npm-package-shrinkwrap', {file: 'yarn.lock'})
.catch((e) => {
t.includes(e.message, '--file=yarn.lock', 'Contains enough info about error');
});
});

test('`test npm-package-shrinkwrap --file=yarn.lock ` with npm-shrinkwrap errors', function (t) {
t.plan(1);
chdirWorkspaces();
return cli.test('npm-package-shrinkwrap', {file: 'yarn.lock'})
.catch((e) => {
t.includes(e.message, '--file=yarn.lock', 'Contains enough info about error');
});
});
test('`test npm-package-with-subfolder --file=yarn.lock ` picks top-level files', function (t) {
chdirWorkspaces();
return cli.test('npm-package-with-subfolder', {file: 'yarn.lock'})
.then(function () {
var req = server.popRequest();
var pkg = req.body;
t.equal(pkg.name, 'npm-package-top-level', 'correct package is taken');
t.ok(pkg.dependencies['to-array'], 'dependency');
});
});

test('`test npm-package-with-subfolder --file=yarn.lock ` picks top-level files', function (t) {
chdirWorkspaces();
return cli.test('npm-package-with-subfolder', {file: 'yarn.lock'})
.then(function () {
var req = server.popRequest();
var pkg = req.body;
t.equal(pkg.name, 'npm-package-top-level', 'correct package is taken');
t.ok(pkg.dependencies['to-array'], 'dependency');
});
});
test('`test npm-package-with-subfolder --file=subfolder/yarn.lock ` picks subfolder files', function (t) {
chdirWorkspaces();
return cli.test('npm-package-with-subfolder', {file: 'subfolder/yarn.lock'})
.then(function () {
var req = server.popRequest();
var pkg = req.body;
t.equal(pkg.name, 'npm-package-subfolder', 'correct package is taken');
t.ok(pkg.dependencies['to-array'], 'dependency');
});
});

test('`test npm-package-with-subfolder --file=subfolder/yarn.lock ` picks subfolder files', function (t) {
chdirWorkspaces();
return cli.test('npm-package-with-subfolder', {file: 'subfolder/yarn.lock'})
.then(function () {
var req = server.popRequest();
var pkg = req.body;
t.equal(pkg.name, 'npm-package-subfolder', 'correct package is taken');
t.ok(pkg.dependencies['to-array'], 'dependency');
});
});
test('`test npm-package-missing-dep --file=yarn.lock ` with missing dep errors', function (t) {
t.plan(1);
chdirWorkspaces();
return cli.test('npm-package-missing-dep', {file: 'yarn.lock'})
.catch((e) => {
t.includes(e.message, 'out of sync', 'Contains enough info about error');
});
});

test('`test npm-package-missing-dep --file=yarn.lock ` with missing dep errors', function (t) {
t.plan(1);
chdirWorkspaces();
return cli.test('npm-package-missing-dep', {file: 'yarn.lock'})
.catch((e) => {
t.includes(e.message, 'out of sync', 'Contains enough info about error');
});
});
}

test('`test` on a yarn package does work and displays appropriate text',
function (t) {
Expand Down Expand Up @@ -1851,8 +1812,8 @@ test('`wizard` for unsupported package managers', function (t) {
});
}
var cases = [
{ file: 'maven-app/pom.xml', type: 'Maven' },
{ file: 'ruby-app/Gemfile.lock', type: 'RubyGems' },
{ file: 'maven-app/pom.xml', type: 'Maven' },
{ file: 'pip-app/requirements.txt', type: 'Python' },
{ file: 'sbt-app/build.sbt', type: 'SBT' },
{ file: 'gradle-app/build.gradle', type: 'Gradle' },
Expand Down
Loading

0 comments on commit 6faa02b

Please sign in to comment.