1const t = require('tap')
2const pack = require('libnpmpack')
3const ssri = require('ssri')
4const tmock = require('../../fixtures/tmock')
5const { cleanZlib } = require('../../fixtures/clean-snapshot')
6
7const { getContents } = require('../../../lib/utils/tar.js')
8t.cleanSnapshot = data => cleanZlib(data)
9
10const mockTar = ({ notice }) => tmock(t, '{LIB}/utils/tar.js', {
11  'proc-log': {
12    notice,
13  },
14})
15
16const printLogs = (tarball, options) => {
17  const logs = []
18  const { logTar } = mockTar({
19    notice: (...args) => args.map(el => logs.push(el)),
20  })
21  logTar(tarball, options)
22  return logs.join('\n')
23}
24
25t.test('should log tarball contents', async (t) => {
26  const testDir = t.testdir({
27    'package.json': JSON.stringify({
28      name: 'my-cool-pkg',
29      version: '1.0.0',
30      bundleDependencies: [
31        'bundle-dep',
32      ],
33      dependencies: {
34        'bundle-dep': '1.0.0',
35      },
36    }),
37    cat: 'meow',
38    chai: 'blub',
39    dog: 'woof',
40    node_modules: {
41      'bundle-dep': {
42        'package.json': '',
43      },
44    },
45  })
46
47  const tarball = await pack(testDir)
48  const tarballContents = await getContents({
49    _id: '1',
50    name: 'my-cool-pkg',
51    version: '1.0.0',
52  }, tarball)
53
54  t.matchSnapshot(printLogs(tarballContents))
55})
56
57t.test('should log tarball contents of a scoped package', async (t) => {
58  const testDir = t.testdir({
59    'package.json': JSON.stringify({
60      name: '@myscope/my-cool-pkg',
61      version: '1.0.0',
62      bundleDependencies: [
63        'bundle-dep',
64      ],
65      dependencies: {
66        'bundle-dep': '1.0.0',
67      },
68    }),
69    cat: 'meow',
70    chai: 'blub',
71    dog: 'woof',
72    node_modules: {
73      'bundle-dep': {
74        'package.json': '',
75      },
76    },
77  })
78
79  const tarball = await pack(testDir)
80  const tarballContents = await getContents({
81    _id: '1',
82    name: '@myscope/my-cool-pkg',
83    version: '1.0.0',
84  }, tarball)
85
86  t.matchSnapshot(printLogs(tarballContents))
87})
88
89t.test('should log tarball contents with unicode', async (t) => {
90  const { logTar } = mockTar({
91    notice: (str) => {
92      t.ok(true, 'defaults to proc-log')
93      return str
94    },
95  })
96
97  logTar({
98    files: [],
99    bundled: [],
100    size: 0,
101    unpackedSize: 0,
102    integrity: '',
103  }, { unicode: true })
104  t.end()
105})
106
107t.test('should getContents of a tarball', async (t) => {
108  const testDir = t.testdir({
109    'package.json': JSON.stringify({
110      name: 'my-cool-pkg',
111      version: '1.0.0',
112    }, null, 2),
113  })
114
115  const tarball = await pack(testDir)
116
117  const tarballContents = await getContents({
118    name: 'my-cool-pkg',
119    version: '1.0.0',
120  }, tarball)
121
122  const integrity = await ssri.fromData(tarball, {
123    algorithms: ['sha1', 'sha512'],
124  })
125
126  // zlib is nondeterministic
127  t.match(tarballContents.shasum, /^[0-9a-f]{40}$/)
128  delete tarballContents.shasum
129  t.strictSame(tarballContents, {
130    id: 'my-cool-pkg@1.0.0',
131    name: 'my-cool-pkg',
132    version: '1.0.0',
133    size: tarball.length,
134    unpackedSize: 49,
135    integrity: ssri.parse(integrity.sha512[0]),
136    filename: 'my-cool-pkg-1.0.0.tgz',
137    files: [{ path: 'package.json', size: 49, mode: 420 }],
138    entryCount: 1,
139    bundled: [],
140  }, 'contents are correct')
141  t.end()
142})
143