Skip to content

Commit 2ef2193

Browse files
authored
Merge pull request #2244 from Automattic/add/dev-env-overrides
feat(dev-env): overrides for the default configuration
2 parents 1b71e29 + 9a805f7 commit 2ef2193

File tree

5 files changed

+93
-1
lines changed

5 files changed

+93
-1
lines changed

__tests__/devenv-e2e/013-configuration-file.spec.js

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it, jest } from '@jest/globals';
2-
import { mkdtemp, rm, mkdir, realpath } from 'node:fs/promises';
2+
import { mkdtemp, rm, mkdir, realpath, readFile, stat } from 'node:fs/promises';
33
import os from 'node:os';
44
import path from 'node:path';
55
import xdgBaseDir from 'xdg-basedir';
@@ -334,4 +334,80 @@ describe( 'vip dev-env configuration file', () => {
334334
cron: ! expectedCron,
335335
} );
336336
} );
337+
338+
it( 'should create .lando.local.yml if overrides have been provided', async () => {
339+
const slug = getProjectSlug();
340+
const expectedOverrides = `services:\n wordpress:\n services:\n pull_policy: never\n`;
341+
342+
expect( await checkEnvExists( slug ) ).toBe( false );
343+
344+
await writeConfigurationFile( tmpWorkingDirectoryPath, {
345+
'configuration-version': '1',
346+
slug,
347+
overrides: expectedOverrides,
348+
} );
349+
350+
const spawnOptions = {
351+
env,
352+
cwd: tmpWorkingDirectoryPath,
353+
};
354+
355+
const result = await cliTest.spawn(
356+
[ process.argv[ 0 ], vipDevEnvCreate ],
357+
spawnOptions,
358+
true
359+
);
360+
expect( result.rc ).toBe( 0 );
361+
expect( result.stdout ).toContain( `Using environment ${ slug }` );
362+
expect( result.stderr ).toBe( '' );
363+
364+
const data = readEnvironmentData( slug );
365+
expect( data ).toMatchObject( {
366+
siteSlug: slug,
367+
overrides: expectedOverrides,
368+
} );
369+
370+
const overrideFile = path.join( tmpPath, 'vip', 'dev-environment', slug, '.lando.local.yml' );
371+
const overrideContent = await readFile( overrideFile, 'utf8' );
372+
expect( overrideContent ).toBe( expectedOverrides );
373+
} );
374+
375+
it( 'should remove .lando.local.yml if no overrides have been provided', async () => {
376+
const slug = getProjectSlug();
377+
const overrides = `services:\n wordpress:\n services:\n pull_policy: never\n`;
378+
379+
expect( await checkEnvExists( slug ) ).toBe( false );
380+
381+
await writeConfigurationFile( tmpWorkingDirectoryPath, {
382+
'configuration-version': '1',
383+
slug,
384+
overrides,
385+
} );
386+
387+
const spawnOptions = {
388+
env,
389+
cwd: tmpWorkingDirectoryPath,
390+
};
391+
392+
let result = await cliTest.spawn( [ process.argv[ 0 ], vipDevEnvCreate ], spawnOptions, true );
393+
expect( result.rc ).toBe( 0 );
394+
expect( result.stdout ).toContain( `Using environment ${ slug }` );
395+
expect( result.stderr ).toBe( '' );
396+
397+
await writeConfigurationFile( tmpWorkingDirectoryPath, {
398+
'configuration-version': '1',
399+
slug,
400+
overrides: '',
401+
} );
402+
403+
result = await cliTest.spawn( [ process.argv[ 0 ], vipDevEnvUpdate ], spawnOptions, true );
404+
expect( result.rc ).toBe( 0 );
405+
expect( result.stderr ).toBe( '' );
406+
407+
const data = readEnvironmentData( slug );
408+
expect( data.overrides ).toBeFalsy();
409+
410+
const overrideFile = path.join( tmpPath, 'vip', 'dev-environment', slug, '.lando.local.yml' );
411+
return expect( stat( overrideFile ) ).rejects.toThrow();
412+
} );
337413
} );

