Skip to content

Commit

Permalink
chore: use local api key, host for test
Browse files Browse the repository at this point in the history
  • Loading branch information
lili2311 authored and JackuB committed Mar 2, 2021
1 parent 1228b55 commit 49be478
Showing 1 changed file with 67 additions and 0 deletions.
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

0 comments on commit 49be478

Please sign in to comment.