From 071675ee4102d8806d50ae558ee2cc6adbcf5e32 Mon Sep 17 00:00:00 2001 From: uid11 Date: Sat, 23 Sep 2023 12:52:29 +0300 Subject: [PATCH] fix: update pack config by values from `doBeforePack` functions --- autotests/tests/main/exists.ts | 5 +++ src/utils/events/registerStartE2edRunEvent.ts | 5 +++ src/utils/fs/readStartInfoSync.ts | 15 +++++++ src/utils/getFullPackConfig.ts | 23 ----------- .../getFullPackConfig/getFullPackConfig.ts | 40 +++++++++++++++++++ src/utils/getFullPackConfig/index.ts | 3 ++ src/utils/getFullPackConfig/updateConfig.ts | 20 ++++++++++ 7 files changed, 88 insertions(+), 23 deletions(-) create mode 100644 src/utils/fs/readStartInfoSync.ts delete mode 100644 src/utils/getFullPackConfig.ts create mode 100644 src/utils/getFullPackConfig/getFullPackConfig.ts create mode 100644 src/utils/getFullPackConfig/index.ts create mode 100644 src/utils/getFullPackConfig/updateConfig.ts diff --git a/autotests/tests/main/exists.ts b/autotests/tests/main/exists.ts index 3041f094..d6213b49 100644 --- a/autotests/tests/main/exists.ts +++ b/autotests/tests/main/exists.ts @@ -32,6 +32,11 @@ it('exists', {meta: {testId: '1'}, testIdleTimeout: 35_000, testTimeout: 90_000} await expect(customPackProperties.name, 'custom pack properties is correct').eql('allTests'); + await expect( + customPackProperties.internalPackRunId, + 'dynamic custom pack properties is correct', + ).gt(0); + const mainPage = await navigateToPage(Main, {language}); await expect(mainPage.pageParams, 'pageParams is correct after navigateToPage').eql({language}); diff --git a/src/utils/events/registerStartE2edRunEvent.ts b/src/utils/events/registerStartE2edRunEvent.ts index d161b98b..9fcfb3a9 100644 --- a/src/utils/events/registerStartE2edRunEvent.ts +++ b/src/utils/events/registerStartE2edRunEvent.ts @@ -3,6 +3,7 @@ import {EVENTS_DIRECTORY_PATH, TMP_DIRECTORY_PATH} from '../../constants/interna import {createDirectory, removeDirectory, writeStartInfo} from '../fs'; import {generalLog, writeLogsToFile} from '../generalLog'; +import {getFullPackConfig, updateConfig} from '../getFullPackConfig'; import {compilePack, setPackTimeout} from '../pack'; import {getStartInfo} from '../startInfo'; @@ -22,6 +23,10 @@ export const registerStartE2edRunEvent = async (): Promise => { await runBeforePackFunctions(startInfo); + const fullPackConfig = getFullPackConfig(); + + updateConfig(fullPackConfig, startInfo); + const {e2ed, runEnvironment} = startInfo; const isDockerRun = runEnvironment === RunEnvironment.Docker; const startMessage = `Run tests ${isDockerRun ? 'in docker' : 'local'} with e2ed@${e2ed.version}`; diff --git a/src/utils/fs/readStartInfoSync.ts b/src/utils/fs/readStartInfoSync.ts new file mode 100644 index 00000000..ff5a24e8 --- /dev/null +++ b/src/utils/fs/readStartInfoSync.ts @@ -0,0 +1,15 @@ +import {readFileSync} from 'node:fs'; + +import {READ_FILE_OPTIONS, START_INFO_PATH} from '../../constants/internal'; + +import type {StartInfo} from '../../types/internal'; + +/** + * Read start info from tmp directory in sync manner. + * @internal + */ +export const readStartInfoSync = (): StartInfo => { + const startInfoJsonString = readFileSync(START_INFO_PATH, READ_FILE_OPTIONS); + + return JSON.parse(startInfoJsonString) as StartInfo; +}; diff --git a/src/utils/getFullPackConfig.ts b/src/utils/getFullPackConfig.ts deleted file mode 100644 index 347bb21c..00000000 --- a/src/utils/getFullPackConfig.ts +++ /dev/null @@ -1,23 +0,0 @@ -import type {FullPackConfig} from '../types/internal'; - -/** - * Get full pack configuration object. - * This function can only be called after the E2edRunEvent is registered, - * because the packs with configurations (e2ed/packs) is compiled when this event is registered. - */ -export const getFullPackConfig = < - CustomPackProperties = unknown, - CustomReportProperties = unknown, - SkipTests = unknown, - TestMeta = unknown, ->(): FullPackConfig => { - // eslint-disable-next-line global-require, @typescript-eslint/no-var-requires - const {fullPackConfig} = require('../testcaferc'); - - return fullPackConfig as unknown as FullPackConfig< - CustomPackProperties, - CustomReportProperties, - SkipTests, - TestMeta - >; -}; diff --git a/src/utils/getFullPackConfig/getFullPackConfig.ts b/src/utils/getFullPackConfig/getFullPackConfig.ts new file mode 100644 index 00000000..2ace521b --- /dev/null +++ b/src/utils/getFullPackConfig/getFullPackConfig.ts @@ -0,0 +1,40 @@ +// eslint-disable-next-line import/no-internal-modules +import {readStartInfoSync} from '../fs/readStartInfoSync'; + +import {updateConfig} from './updateConfig'; + +import type {FullPackConfig} from '../../types/internal'; + +let updatedConfig: FullPackConfig | undefined; + +/** + * Get full pack configuration object. + * This function can only be called after the `E2edRunEvent` is registered, + * because the packs with configurations (`e2ed/packs`) is compiled when this event is registered. + */ +export const getFullPackConfig = < + CustomPackProperties = unknown, + CustomReportProperties = unknown, + SkipTests = unknown, + TestMeta = unknown, +>(): FullPackConfig => { + if (updatedConfig === undefined) { + // eslint-disable-next-line global-require, @typescript-eslint/no-var-requires + const {fullPackConfig} = require('../../testcaferc'); + + updatedConfig = fullPackConfig; + + try { + const startInfo = readStartInfoSync(); + + updateConfig(updatedConfig, startInfo); + } catch (error) {} + } + + return updatedConfig as unknown as FullPackConfig< + CustomPackProperties, + CustomReportProperties, + SkipTests, + TestMeta + >; +}; diff --git a/src/utils/getFullPackConfig/index.ts b/src/utils/getFullPackConfig/index.ts new file mode 100644 index 00000000..d5030a4a --- /dev/null +++ b/src/utils/getFullPackConfig/index.ts @@ -0,0 +1,3 @@ +export {getFullPackConfig} from './getFullPackConfig'; +/** @internal */ +export {updateConfig} from './updateConfig'; diff --git a/src/utils/getFullPackConfig/updateConfig.ts b/src/utils/getFullPackConfig/updateConfig.ts new file mode 100644 index 00000000..9c607a8b --- /dev/null +++ b/src/utils/getFullPackConfig/updateConfig.ts @@ -0,0 +1,20 @@ +import type {FullPackConfig, StartInfo} from '../../types/internal'; + +/** + * Updates full pack config by values from `startInfo` (getted by `doBeforePack` functions). + * @internal + */ +export const updateConfig = (fullPackConfig: FullPackConfig, startInfo: StartInfo): void => { + for (const field of Object.keys(fullPackConfig) as (keyof FullPackConfig)[]) { + if (field === 'doAfterPack' || field === 'doBeforePack') { + continue; + } + + if (typeof fullPackConfig[field] === 'function') { + continue; + } + + // @ts-expect-error: full pack config have different types of field values + fullPackConfig[field] = startInfo.fullPackConfig[field]; // eslint-disable-line no-param-reassign + } +};