/
cli-json-file-output.test.ts
114 lines (100 loc) · 3.07 KB
/
cli-json-file-output.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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);
},
);
},
);