diff --git a/test/acceptance/cli-test/cli-test.python.spec.ts b/test/acceptance/cli-test/cli-test.python.spec.ts index 480e2bfbbdf..76a9f49fc2d 100644 --- a/test/acceptance/cli-test/cli-test.python.spec.ts +++ b/test/acceptance/cli-test/cli-test.python.spec.ts @@ -1,10 +1,7 @@ import * as fs from 'fs'; import * as sinon from 'sinon'; import { AcceptanceTests } from './cli-test.acceptance.test'; - -function loadJson(filename: string) { - return JSON.parse(fs.readFileSync(filename, 'utf-8')); -} +import { loadJson } from '../../utils'; export const PythonTests: AcceptanceTests = { language: 'Python', diff --git a/test/api-key.test.ts b/test/api-key.test.ts index b0d892a7fa7..2e0fd45d0e3 100644 --- a/test/api-key.test.ts +++ b/test/api-key.test.ts @@ -1,7 +1,7 @@ import { test } from 'tap'; import * as snyk from '../src/lib'; -test('api token', (t) => { +test('api token', async (t) => { const fromConfig = snyk.config.get('api'); t.equal(fromConfig, snyk.api, 'current api value matches config store'); const value = '_____test_____'; @@ -12,10 +12,9 @@ test('api token', (t) => { } else { snyk.config.delete('api'); } - t.end(); }); -test('api token via env value', (t) => { +test('api token via env value', async (t) => { const fromConfig = snyk.config.get('api'); t.equal(fromConfig, snyk.api, 'current api value matches config store'); const value = '_____test_____'; @@ -26,5 +25,4 @@ test('api token via env value', (t) => { } else { snyk.config.delete('api'); } - t.end(); }); diff --git a/test/deep-deps.test.ts b/test/deep-deps.test.ts index 2e737eb9158..84faa33f36b 100644 --- a/test/deep-deps.test.ts +++ b/test/deep-deps.test.ts @@ -15,8 +15,6 @@ test('finds all sub-dependencies', async (t) => { 'marked has no dependencies', ); } catch (e) { - t.fail(e.message); - console.log(e.stack); - t.bailout(); + t.fail('Should have passed: ' + e); } }); diff --git a/test/litmus.test.js b/test/litmus.test.js deleted file mode 100644 index 641a620da70..00000000000 --- a/test/litmus.test.js +++ /dev/null @@ -1,8 +0,0 @@ -'use strict'; -var test = require('tape'); - -test('does it load', function(t) { - var snyk = require('../src/lib'); - t.assert(snyk, 'snyk loaded'); - t.end(); -}); diff --git a/test/litmus.test.ts b/test/litmus.test.ts new file mode 100644 index 00000000000..a8934503b0c --- /dev/null +++ b/test/litmus.test.ts @@ -0,0 +1,6 @@ +import { test } from 'tap'; +import * as snyk from '../src/lib'; + +test('does it load', async (t) => { + t.assert(snyk, 'snyk loaded'); +}); diff --git a/test/module-report.test.js b/test/module-report.test.js deleted file mode 100644 index fde1e459e42..00000000000 --- a/test/module-report.test.js +++ /dev/null @@ -1,74 +0,0 @@ -'use strict'; -var test = require('tape'); -var path = require('path'); -var snyk = require('../src/lib'); - -var cwd = process.cwd(); -var osDir = path.resolve(__dirname, 'fixtures', 'demo-private'); - -test('module reporting: private project', function(t) { - function runTests(t, error, modules) { - t.plan(3 + 4 * 3); // 3 coded tests + 5 module specific tests - - var expectedModules = { - express: '3.21.1', - autocache: '0.6.1', - less: '2.5.1', - marked: '0.2.10', - 'demo-private': null, - }; - - if (error) { - t.fail(error.message); - t.bailout(); - } - t.ok(!error, 'module reading did not error'); - t.ok(typeof modules === 'object', 'modules is an object'); - - var keys = Object.keys(modules.dependencies); - var count = keys.length; - t.equal(count, 4, 'dep count'); - - keys.forEach(function(key) { - t.ok(expectedModules[key] !== undefined, key + ' was expected'); - - // specical case for demo-private - as it doesn't have a version - if (key === 'demo-private') { - t.equal(modules[key].version, null, 'no version on demo-private'); - t.equal(modules[key].valid, undefined, 'no dep test on demo-private'); - } else { - t.equal( - expectedModules[key], - modules.dependencies[key].version, - key + ' version is correct', - ); - t.equal( - modules.dependencies[key].valid, - true, - key + ' version was satisified WRT dep', - ); - } - }); - t.end(); - } - - t.test('specified directory', function(t) { - snyk.modules(osDir).then(function(modules) { - runTests(t, null, modules); - }); - }); - - t.test('inferred directory', function(t) { - process.chdir(osDir); - - snyk.modules('.').then(function(modules) { - runTests(t, null, modules); - }); - }); -}); - -test('teardown', function(t) { - process.chdir(cwd); - t.pass('reset cwd'); - t.end(); -}); diff --git a/test/module-report.test.ts b/test/module-report.test.ts new file mode 100644 index 00000000000..227a4afe6d5 --- /dev/null +++ b/test/module-report.test.ts @@ -0,0 +1,66 @@ +import { test } from 'tap'; +import * as path from 'path'; +import * as snyk from '../src/lib'; + +const cwd = process.cwd(); +const osDir = path.resolve(__dirname, 'fixtures', 'demo-private'); + +async function runTests(t, error, modules) { + const expectedModules = { + express: '3.21.1', + autocache: '0.6.1', + less: '2.5.1', + marked: '0.2.10', + 'demo-private': null, + }; + + if (error) { + t.fail(error.message); + } + t.ok(!error, 'module reading did not error'); + t.ok(typeof modules === 'object', 'modules is an object'); + + const keys = Object.keys(modules.dependencies); + const count = keys.length; + t.equal(count, 4, 'dep count'); + + keys.forEach((key) => { + t.ok(expectedModules[key] !== undefined, key + ' was expected'); + + // special case for demo-private - as it doesn't have a version + if (key === 'demo-private') { + t.equal(modules[key].version, null, 'no version on demo-private'); + t.equal(modules[key].valid, undefined, 'no dep test on demo-private'); + } else { + t.equal( + expectedModules[key], + modules.dependencies[key].version, + key + ' version is correct', + ); + t.equal( + modules.dependencies[key].valid, + true, + key + ' version was satisfied WRT dep', + ); + } + }); +} + +test('module reporting: private project', async (t) => { + t.test('specified directory', async (t) => { + const modules = await snyk.modules(osDir); + await runTests(t, null, modules); + }); + + t.test('inferred directory', async (t) => { + process.chdir(osDir); + + const modules = await snyk.modules('.'); + await runTests(t, null, modules); + }); +}); + +test('teardown', async (t) => { + process.chdir(cwd); + t.pass('reset cwd'); +}); diff --git a/test/no-deps.test.js b/test/no-deps.test.js deleted file mode 100644 index d2ff7194cfb..00000000000 --- a/test/no-deps.test.js +++ /dev/null @@ -1,21 +0,0 @@ -'use strict'; -var test = require('tape'); -var path = require('path'); -var snyk = require('../src/lib'); - -var osDir = path.resolve(__dirname, 'fixtures', 'no-deps'); - -test('works when there are no dependencies', function(t) { - t.plan(2); - snyk - .modules(osDir) - .then(function(modules) { - t.ok(true, 'modules did not bail'); - t.deepEqual(modules.dependencies, {}); - }) - .catch(function(e) { - t.fail(e.message); - console.log(e.stack); - t.bailout(); - }); -}); diff --git a/test/no-deps.test.ts b/test/no-deps.test.ts new file mode 100644 index 00000000000..60e6f67c731 --- /dev/null +++ b/test/no-deps.test.ts @@ -0,0 +1,15 @@ +import { test } from 'tap'; +import * as path from 'path'; +import * as snyk from '../src/lib'; + +const osDir = path.resolve(__dirname, 'fixtures', 'no-deps'); + +test('works when there are no dependencies', async (t) => { + try { + const modules = await snyk.modules(osDir); + t.ok(true, 'modules did not bail'); + t.deepEqual(modules.dependencies, {}); + } catch (e) { + t.fail('Should have passed:' + e); + } +}); diff --git a/test/old-snyk-format.test.js b/test/old-snyk-format.test.js deleted file mode 100644 index bfd422ea556..00000000000 --- a/test/old-snyk-format.test.js +++ /dev/null @@ -1,20 +0,0 @@ -var policy = require('snyk-policy'); -var test = require('tape'); - -test('test sensibly bails if gets an old .snyk format', function(t) { - var vulns2 = require('./fixtures/test-jsbin-vulns-updated.json'); - var policy = require('snyk-policy'); - - t.plan(1); - policy - .load(__dirname + '/fixtures/old-snyk-config') - .then(function(config) { - return config.filter(vulns2); - }) - .then(function(res) { - t.fail('was expecting an error, got ' + JSON.stringify(res)); - }) - .catch(function(e) { - t.equal(e.code, 'OLD_DOTFILE_FORMAT'); - }); -}); diff --git a/test/old-snyk-format.test.ts b/test/old-snyk-format.test.ts new file mode 100644 index 00000000000..cb1a1e386c9 --- /dev/null +++ b/test/old-snyk-format.test.ts @@ -0,0 +1,16 @@ +import * as policy from 'snyk-policy'; +import { test } from 'tap'; +import { loadJson } from './utils'; + +test('test sensibly bails if gets an old .snyk format', async (t) => { + try { + const vulns2 = loadJson( + __dirname + '/fixtures/test-jsbin-vulns-updated.json', + ); + const config = await policy.load(__dirname + '/fixtures/old-snyk-config'); + const res = await config.filter(vulns2); + t.fail('was expecting an error, got ' + JSON.stringify(res)); + } catch (e) { + t.equal(e.code, 'OLD_DOTFILE_FORMAT'); + } +}); diff --git a/test/package-no-name.test.ts b/test/package-no-name.test.ts index 650b8747686..3102af40024 100644 --- a/test/package-no-name.test.ts +++ b/test/package-no-name.test.ts @@ -1,4 +1,4 @@ -import * as test from 'tape'; +import { test } from 'tap'; import * as snyk from '../src/lib'; test('packages with no name read dir', async (t) => { diff --git a/test/policy.test.ts b/test/policy.test.ts index fa47417a294..f6de7522eb7 100644 --- a/test/policy.test.ts +++ b/test/policy.test.ts @@ -1,5 +1,5 @@ import * as policy from 'snyk-policy'; -import * as test from 'tape'; +import { test } from 'tap'; import { extendExpiries } from './utils'; import * as path from 'path'; diff --git a/test/prompt-next-step.test.js b/test/prompt-next-step.test.js index 3daae7aceec..1d5a7078769 100644 --- a/test/prompt-next-step.test.js +++ b/test/prompt-next-step.test.js @@ -1,4 +1,4 @@ -var test = require('tape'); +var test = require('tap').test; var prompts = require('../src/cli/commands/protect/prompts').nextSteps; test('wizard next steps include protect correctly', function(t) { diff --git a/test/prompts-no-remediation.test.js b/test/prompts-no-remediation.test.js index 55d56c7e379..20c00f825b8 100644 --- a/test/prompts-no-remediation.test.js +++ b/test/prompts-no-remediation.test.js @@ -1,4 +1,4 @@ -var test = require('tape'); +var test = require('tap').test; var getPrompts = require('../src/cli/commands/protect/prompts').getPrompts; var vulns = require('./fixtures/test-jsbin-vulns-updated.json').vulnerabilities; diff --git a/test/protect-config.test.js b/test/protect-config.test.js index 58c46c07cc5..9f2285e334a 100644 --- a/test/protect-config.test.js +++ b/test/protect-config.test.js @@ -1,5 +1,5 @@ var protect = require('../src/lib/protect'); -var test = require('tape'); +var test = require('tap').test; var plan = require('./fixtures/protect-interactive.json'); var expected = require('./fixtures/protect-interactive-config.json'); diff --git a/test/protect-fail.test.js b/test/protect-fail.test.js index 007f721cf45..2c2b6460538 100644 --- a/test/protect-fail.test.js +++ b/test/protect-fail.test.js @@ -2,7 +2,7 @@ var applyPatch = require('../src/lib/protect/apply-patch'); var path = require('path'); var fs = require('fs'); var thenfs = require('then-fs'); -var test = require('tape'); +var test = require('tap').test; var snyk = require('../src/lib'); test('bad patch file does not apply', function(t) { diff --git a/test/protect-patch-skip.test.js b/test/protect-patch-skip.test.js deleted file mode 100644 index b639d049770..00000000000 --- a/test/protect-patch-skip.test.js +++ /dev/null @@ -1,62 +0,0 @@ -'use strict'; -const debug = require('debug')('snyk'); -const protect = require('../src/lib/protect'); -const path = require('path'); -const test = require('tape'); -const vulns = require('./fixtures/lodash@4.17.11-vuln.json').vulnerabilities; -const exec = require('child_process').exec; - -test('patch is correctly applied', (t) => { - const name = 'lodash'; - const version = '4.17.13'; - - const dir = path.resolve(__dirname, 'fixtures/protect'); - npm('install', name + '@' + version, dir) - .then(() => { - debug('installing to %s', dir); - return protect.patch(vulns, true, dir).then(() => { - t.pass('patch resolved'); - }); - }) - .catch((error) => { - console.log(error.stack); - t.fail(error); - }) - .then(() => { - return npm('uninstall', name, dir).then(() => { - t.pass('packages cleaned up'); - t.end(); - }); - }) - .catch((error) => { - console.log(error.stack); - t.fail(error); - }); -}); - -function npm(method, packages, dir) { - if (!Array.isArray(packages)) { - packages = [packages]; - } - return new Promise(function(resolve, reject) { - const cmd = 'npm ' + method + ' ' + packages.join(' '); - debug(cmd); - exec( - cmd, - { - cwd: dir, - }, - function(error, stdout, stderr) { - if (error) { - return reject(error); - } - - if (stderr) { - return reject(new Error(stderr.trim())); - } - - resolve(stdout.trim()); - }, - ); - }); -} diff --git a/test/protect-patch-skip.test.ts b/test/protect-patch-skip.test.ts new file mode 100644 index 00000000000..ba5aa13965a --- /dev/null +++ b/test/protect-patch-skip.test.ts @@ -0,0 +1,60 @@ +import * as path from 'path'; +import { exec } from 'child_process'; + +import { test } from 'tap'; +import * as protect from '../src/lib/protect'; +import { loadJson } from './utils'; + +const vulns = loadJson(__dirname + '/fixtures/lodash@4.17.11-vuln.json') + .vulnerabilities; + +test('patch is correctly applied', async (t) => { + const name = 'lodash'; + const version = '4.17.13'; + + const dir = path.resolve(__dirname, 'fixtures/protect'); + process.chdir(dir); + try { + await npm('install', name + '@' + version, dir); + await protect.patch(vulns, true).then(() => { + t.pass('patch resolved'); + }); + } catch (error) { + t.fail('Should have passed: ' + error); + } + + try { + await npm('uninstall', name, dir).then(() => { + t.pass('packages cleaned up'); + t.end(); + }); + } catch (error) { + t.fail('Should have passed: ' + error); + } +}); + +function npm(method, packages, dir) { + if (!Array.isArray(packages)) { + packages = [packages]; + } + return new Promise((resolve, reject) => { + const cmd = 'npm ' + method + ' ' + packages.join(' '); + exec( + cmd, + { + cwd: dir, + }, + (error, stdout, stderr) => { + if (error) { + return reject(error); + } + + if (stderr) { + return reject(new Error(stderr.trim())); + } + + resolve(stdout.trim()); + }, + ); + }); +} diff --git a/test/protect-patch.test.js b/test/protect-patch.test.js deleted file mode 100644 index 6f93247c86f..00000000000 --- a/test/protect-patch.test.js +++ /dev/null @@ -1,62 +0,0 @@ -'use strict'; -var debug = require('debug')('snyk'); -var protect = require('../src/lib/protect'); -var path = require('path'); -var test = require('tape'); -var vulns = require('./fixtures/semver-vuln.json').vulnerabilities; -var exec = require('child_process').exec; - -test('patch is correctly applied', function(t) { - var name = 'semver'; - var version = '2'; - - var dir = path.resolve(__dirname, 'fixtures/protect'); - npm('install', name + '@' + version, dir) - .then(function() { - debug('installing to %s', dir); - return protect.patch(vulns, true, dir).then(function() { - t.pass('patch resolved'); - }); - }) - .catch(function(error) { - console.log(error.stack); - t.fail(error); - }) - .then(function() { - return npm('uninstall', name, dir).then(function() { - t.pass('packages cleaned up'); - t.end(); - }); - }) - .catch(function(error) { - console.log(error.stack); - t.fail(error); - }); -}); - -function npm(method, packages, dir) { - if (!Array.isArray(packages)) { - packages = [packages]; - } - return new Promise(function(resolve, reject) { - var cmd = 'npm ' + method + ' ' + packages.join(' '); - debug(cmd); - exec( - cmd, - { - cwd: dir, - }, - function(error, stdout, stderr) { - if (error) { - return reject(error); - } - - if (stderr) { - return reject(new Error(stderr.trim())); - } - - resolve(stdout.trim()); - }, - ); - }); -} diff --git a/test/protect-patch.test.ts b/test/protect-patch.test.ts new file mode 100644 index 00000000000..bcb274106dd --- /dev/null +++ b/test/protect-patch.test.ts @@ -0,0 +1,59 @@ +import * as protect from '../src/lib/protect'; +import * as path from 'path'; +import { test } from 'tap'; +import * as fs from 'fs'; +import { exec } from 'child_process'; + +const vulns = JSON.parse( + fs.readFileSync(__dirname + '/fixtures/semver-vuln.json', 'utf8'), +).vulnerabilities; + +test('patch is correctly applied', async (t) => { + const name = 'semver'; + const version = '2'; + const dir = path.resolve(__dirname, 'fixtures/protect'); + + try { + process.chdir(dir); + + await npm('install', name + '@' + version, dir); + + await protect.patch(vulns, true); + t.pass('patch resolved'); + } catch (error) { + t.fail('Should have passed' + error); + } + + try { + await npm('uninstall', name, dir); + t.pass('packages cleaned up'); + } catch (error) { + t.fail('Should have passed' + error); + } +}); + +function npm(method, packages, dir) { + if (!Array.isArray(packages)) { + packages = [packages]; + } + return new Promise((resolve, reject) => { + const cmd = 'npm ' + method + ' ' + packages.join(' '); + exec( + cmd, + { + cwd: dir, + }, + (error, stdout, stderr) => { + if (error) { + return reject(error); + } + + if (stderr) { + return reject(new Error(stderr.trim())); + } + + resolve(stdout.trim()); + }, + ); + }); +} diff --git a/test/protect-semver-patch.test.ts b/test/protect-semver-patch.test.ts index b9193f0c2ea..4e8848e0d07 100644 --- a/test/protect-semver-patch.test.ts +++ b/test/protect-semver-patch.test.ts @@ -2,7 +2,7 @@ import * as debugModule from 'debug'; const debug = debugModule('snyk'); import * as protect from '../src/lib/protect'; import * as path from 'path'; -import * as test from 'tape'; +import { test } from 'tap'; // tslint:disable-next-line: no-var-requires const vulns = require('./fixtures/hoek@4.2.0-vuln.json').vulnerabilities; // tslint:disable-next-line: no-var-requires diff --git a/test/scoped-module.test.js b/test/scoped-module.test.js index 974ea1d52df..5a14affafdc 100644 --- a/test/scoped-module.test.js +++ b/test/scoped-module.test.js @@ -1,4 +1,4 @@ -var test = require('tape'); +var test = require('tap').test; var path = require('path'); var snyk = require('../src/lib'); diff --git a/test/user-config.test.js b/test/user-config.test.js index c340fc44b77..eea226d4f8d 100644 --- a/test/user-config.test.js +++ b/test/user-config.test.js @@ -1,4 +1,4 @@ -var test = require('tape'); +var test = require('tap').test; var config = require('../src/cli/commands/config'); test('can unset config values', function(t) { diff --git a/test/utils.ts b/test/utils.ts index 5fa41152cc1..e21090ffa56 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -1,6 +1,6 @@ import { tmpdir } from 'os'; import { join } from 'path'; -import { mkdir } from 'fs'; +import { mkdir, readFileSync } from 'fs'; export function silenceLog() { const old = console.log; @@ -46,3 +46,7 @@ export async function makeTmpDirectory(): Promise { ); return makeDirectory(dirname); } + +export function loadJson(filename: string) { + return JSON.parse(readFileSync(filename, 'utf-8')); +} diff --git a/test/version-gleaning.test.js b/test/version-gleaning.test.ts similarity index 51% rename from test/version-gleaning.test.js rename to test/version-gleaning.test.ts index e8b4dfdb951..3ebf38ac4a0 100644 --- a/test/version-gleaning.test.js +++ b/test/version-gleaning.test.ts @@ -1,16 +1,13 @@ -'use strict'; -var test = require('tape'); -var path = require('path'); -var snyk = require('../src/lib'); +import * as path from 'path'; +import { test } from 'tap'; +import * as snyk from '../src/lib'; // this test checks we're collecting the *local* installed version *not* the // version that's stated in the package. i.e. handlebars@~2.0.0-alpha.2 is not // valid, because it's a range (a suggestion as to which version to use) but // handlebars@2.0.0-alpha.2 *is* right -test('version gleaned is installed version, not package dep version', function(t) { - t.plan(1); - var dir = path.resolve(__dirname, 'fixtures', 'hbs-demo'); - snyk.modules(dir).then(function(modules) { - t.equal(modules.dependencies.handlebars.full, 'handlebars@2.0.0'); - }); +test('version gleaned is installed version, not package dep version', async (t) => { + const dir = path.resolve(__dirname, 'fixtures', 'hbs-demo'); + const modules = await snyk.modules(dir); + t.equal(modules.dependencies.handlebars.full, 'handlebars@2.0.0'); });