1const t = require('tap') 2const mockLogs = require('../../fixtures/mock-logs') 3const mockNpm = require('../../fixtures/mock-npm') 4const tmock = require('../../fixtures/tmock') 5 6const auditError = async (t, { command, error, ...config } = {}) => { 7 const { logs, logMocks } = mockLogs() 8 const mockAuditError = tmock(t, '{LIB}/utils/audit-error', logMocks) 9 10 const mock = await mockNpm(t, { 11 command, 12 config, 13 exec: true, 14 prefixDir: { 'package.json': '{}', 'package-lock.json': '{}' }, 15 }) 16 17 const res = {} 18 try { 19 res.result = mockAuditError(mock.npm, error ? { error } : {}) 20 } catch (err) { 21 res.error = err 22 } 23 24 return { 25 ...res, 26 logs: logs.warn.filter((l) => l[0] === 'audit'), 27 output: mock.joinedOutput(), 28 } 29} 30 31t.test('no error, not audit command', async t => { 32 const { result, error, logs, output } = await auditError(t, { command: 'install' }) 33 34 t.equal(result, false, 'no error') 35 t.notOk(error, 'no error') 36 37 t.match(output.trim(), /up to date/, 'install output') 38 t.match(output.trim(), /found 0 vulnerabilities/, 'install output') 39 t.strictSame(logs, [], 'no warnings') 40}) 41 42t.test('error, not audit command', async t => { 43 const { result, error, logs, output } = await auditError(t, { 44 command: 'install', 45 error: { 46 message: 'message', 47 body: Buffer.from('body'), 48 method: 'POST', 49 uri: 'https://example.com/not/a/registry', 50 headers: { 51 head: ['ers'], 52 }, 53 statusCode: '420', 54 }, 55 }) 56 57 t.equal(result, true, 'had error') 58 t.notOk(error, 'no error') 59 t.match(output.trim(), /up to date/, 'install output') 60 t.match(output.trim(), /found 0 vulnerabilities/, 'install output') 61 t.strictSame(logs, [], 'no warnings') 62}) 63 64t.test('error, audit command, not json', async t => { 65 const { result, error, logs, output } = await auditError(t, { 66 command: 'audit', 67 error: { 68 message: 'message', 69 body: Buffer.from('body error text'), 70 method: 'POST', 71 uri: 'https://example.com/not/a/registry', 72 headers: { 73 head: ['ers'], 74 }, 75 statusCode: '420', 76 }, 77 }) 78 79 t.equal(result, undefined) 80 81 t.ok(error, 'throws error') 82 t.match(output, 'body error text', 'some output') 83 t.strictSame(logs, [['audit', 'message']], 'some warnings') 84}) 85 86t.test('error, audit command, json', async t => { 87 const { result, error, logs, output } = await auditError(t, { 88 json: true, 89 command: 'audit', 90 error: { 91 message: 'message', 92 body: { response: 'body' }, 93 method: 'POST', 94 uri: 'https://username:password@example.com/not/a/registry', 95 headers: { 96 head: ['ers'], 97 }, 98 statusCode: '420', 99 }, 100 }) 101 102 t.equal(result, undefined) 103 t.ok(error, 'throws error') 104 t.match(output, 105 '{\n' + 106 ' "message": "message",\n' + 107 ' "method": "POST",\n' + 108 ' "uri": "https://username:***@example.com/not/a/registry",\n' + 109 ' "headers": {\n' + 110 ' "head": [\n' + 111 ' "ers"\n' + 112 ' ]\n' + 113 ' },\n' + 114 ' "statusCode": "420",\n' + 115 ' "body": {\n' + 116 ' "response": "body"\n' + 117 ' }\n' + 118 '}' 119 , 'some output') 120 t.strictSame(logs, [['audit', 'message']], 'some warnings') 121}) 122