Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pack.js « commands « lib « test - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bc887720863226b93c86a96a220e77e33f245345 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const t = require('tap')
const { real: mockNpm } = require('../../fixtures/mock-npm')
const path = require('path')
const fs = require('fs')

const cwd = process.cwd()
t.afterEach(t => {
  process.chdir(cwd)
})

t.test('should pack current directory with no arguments', async t => {
  const { Npm, outputs, filteredLogs } = mockNpm(t)
  const npm = new Npm()
  await npm.load()
  npm.prefix = t.testdir({
    'package.json': JSON.stringify({
      name: 'test-package',
      version: '1.0.0',
    }),
  })
  process.chdir(npm.prefix)
  await npm.exec('pack', [])
  const filename = 'test-package-1.0.0.tgz'
  t.strictSame(outputs, [[filename]])
  t.matchSnapshot(filteredLogs('notice'), 'logs pack contents')
  t.ok(fs.statSync(path.resolve(npm.prefix, filename)))
})

t.test('follows pack-destination config', async t => {
  const { Npm, outputs } = mockNpm(t)
  const npm = new Npm()
  await npm.load()
  npm.prefix = t.testdir({
    'package.json': JSON.stringify({
      name: 'test-package',
      version: '1.0.0',
    }),
    'tar-destination': {},
  })
  process.chdir(npm.prefix)
  npm.config.set('pack-destination', path.join(npm.prefix, 'tar-destination'))
  await npm.exec('pack', [])
  const filename = 'test-package-1.0.0.tgz'
  t.strictSame(outputs, [[filename]])
  t.ok(fs.statSync(path.resolve(npm.prefix, 'tar-destination', filename)))
})

t.test('should pack given directory for scoped package', async t => {
  const { Npm, outputs } = mockNpm(t)
  const npm = new Npm()
  await npm.load()
  npm.prefix = t.testdir({
    'package.json': JSON.stringify({
      name: '@npm/test-package',
      version: '1.0.0',
    }),
  })
  process.chdir(npm.prefix)
  await npm.exec('pack', [])
  const filename = 'npm-test-package-1.0.0.tgz'
  t.strictSame(outputs, [[filename]])
  t.ok(fs.statSync(path.resolve(npm.prefix, filename)))
})

t.test('should log output as valid json', async t => {
  const { Npm, outputs, filteredLogs } = mockNpm(t)
  const npm = new Npm()
  await npm.load()
  npm.prefix = t.testdir({
    'package.json': JSON.stringify({
      name: 'test-package',
      version: '1.0.0',
    }),
  })
  process.chdir(npm.prefix)
  npm.config.set('json', true)
  await npm.exec('pack', [])
  const filename = 'test-package-1.0.0.tgz'
  t.matchSnapshot(outputs.map(JSON.parse), 'outputs as json')
  t.matchSnapshot(filteredLogs('notice'), 'logs pack contents')
  t.ok(fs.statSync(path.resolve(npm.prefix, filename)))
})

t.test('dry run', async t => {
  const { Npm, outputs, filteredLogs } = mockNpm(t)
  const npm = new Npm()
  await npm.load()
  npm.prefix = t.testdir({
    'package.json': JSON.stringify({
      name: 'test-package',
      version: '1.0.0',
    }),
  })
  npm.config.set('dry-run', true)
  process.chdir(npm.prefix)
  await npm.exec('pack', [])
  const filename = 'test-package-1.0.0.tgz'
  t.strictSame(outputs, [[filename]])
  t.matchSnapshot(filteredLogs('notice'), 'logs pack contents')
  t.throws(() => fs.statSync(path.resolve(npm.prefix, filename)))
})

t.test('invalid packument', async t => {
  const { Npm, outputs } = mockNpm(t)
  const npm = new Npm()
  await npm.load()
  npm.prefix = t.testdir({
    'package.json': '{}',
  })
  process.chdir(npm.prefix)
  await t.rejects(
    npm.exec('pack', []),
    /Invalid package, must have name and version/
  )
  t.strictSame(outputs, [])
})

t.test('workspaces', async t => {
  const { Npm, outputs } = mockNpm(t)
  const npm = new Npm()
  await npm.load()
  npm.prefix = t.testdir({
    'package.json': JSON.stringify(
      {
        name: 'workspaces-test',
        version: '1.0.0',
        workspaces: ['workspace-a', 'workspace-b'],
      },
      null,
      2
    ),
    'workspace-a': {
      'package.json': JSON.stringify({
        name: 'workspace-a',
        version: '1.0.0',
      }),
    },
    'workspace-b': {
      'package.json': JSON.stringify({
        name: 'workspace-b',
        version: '1.0.0',
      }),
    },
  })
  npm.config.set('workspaces', true)
  t.test('all workspaces', async t => {
    process.chdir(npm.prefix)
    await npm.exec('pack', [])
    t.strictSame(outputs, [['workspace-a-1.0.0.tgz'], ['workspace-b-1.0.0.tgz']])
  })

  t.test('all workspaces, `.` first arg', async t => {
    process.chdir(npm.prefix)
    await npm.exec('pack', ['.'])
    t.strictSame(outputs, [['workspace-a-1.0.0.tgz'], ['workspace-b-1.0.0.tgz']])
  })

  t.test('one workspace', async t => {
    process.chdir(npm.prefix)
    await npm.exec('pack', ['workspace-a'])
    t.strictSame(outputs, [['workspace-a-1.0.0.tgz']])
  })

  t.test('specific package', async t => {
    process.chdir(npm.prefix)
    await npm.exec('pack', [npm.prefix])
    t.strictSame(outputs, [['workspaces-test-1.0.0.tgz']])
  })
})