@wordpress/env 6.0.0 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/README.md +130 -66
  2. package/lib/build-docker-compose-config.js +67 -96
  3. package/lib/cache.js +1 -0
  4. package/lib/cli.js +39 -10
  5. package/lib/commands/clean.js +13 -1
  6. package/lib/commands/destroy.js +21 -42
  7. package/lib/commands/index.js +1 -0
  8. package/lib/commands/install-path.js +1 -0
  9. package/lib/commands/logs.js +1 -0
  10. package/lib/commands/run.js +45 -21
  11. package/lib/commands/start.js +29 -8
  12. package/lib/commands/stop.js +1 -0
  13. package/lib/config/add-or-replace-port.js +12 -3
  14. package/lib/config/db-env.js +1 -0
  15. package/lib/config/detect-directory-type.js +1 -1
  16. package/lib/config/get-cache-directory.js +39 -0
  17. package/lib/config/get-config-from-environment-vars.js +106 -0
  18. package/lib/config/index.js +7 -5
  19. package/lib/config/load-config.js +92 -0
  20. package/lib/config/merge-configs.js +105 -0
  21. package/lib/config/parse-config.js +501 -157
  22. package/lib/config/parse-source-string.js +164 -0
  23. package/lib/config/post-process-config.js +202 -0
  24. package/lib/config/read-raw-config-file.js +7 -33
  25. package/lib/config/test/__snapshots__/config-integration.js.snap +295 -0
  26. package/lib/config/test/add-or-replace-port.js +29 -1
  27. package/lib/config/test/config-integration.js +164 -0
  28. package/lib/config/test/get-cache-directory.js +57 -0
  29. package/lib/config/test/merge-configs.js +121 -0
  30. package/lib/config/test/parse-config.js +393 -0
  31. package/lib/config/test/parse-source-string.js +154 -0
  32. package/lib/config/test/post-process-config.js +299 -0
  33. package/lib/config/test/read-raw-config-file.js +19 -63
  34. package/lib/config/test/validate-config.js +363 -0
  35. package/lib/config/validate-config.js +119 -54
  36. package/lib/env.js +2 -0
  37. package/lib/execute-lifecycle-script.js +86 -0
  38. package/lib/get-host-user.js +27 -0
  39. package/lib/init-config.js +186 -63
  40. package/lib/md5.js +1 -0
  41. package/lib/parse-xdebug-mode.js +1 -0
  42. package/lib/retry.js +1 -0
  43. package/lib/test/__snapshots__/md5.js.snap +9 -0
  44. package/lib/test/build-docker-compose-config.js +134 -0
  45. package/lib/test/cache.js +154 -0
  46. package/lib/test/cli.js +149 -0
  47. package/lib/test/execute-lifecycle-script.js +59 -0
  48. package/lib/test/md5.js +35 -0
  49. package/lib/test/parse-xdebug-mode.js +41 -0
  50. package/lib/validate-run-container.js +41 -0
  51. package/lib/wordpress.js +4 -19
  52. package/package.json +2 -2
  53. package/lib/config/config.js +0 -353
  54. package/lib/config/test/__snapshots__/config.js.snap +0 -83
  55. package/lib/config/test/config.js +0 -1241
