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

Test/use mock server #1667

Merged
merged 1 commit into from
Mar 9, 2021
Merged
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
67 changes: 67 additions & 0 deletions test/cli-json-file-output.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { exec } from 'child_process';
import { sep, join } from 'path';
import { readFileSync, unlinkSync, rmdirSync, mkdirSync, existsSync } from 'fs';
import { v4 as uuidv4 } from 'uuid';
import { fakeServer } from './acceptance/fake-server';
import cli = require('../src/cli/commands');

const osName = require('os-name');

Expand All @@ -12,18 +14,66 @@ const isWindows =
.toLowerCase()
.indexOf('windows') === 0;
describe('test --json-file-output ', () => {
let oldkey;
let oldendpoint;
const apiKey = '123456789';
const port = process.env.PORT || process.env.SNYK_PORT || '12345';

const BASE_API = '/api/v1';
const SNYK_API = 'http://localhost:' + port + BASE_API;
const SNYK_HOST = 'http://localhost:' + port;

const server = fakeServer(BASE_API, apiKey);

const noVulnsProjectPath = join(
__dirname,
'/acceptance',
'workspaces',
'no-vulns',
);
beforeAll(async () => {
let key = await cli.config('get', 'api');
oldkey = key;

key = await cli.config('get', 'endpoint');
oldendpoint = key;

await new Promise((resolve) => {
server.listen(port, resolve);
});
});

afterAll(async () => {
delete process.env.SNYK_API;
delete process.env.SNYK_HOST;
delete process.env.SNYK_PORT;

await server.close();
let key = 'set';
let value = 'api=' + oldkey;
if (!oldkey) {
key = 'unset';
value = 'api';
}
await cli.config(key, value);
if (oldendpoint) {
await cli.config('endpoint', oldendpoint);
}
});
it(
'`can save JSON output to file while sending human readable output to stdout`',
(done) => {
const jsonOutputFilename = `${uuidv4()}.json`;
exec(
`node ${main} test ${noVulnsProjectPath} --json-file-output=${jsonOutputFilename}`,
{
env: {
PATH: process.env.PATH,
SNYK_TOKEN: apiKey,
SNYK_API,
SNYK_HOST,
},
},
(err, stdout) => {
if (err) {
throw err;
Expand All @@ -48,6 +98,14 @@ describe('test --json-file-output ', () => {
const jsonOutputFilename = `${uuidv4()}.json`;
return exec(
`node ${main} test ${noVulnsProjectPath} --json --json-file-output=${jsonOutputFilename}`,
{
env: {
PATH: process.env.PATH,
SNYK_TOKEN: apiKey,
SNYK_API,
SNYK_HOST,
},
},
async (err, stdout) => {
if (err) {
throw err;
Expand All @@ -67,6 +125,7 @@ describe('test --json-file-output ', () => {

it(
'`test --json-file-output can handle a relative path`',

(done) => {
// if 'test-output' doesn't exist, created it
if (!existsSync('test-output')) {
Expand All @@ -78,6 +137,14 @@ describe('test --json-file-output ', () => {

exec(
`node ${main} test ${noVulnsProjectPath} --json --json-file-output=${outputPath}`,
{
env: {
PATH: process.env.PATH,
SNYK_TOKEN: apiKey,
SNYK_API,
SNYK_HOST,
},
},
async (err, stdout) => {
if (err) {
throw err;
Expand Down