Skip to content

Commit

Permalink
Merge pull request #2156 from snyk/test/json-file-output-cleanup
Browse files Browse the repository at this point in the history
test: use async/await instead of callbacks
  • Loading branch information
Jahed Ahmed committed Aug 12, 2021
2 parents 271a052 + 1fec66e commit 2f32793
Showing 1 changed file with 48 additions and 106 deletions.
154 changes: 48 additions & 106 deletions test/jest/acceptance/cli-json-file-output.spec.ts
@@ -1,120 +1,62 @@
import { exec } from 'child_process';
import { sep, join } from 'path';
import { readFileSync, unlinkSync } from 'fs';
import { v4 as uuidv4 } from 'uuid';
import { fakeServer } from '../../acceptance/fake-server';
import cli = require('../../../src/cli/commands');
import { createProjectFromWorkspace } from '../util/createProject';
import { runSnykCLI } from '../util/runSnykCLI';

const main = './bin/snyk'.replace(/\//g, sep);
const testTimeout = 50000;
jest.setTimeout(1000 * 60);

describe('test --json-file-output ', () => {
let oldkey;
let oldendpoint;
const apiKey = '123456789';
const port = process.env.PORT || process.env.SNYK_PORT || '12345';
let server: ReturnType<typeof fakeServer>;
let env: Record<string, string>;

beforeAll((done) => {
const apiPath = '/api/v1';
const apiPort = process.env.PORT || process.env.SNYK_PORT || '12345';
env = {
...process.env,
SNYK_API: 'http://localhost:' + apiPort + apiPath,
SNYK_TOKEN: '123456789',
};

server = fakeServer(apiPath, env.SNYK_TOKEN);
server.listen(apiPort, () => done());
});

const BASE_API = '/api/v1';
const SNYK_API = 'http://localhost:' + port + BASE_API;
const SNYK_HOST = 'http://localhost:' + port;
afterAll((done) => {
server.close(() => done());
});

const server = fakeServer(BASE_API, apiKey);
it('can save JSON output to file while sending human readable output to stdout', async () => {
const project = await createProjectFromWorkspace('no-vulns');
const outputPath = 'json-file-output.json';

const noVulnsProjectPath = join(
__dirname,
'../../acceptance',
'workspaces',
'no-vulns',
);
beforeAll(async () => {
let key = await cli.config('get', 'api');
oldkey = key;
const { code, stdout } = await runSnykCLI(
`test --json-file-output=${outputPath}`,
{
cwd: project.path(),
env,
},
);

key = await cli.config('get', 'endpoint');
oldendpoint = key;
expect(code).toEqual(0);
expect(stdout).toMatch('Organization:');

await new Promise((resolve) => {
server.listen(port, resolve);
});
const jsonObj = JSON.parse(await project.read(outputPath));
expect(jsonObj).toMatchObject({ ok: true });
});

afterAll(async () => {
delete process.env.SNYK_API;
delete process.env.SNYK_HOST;
delete process.env.SNYK_PORT;
it('test --json-file-output produces same JSON output as normal JSON output to stdout', async () => {
const project = await createProjectFromWorkspace('no-vulns');
const outputPath = 'json-file-output.json';

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;
}
const { code, stdout } = await runSnykCLI(
`test --json --json-file-output=${outputPath}`,
{
cwd: project.path(),
env,
},
);

expect(stdout).toMatch('Organization:');
const outputFileContents = readFileSync(jsonOutputFilename, 'utf-8');
unlinkSync(`./${jsonOutputFilename}`);
const jsonObj = JSON.parse(outputFileContents);
const okValue = jsonObj.ok as boolean;
expect(okValue).toBeTruthy();
done();
},
);
},
testTimeout,
);

it(
'`test --json-file-output produces same JSON output as normal JSON output to stdout`',
(done) => {
const jsonOutputFilename = `${uuidv4()}.json`;
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;
}
// give file a little time to be finished to be written
await new Promise((r) => setTimeout(r, 3000));
const stdoutJson = stdout;
const outputFileContents = readFileSync(jsonOutputFilename, 'utf-8');
unlinkSync(`./${jsonOutputFilename}`);
expect(stdoutJson).toEqual(outputFileContents);
done();
},
);
},
testTimeout,
);
expect(code).toEqual(0);
expect(await project.read(outputPath)).toEqual(stdout + '\n');
});
});

0 comments on commit 2f32793

Please sign in to comment.