Skip to content

Commit 771f765

Browse files
author
Barthélémy Ledoux
authored
fix: allow migration of pluginsFile using env properties (#20770)
* fix: allow migration of code-coverage plugin * fix: use 9.X options to default migration * fix: remove internal values from options * test: add unit tests to the new model * test: e2e test all error prone defaultValues * fix: avoid removing values in pluginsFile * test: fix tests * test: fix test * fix unit test
1 parent 1033e04 commit 771f765

File tree

8 files changed

+431
-5
lines changed

8 files changed

+431
-5
lines changed

packages/data-context/src/actions/MigrationActions.ts

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from 'path'
33
import { fork } from 'child_process'
44
import type { ForkOptions } from 'child_process'
55
import assert from 'assert'
6+
import _ from 'lodash'
67
import type { DataContext } from '..'
78
import {
89
cleanUpIntegrationFolder,
@@ -23,10 +24,45 @@ import {
2324
getComponentFolder,
2425
getIntegrationTestFilesGlobs,
2526
getSpecPattern,
27+
legacyOptions,
2628
} from '../sources/migration'
2729
import { makeCoreData } from '../data'
2830
import { LegacyPluginsIpc } from '../data/LegacyPluginsIpc'
2931

32+
export function getConfigWithDefaults (legacyConfig: any) {
33+
const newConfig = _.cloneDeep(legacyConfig)
34+
35+
legacyOptions.forEach(({ defaultValue, name }) => {
36+
if (defaultValue !== undefined && legacyConfig[name] === undefined) {
37+
newConfig[name] = typeof defaultValue === 'function' ? defaultValue() : defaultValue
38+
}
39+
})
40+
41+
return newConfig
42+
}
43+
44+
export function getDiff (oldConfig: any, newConfig: any) {
45+
// get all the values updated
46+
const result: any = _.reduce(oldConfig, (acc: any, value, key) => {
47+
// ignore values that have been removed
48+
if (newConfig[key] && !_.isEqual(value, newConfig[key])) {
49+
acc[key] = newConfig[key]
50+
}
51+
52+
return acc
53+
}, {})
54+
55+
// get all the values added
56+
return _.reduce(newConfig, (acc: any, value, key) => {
57+
// their key is in the new config but not in the old config
58+
if (!oldConfig.hasOwnProperty(key)) {
59+
acc[key] = value
60+
}
61+
62+
return acc
63+
}, result)
64+
}
65+
3066
export async function processConfigViaLegacyPlugins (projectRoot: string, legacyConfig: LegacyCypressConfigJson): Promise<LegacyCypressConfigJson> {
3167
const pluginFile = legacyConfig.pluginsFile ?? await tryGetDefaultLegacyPluginsFile(projectRoot)
3268

@@ -49,12 +85,23 @@ export async function processConfigViaLegacyPlugins (projectRoot: string, legacy
4985
const CHILD_PROCESS_FILE_PATH = require.resolve('@packages/server/lib/plugins/child/require_async_child')
5086
const ipc = new LegacyPluginsIpc(fork(CHILD_PROCESS_FILE_PATH, configProcessArgs, childOptions))
5187

88+
const legacyConfigWithDefaults = getConfigWithDefaults(legacyConfig)
89+
5290
ipc.on('ready', () => {
53-
ipc.send('loadLegacyPlugins', legacyConfig)
91+
ipc.send('loadLegacyPlugins', legacyConfigWithDefaults)
5492
})
5593

5694
ipc.on('loadLegacyPlugins:reply', (modifiedLegacyConfig) => {
57-
resolve(modifiedLegacyConfig)
95+
const diff = getDiff(legacyConfigWithDefaults, modifiedLegacyConfig)
96+
97+
// if env is updated by plugins, avoid adding it to the config file
98+
if (diff.env) {
99+
delete diff.env
100+
}
101+
102+
const legacyConfigWithChanges = _.merge(legacyConfig, diff)
103+
104+
resolve(legacyConfigWithChanges)
58105
ipc.childProcess.kill()
59106
})
60107

packages/data-context/src/sources/MigrationDataSource.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export type LegacyCypressConfigJson = Partial<{
3131
integrationFolder: string
3232
testFiles: string | string[]
3333
ignoreTestFiles: string | string[]
34+
env: { [key: string]: any }
35+
[index: string]: any
3436
}>
3537

3638
export interface MigrationFile {

packages/data-context/src/sources/migration/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
export * from './autoRename'
55
export * from './codegen'
66
export * from './format'
7+
export * from './legacyOptions'
78
export * from './parserUtils'
89
export * from './regexps'
910
export * from './shouldShowSteps'

0 commit comments

Comments
 (0)