@wordpress/env 5.16.0 → 7.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 (54) hide show
  1. package/README.md +189 -120
  2. package/lib/build-docker-compose-config.js +67 -96
  3. package/lib/cache.js +1 -0
  4. package/lib/cli.js +23 -3
  5. package/lib/commands/clean.js +14 -1
  6. package/lib/commands/destroy.js +12 -37
  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 +79 -15
  11. package/lib/commands/start.js +32 -10
  12. package/lib/commands/stop.js +1 -0
  13. package/lib/config/add-or-replace-port.js +41 -0
  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 +86 -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 +104 -0
  21. package/lib/config/parse-config.js +418 -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 +271 -0
  26. package/lib/config/test/add-or-replace-port.js +81 -0
  27. package/lib/config/test/config-integration.js +143 -0
  28. package/lib/config/test/get-cache-directory.js +57 -0
  29. package/lib/config/test/merge-configs.js +111 -0
  30. package/lib/config/test/parse-config.js +342 -0
  31. package/lib/config/test/parse-source-string.js +154 -0
  32. package/lib/config/test/post-process-config.js +295 -0
  33. package/lib/config/test/read-raw-config-file.js +19 -63
  34. package/lib/config/test/validate-config.js +305 -0
  35. package/lib/config/validate-config.js +103 -40
  36. package/lib/env.js +2 -0
  37. package/lib/execute-after-setup.js +51 -0
  38. package/lib/get-host-user.js +31 -0
  39. package/lib/init-config.js +181 -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-after-setup.js +66 -0
  48. package/lib/test/md5.js +35 -0
  49. package/lib/test/parse-xdebug-mode.js +41 -0
  50. package/lib/wordpress.js +5 -33
  51. package/package.json +2 -2
  52. package/lib/config/config.js +0 -357
  53. package/lib/config/test/__snapshots__/config.js.snap +0 -83
  54. package/lib/config/test/config.js +0 -1203
