diff --git a/test/cli-json-file-output.spec.ts b/test/cli-json-file-output.spec.ts index 25714efe23..a20db2029a 100644 --- a/test/cli-json-file-output.spec.ts +++ b/test/cli-json-file-output.spec.ts @@ -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'); @@ -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; @@ -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; @@ -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')) { @@ -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;