2021-04-15 21:53:45 -04:00
|
|
|
const t = require('tap')
|
2020-10-02 17:52:19 -04:00
|
|
|
const pack = require('libnpmpack')
|
|
|
|
const ssri = require('ssri')
|
|
|
|
|
|
|
|
const { logTar, getContents } = require('../../../lib/utils/tar.js')
|
|
|
|
|
|
|
|
const printLogs = (tarball, unicode) => {
|
|
|
|
const logs = []
|
|
|
|
logTar(tarball, {
|
|
|
|
log: {
|
|
|
|
notice: (...args) => {
|
|
|
|
args.map(el => logs.push(el))
|
2020-11-17 15:37:44 -05:00
|
|
|
},
|
2020-10-02 17:52:19 -04:00
|
|
|
},
|
2020-11-17 15:37:44 -05:00
|
|
|
unicode,
|
2020-10-02 17:52:19 -04:00
|
|
|
})
|
|
|
|
return logs.join('\n')
|
|
|
|
}
|
|
|
|
|
2021-04-15 21:53:45 -04:00
|
|
|
t.test('should log tarball contents', async (t) => {
|
2020-10-02 17:52:19 -04:00
|
|
|
const testDir = t.testdir({
|
|
|
|
'package.json': JSON.stringify({
|
|
|
|
name: 'my-cool-pkg',
|
|
|
|
version: '1.0.0',
|
|
|
|
bundleDependencies: [
|
2020-11-17 15:37:44 -05:00
|
|
|
'bundle-dep',
|
|
|
|
],
|
2020-10-02 17:52:19 -04:00
|
|
|
}, null, 2),
|
2021-05-10 17:31:02 -04:00
|
|
|
cat: 'meow',
|
|
|
|
chai: 'blub',
|
|
|
|
dog: 'woof',
|
2020-11-17 15:37:44 -05:00
|
|
|
node_modules: {
|
|
|
|
'bundle-dep': 'toto',
|
|
|
|
},
|
2020-10-02 17:52:19 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
const tarball = await pack(testDir)
|
|
|
|
const tarballContents = await getContents({
|
|
|
|
_id: '1',
|
|
|
|
name: 'my-cool-pkg',
|
2020-11-17 15:37:44 -05:00
|
|
|
version: '1.0.0',
|
2020-10-02 17:52:19 -04:00
|
|
|
}, tarball)
|
|
|
|
|
|
|
|
t.matchSnapshot(printLogs(tarballContents, false))
|
|
|
|
})
|
|
|
|
|
2021-04-15 21:53:45 -04:00
|
|
|
t.test('should log tarball contents with unicode', async (t) => {
|
|
|
|
const { logTar } = t.mock('../../../lib/utils/tar.js', {
|
2020-11-17 15:37:44 -05:00
|
|
|
npmlog: {
|
|
|
|
notice: (str) => {
|
2020-10-02 17:52:19 -04:00
|
|
|
t.ok(true, 'defaults to npmlog')
|
|
|
|
return str
|
2020-11-17 15:37:44 -05:00
|
|
|
},
|
|
|
|
},
|
2020-10-02 17:52:19 -04:00
|
|
|
})
|
2020-11-17 15:37:44 -05:00
|
|
|
|
|
|
|
logTar({
|
|
|
|
files: [],
|
2020-10-02 17:52:19 -04:00
|
|
|
bundled: [],
|
2021-08-19 17:47:33 +00:00
|
|
|
size: 0,
|
|
|
|
unpackedSize: 0,
|
2020-11-17 15:37:44 -05:00
|
|
|
integrity: '',
|
2020-10-02 17:52:19 -04:00
|
|
|
}, { unicode: true })
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
|
2021-04-15 21:53:45 -04:00
|
|
|
t.test('should default to npmlog', async (t) => {
|
|
|
|
const { logTar } = t.mock('../../../lib/utils/tar.js', {
|
2020-11-17 15:37:44 -05:00
|
|
|
npmlog: {
|
|
|
|
notice: (str) => {
|
2020-10-02 17:52:19 -04:00
|
|
|
t.ok(true, 'defaults to npmlog')
|
|
|
|
return str
|
2020-11-17 15:37:44 -05:00
|
|
|
},
|
|
|
|
},
|
2020-10-02 17:52:19 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
logTar({
|
|
|
|
files: [],
|
|
|
|
bundled: [],
|
2021-08-19 17:47:33 +00:00
|
|
|
size: 0,
|
|
|
|
unpackedSize: 0,
|
2020-11-17 15:37:44 -05:00
|
|
|
integrity: '',
|
2020-10-02 17:52:19 -04:00
|
|
|
})
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
|
2021-04-15 21:53:45 -04:00
|
|
|
t.test('should getContents of a tarball', async (t) => {
|
2020-10-02 17:52:19 -04:00
|
|
|
const testDir = t.testdir({
|
|
|
|
'package.json': JSON.stringify({
|
|
|
|
name: 'my-cool-pkg',
|
2020-11-17 15:37:44 -05:00
|
|
|
version: '1.0.0',
|
|
|
|
}, null, 2),
|
2020-10-02 17:52:19 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
const tarball = await pack(testDir)
|
|
|
|
|
|
|
|
const tarballContents = await getContents({
|
|
|
|
name: 'my-cool-pkg',
|
2020-11-17 15:37:44 -05:00
|
|
|
version: '1.0.0',
|
2020-10-02 17:52:19 -04:00
|
|
|
}, tarball)
|
|
|
|
|
|
|
|
const integrity = await ssri.fromData(tarball, {
|
2020-11-17 15:37:44 -05:00
|
|
|
algorithms: ['sha1', 'sha512'],
|
2020-10-02 17:52:19 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
t.strictSame(tarballContents, {
|
|
|
|
id: 'my-cool-pkg@1.0.0',
|
|
|
|
name: 'my-cool-pkg',
|
|
|
|
version: '1.0.0',
|
2021-03-23 14:58:11 -04:00
|
|
|
size: 146,
|
2020-10-02 17:52:19 -04:00
|
|
|
unpackedSize: 49,
|
2021-03-23 14:58:11 -04:00
|
|
|
shasum: 'b8379c5e69693cdda73aec3d81dae1d11c1e75bd',
|
2020-10-02 17:52:19 -04:00
|
|
|
integrity: ssri.parse(integrity.sha512[0]),
|
|
|
|
filename: 'my-cool-pkg-1.0.0.tgz',
|
2020-11-17 15:37:44 -05:00
|
|
|
files: [{ path: 'package.json', size: 49, mode: 420 }],
|
2020-10-02 17:52:19 -04:00
|
|
|
entryCount: 1,
|
2020-11-17 15:37:44 -05:00
|
|
|
bundled: [],
|
2020-10-02 17:52:19 -04:00
|
|
|
}, 'contents are correct')
|
|
|
|
t.end()
|
|
|
|
})
|