src/lib/dev-environment/dev-environment-cli.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ export async function promptForArguments(
309309
mailpit: false,
310310
photon: false,
311311
cron: false,
312+
overrides: preselectedOptions.overrides,
312313
};
313314

314315
const promptLabels = {

src/lib/dev-environment/dev-environment-configuration-file.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ function sanitizeConfiguration(
116116
'media-redirect-domain': configuration[ 'media-redirect-domain' ]?.toString(),
117117
photon: stringToBooleanIfDefined( configuration.photon ),
118118
cron: stringToBooleanIfDefined( configuration.cron ),
119+
overrides: configuration.overrides?.toString(),
119120
meta: configurationMeta,
120121
};
121122

@@ -174,6 +175,7 @@ export function mergeConfigurationFileOptions(
174175
mediaRedirectDomain: configurationFileOptions[ 'media-redirect-domain' ],
175176
photon: configurationFileOptions.photon,
176177
cron: configurationFileOptions.cron,
178+
overrides: configurationFileOptions.overrides,
177179
};
178180

179181
const mergedOptions: InstanceOptions = {};

src/lib/dev-environment/dev-environment-core.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ const nginxFileTemplatePath = path.join(
7373
'dev-env.nginx.template.conf.ejs'
7474
);
7575
const landoFileName = '.lando.yml';
76+
const landoOverridesFileName = '.lando.local.yml';
7677
const landoBackupFileName = '.lando.backup.yml';
7778
const nginxFileName = 'extra.conf';
7879
const instanceDataFileName = 'instance_data.json';
@@ -257,6 +258,8 @@ function preProcessInstanceData( instanceData: InstanceData ): InstanceData {
257258
newInstanceData.autologinKey = uuid();
258259
newInstanceData.version = DEV_ENVIRONMENT_VERSION;
259260

261+
newInstanceData.overrides = instanceData.overrides ?? '';
262+
260263
return newInstanceData;
261264
}
262265

@@ -557,6 +560,7 @@ async function prepareLandoEnv(
557560
const instanceDataFile = JSON.stringify( instanceData );
558561

559562
const landoFileTargetPath = path.join( instancePath, landoFileName );
563+
const landoOverridesFileTargetPath = path.join( instancePath, landoOverridesFileName );
560564
const landoBackupFileTargetPath = path.join( instancePath, landoBackupFileName );
561565
const nginxFolderPath = path.join( instancePath, nginxPathString );
562566
const nginxFileTargetPath = path.join( nginxFolderPath, nginxFileName );
@@ -586,6 +590,12 @@ async function prepareLandoEnv(
586590
debug( `Instance data file created in ${ instanceDataTargetPath }` );
587591

588592
await writeIntegrationsConfig( instancePath, integrationsConfig );
593+
594+
if ( instanceData.overrides ) {
595+
await fs.promises.writeFile( landoOverridesFileTargetPath, instanceData.overrides );
596+
} else {
597+
await fs.promises.rm( landoOverridesFileTargetPath, { force: true } );
598+
}
589599
}
590600

591601
export function getAllEnvironmentNames(): string[] {

src/lib/dev-environment/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface InstanceOptions {
1616
mailpit?: boolean;
1717
photon?: boolean;
1818
cron?: boolean;
19+
overrides?: string;
1920

2021
[ index: string ]: unknown;
2122
}
@@ -86,6 +87,7 @@ export interface ConfigurationFileOptions {
8687
'media-redirect-domain'?: string;
8788
photon?: boolean;
8889
cron?: boolean;
90+
overrides?: string;
8991

9092
meta?: ConfigurationFileMeta;
9193
[ index: string ]: unknown;
@@ -116,4 +118,5 @@ export interface InstanceData {
116118
pullAfter?: number;
117119
autologinKey?: string;
118120
version?: string;
121+
overrides?: string;
119122
}

0 commit comments

Comments
 (0)