@@ -0,0 +1,299 @@
1
+ 'use strict';
2
+ /**
3
+ * Internal dependencies
4
+ */
5
+ const { ValidationError } = require( '..' );
6
+ const postProcessConfig = require( '../post-process-config' );
7
+
8
+ describe( 'postProcessConfig', () => {
9
+ afterEach( () => {
10
+ jest.clearAllMocks();
11
+ } );
12
+
13
+ it( 'should merge relevant root options into environment options', () => {
14
+ const processed = postProcessConfig( {
15
+ port: 123,
16
+ testsPort: 456,
17
+ coreSource: {
18
+ type: 'test',
19
+ },
20
+ config: {
21
+ TESTS_ROOT: 'root',
22
+ },
23
+ pluginSources: [
24
+ {
25
+ type: 'root-plugin',
26
+ },
27
+ ],
28
+ themeSources: [
29
+ {
30
+ type: 'root-theme',
31
+ },
32
+ ],
33
+ mappings: {
34
+ 'root-mapping': {
35
+ type: 'root-mapping',
36
+ },
37
+ },
38
+ env: {
39
+ development: {
40
+ coreSource: {
41
+ type: 'test',
42
+ },
43
+ config: {
44
+ TEST_ENV: 'development',
45
+ },
46
+ pluginSources: [
47
+ {
48
+ type: 'development-plugin',
49
+ },
50
+ ],
51
+ themeSources: [
52
+ {
53
+ type: 'development-theme',
54
+ },
55
+ ],
56
+ mappings: {
57
+ 'development-mapping': {
58
+ type: 'development-mapping',
59
+ },
60
+ },
61
+ },
62
+ tests: {
63
+ coreSource: {
64
+ type: 'test',
65
+ },
66
+ config: {
67
+ TEST_ENV: 'tests',
68
+ },
69
+ },
70
+ },
71
+ } );
72
+
73
+ expect( processed ).toEqual( {
74
+ port: 123,
75
+ testsPort: 456,
76
+ coreSource: {
77
+ type: 'test',
78
+ },
79
+ config: {
80
+ TESTS_ROOT: 'root',
81
+ },
82
+ pluginSources: [
83
+ {
84
+ type: 'root-plugin',
85
+ },
86
+ ],
87
+ themeSources: [
88
+ {
89
+ type: 'root-theme',
90
+ },
91
+ ],
92
+ mappings: {
93
+ 'root-mapping': {
94
+ type: 'root-mapping',
95
+ },
96
+ },
97
+ env: {
98
+ development: {
99
+ port: 123,
100
+ coreSource: {
101
+ type: 'test',
102
+ },
103
+ config: {
104
+ TESTS_ROOT: 'root',
105
+ TEST_ENV: 'development',
106
+ },
107
+ pluginSources: [
108
+ {
109
+ type: 'development-plugin',
110
+ },
111
+ ],
112
+ themeSources: [
113
+ {
114
+ type: 'development-theme',
115
+ },
116
+ ],
117
+ mappings: {
118
+ 'root-mapping': {
119
+ type: 'root-mapping',
120
+ },
121
+ 'development-mapping': {
122
+ type: 'development-mapping',
123
+ },
124
+ },
125
+ },
126
+ tests: {
127
+ port: 456,
128
+ coreSource: {
129
+ type: 'test',
130
+ },
131
+ config: {
132
+ TESTS_ROOT: 'root',
133
+ TEST_ENV: 'tests',
134
+ },
135
+ pluginSources: [
136
+ {
137
+ type: 'root-plugin',
138
+ },
139
+ ],
140
+ themeSources: [
141
+ {
142
+ type: 'root-theme',
143
+ },
144
+ ],
145
+ mappings: {
146
+ 'root-mapping': {
147
+ type: 'root-mapping',
148
+ },
149
+ },
150
+ },
151
+ },
152
+ } );
153
+ } );
154
+
155
+ it( 'should not merge some root options into environment options', () => {
156
+ const processed = postProcessConfig( {
157
+ port: 8888,
158
+ testsPort: 8889,
159
+ lifecycleScripts: {
160
+ afterStart: 'test',
161
+ },
162
+ env: {
163
+ development: {},
164
+ tests: {},
165
+ },
166
+ } );
167
+
168
+ expect( processed ).toEqual( {
169
+ port: 8888,
170
+ testsPort: 8889,
171
+ lifecycleScripts: {
172
+ afterStart: 'test',
173
+ },
174
+ env: {
175
+ development: {
176
+ port: 8888,
177
+ },
178
+ tests: {
179
+ port: 8889,
180
+ },
181
+ },
182
+ } );
183
+ } );
184
+
185
+ describe( 'appendPortToWPConfigs', () => {
186
+ it( 'should add port to certain environment config options', () => {
187
+ const processed = postProcessConfig( {
188
+ port: 123,
189
+ config: {
190
+ WP_TESTS_DOMAIN: 'localhost',
191
+ WP_SITEURL: 'localhost',
192
+ WP_HOME: 'localhost',
193
+ },
194
+ env: {
195
+ development: {
196
+ port: 123,
197
+ },
198
+ tests: {
199
+ port: 456,
200
+ },
201
+ },
202
+ } );
203
+
204
+ expect( processed ).toEqual( {
205
+ // Since the root-level config shouldn't apply to an environment,
206
+ // we shouldn't add the port to the config options for it.
207
+ port: 123,
208
+ config: {
209
+ WP_TESTS_DOMAIN: 'localhost',
210
+ WP_SITEURL: 'localhost',
211
+ WP_HOME: 'localhost',
212
+ },
213
+ env: {
214
+ development: {
215
+ port: 123,
216
+ config: {
217
+ WP_TESTS_DOMAIN: 'localhost:123',
218
+ WP_SITEURL: 'localhost:123',
219
+ WP_HOME: 'localhost:123',
220
+ },
221
+ },
222
+ tests: {
223
+ port: 456,
224
+ config: {
225
+ WP_TESTS_DOMAIN: 'localhost:456',
226
+ WP_SITEURL: 'localhost:456',
227
+ WP_HOME: 'localhost:456',
228
+ },
229
+ },
230
+ },
231
+ } );
232
+ } );
233
+
234
+ it( 'should not overwrite port in WP_HOME', () => {
235
+ const processed = postProcessConfig( {
236
+ env: {
237
+ development: {
238
+ port: 123,
239
+ config: {
240
+ WP_TESTS_DOMAIN: 'localhost:777',
241
+ WP_SITEURL: 'localhost:777',
242
+ WP_HOME: 'localhost:777',
243
+ },
244
+ },
245
+ tests: {
246
+ port: 456,
247
+ config: {
248
+ WP_TESTS_DOMAIN: 'localhost:777',
249
+ WP_SITEURL: 'localhost:777',
250
+ WP_HOME: 'localhost:777',
251
+ },
252
+ },
253
+ },
254
+ } );
255
+
256
+ expect( processed ).toEqual( {
257
+ env: {
258
+ development: {
259
+ port: 123,
260
+ config: {
261
+ WP_TESTS_DOMAIN: 'localhost:123',
262
+ WP_SITEURL: 'localhost:123',
263
+ WP_HOME: 'localhost:777',
264
+ },
265
+ },
266
+ tests: {
267
+ port: 456,
268
+ config: {
269
+ WP_TESTS_DOMAIN: 'localhost:456',
270
+ WP_SITEURL: 'localhost:456',
271
+ WP_HOME: 'localhost:777',
272
+ },
273
+ },
274
+ },
275
+ } );
276
+ } );
277
+ } );
278
+
279
+ describe( 'validatePortUniqueness', () => {
280
+ it( 'should fail when two environments have the same port', () => {
281
+ expect( () => {
282
+ postProcessConfig( {
283
+ env: {
284
+ development: {
285
+ port: 123,
286
+ },
287
+ tests: {
288
+ port: 123,
289
+ },
290
+ },
291
+ } );
292
+ } ).toThrow(
293
+ new ValidationError(
294
+ 'The "development" and "tests" environments may not have the same port.'
295
+ )
296
+ );
297
+ } );
298
+ } );
299
+ } );
@@ -1,3 +1,5 @@
1
+ 'use strict';
2
+ /* eslint-disable jest/no-conditional-expect */
1
3
  /**
2
4
  * External dependencies
3
5
  */