@@ -0,0 +1,342 @@
1
+ 'use strict';
2
+ /* eslint-disable jest/no-conditional-expect */
3
+ /**
4
+ * External dependencies
5
+ */
6
+ const path = require( 'path' );
7
+
8
+ /**
9
+ * Internal dependencies
10
+ */
11
+ const { parseConfig } = require( '../parse-config' );
12
+ const readRawConfigFile = require( '../read-raw-config-file' );
13
+ const { getLatestWordPressVersion } = require( '../../wordpress' );
14
+ const { ValidationError } = require( '../validate-config' );
15
+ const detectDirectoryType = require( '../detect-directory-type' );
16
+
17
+ jest.mock( 'got', () => jest.fn() );
18
+ jest.mock( '../read-raw-config-file', () => jest.fn() );
19
+ jest.mock( '../detect-directory-type', () => jest.fn() );
20
+ jest.mock( '../../wordpress', () => ( {
21
+ getLatestWordPressVersion: jest.fn(),
22
+ } ) );
23
+
24
+ /**
25
+ * Since our configurations are merged, we will want to refer to the parsed default config frequently.
26
+ */
27
+ const DEFAULT_CONFIG = {
28
+ port: 8888,
29
+ testsPort: 8889,
30
+ phpVersion: null,
31
+ coreSource: {
32
+ type: 'git',
33
+ url: 'https://github.com/WordPress/WordPress.git',
34
+ ref: '100.0.0',
35
+ path: '/cache/WordPress',
36
+ clonePath: '/cache/WordPress',
37
+ basename: 'WordPress',
38
+ testsPath: '/cache/tests-WordPress',
39
+ },
40
+ pluginSources: [],
41
+ themeSources: [],
42
+ config: {
43
+ WP_DEBUG: true,
44
+ SCRIPT_DEBUG: true,
45
+ WP_ENVIRONMENT_TYPE: 'local',
46
+ WP_PHP_BINARY: 'php',
47
+ WP_TESTS_EMAIL: 'admin@example.org',
48
+ WP_TESTS_TITLE: 'Test Blog',
49
+ WP_TESTS_DOMAIN: 'localhost',
50
+ WP_SITEURL: 'http://localhost',
51
+ WP_HOME: 'http://localhost',
52
+ },
53
+ mappings: {},
54
+ afterSetup: null,
55
+ env: {
56
+ development: {},
57
+ tests: {
58
+ config: {
59
+ WP_DEBUG: false,
60
+ SCRIPT_DEBUG: false,
61
+ },
62
+ },
63
+ },
64
+ };
65
+
66
+ describe( 'parseConfig', () => {
67
+ beforeEach( () => {
68
+ readRawConfigFile.mockResolvedValue( null );
69
+ detectDirectoryType.mockResolvedValue( null );
70
+ getLatestWordPressVersion.mockResolvedValue( '100.0.0' );
71
+ } );
72
+
73
+ afterEach( () => {
74
+ jest.clearAllMocks();
75
+ delete process.env.WP_ENV_PORT;
76
+ delete process.env.WP_ENV_TESTS_PORT;
77
+ delete process.env.WP_ENV_CORE;
78
+ delete process.env.WP_ENV_PHP_VERSION;
79
+ delete process.env.WP_ENV_AFTER_SETUP;
80
+ } );
81
+
82
+ it( 'should return default config', async () => {
83
+ const parsed = await parseConfig( './', '/cache' );
84
+
85
+ expect( parsed ).toEqual( DEFAULT_CONFIG );
86
+ } );
87
+
88
+ it( 'should infer a core mounting default when ran from a WordPress directory', async () => {
89
+ detectDirectoryType.mockResolvedValue( 'core' );
90
+
91
+ const parsed = await parseConfig( './', '/cache' );
92
+
93
+ expect( parsed ).toEqual( {
94
+ ...DEFAULT_CONFIG,
95
+ coreSource: {
96
+ basename: 'gutenberg',
97
+ path: path.resolve( '.' ),
98
+ testsPath: '/cache/tests-gutenberg',
99
+ type: 'local',
100
+ },
101
+ } );
102
+ } );
103
+
104
+ it( 'should infer a plugin mounting default when ran from a plugin directory', async () => {
105
+ detectDirectoryType.mockResolvedValue( 'plugin' );
106
+
107
+ const parsed = await parseConfig( './', '/cache' );
108
+
109
+ expect( parsed ).toEqual( {
110
+ ...DEFAULT_CONFIG,
111
+ pluginSources: [
112
+ {
113
+ basename: 'gutenberg',
114
+ path: path.resolve( '.' ),
115
+ type: 'local',
116
+ },
117
+ ],
118
+ } );
119
+ } );
120
+
121
+ it( 'should infer a theme mounting default when ran from a theme directory', async () => {
122
+ detectDirectoryType.mockResolvedValue( 'theme' );
123
+
124
+ const parsed = await parseConfig( './', '/cache' );
125
+
126
+ expect( parsed ).toEqual( {
127
+ ...DEFAULT_CONFIG,
128
+ themeSources: [
129
+ {
130
+ basename: 'gutenberg',
131
+ path: path.resolve( '.' ),
132
+ type: 'local',
133
+ },
134
+ ],
135
+ } );
136
+ } );
137
+
138
+ it( 'should merge configs with precedence', async () => {
139
+ readRawConfigFile.mockImplementation( async ( configFile ) => {
140
+ if ( configFile === path.resolve( './.wp-env.json' ) ) {
141
+ return {
142
+ core: 'WordPress/WordPress#Test',
143
+ phpVersion: '1.0',
144
+ afterSetup: 'test',
145
+ env: {
146
+ development: {
147
+ port: 1234,
148
+ },
149
+ tests: {
150
+ port: 5678,
151
+ },
152
+ },
153
+ };
154
+ }
155
+
156
+ if ( configFile === path.resolve( './.wp-env.override.json' ) ) {
157
+ return {
158
+ phpVersion: '2.0',
159
+ env: {
160
+ tests: {
161
+ port: 1011,
162
+ },
163
+ },
164
+ };
165
+ }
166
+
167
+ throw new Error( 'Invalid File: ' + configFile );
168
+ } );
169
+
170
+ const parsed = await parseConfig( './', '/cache' );
171
+
172
+ const expected = {
173
+ ...DEFAULT_CONFIG,
174
+ coreSource: {
175
+ basename: 'WordPress',
176
+ path: '/cache/WordPress',
177
+ clonePath: '/cache/WordPress',
178
+ ref: 'Test',
179
+ testsPath: '/cache/tests-WordPress',
180
+ url: 'https://github.com/WordPress/WordPress.git',
181
+ type: 'git',
182
+ },
183
+ phpVersion: '2.0',
184
+ afterSetup: 'test',
185
+ env: {
186
+ development: {
187
+ ...DEFAULT_CONFIG.env.development,
188
+ port: 1234,
189
+ },
190
+ tests: {
191
+ ...DEFAULT_CONFIG.env.tests,
192
+ port: 1011,
193
+ },
194
+ },
195
+ };
196
+ expect( parsed ).toEqual( expected );
197
+ } );
198
+
199
+ it( 'should parse core, plugin, theme, and mapping sources', async () => {
200
+ readRawConfigFile.mockImplementation( async ( configFile ) => {
201
+ if ( configFile === path.resolve( '.', './.wp-env.json' ) ) {
202
+ return {
203
+ core: 'WordPress/WordPress#Test',
204
+ plugins: [ 'WordPress/TestPlugin#Test' ],
205
+ themes: [ 'WordPress/TestTheme#Test' ],
206
+ mappings: {
207
+ '/var/www/html/wp-content/plugins/test-mapping':
208
+ 'WordPress/TestMapping#Test',
209
+ },
210
+ };
211
+ }
212
+
213
+ if (
214
+ configFile === path.resolve( '.', './.wp-env.override.json' )
215
+ ) {
216
+ return {};
217
+ }
218
+
219
+ throw new Error( 'Invalid File: ' + configFile );
220
+ } );
221
+
222
+ const parsed = await parseConfig( './', '/cache' );
223
+
224
+ expect( parsed ).toEqual( {
225
+ ...DEFAULT_CONFIG,
226
+ coreSource: {
227
+ basename: 'WordPress',
228
+ path: '/cache/WordPress',
229
+ clonePath: '/cache/WordPress',
230
+ ref: 'Test',
231
+ testsPath: '/cache/tests-WordPress',
232
+ url: 'https://github.com/WordPress/WordPress.git',
233
+ type: 'git',
234
+ },
235
+ pluginSources: [
236
+ {
237
+ basename: 'TestPlugin',
238
+ path: '/cache/TestPlugin',
239
+ clonePath: '/cache/TestPlugin',
240
+ ref: 'Test',
241
+ url: 'https://github.com/WordPress/TestPlugin.git',
242
+ type: 'git',
243
+ },
244
+ ],
245
+ themeSources: [
246
+ {
247
+ basename: 'TestTheme',
248
+ path: '/cache/TestTheme',
249
+ clonePath: '/cache/TestTheme',
250
+ ref: 'Test',
251
+ url: 'https://github.com/WordPress/TestTheme.git',
252
+ type: 'git',
253
+ },
254
+ ],
255
+ mappings: {
256
+ '/var/www/html/wp-content/plugins/test-mapping': {
257
+ basename: 'TestMapping',
258
+ path: '/cache/TestMapping',
259
+ clonePath: '/cache/TestMapping',
260
+ ref: 'Test',
261
+ url: 'https://github.com/WordPress/TestMapping.git',
262
+ type: 'git',
263
+ },
264
+ },
265
+ } );
266
+ } );
267
+
268
+ it( 'should override with environment variables', async () => {
269
+ process.env.WP_ENV_PORT = 123;
270
+ process.env.WP_ENV_TESTS_PORT = 456;
271
+ process.env.WP_ENV_CORE = 'WordPress/WordPress#test';
272
+ process.env.WP_ENV_PHP_VERSION = '3.0';
273
+ process.env.WP_ENV_AFTER_SETUP = 'test after';
274
+
275
+ const parsed = await parseConfig( './', '/cache' );
276
+
277
+ expect( parsed ).toEqual( {
278
+ ...DEFAULT_CONFIG,
279
+ port: 123,
280
+ testsPort: 456,
281
+ coreSource: {
282
+ basename: 'WordPress',
283
+ path: '/cache/WordPress',
284
+ clonePath: '/cache/WordPress',
285
+ ref: 'test',
286
+ testsPath: '/cache/tests-WordPress',
287
+ url: 'https://github.com/WordPress/WordPress.git',
288
+ type: 'git',
289
+ },
290
+ phpVersion: '3.0',
291
+ afterSetup: 'test after',
292
+ env: {
293
+ development: {
294
+ port: 123,
295
+ phpVersion: '3.0',
296
+ coreSource: {
297
+ basename: 'WordPress',
298
+ path: '/cache/WordPress',
299
+ clonePath: '/cache/WordPress',
300
+ ref: 'test',
301
+ testsPath: '/cache/tests-WordPress',
302
+ url: 'https://github.com/WordPress/WordPress.git',
303
+ type: 'git',
304
+ },
305
+ },
306
+ tests: {
307
+ port: 456,
308
+ phpVersion: '3.0',
309
+ coreSource: {
310
+ basename: 'WordPress',
311
+ path: '/cache/WordPress',
312
+ clonePath: '/cache/WordPress',
313
+ ref: 'test',
314
+ testsPath: '/cache/tests-WordPress',
315
+ url: 'https://github.com/WordPress/WordPress.git',
316
+ type: 'git',
317
+ },
318
+ config: {
319
+ WP_DEBUG: false,
320
+ SCRIPT_DEBUG: false,
321
+ },
322
+ },
323
+ },
324
+ } );
325
+ } );
326
+
327
+ it( 'throws when latest WordPress version needed but not found', async () => {
328
+ getLatestWordPressVersion.mockResolvedValue( null );
329
+
330
+ expect.assertions( 1 );
331
+ try {
332
+ await parseConfig( './', '/cache' );
333
+ } catch ( error ) {
334
+ expect( error ).toEqual(
335
+ new ValidationError(
336
+ 'Could not find the latest WordPress version. There may be a network issue.'
337
+ )
338
+ );
339
+ }
340
+ } );
341
+ } );
342
+ /* eslint-enable jest/no-conditional-expect */
@@ -0,0 +1,154 @@
1
+ 'use strict';
2
+ /* eslint-disable jest/no-conditional-expect */
3
+ /**
4
+ * External dependencies
5
+ */
6
+ const path = require( 'path' );
7
+ const { homedir } = require( 'os' );
8
+
9
+ /**
10
+ * Internal dependencies
11
+ */
12
+ const { ValidationError } = require( '../validate-config' );
13
+ const { parseSourceString } = require( '../parse-source-string' );
14
+
15
+ jest.mock( 'os', () => ( {
16
+ homedir: jest.fn(),
17
+ } ) );
18
+
19
+ describe( 'parseSourceString', () => {
20
+ const options = {
21
+ cacheDirectoryPath: '/test/cache',
22
+ };
23
+
24
+ beforeEach( () => {
25
+ homedir.mockReturnValue( '/home/test' );
26
+ } );
27
+
28
+ it( 'should do nothing when given an empty source', () => {
29
+ expect( parseSourceString( null, options ) ).toEqual( null );
30
+ } );
31
+
32
+ it( 'should throw when source not parseable', () => {
33
+ expect.assertions( 1 );
34
+ try {
35
+ parseSourceString( 'test://test', options );
36
+ } catch ( error ) {
37
+ expect( error ).toEqual(
38
+ new ValidationError(
39
+ 'Invalid or unrecognized source: "test://test".'
40
+ )
41
+ );
42
+ }
43
+ } );
44
+
45
+ describe( 'local sources', () => {
46
+ it( 'should parse relative directories', () => {
47
+ expect( parseSourceString( '.', options ) ).toEqual( {
48
+ basename: 'gutenberg',
49
+ path: path.resolve( '.' ),
50
+ type: 'local',
51
+ } );
52
+ } );
53
+
54
+ it( 'should parse home directories', () => {
55
+ expect( parseSourceString( '~/test', options ) ).toEqual( {
56
+ basename: 'test',
57
+ path: '/home/test/test',
58
+ type: 'local',
59
+ } );
60
+ } );
61
+
62
+ it( 'should parse absolute directories', () => {
63
+ expect( parseSourceString( '/absolute/test', options ) ).toEqual( {
64
+ basename: 'test',
65
+ path: '/absolute/test',
66
+ type: 'local',
67
+ } );
68
+ } );
69
+ } );
70
+
71
+ describe( 'zip sources', () => {
72
+ it( 'should parse WordPress.org sources', () => {
73
+ expect(
74
+ parseSourceString(
75
+ 'http://downloads.wordpress.org/plugin/gutenberg.zip',
76
+ options
77
+ )
78
+ ).toEqual( {
79
+ basename: 'gutenberg',
80
+ path: '/test/cache/gutenberg',
81
+ type: 'zip',
82
+ url: 'http://downloads.wordpress.org/plugin/gutenberg.zip',
83
+ } );
84
+ } );
85
+
86
+ it( 'should parse other sources', () => {
87
+ expect(
88
+ parseSourceString( 'http://test.com/testing.zip', options )
89
+ ).toEqual( {
90
+ basename: 'testing',
91
+ path: '/test/cache/testing',
92
+ type: 'zip',
93
+ url: 'http://test.com/testing.zip',
94
+ } );
95
+ } );
96
+ } );
97
+
98
+ describe( 'Git SSH sources', () => {
99
+ it( 'should parse ssh protocol', () => {
100
+ expect(
101
+ parseSourceString( 'ssh://test/test.git#trunk', options )
102
+ ).toEqual( {
103
+ basename: 'test',
104
+ path: '/test/cache/test',
105
+ clonePath: '/test/cache/test',
106
+ ref: 'trunk',
107
+ type: 'git',
108
+ url: 'ssh://test/test.git',
109
+ } );
110
+ } );
111
+
112
+ it( 'should parse git+ssh protocol', () => {
113
+ expect(
114
+ parseSourceString( 'git+ssh://test/test.git#trunk', options )
115
+ ).toEqual( {
116
+ basename: 'test',
117
+ path: '/test/cache/test',
118
+ clonePath: '/test/cache/test',
119
+ ref: 'trunk',
120
+ type: 'git',
121
+ url: 'git+ssh://test/test.git',
122
+ } );
123
+ } );
124
+
125
+ it( 'should work without ref', () => {
126
+ expect(
127
+ parseSourceString( 'ssh://test/test.git', options )
128
+ ).toEqual( {
129
+ basename: 'test',
130
+ path: '/test/cache/test',
131
+ clonePath: '/test/cache/test',
132
+ ref: undefined,
133
+ type: 'git',
134
+ url: 'ssh://test/test.git',
135
+ } );
136
+ } );
137
+ } );
138
+
139
+ describe( 'GitHub sources', () => {
140
+ it( 'should parse', () => {
141
+ expect(
142
+ parseSourceString( 'WordPress/gutenberg#trunk', options )
143
+ ).toEqual( {
144
+ basename: 'gutenberg',
145
+ path: '/test/cache/gutenberg',
146
+ clonePath: '/test/cache/gutenberg',
147
+ ref: 'trunk',
148
+ type: 'git',
149
+ url: 'https://github.com/WordPress/gutenberg.git',
150
+ } );
151
+ } );
152
+ } );
153
+ } );
154
+ /* eslint-enable jest/no-conditional-expect */