Skip to content

Commit

Permalink
fix: reinstate missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Yegupov committed Jul 4, 2019
1 parent 9551bdb commit c28d85e
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 33 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
"find-circular": "npm run build && madge --circular ./dist",
"tslint": "tslint --project tsconfig.json --format stylish --exclude **/src/**/*.js",
"prepare": "npm run build",
"tap": "tap test/*.test.* -Rspec --timeout=300 --node-arg=-r --node-arg=ts-node/register",
"tap": "tap test/*.test.* test/acceptance/*.test.* test/system/*.test.* -Rspec --timeout=300 --node-arg=-r --node-arg=ts-node/register",
"test": "npm run test-common && npm run tap",
"test-common": "npm run check-tests && npm run lint && node --require ts-node/register src/cli test --org=snyk",
"test-common": "npm run check-tests && npm run build && npm run lint && node --require ts-node/register src/cli test --org=snyk",
"lint": "npm run eslint && npm run tslint",
"check-tests": "! grep 'test\\.only' test/*.test.js -n",
"snyk-auth": "node --require ts-node/register src/cli auth $SNYK_API_KEY",
Expand Down
6 changes: 6 additions & 0 deletions src/lib/sln/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as path from 'path';
import * as detect from '../detect';
import {NoSupportedManifestsFoundError} from '../errors/no-supported-manifests-found';
import * as Debug from 'debug';
import { FileFlagBadInputError } from '../errors';

const debug = Debug('snyk');

Expand Down Expand Up @@ -32,6 +33,11 @@ export const parsePathsFromSln = (slnFile) => {
};

