Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Tap Test improvements/fixes #4145

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions test/tap/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import * as isAuthed from '../../src/cli/commands/auth/is-authed';
import * as errors from '../../src/lib/errors/legacy-errors';
import { fakeServer } from '../acceptance/fake-server';

const port = process.env.PORT || process.env.SNYK_PORT || '12345';
const port =
process.env.PORT ||
process.env.SNYK_PORT ||
(12345 + +process.env.TAP_CHILD_ID!).toString();

const apiKey = '123456789';
const BASE_API = '/api/v1';
Expand Down Expand Up @@ -66,15 +69,17 @@ test('auth shows an appropriate error message when a request fails with a user m
}
});

test('teardown', (t) => {
test('teardown', async (t) => {
t.plan(2);

delete process.env.SNYK_API;
delete process.env.SNYK_HOST;
delete process.env.SNYK_PORT;
t.notOk(process.env.SNYK_PORT, 'fake env values cleared');

server.close(() => {
t.pass('server shutdown');

await new Promise<void>((resolve) => {
server.close(resolve);
});
t.pass('server shutdown');
});
7 changes: 6 additions & 1 deletion test/tap/cli-fail-on-docker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import { fakeServer } from '../acceptance/fake-server';
import * as sinon from 'sinon';
import * as snyk from '../../src/lib';
import { getWorkspaceJSON } from '../acceptance/workspace-helper';
import { makeTmpDirectory } from '../utils';

const { test, only } = tap;
(tap as any).runOnly = false; // <- for debug. set to true, and replace a test to only(..)

const port = (process.env.PORT = process.env.SNYK_PORT = '12345');
const port =
process.env.PORT ||
process.env.SNYK_PORT ||
(12345 + +process.env.TAP_CHILD_ID!).toString();
const BASE_API = '/api/v1';
process.env.SNYK_API = 'http://localhost:' + port + BASE_API;
process.env.SNYK_HOST = 'http://localhost:' + port;
Expand Down Expand Up @@ -49,6 +53,7 @@ const dockerNoFixableVulns = getWorkspaceJSON(
);

before('setup', async (t) => {
process.env.XDG_CONFIG_HOME = await makeTmpDirectory();
t.plan(3);
let key = await cli.config('get', 'api');
oldkey = key;
Expand Down
7 changes: 6 additions & 1 deletion test/tap/cli-fail-on-pinnable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import {
getWorkspaceJSON,
chdirWorkspaces,
} from '../acceptance/workspace-helper';
import { makeTmpDirectory } from '../utils';

const { test, only } = tap;
(tap as any).runOnly = false; // <- for debug. set to true, and replace a test to only(..)

const port = (process.env.PORT = process.env.SNYK_PORT = '12345');
const port =
process.env.PORT ||
process.env.SNYK_PORT ||
(12345 + +process.env.TAP_CHILD_ID!).toString();
const BASE_API = '/api/v1';
process.env.SNYK_API = 'http://localhost:' + port + BASE_API;
process.env.SNYK_HOST = 'http://localhost:' + port;
Expand All @@ -33,6 +37,7 @@ const pinnableVulnsResult = getWorkspaceJSON(
const pinnableVulns = getWorkspaceJSON('fail-on', 'pinnable', 'vulns.json');

before('setup', async (t) => {
process.env.XDG_CONFIG_HOME = await makeTmpDirectory();
t.plan(3);
let key = await cli.config('get', 'api');
oldkey = key;
Expand Down
11 changes: 8 additions & 3 deletions test/tap/cli-monitor.acceptance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
chdirWorkspaces,
getWorkspaceJSON,
} from '../acceptance/workspace-helper';
import { makeTmpDirectory } from '../utils';
const isEmpty = require('lodash.isempty');
const isObject = require('lodash.isobject');
const get = require('lodash.get');
Expand All @@ -21,7 +22,10 @@ import { AllProjectsTests } from './cli-monitor/cli-monitor.all-projects.spec';
const { test, only, beforeEach } = tap;
(tap as any).runOnly = false; // <- for debug. set to true, and replace a test to only(..)

const port = (process.env.PORT = process.env.SNYK_PORT = '12345');
const port =
process.env.PORT ||
process.env.SNYK_PORT ||
(12345 + +process.env.TAP_CHILD_ID!).toString();
const BASE_API = '/api/v1';
process.env.SNYK_API = 'http://localhost:' + port + BASE_API;
process.env.SNYK_HOST = 'http://localhost:' + port;
Expand Down Expand Up @@ -58,6 +62,7 @@ const isWindows =

if (!isWindows) {
before('setup', async (t) => {
process.env.XDG_CONFIG_HOME = await makeTmpDirectory();
versionNumber = await getVersion();

t.plan(3);
Expand Down Expand Up @@ -1589,12 +1594,12 @@ if (!isWindows) {
// assert results contain monitor urls
t.match(
results[0].manageUrl,
'http://localhost:12345/manage',
`http://localhost:${port}/manage`,
'first monitor url is present',
);
t.match(
results[1].manageUrl,
'http://localhost:12345/manage',
`http://localhost:${port}/manage`,
'second monitor url is present',
);

Expand Down
7 changes: 6 additions & 1 deletion test/tap/cli-test.acceptance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as cli from '../../src/cli/commands';
import { fakeServer } from '../acceptance/fake-server';
import { getVersion } from '../../src/lib/version';
import { chdirWorkspaces } from '../acceptance/workspace-helper';
import { makeTmpDirectory } from '../utils';

export interface AcceptanceTests {
language: string;
Expand Down Expand Up @@ -49,7 +50,10 @@ const languageTests: AcceptanceTests[] = [
const { test, only } = tap;
(tap as any).runOnly = false; // <- for debug. set to true, and replace a test to only(..)

const port = (process.env.PORT = process.env.SNYK_PORT = '12345');
const port =
process.env.PORT ||
process.env.SNYK_PORT ||
(12345 + +process.env.TAP_CHILD_ID!).toString();
const BASE_API = '/api/v1';
process.env.SNYK_API = 'http://localhost:' + port + BASE_API;
process.env.SNYK_HOST = 'http://localhost:' + port;
Expand All @@ -76,6 +80,7 @@ import { snykHttpClient } from '../../src/lib/request/snyk-http-client';
*/

before('setup', async (t) => {
process.env.XDG_CONFIG_HOME = await makeTmpDirectory();
versionNumber = await getVersion();

t.plan(3);
Expand Down
8 changes: 6 additions & 2 deletions test/tap/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ type Policy = {
[id: string]: Ignore[];
};

const port = process.env.PORT || process.env.SNYK_PORT || '12345';
const port =
process.env.PORT ||
process.env.SNYK_PORT ||
(12345 + +process.env.TAP_CHILD_ID!).toString();

const apiKey = '123456789';
let oldKey;
Expand All @@ -49,6 +52,7 @@ const before = test;
const after = test;

before('setup', async (t) => {
process.env.XDG_CONFIG_HOME = await makeTmpDirectory();
const key = await cli.config('get', 'api');
oldKey = key; // just in case
t.pass('existing user config captured');
Expand Down Expand Up @@ -129,7 +133,7 @@ test('auth with no args', async (t) => {
t.ok(open.calledOnce, 'called open once');
t.match(
open.firstCall.args[0],
'http://localhost:12345/login?token=',
`http://localhost:${port}login?token=`,
'opens login with token param',
);
ciStub.restore();
Expand Down
17 changes: 9 additions & 8 deletions test/tap/display-test-results.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as tap from 'tap';
import * as sinon from 'sinon';
import * as fs from 'fs';
import stripAnsi from 'strip-ansi';

import config from '../../src/lib/config';
import * as cli from '../../src/cli/commands';
Expand All @@ -24,7 +25,7 @@ test('`test ruby-app` remediation displayed', async (t) => {
try {
await cli.test('ruby-app');
} catch (error) {
const res = error.message;
const res = stripAnsi(error.message);
t.match(
res,
'Upgrade [email protected] to [email protected] to fix',
Expand Down Expand Up @@ -61,7 +62,7 @@ test('`test ruby-app` legal instructions displayed', async (t) => {
try {
await cli.test('ruby-app');
} catch (error) {
const res = error.message;
const res = stripAnsi(error.message);
t.match(res, 'I am legal license instruction');
}

Expand All @@ -83,7 +84,7 @@ test('`test pip-app-license-issue` legal instructions displayed (legacy formatte
try {
await cli.test('pip-app-license-issue');
} catch (error) {
const res = error.message;
const res = stripAnsi(error.message);
t.match(res, 'I am legal license instruction');
}

Expand All @@ -106,7 +107,7 @@ test('`test npm-package-with-severity-override` show original severity upgrade',
try {
await cli.test('npm-package-with-severity-override');
} catch (error) {
const { message } = error;
const message = stripAnsi(error.message);
t.match(
message,
`[Low Severity (originally Medium)][${config.PUBLIC_VULN_DB_URL}/vuln/npm:node-uuid:20160328]`,
Expand All @@ -132,7 +133,7 @@ test('`test npm-package-with-severity-override` show original severity patches',
try {
await cli.test('npm-package-with-severity-override');
} catch (error) {
const { message } = error;
const message = stripAnsi(error.message);
t.match(message, 'Patch available for [email protected]');
t.match(
message,
Expand All @@ -159,7 +160,7 @@ test('`test npm-package-with-severity-override` show original severity no remedi
try {
await cli.test('npm-package-with-severity-override');
} catch (error) {
const { message } = error;
const message = stripAnsi(error.message);
t.match(
message,
`Low severity (originally Medium) vulnerability found in node-uuid`,
Expand All @@ -185,7 +186,7 @@ test('`test npm-package-with-severity-override` show original severity unresolve
try {
await cli.test('npm-package-with-severity-override');
} catch (error) {
const { message } = error;
const message = stripAnsi(error.message);
t.match(
message,
`Malicious Package [Low Severity (originally Medium)][${config.PUBLIC_VULN_DB_URL}/vuln/npm:node-uuid:20160328`,
Expand All @@ -211,7 +212,7 @@ test('`test npm-package-with-severity-override` dont show original severity if i
try {
await cli.test('npm-package-with-severity-override');
} catch (error) {
const { message } = error;
const message = stripAnsi(error.message);
t.match(
message,
`[Low Severity][${config.PUBLIC_VULN_DB_URL}/vuln/npm:node-uuid:20160328]`,
Expand Down
7 changes: 6 additions & 1 deletion test/tap/docker-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import * as tap from 'tap';
import * as cli from '../../src/cli/commands';
import { fakeServer } from '../acceptance/fake-server';
import * as sinon from 'sinon';
import { makeTmpDirectory } from '../utils';

const { test } = tap;

const port = (process.env.PORT = process.env.SNYK_PORT = '12345');
const port =
process.env.PORT ||
process.env.SNYK_PORT ||
(12345 + +process.env.TAP_CHILD_ID!).toString();
const BASE_API = '/api/v1';
process.env.SNYK_API = 'http://localhost:' + port + BASE_API;
process.env.SNYK_HOST = 'http://localhost:' + port;
Expand All @@ -21,6 +25,7 @@ import * as plugins from '../../src/lib/ecosystems/plugins';
import { getFixturePath } from '../jest/util/getFixturePath';

test('setup', async (t) => {
process.env.XDG_CONFIG_HOME = await makeTmpDirectory();
t.plan(3);

let key = await cli.config('get', 'api');
Expand Down
7 changes: 6 additions & 1 deletion test/tap/monitor-target.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import * as sinon from 'sinon';
import * as cli from '../../src/cli/commands';
import subProcess = require('../../src/lib/sub-process');
import { fakeServer } from '../acceptance/fake-server';
import { makeTmpDirectory } from '../utils';

const apiKey = '123456789';

const port = (process.env.PORT = process.env.SNYK_PORT = '12345');
const port =
process.env.PORT ||
process.env.SNYK_PORT ||
(12345 + +process.env.TAP_CHILD_ID!).toString();
const BASE_API = '/api/v1';
process.env.SNYK_API = 'http://localhost:' + port + BASE_API;
process.env.SNYK_HOST = 'http://localhost:' + port;
Expand All @@ -21,6 +25,7 @@ let oldendpoint;
const server = fakeServer(BASE_API, apiKey);

test('setup', async (t) => {
process.env.XDG_CONFIG_HOME = await makeTmpDirectory();
let key = await cli.config('get', 'api');
oldkey = key;
t.pass('existing user config captured');
Expand Down
7 changes: 6 additions & 1 deletion test/tap/remote-package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import { test } from 'tap';
import * as ciChecker from '../../src/lib/is-ci';
import * as sinon from 'sinon';
import { fakeServer } from '../acceptance/fake-server';
import { makeTmpDirectory } from '../utils';

const port = process.env.PORT || process.env.SNYK_PORT || '12345';
const port =
process.env.PORT ||
process.env.SNYK_PORT ||
(12345 + +process.env.TAP_CHILD_ID!).toString();

const apiKey = '123456789';
let oldkey;
Expand All @@ -24,6 +28,7 @@ const before = test;
const after = test;

before('setup', async (t) => {
process.env.XDG_CONFIG_HOME = await makeTmpDirectory();
let key = await cli.config('get', 'api');
oldkey = key; // just in case
t.pass('existing user config captured');
Expand Down
7 changes: 6 additions & 1 deletion test/tap/run-test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import * as tap from 'tap';
import { only, test } from 'tap';
import { fakeServer } from '../acceptance/fake-server';
import * as cli from '../../src/cli/commands';
import { makeTmpDirectory } from '../utils';

const port = (process.env.PORT = process.env.SNYK_PORT = '12345');
const port =
process.env.PORT ||
process.env.SNYK_PORT ||
(12345 + +process.env.TAP_CHILD_ID!).toString();
const BASE_API = '/api/v1';
process.env.SNYK_API = 'http://localhost:' + port + BASE_API;
process.env.SNYK_HOST = 'http://localhost:' + port;
Expand All @@ -23,6 +27,7 @@ import { getFixturePath } from '../jest/util/getFixturePath';
import { getWorkspacePath } from '../jest/util/getWorkspacePath';

before('setup', async (t) => {
process.env.XDG_CONFIG_HOME = await makeTmpDirectory();
t.plan(3);
let key = await cli.config('get', 'api');
oldkey = key;
Expand Down
7 changes: 6 additions & 1 deletion test/tap/severity-threshold.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { test } from 'tap';
import cli = require('../../src/cli/commands');
import { makeTmpDirectory } from '../utils';

import { fakeServer } from '../acceptance/fake-server';

const apiKey = '123456789';

const port = (process.env.PORT = process.env.SNYK_PORT = '12345');
const port =
process.env.PORT ||
process.env.SNYK_PORT ||
(12345 + +process.env.TAP_CHILD_ID!).toString();
const BASE_API = '/api/v1';
process.env.SNYK_API = 'http://localhost:' + port + BASE_API;
process.env.SNYK_HOST = 'http://localhost:' + port;
Expand All @@ -15,6 +19,7 @@ let oldendpoint;
const server = fakeServer(BASE_API, apiKey);

test('setup', async (t) => {
process.env.XDG_CONFIG_HOME = await makeTmpDirectory();
let key = await cli.config('get', 'api');
oldkey = key;
t.pass('existing user config captured');
Expand Down