-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathupgrade.test.ts
More file actions
333 lines (274 loc) · 12 KB
/
Copy pathupgrade.test.ts
File metadata and controls
333 lines (274 loc) · 12 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import {isDevelopment} from './context/local.js'
import {currentProcessIsGlobal, inferPackageManagerForGlobalCLI} from './is-global.js'
import {checkForCachedNewVersion, packageManagerFromUserAgent, PackageManager} from './node-package-manager.js'
import {exec, isCI} from './system.js'
import {
cliInstallCommand,
getOutputUpdateCLIReminder,
hasBlockingAutoUpgradeNotification,
runCLIUpgrade,
versionToAutoUpgrade,
} from './upgrade.js'
import {Notification, fetchNotifications} from './notifications-system.js'
import {isPreReleaseVersion} from './version.js'
import {getAutoUpgradeEnabled} from '../../private/node/conf-store.js'
import {vi, describe, test, expect, beforeEach} from 'vitest'
vi.mock('./notifications-system.js', async (importOriginal) => {
const actual: any = await importOriginal()
return {
...actual,
fetchNotifications: vi.fn(),
}
})
vi.mock('./context/local.js')
vi.mock('./is-global.js')
vi.mock('./node-package-manager.js')
vi.mock('./system.js')
vi.mock('../../private/node/conf-store.js')
vi.mock('./version.js', async (importOriginal) => {
const actual: any = await importOriginal()
return {
...actual,
isPreReleaseVersion: vi.fn(() => false),
}
})
describe('cliInstallCommand', () => {
beforeEach(() => {
// Mock isDevelopment to return false by default (not in CLI development mode)
vi.mocked(isDevelopment).mockReturnValue(false)
})
test('says to install globally via npm if the current process is globally installed and no package manager is provided', () => {
// Given
vi.mocked(currentProcessIsGlobal).mockReturnValue(true)
vi.mocked(inferPackageManagerForGlobalCLI).mockReturnValue('npm')
// When
const got = cliInstallCommand()
// Then
expect(got).toMatchInlineSnapshot(`
"npm install -g @shopify/cli@latest"
`)
})
test('says to install globally via yarn if the current process is globally installed and yarn is the global package manager', () => {
// Given
vi.mocked(currentProcessIsGlobal).mockReturnValue(true)
vi.mocked(packageManagerFromUserAgent).mockReturnValue('unknown')
vi.mocked(inferPackageManagerForGlobalCLI).mockReturnValue('yarn')
// When
const got = cliInstallCommand()
// Then
expect(got).toMatchInlineSnapshot(`
"yarn global add @shopify/cli@latest"
`)
})
test('says to install globally via npm if the current process is globally installed and npm is the global package manager', () => {
// Given
vi.mocked(currentProcessIsGlobal).mockReturnValue(true)
vi.mocked(packageManagerFromUserAgent).mockReturnValue('unknown')
vi.mocked(inferPackageManagerForGlobalCLI).mockReturnValue('npm')
// When
const got = cliInstallCommand()
// Then
expect(got).toMatchInlineSnapshot(`
"npm install -g @shopify/cli@latest"
`)
})
test('says to install globally via pnpm if the current process is globally installed and pnpm is the global package manager', () => {
// Given
vi.mocked(currentProcessIsGlobal).mockReturnValue(true)
vi.mocked(packageManagerFromUserAgent).mockReturnValue('unknown')
vi.mocked(inferPackageManagerForGlobalCLI).mockReturnValue('pnpm')
// When
const got = cliInstallCommand()
// Then
expect(got).toMatchInlineSnapshot(`
"pnpm add -g @shopify/cli@latest"
`)
})
test('returns undefined if the current process is locally installed', () => {
// Given
vi.mocked(currentProcessIsGlobal).mockReturnValue(false)
// When
const got = cliInstallCommand()
// Then
expect(got).toBeUndefined()
})
})
describe('getOutputUpdateCLIReminder', () => {
test('returns a basic upgrade message for a minor version bump', () => {
vi.mocked(inferPackageManagerForGlobalCLI).mockReturnValue('homebrew')
const message = getOutputUpdateCLIReminder('3.91.0')
expect(message).toContain('3.91.0')
expect(message).toContain('brew upgrade shopify-cli')
expect(message).not.toContain('major version')
})
test('appends the GitHub release URL for a major version bump', () => {
vi.mocked(inferPackageManagerForGlobalCLI).mockReturnValue('homebrew')
const message = getOutputUpdateCLIReminder('4.0.0', true)
expect(message).toContain('4.0.0')
expect(message).toContain('brew upgrade shopify-cli')
expect(message).toContain('major version')
expect(message).toContain('https://github.com/Shopify/cli/releases/tag/4.0.0')
})
test('does not append the release URL for a minor version bump even when isMajor is false', () => {
vi.mocked(inferPackageManagerForGlobalCLI).mockReturnValue('npm')
const message = getOutputUpdateCLIReminder('3.91.0', false)
expect(message).not.toContain('major version')
expect(message).not.toContain('releases/tag')
})
})
describe('runCLIUpgrade', () => {
beforeEach(() => {
// Mock isDevelopment to return false by default (not in CLI development mode)
vi.mocked(isDevelopment).mockReturnValue(false)
})
test('runs the install command via exec for a global npm install', async () => {
// Given
vi.mocked(currentProcessIsGlobal).mockReturnValue(true)
vi.mocked(inferPackageManagerForGlobalCLI).mockReturnValue('npm')
vi.mocked(exec).mockResolvedValue()
// When
await runCLIUpgrade()
// Then
expect(exec).toHaveBeenCalledWith('npm', ['install', '-g', '@shopify/cli@latest'], {stdio: 'inherit'})
})
test('runs the install command via exec for a global yarn install', async () => {
// Given
vi.mocked(currentProcessIsGlobal).mockReturnValue(true)
vi.mocked(inferPackageManagerForGlobalCLI).mockReturnValue('yarn')
vi.mocked(exec).mockResolvedValue()
// When
await runCLIUpgrade()
// Then
expect(exec).toHaveBeenCalledWith('yarn', ['global', 'add', '@shopify/cli@latest'], {stdio: 'inherit'})
})
test('runs the install command via exec for a global homebrew install', async () => {
// Given
vi.mocked(currentProcessIsGlobal).mockReturnValue(true)
vi.mocked(inferPackageManagerForGlobalCLI).mockReturnValue('homebrew')
vi.mocked(exec).mockResolvedValue()
// When
await runCLIUpgrade()
// Then
expect(exec).toHaveBeenCalledWith('brew', ['upgrade', 'shopify-cli'], {stdio: 'inherit'})
})
test('throws an error when cliInstallCommand returns undefined', async () => {
// Given
vi.mocked(currentProcessIsGlobal).mockReturnValue(true)
// 'unknown' is returned by inferPackageManagerForGlobalCLI for local installs
vi.mocked(inferPackageManagerForGlobalCLI).mockReturnValue('unknown' as PackageManager)
// When/Then
await expect(runCLIUpgrade()).rejects.toThrow('Could not determine the package manager')
})
test('does nothing when running in development mode for local install (SHOPIFY_ENV=development)', async () => {
// Given
vi.mocked(currentProcessIsGlobal).mockReturnValue(false)
vi.mocked(isDevelopment).mockReturnValue(true)
// When
await runCLIUpgrade()
// Then
expect(exec).not.toHaveBeenCalled()
})
test('skips project-local upgrade when called from the auto-upgrade postrun hook', async () => {
// Given
vi.mocked(currentProcessIsGlobal).mockReturnValue(false)
// When
await runCLIUpgrade({autoupgrade: true})
// Then
expect(exec).not.toHaveBeenCalled()
})
})
describe('versionToAutoUpgrade', () => {
test('returns the newer version for a minor bump when auto-upgrade is enabled', () => {
vi.mocked(checkForCachedNewVersion).mockReturnValue('3.91.0')
vi.mocked(isCI).mockReturnValue(false)
vi.mocked(getAutoUpgradeEnabled).mockReturnValue(true)
expect(versionToAutoUpgrade()).toBe('3.91.0')
})
test('returns the newer version for a patch bump when auto-upgrade is enabled', () => {
vi.mocked(checkForCachedNewVersion).mockReturnValue('3.90.1')
vi.mocked(isCI).mockReturnValue(false)
vi.mocked(getAutoUpgradeEnabled).mockReturnValue(true)
expect(versionToAutoUpgrade()).toBe('3.90.1')
})
test('returns undefined when no cached newer version exists', () => {
vi.mocked(checkForCachedNewVersion).mockReturnValue(undefined)
expect(versionToAutoUpgrade()).toBeUndefined()
})
test('returns undefined when running in CI', () => {
vi.mocked(checkForCachedNewVersion).mockReturnValue('3.91.0')
vi.mocked(isCI).mockReturnValue(true)
vi.mocked(getAutoUpgradeEnabled).mockReturnValue(true)
expect(versionToAutoUpgrade()).toBeUndefined()
})
test('returns undefined when auto-upgrade is not enabled', () => {
vi.mocked(checkForCachedNewVersion).mockReturnValue('3.91.0')
vi.mocked(isCI).mockReturnValue(false)
vi.mocked(getAutoUpgradeEnabled).mockReturnValue(false)
expect(versionToAutoUpgrade()).toBeUndefined()
})
test('returns the newer version when auto-upgrade preference has never been set (default enabled)', () => {
vi.mocked(checkForCachedNewVersion).mockReturnValue('3.91.0')
vi.mocked(isCI).mockReturnValue(false)
vi.mocked(getAutoUpgradeEnabled).mockReturnValue(true)
expect(versionToAutoUpgrade()).toBe('3.91.0')
})
test('returns the newer version for a major version change when auto-upgrade is enabled', () => {
vi.mocked(checkForCachedNewVersion).mockReturnValue('4.0.0')
vi.mocked(isCI).mockReturnValue(false)
vi.mocked(getAutoUpgradeEnabled).mockReturnValue(true)
expect(versionToAutoUpgrade()).toEqual('4.0.0')
})
test('returns undefined for a pre-release (nightly/snapshot) version', () => {
vi.mocked(checkForCachedNewVersion).mockReturnValue('3.91.0')
vi.mocked(isCI).mockReturnValue(false)
vi.mocked(getAutoUpgradeEnabled).mockReturnValue(true)
vi.mocked(isPreReleaseVersion).mockReturnValue(true)
expect(versionToAutoUpgrade()).toBeUndefined()
vi.mocked(isPreReleaseVersion).mockReturnValue(false)
})
})
describe('hasBlockingAutoUpgradeNotification', () => {
function notification(overrides: Partial<Notification> = {}): Notification {
return {
id: 'block-autoupgrade',
message: 'Auto-upgrade temporarily disabled',
type: 'error',
frequency: 'always',
ownerChannel: '#cli',
surface: 'autoupgrade',
...overrides,
}
}
test('returns true when an error notification on the autoupgrade surface is active', async () => {
vi.mocked(fetchNotifications).mockResolvedValue({notifications: [notification()]})
await expect(hasBlockingAutoUpgradeNotification()).resolves.toBe(true)
})
test('returns false for non-error notifications on the autoupgrade surface', async () => {
vi.mocked(fetchNotifications).mockResolvedValue({
notifications: [notification({type: 'warning'}), notification({type: 'info'})],
})
await expect(hasBlockingAutoUpgradeNotification()).resolves.toBe(false)
})
test('returns false for error notifications on other surfaces', async () => {
vi.mocked(fetchNotifications).mockResolvedValue({notifications: [notification({surface: 'app'})]})
await expect(hasBlockingAutoUpgradeNotification()).resolves.toBe(false)
})
test('returns false when the current CLI version is below minVersion', async () => {
vi.mocked(fetchNotifications).mockResolvedValue({notifications: [notification({minVersion: '999.0.0'})]})
await expect(hasBlockingAutoUpgradeNotification()).resolves.toBe(false)
})
test('returns false when the current CLI version is above maxVersion', async () => {
vi.mocked(fetchNotifications).mockResolvedValue({notifications: [notification({maxVersion: '0.0.1'})]})
await expect(hasBlockingAutoUpgradeNotification()).resolves.toBe(false)
})
test('returns false when the notification window is in the past', async () => {
vi.mocked(fetchNotifications).mockResolvedValue({
notifications: [notification({minDate: '2000-01-01', maxDate: '2000-01-02'})],
})
await expect(hasBlockingAutoUpgradeNotification()).resolves.toBe(false)
})
test('fails open and returns false when fetching notifications throws', async () => {
vi.mocked(fetchNotifications).mockRejectedValue(new Error('network down'))
await expect(hasBlockingAutoUpgradeNotification()).resolves.toBe(false)
})
})