@@ -6,7 +8,8 @@ const { readFile } = require( 'fs' ).promises;
6
8
  /**
7
9
  * Internal dependencies
8
10
  */
9
- const readConfigFile = require( '../read-raw-config-file' );
11
+ const readRawConfigFile = require( '../read-raw-config-file' );
12
+ const { ValidationError } = require( '../validate-config' );
10
13
 
11
14
  jest.mock( 'fs', () => ( {
12
15
  promises: {
@@ -15,76 +18,29 @@ jest.mock( 'fs', () => ( {
15
18
  } ) );
16
19
 
17
20
  describe( 'readRawConfigFile', () => {
18
- beforeEach( () => {
21
+ afterEach( () => {
19
22
  jest.clearAllMocks();
20
23
  } );
21
24
 
22
25
  it( 'returns null if it cannot find a file', async () => {
23
- readFile.mockImplementation( () =>
24
- Promise.reject( { code: 'ENOENT' } )
25
- );
26
- const result = await readConfigFile( 'wp-env', '/.wp-env.json' );
26
+ readFile.mockRejectedValue( { code: 'ENOENT' } );
27
+
28
+ const result = await readRawConfigFile( '/.wp-env.json' );
27
29
  expect( result ).toBe( null );
28
30
  } );
29
31
 
30
- it( 'converts testPort into tests.port', async () => {
31
- readFile.mockImplementation( () =>
32
- Promise.resolve( JSON.stringify( { testsPort: 100 } ) )
33
- );
34
- const result = await readConfigFile( 'wp-env', '/.wp-env.json' );
35
- expect( result ).toEqual( {
36
- env: {
37
- tests: {
38
- port: 100,
39
- },
40
- },
41
- } );
42
- } );
32
+ it( 'rejects when read file fails', async () => {
33
+ readFile.mockRejectedValue( { message: 'Test' } );
43
34
 
44
- it( 'does not overwrite other test config values', async () => {
45
- readFile.mockImplementation( () =>
46
- Promise.resolve(
47
- JSON.stringify( {
48
- testsPort: 100,
49
- env: {
50
- tests: {
51
- something: 'test',
52
- },
53
- },
54
- } )
55
- )
56
- );
57
- const result = await readConfigFile( 'wp-env', '/.wp-env.json' );
58
- expect( result ).toEqual( {
59
- env: {
60
- tests: {
61
- port: 100,
62
- something: 'test',
63
- },
64
- },
65
- } );
66
- } );
35
+ expect.assertions( 1 );
67
36
 
68
- it( 'uses tests.port if both tests.port and testsPort exist', async () => {
69
- readFile.mockImplementation( () =>
70
- Promise.resolve(
71
- JSON.stringify( {
72
- testsPort: 100,
73
- env: {
74
- tests: {
75
- port: 200,
76
- },
77
- },
78
- } )
79
- )
80
- );
81
- const result = await readConfigFile( 'wp-env', '/.wp-env.json' );
82
- expect( result ).toEqual( {
83
- env: {
84
- tests: {
85
- port: 200,
86
- },
87
- },
88
- } );
37
+ try {
38
+ await readRawConfigFile( '/.wp-env.json' );
39
+ } catch ( error ) {
40
+ expect( error ).toEqual(
41
+ new ValidationError( 'Could not read .wp-env.json: Test' )
42
+ );
43
+ }
89
44
  } );
90
45
  } );
46
+ /* eslint-enable jest/no-conditional-expect */