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

shrinkwrap.js « commands « lib « test - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 812a9e23ec7f63f732f01e0ac4e27b0cece3c443 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const t = require('tap')
const fs = require('fs')
const { resolve } = require('path')
const { load: loadMockNpm } = require('../../fixtures/mock-npm')

// Attempt to parse json values in snapshots before
// stringifying to remove escaped values like \\"
// This also doesn't reorder the keys of the object
// like tap does by default which is nice in this case
t.formatSnapshot = obj =>
  JSON.stringify(
    obj,
    (k, v) => {
      try {
        return JSON.parse(v)
      } catch {
        // leave invalid JSON as a string
      }
      return v
    },
    2
  )

// Run shrinkwrap against a specified prefixDir with config items
// and make some assertions that should always be true. Sets
// the results on t.context for use in child tests
const shrinkwrap = async (t, prefixDir = {}, config = {}, mocks = {}) => {
  const { npm, logs } = await loadMockNpm(t, {
    mocks,
    config,
    prefixDir,
  })

  await npm.exec('shrinkwrap', [])

  const newFile = resolve(npm.prefix, 'npm-shrinkwrap.json')
  const oldFile = resolve(npm.prefix, 'package-lock.json')

  t.notOk(fs.existsSync(oldFile), 'package-lock is always deleted')
  t.same(logs.warn, [], 'no warnings')
  t.teardown(() => delete t.context)
  t.context = {
    localPrefix: prefixDir,
    config,
    shrinkwrap: JSON.parse(fs.readFileSync(newFile)),
    logs: logs.notice.map(([, m]) => m),
  }
}

// Run shrinkwrap against all combinations of existing and config
// lockfile versions
const shrinkwrapMatrix = async (t, file, assertions) => {
  const ancient = JSON.stringify({ lockfileVersion: 1 })
  const existing = JSON.stringify({ lockfileVersion: 2 })
  const upgrade = { 'lockfile-version': 3 }
  const downgrade = { 'lockfile-version': 1 }

  let ancientDir = {}
  let existingDir = null
  if (file === 'package-lock') {
    ancientDir = { 'package-lock.json': ancient }
    existingDir = { 'package-lock.json': existing }
  } else if (file === 'npm-shrinkwrap') {
    ancientDir = { 'npm-shrinkwrap.json': ancient }
    existingDir = { 'npm-shrinkwrap.json': existing }
  } else if (file === 'hidden-lockfile') {
    ancientDir = { node_modules: { '.package-lock.json': ancient } }
    existingDir = { node_modules: { '.package-lock.json': existing } }
  }

  await t.test('ancient', async t => {
    await shrinkwrap(t, ancientDir)
    t.match(t.context, assertions.ancient)
    t.matchSnapshot(t.context)
  })
  await t.test('ancient upgrade', async t => {
    await shrinkwrap(t, ancientDir, upgrade)
    t.match(t.context, assertions.ancientUpgrade)
    t.matchSnapshot(t.context)
  })

  if (existingDir) {
    await t.test('existing', async t => {
      await shrinkwrap(t, existingDir)
      t.match(t.context, assertions.existing)
      t.matchSnapshot(t.context)
    })
    await t.test('existing upgrade', async t => {
      await shrinkwrap(t, existingDir, upgrade)
      t.match(t.context, assertions.existingUpgrade)
      t.matchSnapshot(t.context)
    })
    await t.test('existing downgrade', async t => {
      await shrinkwrap(t, existingDir, downgrade)
      t.match(t.context, assertions.existingDowngrade)
      t.matchSnapshot(t.context)
    })
  }
}

const NOTICES = {
  CREATED: (v = '') => [`created a lockfile as npm-shrinkwrap.json${v && ` with version ${v}`}`],
  RENAMED: (v = '') => [
    `package-lock.json has been renamed to npm-shrinkwrap.json${
      v && ` and updated to version ${v}`
    }`,
  ],
  UPDATED: (v = '') => [`npm-shrinkwrap.json updated to version ${v}`],
  SAME: () => [`npm-shrinkwrap.json up to date`],
}

t.test('with nothing', t =>
  shrinkwrapMatrix(t, null, {
    ancient: {
      shrinkwrap: { lockfileVersion: 2 },
      logs: NOTICES.CREATED(2),
    },
    ancientUpgrade: {
      shrinkwrap: { lockfileVersion: 3 },
      logs: NOTICES.CREATED(3),
    },
  })
)

t.test('with package-lock.json', t =>
  shrinkwrapMatrix(t, 'package-lock', {
    ancient: {
      shrinkwrap: { lockfileVersion: 2 },
      logs: NOTICES.RENAMED(2),
    },
    ancientUpgrade: {
      shrinkwrap: { lockfileVersion: 3 },
      logs: NOTICES.RENAMED(3),
    },
    existing: {
      shrinkwrap: { lockfileVersion: 2 },
      logs: NOTICES.RENAMED(),
    },
    existingUpgrade: {
      shrinkwrap: { lockfileVersion: 3 },
      logs: NOTICES.RENAMED(3),
    },
    existingDowngrade: {
      shrinkwrap: { lockfileVersion: 1 },
      logs: NOTICES.RENAMED(1),
    },
  })
)

t.test('with npm-shrinkwrap.json', t =>
  shrinkwrapMatrix(t, 'npm-shrinkwrap', {
    ancient: {
      shrinkwrap: { lockfileVersion: 2 },
      logs: NOTICES.UPDATED(2),
    },
    ancientUpgrade: {
      shrinkwrap: { lockfileVersion: 3 },
      logs: NOTICES.UPDATED(3),
    },
    existing: {
      shrinkwrap: { lockfileVersion: 2 },
      logs: NOTICES.SAME(),
    },
    existingUpgrade: {
      shrinkwrap: { lockfileVersion: 3 },
      logs: NOTICES.UPDATED(3),
    },
    existingDowngrade: {
      shrinkwrap: { lockfileVersion: 1 },
      logs: NOTICES.UPDATED(1),
    },
  })
)

t.test('with hidden lockfile', t =>
  shrinkwrapMatrix(t, 'hidden-lockfile', {
    ancient: {
      shrinkwrap: { lockfileVersion: 1 },
      logs: NOTICES.CREATED(),
    },
    ancientUpgrade: {
      shrinkwrap: { lockfileVersion: 3 },
      logs: NOTICES.CREATED(),
    },
    existing: {
      shrinkwrap: { lockfileVersion: 2 },
      logs: NOTICES.CREATED(),
    },
    existingUpgrade: {
      shrinkwrap: { lockfileVersion: 3 },
      logs: NOTICES.CREATED(3),
    },
    existingDowngrade: {
      shrinkwrap: { lockfileVersion: 1 },
      logs: NOTICES.CREATED(1),
    },
  })
)

t.test('throws in global mode', async t => {
  t.rejects(
    shrinkwrap(
      t,
      {},
      {
        global: true,
      }
    ),
    {
      message: '`npm shrinkwrap` does not work for global packages',
      code: 'ESHRINKWRAPGLOBAL',
    }
  )
})