-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathMigrationActions.ts
442 lines (350 loc) · 14.9 KB
/
MigrationActions.ts
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/* eslint-disable no-dupe-class-members */
import path from 'path'
import debugLib from 'debug'
import { fork } from 'child_process'
import fs from 'fs-extra'
import semver from 'semver'
import type { ForkOptions } from 'child_process'
import assert from 'assert'
import _ from 'lodash'
import type { DataContext } from '..'
import { getError } from '@packages/errors'
import {
cleanUpIntegrationFolder,
formatConfig,
LegacyCypressConfigJson,
moveSpecFiles,
NonStandardMigrationError,
SpecToMove,
} from '../sources'
import {
tryGetDefaultLegacyPluginsFile,
supportFilesForMigration,
hasSpecFile,
getStepsForMigration,
getIntegrationFolder,
isDefaultTestFiles,
getComponentTestFilesGlobs,
getComponentFolder,
getIntegrationTestFilesGlobs,
getSpecPattern,
legacyOptions,
legacyIntegrationFolder,
getLegacyPluginsCustomFilePath,
} from '../sources/migration'
import { makeCoreData } from '../data'
import { LegacyPluginsIpc } from '../data/LegacyPluginsIpc'
import { hasTypeScriptInstalled, toPosix } from '../util'
const debug = debugLib('cypress:data-context:MigrationActions')
const tsNode = toPosix(require.resolve('@packages/server/lib/plugins/child/register_ts_node'))
export function getConfigWithDefaults (legacyConfig: any) {
const newConfig = _.cloneDeep(legacyConfig)
legacyOptions.forEach(({ defaultValue, name }) => {
if (defaultValue !== undefined && legacyConfig[name] === undefined) {
newConfig[name] = typeof defaultValue === 'function' ? defaultValue() : defaultValue
}
})
return newConfig
}
export function getDiff (oldConfig: any, newConfig: any) {
// get all the values updated
const result: any = _.reduce(oldConfig, (acc: any, value, key) => {
// ignore values that have been removed
if (newConfig[key] && !_.isEqual(value, newConfig[key])) {
acc[key] = newConfig[key]
}
return acc
}, {})
// get all the values added
return _.reduce(newConfig, (acc: any, value, key) => {
// their key is in the new config but not in the old config
if (!oldConfig.hasOwnProperty(key)) {
acc[key] = value
}
return acc
}, result)
}
export async function processConfigViaLegacyPlugins (projectRoot: string, legacyConfig: LegacyCypressConfigJson): Promise<LegacyCypressConfigJson> {
const pluginFile = legacyConfig.pluginsFile
? await getLegacyPluginsCustomFilePath(projectRoot, legacyConfig.pluginsFile)
: await tryGetDefaultLegacyPluginsFile(projectRoot)
debug('found legacy pluginsFile at %s', pluginFile)
return new Promise((resolve, reject) => {
// couldn't find a pluginsFile
// just bail with initial config
if (!pluginFile) {
return resolve(legacyConfig)
}
const cwd = path.join(projectRoot, pluginFile)
const childOptions: ForkOptions = {
stdio: 'inherit',
cwd: path.dirname(cwd),
env: _.omit(process.env, 'CYPRESS_INTERNAL_E2E_TESTING_SELF'),
}
const configProcessArgs = ['--projectRoot', projectRoot, '--file', cwd]
const CHILD_PROCESS_FILE_PATH = require.resolve('@packages/server/lib/plugins/child/require_async_child')
// use ts-node if they've got typescript installed
// this matches the 9.x behavior, which is what we want for
// processing legacy pluginsFile (we never supported `"type": "module") in 9.x.
if (hasTypeScriptInstalled(projectRoot)) {
const tsNodeLoader = `--require "${tsNode}"`
if (!childOptions.env) {
childOptions.env = {}
}
if (childOptions.env.NODE_OPTIONS) {
childOptions.env.NODE_OPTIONS += ` ${tsNodeLoader}`
} else {
childOptions.env.NODE_OPTIONS = tsNodeLoader
}
}
const childProcess = fork(CHILD_PROCESS_FILE_PATH, configProcessArgs, childOptions)
const ipc = new LegacyPluginsIpc(childProcess)
childProcess.on('error', (error) => {
error = getError('LEGACY_CONFIG_ERROR_DURING_MIGRATION', cwd, error)
reject(error)
ipc.killChildProcess()
})
const legacyConfigWithDefaults = getConfigWithDefaults(legacyConfig)
ipc.on('ready', () => {
debug('legacyConfigIpc:ready')
ipc.send('loadLegacyPlugins', legacyConfigWithDefaults)
})
ipc.on('loadLegacyPlugins:reply', (modifiedLegacyConfig) => {
debug('loadLegacyPlugins:reply')
const diff = getDiff(legacyConfigWithDefaults, modifiedLegacyConfig)
// if env is updated by plugins, avoid adding it to the config file
if (diff.env) {
delete diff.env
}
const legacyConfigWithChanges = _.merge(legacyConfig, diff)
resolve(legacyConfigWithChanges)
ipc.killChildProcess()
})
ipc.on('loadLegacyPlugins:error', (error) => {
debug('loadLegacyPlugins:error')
error = getError('LEGACY_CONFIG_ERROR_DURING_MIGRATION', cwd, error)
reject(error)
ipc.killChildProcess()
})
ipc.on('childProcess:unhandledError', (error) => {
debug('childProcess:unhandledError')
reject(error)
ipc.killChildProcess()
})
})
}
export class MigrationActions {
constructor (private ctx: DataContext) { }
async initialize (config: LegacyCypressConfigJson) {
const legacyConfigForMigration = await this.setLegacyConfigForMigration(config)
this.reset(legacyConfigForMigration)
if (!this.ctx.currentProject || !legacyConfigForMigration) {
throw Error('cannot do migration without currentProject!')
}
if (this.ctx.coreData.app.isGlobalMode) {
const version = await this.locallyInstalledCypressVersion(this.ctx.currentProject)
if (!version) {
// Could not resolve Cypress. Unlikely, but they are using a
// project with Cypress that is nested more deeply than
// another project, which has a `cypress.json` but has not had
// it's node_modules installed, or it relies on a global version
// of Cypress that is missing for whatever reason.
return this.ctx.onError(getError('MIGRATION_CYPRESS_NOT_FOUND'))
}
const currentVersion = (await this.ctx.versions.versionData()).current.version
// Validate that the project being migrated has a version of Cypress compatible with the version being executed.
// This handles situations where Cypress is launched in global mode to migrate a project with an older version of
// Cypress as a dependency which could break the project when launched directly.
// For example:
// Local: 9.6.0 Global: 10.0.0 FAIL
// Local: 10.0.1 Global: 10.0.0 PASS
// Local: 12.0.0 Global: 12.0.1 FAIL
if (!semver.satisfies(version, `^${currentVersion}`)) {
return this.ctx.onError(getError('MIGRATION_MISMATCHED_CYPRESS_VERSIONS', version, currentVersion))
}
}
await this.initializeFlags()
const legacyConfigFileExist = this.ctx.migration.legacyConfigFileExists()
const filteredSteps = await getStepsForMigration(this.ctx.currentProject, legacyConfigForMigration, Boolean(legacyConfigFileExist))
this.ctx.update((coreData) => {
if (!filteredSteps[0]) {
throw Error(`Impossible to initialize a migration. No steps fit the configuration of this project.`)
}
coreData.migration.filteredSteps = filteredSteps
coreData.migration.step = filteredSteps[0]
})
}
async locallyInstalledCypressVersion (currentProject: string) {
try {
const localCypressPkgJsonPath = require.resolve(path.join('cypress', 'package.json'), {
paths: [currentProject],
})
const localCypressPkgJson = await fs.readJson(path.join(localCypressPkgJsonPath)) as { version: string }
return localCypressPkgJson?.version ?? undefined
} catch (e) {
// node_modules was not found, or some other unexpected error
// return undefined and surface the correct error.
return undefined
}
}
/**
* Figure out all the data required for the migration UI.
* This drives which migration steps need be shown and performed.
*/
private async initializeFlags () {
const legacyConfigForMigration = this.ctx.coreData.migration.legacyConfigForMigration
if (!this.ctx.currentProject || !legacyConfigForMigration) {
throw Error('Need currentProject to do migration')
}
const integrationFolder = getIntegrationFolder(legacyConfigForMigration)
const integrationTestFiles = getIntegrationTestFilesGlobs(legacyConfigForMigration)
const hasCustomIntegrationFolder = getIntegrationFolder(legacyConfigForMigration) !== legacyIntegrationFolder
const hasCustomIntegrationTestFiles = !isDefaultTestFiles(legacyConfigForMigration, 'integration')
const shouldAddCustomE2ESpecPattern = Boolean(this.ctx.migration.legacyConfigProjectId)
let hasE2ESpec = integrationFolder
? await hasSpecFile(this.ctx.currentProject, integrationFolder, integrationTestFiles)
: false
// if we don't find specs in the 9.X scope,
// let's check already migrated files.
// this allows users to stop migration halfway,
// then to pick up where they left migration off
if (!hasE2ESpec && (!hasCustomIntegrationTestFiles || !hasCustomIntegrationFolder)) {
const newE2eSpecPattern = getSpecPattern(legacyConfigForMigration, 'e2e', shouldAddCustomE2ESpecPattern)
hasE2ESpec = await hasSpecFile(this.ctx.currentProject, '', newE2eSpecPattern)
}
const componentFolder = getComponentFolder(legacyConfigForMigration)
const componentTestFiles = getComponentTestFilesGlobs(legacyConfigForMigration)
const hasCustomComponentFolder = componentFolder !== 'cypress/component'
const hasCustomComponentTestFiles = !isDefaultTestFiles(legacyConfigForMigration, 'component')
// A user is considered to "have" component testing if either
// 1. they have a default component folder (cypress/component) with at least 1 spec file
// OR
// 2. they have configured a non-default componentFolder (even if it doesn't have any specs.)
const hasSpecInDefaultComponentFolder = await hasSpecFile(this.ctx.currentProject, componentFolder, componentTestFiles)
const hasComponentTesting = (hasCustomComponentFolder || hasSpecInDefaultComponentFolder) ?? false
this.ctx.update((coreData) => {
coreData.migration.flags = {
hasCustomIntegrationFolder,
hasCustomIntegrationTestFiles,
hasCustomComponentFolder,
hasCustomComponentTestFiles,
hasCustomSupportFile: false,
hasComponentTesting,
hasE2ESpec,
hasPluginsFile: true,
shouldAddCustomE2ESpecPattern,
}
})
}
get configFileNameAfterMigration () {
return this.ctx.migration.legacyConfigFile.replace('.json', `.config.${this.ctx.lifecycleManager.fileExtensionToUse}`)
}
async createConfigFile () {
const config = await this.ctx.migration.createConfigString()
this.ctx.lifecycleManager.setConfigFilePath(this.configFileNameAfterMigration)
await this.ctx.fs.writeFile(this.ctx.lifecycleManager.configFilePath, config).catch((error) => {
throw error
})
await this.ctx.actions.file.removeFileInProject(this.ctx.migration.legacyConfigFile).catch((error) => {
throw error
})
if (this.ctx.modeOptions.configFile) {
// @ts-ignore configFile needs to be updated with the new one, so it finds the correct one
// with the new file, instead of the deleted one which is not supported anymore
this.ctx.modeOptions.configFile = this.ctx.migration.configFileNameAfterMigration
}
}
async setLegacyConfigForMigration (config: LegacyCypressConfigJson) {
assert(this.ctx.currentProject)
const legacyConfigForMigration = await processConfigViaLegacyPlugins(this.ctx.currentProject, config)
this.ctx.update((coreData) => {
coreData.migration.legacyConfigForMigration = legacyConfigForMigration
})
return legacyConfigForMigration
}
async renameSpecsFolder () {
if (!this.ctx.currentProject) {
throw Error('Need to set currentProject before you can rename specs folder')
}
const projectRoot = this.ctx.path.join(this.ctx.currentProject)
const from = path.join(projectRoot, 'cypress', 'integration')
const to = path.join(projectRoot, 'cypress', 'e2e')
this.ctx.update((coreData) => {
coreData.migration.flags = {
...coreData.migration.flags,
shouldAddCustomE2ESpecPattern: true,
}
})
await this.ctx.fs.move(from, to)
}
async renameSpecFiles (beforeSpecs: string[], afterSpecs: string[]) {
if (!this.ctx.currentProject) {
throw Error('Need to set currentProject before you can rename files')
}
const specsToMove: SpecToMove[] = []
for (let i = 0; i < beforeSpecs.length; i++) {
const from = beforeSpecs[i]
const to = afterSpecs[i]
if (!from || !to) {
throw Error(`Must have matching to and from. Got from: ${from} and to: ${to}`)
}
specsToMove.push({ from, to })
}
const projectRoot = this.ctx.path.join(this.ctx.currentProject)
await moveSpecFiles(projectRoot, specsToMove)
await cleanUpIntegrationFolder(this.ctx.currentProject)
}
async renameSupportFile () {
if (!this.ctx.currentProject) {
throw Error(`Need current project before starting migration!`)
}
const result = await supportFilesForMigration(this.ctx.currentProject)
const beforeRelative = result.before.relative
const afterRelative = result.after.relative
if (!beforeRelative || !afterRelative) {
throw new NonStandardMigrationError('support')
}
await this.ctx.fs.rename(
path.join(this.ctx.currentProject, beforeRelative),
path.join(this.ctx.currentProject, afterRelative),
)
}
async finishReconfigurationWizard () {
this.ctx.lifecycleManager.refreshMetaState()
await this.ctx.lifecycleManager.refreshLifecycle()
}
async nextStep () {
const filteredSteps = this.ctx.coreData.migration.filteredSteps
const index = filteredSteps.indexOf(this.ctx.coreData.migration.step)
if (index === -1) {
throw new Error('Invalid step')
}
const nextIndex = index + 1
if (nextIndex < filteredSteps.length) {
const nextStep = filteredSteps[nextIndex]
if (nextStep) {
this.ctx.update((coreData) => {
coreData.migration.step = nextStep
})
}
} else {
await this.finishReconfigurationWizard()
}
}
async closeManualRenameWatcher () {
await this.ctx.migration.closeManualRenameWatcher()
}
async assertSuccessfulConfigMigration (migratedConfigFile: string = 'cypress.config.js') {
const actual = formatConfig(await this.ctx.file.readFileInProject(migratedConfigFile))
const configExtension = path.extname(migratedConfigFile)
const expected = formatConfig(await this.ctx.file.readFileInProject(`expected-cypress.config${configExtension}`))
if (actual !== expected) {
throw Error(`Expected ${actual} to equal ${expected}`)
}
}
reset (config?: LegacyCypressConfigJson) {
this.ctx.update((coreData) => {
coreData.migration = { ...makeCoreData().migration, legacyConfigForMigration: config }
})
}
}