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

index.js « test « libnpmpack « workspaces - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fb50e197bceaf3b30a8d1b5f992449343d7bcd8c (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
170
171
172
173
174
'use strict'

const t = require('tap')

const tspawk = require('./fixtures/tspawk.js')

const fs = require('fs')
const path = require('path')
const pack = require('../lib/index.js')
const tnock = require('./fixtures/tnock.js')

const OPTS = {
  registry: 'https://mock.reg/',
}

const REG = OPTS.registry

// TODO this ... smells.  npm "script-shell" config mentions defaults but those
// are handled by run-script, not npm.  So for now we have to tie tests to some
// pretty specific internals of runScript
const makeSpawnArgs = require('@npmcli/run-script/lib/make-spawn-args.js')

t.test('packs from local directory', async t => {
  const testDir = t.testdir({
    'package.json': JSON.stringify({
      name: 'my-cool-pkg',
      version: '1.0.0',
    }, null, 2),
  })

  const cwd = process.cwd()
  process.chdir(testDir)

  const tarball = await pack()
  t.ok(tarball)

  t.teardown(async () => {
    process.chdir(cwd)
  })
})

t.test('writes tarball to file when dryRun === false', async t => {
  const testDir = t.testdir({
    'package.json': JSON.stringify({
      name: 'my-cool-pkg',
      version: '1.0.0',
      scripts: {
        prepack: 'touch prepack && sleep 1',
        postpack: 'sleep 1 && touch postpack',
      },
    }, null, 2),
  })

  const cwd = process.cwd()
  process.chdir(testDir)

  const tarball = await pack('file:.', {
    dryRun: false,
    packDestination: testDir,
    silent: true,
  })
  t.ok(tarball)
  const expectedTarball = path.join(testDir, 'my-cool-pkg-1.0.0.tgz')
  t.ok(fs.existsSync(expectedTarball), 'file was written')
  t.same(fs.readFileSync(expectedTarball), tarball, 'wrote same data that was returned')

  const prepackTimestamp = (await fs.promises.stat(path.join(testDir, 'prepack'))).mtime
  const tarballTimestamp = (await fs.promises.stat(expectedTarball)).mtime
  const postpackTimestamp = (await fs.promises.stat(path.join(testDir, 'postpack'))).mtime

  t.ok(prepackTimestamp < tarballTimestamp, 'prepack ran before tarball was written')
  t.ok(tarballTimestamp < postpackTimestamp, 'postpack ran after tarball was written')

  t.teardown(async () => {
    process.chdir(cwd)
  })
})

t.test('packs from local directory with silent', async t => {
  const testDir = t.testdir({
    'package.json': JSON.stringify({
      name: 'my-cool-pkg',
      version: '1.0.0',
    }, null, 2),
  })

  const cwd = process.cwd()
  process.chdir(testDir)

  const tarball = await pack('file:', { silent: true })
  t.ok(tarball)

  t.teardown(async () => {
    process.chdir(cwd)
  })
})

t.test('packs from registry spec', async t => {
  const spec = 'my-cool-pkg'
  const packument = {
    _id: 'my-cool-pkg',
    name: 'my-cool-pkg',
    description: 'some stuff',
    'dist-tags': {
      latest: '1.0.0',
    },
    versions: {
      '1.0.0': {
        _id: 'my-cool-pkg@1.0.0',
        _nodeVersion: process.versions.node,
        name: 'my-cool-pkg',
        version: '1.0.0',
        description: 'some stuff',
        dist: {
          shasum: 'some-shasum',
          integrity: '123',
          tarball: 'https://mock.reg/my-cool-pkg/-/my-cool-pkg-1.0.0.tgz',
        },
      },
    },
    readme: '',
    access: 'public',
    _attachments: {
      'my-cool-pkg-1.0.0.tgz': {
        content_type: 'application/octet-stream',
        data: '',
        length: '0',
      },
    },
  }

  const srv = tnock(t, REG)
  srv.get('/my-cool-pkg').reply(200, packument)
  srv.get('/my-cool-pkg/-/my-cool-pkg-1.0.0.tgz').reply(200, '')

  const tarball = await pack(spec, { ...OPTS })
  t.ok(tarball)
})

t.test('runs scripts in foreground when foregroundScripts === true', async t => {
  const spawk = tspawk(t)

  const testDir = t.testdir({
    'package.json': JSON.stringify({
      name: 'my-cool-pkg',
      version: '1.0.0',
      scripts: {
        prepack: 'touch prepack',
      },
    }, null, 2),
  })

  const cwd = process.cwd()
  process.chdir(testDir)

  const [scriptShell, scriptArgs] = makeSpawnArgs({
    event: 'prepack',
    path: testDir,
    cmd: 'touch prepack',
  })

  const prepack = spawk.spawn(scriptShell, scriptArgs)

  await pack('file:.', {
    packDestination: testDir,
    foregroundScripts: true,
  })

  t.ok(prepack.called)

  t.teardown(async () => {
    process.chdir(cwd)
  })
})