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')
|
2023-01-16 22:38:23 -05:00
|
|
|
const tmock = require('../../fixtures/tmock')
|
2024-01-11 06:28:56 -08:00
|
|
|
const { cleanZlib } = require('../../fixtures/clean-snapshot')
|
2020-10-02 17:52:19 -04:00
|
|
|
|
2021-12-02 22:04:46 +00:00
|
|
|
const { getContents } = require('../../../lib/utils/tar.js')
|
2024-01-11 06:28:56 -08:00
|
|
|
t.cleanSnapshot = data => cleanZlib(data)
|
2020-10-02 17:52:19 -04:00
|
|
|
|
2023-01-16 22:38:23 -05:00
|
|
|
const mockTar = ({ notice }) => tmock(t, '{LIB}/utils/tar.js', {
|
2021-12-02 22:04:46 +00:00
|
|
|
'proc-log': {
|
2024-04-30 23:53:22 -07:00
|
|
|
log: {
|
|
|
|
notice,
|
|
|
|
},
|
2021-12-02 22:04:46 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const printLogs = (tarball, options) => {
|
2020-10-02 17:52:19 -04:00
|
|
|
const logs = []
|
2021-12-02 22:04:46 +00:00
|
|
|
const { logTar } = mockTar({
|
2024-04-30 23:53:22 -07:00
|
|
|
notice: (...args) => logs.push(...args),
|
2020-10-02 17:52:19 -04:00
|
|
|
})
|
2021-12-02 22:04:46 +00:00
|
|
|
logTar(tarball, options)
|
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',
|
|
|
|
],
|
2022-12-06 22:18:33 -05:00
|
|
|
dependencies: {
|
|
|
|
'bundle-dep': '1.0.0',
|
|
|
|
},
|
|
|
|
}),
|
2021-05-10 17:31:02 -04:00
|
|
|
cat: 'meow',
|
|
|
|
chai: 'blub',
|
|
|
|
dog: 'woof',
|
2020-11-17 15:37:44 -05:00
|
|
|
node_modules: {
|
2022-12-06 22:18:33 -05:00
|
|
|
'bundle-dep': {
|
|
|
|
'package.json': '',
|
|
|
|
},
|
2020-11-17 15:37:44 -05:00
|
|
|
},
|
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)
|
|
|
|
|
2021-12-02 22:04:46 +00:00
|
|
|
t.matchSnapshot(printLogs(tarballContents))
|
2020-10-02 17:52:19 -04:00
|
|
|
})
|
|
|
|
|
2022-12-06 22:18:33 -05:00
|
|
|
t.test('should log tarball contents of a scoped package', async (t) => {
|
|
|
|
const testDir = t.testdir({
|
|
|
|
'package.json': JSON.stringify({
|
|
|
|
name: '@myscope/my-cool-pkg',
|
|
|
|
version: '1.0.0',
|
|
|
|
bundleDependencies: [
|
|
|
|
'bundle-dep',
|
|
|
|
],
|
|
|
|
dependencies: {
|
|
|
|
'bundle-dep': '1.0.0',
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
cat: 'meow',
|
|
|
|
chai: 'blub',
|
|
|
|
dog: 'woof',
|
|
|
|
node_modules: {
|
|
|
|
'bundle-dep': {
|
|
|
|
'package.json': '',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const tarball = await pack(testDir)
|
|
|
|
const tarballContents = await getContents({
|
|
|
|
_id: '1',
|
|
|
|
name: '@myscope/my-cool-pkg',
|
|
|
|
version: '1.0.0',
|
|
|
|
}, tarball)
|
|
|
|
|
|
|
|
t.matchSnapshot(printLogs(tarballContents))
|
|
|
|
})
|
|
|
|
|
2021-04-15 21:53:45 -04:00
|
|
|
t.test('should log tarball contents with unicode', async (t) => {
|
2021-12-02 22:04:46 +00:00
|
|
|
const { logTar } = mockTar({
|
|
|
|
notice: (str) => {
|
|
|
|
t.ok(true, 'defaults to proc-log')
|
|
|
|
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 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
|
|
|
})
|
|
|
|
|
2024-01-11 06:28:56 -08:00
|
|
|
// zlib is nondeterministic
|
|
|
|
t.match(tarballContents.shasum, /^[0-9a-f]{40}$/)
|
|
|
|
delete tarballContents.shasum
|
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',
|
2024-01-11 06:28:56 -08:00
|
|
|
size: tarball.length,
|
2020-10-02 17:52:19 -04:00
|
|
|
unpackedSize: 49,
|
|
|
|
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()
|
|
|
|
})
|