• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1const t = require('tap')
2const { load: loadMockNpm } = require('../../fixtures/mock-npm')
3const path = require('path')
4const fs = require('fs')
5
6t.test('should pack current directory with no arguments', async t => {
7  const { npm, outputs, logs } = await loadMockNpm(t, {
8    prefixDir: {
9      'package.json': JSON.stringify({
10        name: 'test-package',
11        version: '1.0.0',
12      }),
13    },
14  })
15  await npm.exec('pack', [])
16  const filename = 'test-package-1.0.0.tgz'
17  t.strictSame(outputs, [[filename]])
18  t.matchSnapshot(logs.notice.map(([, m]) => m), 'logs pack contents')
19  t.ok(fs.statSync(path.resolve(npm.prefix, filename)))
20})
21
22t.test('follows pack-destination config', async t => {
23  const { npm, outputs } = await loadMockNpm(t, {
24    prefixDir: {
25      'package.json': JSON.stringify({
26        name: 'test-package',
27        version: '1.0.0',
28      }),
29      'tar-destination': {},
30    },
31    config: ({ prefix }) => ({ 'pack-destination': path.join(prefix, 'tar-destination') }),
32  })
33  await npm.exec('pack', [])
34  const filename = 'test-package-1.0.0.tgz'
35  t.strictSame(outputs, [[filename]])
36  t.ok(fs.statSync(path.resolve(npm.prefix, 'tar-destination', filename)))
37})
38
39t.test('should pack given directory for scoped package', async t => {
40  const { npm, outputs } = await loadMockNpm(t, {
41    prefixDir: {
42      'package.json': JSON.stringify({
43        name: '@npm/test-package',
44        version: '1.0.0',
45      }),
46    },
47  })
48  await npm.exec('pack', [])
49  const filename = 'npm-test-package-1.0.0.tgz'
50  t.strictSame(outputs, [[filename]])
51  t.ok(fs.statSync(path.resolve(npm.prefix, filename)))
52})
53
54t.test('should log output as valid json', async t => {
55  const { npm, outputs, logs } = await loadMockNpm(t, {
56    prefixDir: {
57      'package.json': JSON.stringify({
58        name: 'test-package',
59        version: '1.0.0',
60      }),
61    },
62    config: { json: true },
63  })
64  await npm.exec('pack', [])
65  const filename = 'test-package-1.0.0.tgz'
66  t.matchSnapshot(outputs.map(JSON.parse), 'outputs as json')
67  t.matchSnapshot(logs.notice.map(([, m]) => m), 'logs pack contents')
68  t.ok(fs.statSync(path.resolve(npm.prefix, filename)))
69})
70
71t.test('should log scoped package output as valid json', async t => {
72  const { npm, outputs, logs } = await loadMockNpm(t, {
73    prefixDir: {
74      'package.json': JSON.stringify({
75        name: '@myscope/test-package',
76        version: '1.0.0',
77      }),
78    },
79    config: { json: true },
80  })
81  await npm.exec('pack', [])
82  const filename = 'myscope-test-package-1.0.0.tgz'
83  t.matchSnapshot(outputs.map(JSON.parse), 'outputs as json')
84  t.matchSnapshot(logs.notice.map(([, m]) => m), 'logs pack contents')
85  t.ok(fs.statSync(path.resolve(npm.prefix, filename)))
86})
87
88t.test('dry run', async t => {
89  const { npm, outputs, logs } = await loadMockNpm(t, {
90    prefixDir: {
91      'package.json': JSON.stringify({
92        name: 'test-package',
93        version: '1.0.0',
94      }),
95    },
96    config: { 'dry-run': true },
97  })
98  await npm.exec('pack', [])
99  const filename = 'test-package-1.0.0.tgz'
100  t.strictSame(outputs, [[filename]])
101  t.matchSnapshot(logs.notice.map(([, m]) => m), 'logs pack contents')
102  t.throws(() => fs.statSync(path.resolve(npm.prefix, filename)))
103})
104
105t.test('invalid packument', async t => {
106  const { npm, outputs } = await loadMockNpm(t, {
107    prefixDir: {
108      'package.json': '{}',
109    },
110  })
111  await t.rejects(
112    npm.exec('pack', []),
113    /Invalid package, must have name and version/
114  )
115  t.strictSame(outputs, [])
116})
117
118t.test('workspaces', async t => {
119  const loadWorkspaces = (t) => loadMockNpm(t, {
120    prefixDir: {
121      'package.json': JSON.stringify(
122        {
123          name: 'workspaces-test',
124          version: '1.0.0',
125          workspaces: ['workspace-a', 'workspace-b'],
126        },
127        null,
128        2
129      ),
130      'workspace-a': {
131        'package.json': JSON.stringify({
132          name: 'workspace-a',
133          version: '1.0.0',
134        }),
135      },
136      'workspace-b': {
137        'package.json': JSON.stringify({
138          name: 'workspace-b',
139          version: '1.0.0',
140        }),
141      },
142    },
143    config: {
144      workspaces: true,
145      // TODO: this is a workaround for npm run test-all
146      // somehow leaking include-workspace-root
147      'include-workspace-root': false,
148    },
149  })
150
151  t.test('all workspaces', async t => {
152    const { npm, outputs } = await loadWorkspaces(t)
153    await npm.exec('pack', [])
154    t.strictSame(outputs, [['workspace-a-1.0.0.tgz'], ['workspace-b-1.0.0.tgz']])
155  })
156
157  t.test('all workspaces, `.` first arg', async t => {
158    const { npm, outputs } = await loadWorkspaces(t)
159    await npm.exec('pack', ['.'])
160    t.strictSame(outputs, [['workspace-a-1.0.0.tgz'], ['workspace-b-1.0.0.tgz']])
161  })
162
163  t.test('one workspace', async t => {
164    const { npm, outputs } = await loadWorkspaces(t)
165    await npm.exec('pack', ['workspace-a'])
166    t.strictSame(outputs, [['workspace-a-1.0.0.tgz']])
167  })
168
169  t.test('specific package', async t => {
170    const { npm, outputs } = await loadWorkspaces(t)
171    await npm.exec('pack', [npm.prefix])
172    t.strictSame(outputs, [['workspaces-test-1.0.0.tgz']])
173  })
174})
175