diff --git a/test/acceptance/cli-args.test.ts b/test/acceptance/cli-args.test.ts index 5de5039111..7abdff22f4 100644 --- a/test/acceptance/cli-args.test.ts +++ b/test/acceptance/cli-args.test.ts @@ -346,118 +346,6 @@ test('`test --json-file-output no value produces error message`', (t) => { optionsToTest.forEach(validate); }); -test('`test --json-file-output can save JSON output to file while sending human readable output to stdout`', (t) => { - t.plan(2); - const tmpFolder = randomTmpFolderPath(); - const jsonPath = path.normalize( - `${tmpFolder}/snyk-direct-json-test-output.json`, - ); - - const testFixture = - 'test/acceptance/workspaces/npm-package-no-vulns/package.json'; - - exec( - `node ${main} test --file=${testFixture} --json-file-output=${jsonPath}`, - (err, stdout) => { - if (err) { - console.log('CLI stdout: ', stdout); - throw err; - } - if (!existsSync(jsonPath)) { - console.log('CLI stdout: ', stdout); - } - const outputFileContents = readFileSync(jsonPath, 'utf-8'); - const jsonObj = JSON.parse(outputFileContents); - const okValue = jsonObj.ok as boolean; - - t.match(stdout, 'Organization:', 'contains human readable output'); - t.ok(okValue, 'JSON output ok'); - }, - ); -}); - -test('`test --json-file-output produces same JSON output as normal JSON output to stdout`', (t) => { - t.plan(1); - const tmpFolder = randomTmpFolderPath(); - const jsonPath = path.normalize( - `${tmpFolder}/snyk-direct-json-test-output.json`, - ); - const testFixture = - 'test/acceptance/workspaces/npm-package-no-vulns/package.json'; - exec( - `node ${main} test --file=${testFixture} --json --json-file-output=${jsonPath}`, - (err, stdout) => { - if (err) { - console.log('CLI stdout: ', stdout); - throw err; - } - const stdoutJson = stdout; - if (!existsSync(jsonPath)) { - console.log('CLI stdout: ', stdout); - } - const outputFileContents = readFileSync(jsonPath, 'utf-8'); - - t.equals(stdoutJson, outputFileContents); - }, - ); -}); - -test('`test --json-file-output can handle a relative path`', (t) => { - t.plan(1); - const tmpFolder = randomTmpFolderPath(); - const outputPath = path.normalize( - `${tmpFolder}/snyk-direct-json-test-output.json`, - ); - const testFixture = - 'test/acceptance/workspaces/npm-package-no-vulns/package.json'; - exec( - `node ${main} test --file=${testFixture} --json --json-file-output=${outputPath}`, - (err, stdout) => { - if (err) { - console.log('CLI stdout: ', stdout); - throw err; - } - const stdoutJson = stdout; - if (!existsSync(outputPath)) { - console.log('CLI stdout: ', stdout); - } - const outputFileContents = readFileSync(outputPath, 'utf-8'); - - t.equals(stdoutJson, outputFileContents); - }, - ); -}); - -test( - '`test --json-file-output can handle an absolute path`', - { skip: isWindows }, - (t) => { - t.plan(1); - const tmpFolder = randomTmpFolderPath(); - const outputPath = path.normalize( - `${tmpFolder}/snyk-direct-json-test-output.json`, - ); - const testFixture = - 'test/acceptance/workspaces/npm-package-no-vulns/package.json'; - exec( - `node ${main} test --file=${testFixture} --json --json-file-output=${outputPath}`, - (err, stdout) => { - if (err) { - console.log('CLI stdout: ', stdout); - throw err; - } - const stdoutJson = stdout; - if (!existsSync(outputPath)) { - console.log('CLI stdout: ', stdout); - } - const outputFileContents = readFileSync(outputPath, 'utf-8'); - - t.equals(stdoutJson, outputFileContents); - }, - ); - }, -); - test('flags not allowed with --sarif', (t) => { t.plan(4); exec(`node ${main} test iac --sarif --json`, (err, stdout) => { diff --git a/test/system/cli-json-file-output.test.ts b/test/system/cli-json-file-output.test.ts deleted file mode 100644 index 36e0e356dc..0000000000 --- a/test/system/cli-json-file-output.test.ts +++ /dev/null @@ -1,114 +0,0 @@ -import { test } from 'tap'; -import { exec } from 'child_process'; -import { sep, join } from 'path'; -import { readFileSync, unlinkSync, rmdirSync, mkdirSync, existsSync } from 'fs'; -import { v4 as uuidv4 } from 'uuid'; - -const osName = require('os-name'); - -const main = './dist/cli/index.js'.replace(/\//g, sep); -const iswindows = - osName() - .toLowerCase() - .indexOf('windows') === 0; - -test('`test --json-file-output can save JSON output to file while sending human readable output to stdout`', (t) => { - t.plan(2); - - exec( - `node ${main} test --json-file-output=snyk-direct-json-test-output.json`, - (err, stdout) => { - if (err) { - throw err; - } - t.match(stdout, 'Organization:', 'contains human readable output'); - const outputFileContents = readFileSync( - 'snyk-direct-json-test-output.json', - 'utf-8', - ); - unlinkSync('./snyk-direct-json-test-output.json'); - const jsonObj = JSON.parse(outputFileContents); - const okValue = jsonObj.ok as boolean; - t.ok(okValue, 'JSON output ok'); - }, - ); -}); - -test('`test --json-file-output produces same JSON output as normal JSON output to stdout`', (t) => { - t.plan(1); - - exec( - `node ${main} test --json --json-file-output=snyk-direct-json-test-output.json`, - (err, stdout) => { - if (err) { - throw err; - } - const stdoutJson = stdout; - const outputFileContents = readFileSync( - 'snyk-direct-json-test-output.json', - 'utf-8', - ); - unlinkSync('./snyk-direct-json-test-output.json'); - t.equals(stdoutJson, outputFileContents); - }, - ); -}); - -test('`test --json-file-output can handle a relative path`', (t) => { - t.plan(1); - - // if 'test-output' doesn't exist, created it - if (!existsSync('test-output')) { - mkdirSync('test-output'); - } - - const tempFolder = uuidv4(); - const outputPath = `test-output/${tempFolder}/snyk-direct-json-test-output.json`; - - exec( - `node ${main} test --json --json-file-output=${outputPath}`, - (err, stdout) => { - if (err) { - throw err; - } - const stdoutJson = stdout; - const outputFileContents = readFileSync(outputPath, 'utf-8'); - unlinkSync(outputPath); - rmdirSync(`test-output/${tempFolder}`); - t.equals(stdoutJson, outputFileContents); - }, - ); -}); - -test( - '`test --json-file-output can handle an absolute path`', - { skip: iswindows }, - (t) => { - t.plan(1); - - // if 'test-output' doesn't exist, created it - if (!existsSync('test-output')) { - mkdirSync('test-output'); - } - - const tempFolder = uuidv4(); - const outputPath = join( - process.cwd(), - `test-output/${tempFolder}/snyk-direct-json-test-output.json`, - ); - - exec( - `node ${main} test --json --json-file-output=${outputPath}`, - (err, stdout) => { - if (err) { - throw err; - } - const stdoutJson = stdout; - const outputFileContents = readFileSync(outputPath, 'utf-8'); - unlinkSync(outputPath); - rmdirSync(`test-output/${tempFolder}`); - t.equals(stdoutJson, outputFileContents); - }, - ); - }, -);