export const updateArgs = (args) => {

if (!args.options.file || typeof args.options.file !== 'string') {
throw new FileFlagBadInputError();
}

// save the path if --file=path/file.sln
const slnFilePath = path.dirname(args.options.file);

Expand Down
18 changes: 12 additions & 6 deletions test/acceptance/cli-args.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import {test} from 'tap';
import {exec} from 'child_process';
import { test } from 'tap';
import { exec } from 'child_process';
import { sep } from 'path';

test('snyk test command should fail when --file is not specified correctly', (t) => {
const main = './dist/cli/index.js'.replace(/\//g, sep);

// TODO(kyegupov): make these work in Windows
if (sep === '/') {
test('snyk test command should fail when --file is not specified correctly', (t) => {
t.plan(1);

exec('node ./dist/cli/index.js test --file package-lock.json', (_, stdout) => {
t.equal(stdout.trim(), 'Empty --file argument. Did you mean --file=path/to/file ?', 'correct error output');
exec(`node ${main} test --file package-lock.json`, (_, stdout) => {
t.equal(stdout.trim(), 'Empty --file argument. Did you mean --file=path/to/file ?', 'correct error output');
});
});
});
}
26 changes: 16 additions & 10 deletions test/acceptance/protect.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import {test} from 'tap';
import {exec} from 'child_process';
import * as userConfig from '../../src/lib/user-config';
import { sep } from 'path';

test('`protect` should not fail for unauthorized users', (t) => {
t.plan(1);
const main = './dist/cli/index.js'.replace(/\//g, sep);

const apiUserConfig = userConfig.get('api');
// temporally remove api param in userConfig to test for unauthenticated users
userConfig.delete('api');
// TODO(kyegupov): make these work in Windows
if (sep === '/') {
test('`protect` should not fail for unauthorized users', (t) => {
t.plan(1);

exec('node ./dist/cli/index.js protect', (_, stdout) => {
t.equal(stdout.trim(), 'Successfully applied Snyk patches', 'correct output for unauthenticated user');
const apiUserConfig = userConfig.get('api');
// temporally remove api param in userConfig to test for unauthenticated users
userConfig.delete('api');

// Restore api param
userConfig.set('api', apiUserConfig);
exec(`node ${main} protect`, (_, stdout) => {
t.equal(stdout.trim(), 'Successfully applied Snyk patches', 'correct output for unauthenticated user');

// Restore api param
userConfig.set('api', apiUserConfig);
});
});
});
}
5 changes: 3 additions & 2 deletions test/acceptance/run-test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const after = tap.runOnly ? only : test;

// Import has to happen after setting SNYK_API
import runTest = require('../../src/lib/snyk-test/run-test');
import { TestOptions, Options } from '../../src/lib/types';

// @later: remove this config stuff.
// Was copied straight from ../src/cli-server.js
Expand All @@ -43,7 +44,7 @@ test('runTest annotates results with remediation data when using node_modules',
server.setNextResponse(vulns);

let result = await runTest('npm', 'test/acceptance/workspaces/npm-package-with-git-url',
{packageManager: 'npm'});
{ packageManager: 'npm' } as Options & TestOptions);
t.ok(result[0].vulnerabilities[0].parentDepType, 'has parentDepType');
});

Expand All @@ -52,7 +53,7 @@ test('runTest annotates results with remediation data when traverseNodeModules',
server.setNextResponse(vulns);

let result = await runTest('npm', 'test/acceptance/workspaces/npm-package',
{packageManager: 'npm', traverseNodeModules: true});
{packageManager: 'npm', traverseNodeModules: true} as Options & TestOptions);
t.ok(result[0].vulnerabilities[0].parentDepType, 'has parentDepType');
});

Expand Down
8 changes: 4 additions & 4 deletions test/acceptance/sln-app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import * as sln from '../../src/lib/sln';
test('parseFoldersFromSln when passed an existent filename', (t) => {
const slnFile = 'test/acceptance/workspaces/sln-example-app/mySolution.sln';
const expected = JSON.stringify([
'dotnet2_new_mvc_project/new_mvc_project.csproj',
'WebApplication2/WebApplication2.csproj',
'dotnet2_new_mvc_project' + path.sep + 'new_mvc_project.csproj',
'WebApplication2' + path.sep + 'WebApplication2.csproj',
]);
const actual = JSON.stringify(sln.parsePathsFromSln(slnFile));
t.equal(actual, expected, 'should parse & extract csproj folders');
Expand Down Expand Up @@ -71,7 +71,7 @@ test('sln.updateArgs for sln with no relevant projects', (t) => {
sln.updateArgs(args);
t.fail('should have exploded');
} catch (e) {
t.equal(e.message, 'No relevant projects found in Solution',
t.match(e.message, /Could not detect supported target files in/,
'Error thrown on solution with no valid projects');
}
t.end();
Expand All @@ -85,7 +85,7 @@ test('sln.updateArgs for sln without --file', (t) => {
sln.updateArgs(args);
t.fail('should have exploded');
} catch (e) {
t.equal(e.message, 'No relevant projects found in Solution',
t.match(e.message, /Empty --file argument/,
'Error thrown on solution with no valid projects');
}
t.end();
Expand Down
1 change: 0 additions & 1 deletion test/cli.acceptance.test.js

This file was deleted.

8 changes: 4 additions & 4 deletions test/system/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {test} from 'tap';
import * as testUtils from '../utils';
import * as ciChecker from '../../src/lib/is-ci';
import * as sinon from 'sinon';
import * as proxyquire from 'proxyquire';
import {parse} from 'url';
import proxyquire = require('proxyquire');
import { parse, Url } from 'url';
import * as policy from 'snyk-policy';
import stripAnsi from 'strip-ansi';
const port = process.env.PORT || process.env.SNYK_PORT || '12345';
Expand Down Expand Up @@ -213,14 +213,14 @@ test('auth via invalid key', (t) => {
});

test('auth via github', (t) => {
let tokenRequest = null;
let tokenRequest: Url & {token?: string} | null = null;

const openSpy = sinon.spy((url) => {
tokenRequest = parse(url);
tokenRequest.token = tokenRequest.query.split('=').pop();
});

const auth = proxyquire('../src/cli/commands/auth', {
const auth = proxyquire('../../src/cli/commands/auth', {
open: openSpy,
});
sinon.stub(ciChecker, 'isCI').returns(false);
Expand Down
16 changes: 12 additions & 4 deletions test/system/remote-package.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as _ from 'lodash';
import {test} from 'tap';
import * as ciChecker from '../../src/lib/is-ci';
import * as sinon from 'sinon';

const port = process.env.PORT || process.env.SNYK_PORT || '12345';

Expand Down Expand Up @@ -125,11 +127,17 @@ test('test for existing remote package with dev-deps only with --dev', async (t)

test('test for existing remote package with dev-deps only', async (t) => {
try {
const res = await cli.test('lodash@4.17.11');
const ciCheckerStub = sinon.stub(ciChecker, 'isCI')
ciCheckerStub.returns(false);
t.teardown(ciCheckerStub.restore);

const res = await cli.test('lodash@4.17.11', {dev: false});
const lastLine = res.trim().split('\n').pop();
t.deepEqual(lastLine, 'Tip: Snyk only tests production dependencies by default ' +
'(which this project had none). Try re-running with the `--dev` flag.',
'tip text as expected');

t.deepEqual(
lastLine,
'Tip: Snyk only tests production dependencies by default. You can try re-running with the `--dev` flag.',
'tip text as expected');
} catch (error) {
t.fail('should not throw, instead received error: ' + error);
}
Expand Down

0 comments on commit c28d85e

Please sign in to comment.