@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,81 @@
1
+ 'use strict';
2
+ /**
3
+ * Internal dependencies
4
+ */
5
+ const addOrReplacePort = require( '../add-or-replace-port.js' );
6
+
7
+ describe( 'addOrReplacePort', () => {
8
+ afterEach( () => {
9
+ jest.clearAllMocks();
10
+ } );
11
+
12
+ it( 'should add or replace port with various inputs', () => {
13
+ const testMap = [
14
+ // Addition
15
+ { in: 'test', expect: 'test:101' },
16
+ { in: 'test/test?test#test', expect: 'test:101/test?test#test' },
17
+ { in: 'http://test.com', expect: 'http://test.com:101' },
18
+ {
19
+ in: 'http://test.com/test?test#test',
20
+ expect: 'http://test.com:101/test?test#test',
21
+ },
22
+ { in: 'ssh://test.com', expect: 'ssh://test.com:101' },
23
+ { in: 'test.com', expect: 'test.com:101' },
24
+
25
+ // Replacement
26
+ { in: 'test:99', expect: 'test:101' },
27
+ { in: 'test:99/test?test#test', expect: 'test:101/test?test#test' },
28
+ { in: 'http://test.com:99', expect: 'http://test.com:101' },
29
+ {
30
+ in: 'http://test.com:99/test?test#test',
31
+ expect: 'http://test.com:101/test?test#test',
32
+ },
33
+ { in: 'ssh://test.com:99', expect: 'ssh://test.com:101' },
34
+ { in: 'test.com:99', expect: 'test.com:101' },
35
+ ];
36
+
37
+ for ( const test of testMap ) {
38
+ const result = addOrReplacePort( test.in, '101' );
39
+ expect( result ).toEqual( test.expect );
40
+ }
41
+ } );
42
+
43
+ it( 'should support number ports', () => {
44
+ const testMap = [ { in: 'test', expect: 'test:104' } ];
45
+
46
+ for ( const test of testMap ) {
47
+ const result = addOrReplacePort( test.in, 104, false );
48
+ expect( result ).toEqual( test.expect );
49
+ }
50
+ } );
51
+
52
+ it( 'should not add default HTTP port', () => {
53
+ const testMap = [ { in: 'test', expect: 'test' } ];
54
+
55
+ for ( const test of testMap ) {
56
+ const result = addOrReplacePort( test.in, 80, false );
57
+ expect( result ).toEqual( test.expect );
58
+ }
59
+ } );
60
+
61
+ it( 'should not add default HTTPS port', () => {
62
+ const testMap = [ { in: 'test', expect: 'test' } ];
63
+
64
+ for ( const test of testMap ) {
65
+ const result = addOrReplacePort( test.in, 443, false );
66
+ expect( result ).toEqual( test.expect );
67
+ }
68
+ } );
69
+
70
+ it( 'should do nothing if port is present but replacement is not requested', () => {
71
+ const testMap = [
72
+ { in: 'test', expect: 'test:103' },
73
+ { in: 'test:99', expect: 'test:99' },
74
+ ];
75
+
76
+ for ( const test of testMap ) {
77
+ const result = addOrReplacePort( test.in, '103', false );
78
+ expect( result ).toEqual( test.expect );
79
+ }
80
+ } );
81
+ } );
@@ -0,0 +1,143 @@
1
+ 'use strict';
2
+ /* eslint-disable jest/no-conditional-expect */
3
+ /**
4
+ * External dependencies
5
+ */
6
+ const path = require( 'path' );
7
+ const { readFile } = require( 'fs' ).promises;
8
+
9
+ /**
10
+ * Internal dependencies
11
+ */
12
+ const loadConfig = require( '../load-config' );
13
+ const detectDirectoryType = require( '../detect-directory-type' );
14
+
15
+ jest.mock( 'fs', () => ( {
16
+ promises: {
17
+ readFile: jest.fn(),
18
+ stat: jest.fn().mockResolvedValue( true ),
19
+ },
20
+ } ) );
21
+
22
+ // This mocks a small response with a format matching the stable-check API.
23
+ // It makes getLatestWordPressVersion resolve to "100.0.0".
24
+ jest.mock( 'got', () =>
25
+ jest.fn( ( url ) => ( {
26
+ json: () => {
27
+ if ( url === 'https://api.wordpress.org/core/stable-check/1.0/' ) {
28
+ return Promise.resolve( {
29
+ '1.0': 'insecure',
30
+ '99.1.1': 'outdated',
31
+ '100.0.0': 'latest',
32
+ '100.0.1': 'fancy',
33
+ } );
34
+ }
35
+ },
36
+ } ) )
37
+ );
38
+
39
+ jest.mock( '../detect-directory-type', () => jest.fn() );
40
+
41
+ describe( 'Config Integration', () => {
42
+ beforeEach( () => {
43
+ process.env.WP_ENV_HOME = '/cache';
44
+ detectDirectoryType.mockResolvedValue( null );
45
+ } );
46
+
47
+ afterEach( () => {
48
+ delete process.env.WP_ENV_HOME;
49
+ delete process.env.WP_ENV_PORT;
50
+ delete process.env.WP_ENV_TESTS_PORT;
51
+ delete process.env.WP_ENV_AFTER_SETUP;
52
+ } );
53
+
54
+ it( 'should use default configuration', async () => {
55
+ readFile.mockImplementation( async () => {
56
+ throw { code: 'ENOENT' };
57
+ } );
58
+
59
+ const config = await loadConfig( '/test/gutenberg' );
60
+
61
+ expect( config.env.development.port ).toEqual( 8888 );
62
+ expect( config.env.tests.port ).toEqual( 8889 );
63
+ expect( config ).toMatchSnapshot();
64
+ } );
65
+
66
+ it( 'should load local configuration file', async () => {
67
+ readFile.mockImplementation( async ( fileName ) => {
68
+ if ( fileName === '/test/gutenberg/.wp-env.json' ) {
69
+ return JSON.stringify( {
70
+ core: 'WordPress/WordPress#trunk',
71
+ port: 123,
72
+ afterSetup: 'test',
73
+ } );
74
+ }
75
+
76
+ throw { code: 'ENOENT' };
77
+ } );
78
+
79
+ const config = await loadConfig( path.resolve( '/test/gutenberg' ) );
80
+
81
+ expect( config.env.development.port ).toEqual( 123 );
82
+ expect( config.env.tests.port ).toEqual( 8889 );
83
+ expect( config ).toMatchSnapshot();
84
+ } );
85
+
86
+ it( 'should load local and override configuration files', async () => {
87
+ readFile.mockImplementation( async ( fileName ) => {
88
+ if ( fileName === '/test/gutenberg/.wp-env.json' ) {
89
+ return JSON.stringify( {
90
+ core: 'WordPress/WordPress#trunk',
91
+ port: 123,
92
+ testsPort: 456,
93
+ } );
94
+ }
95
+
96
+ if ( fileName === '/test/gutenberg/.wp-env.override.json' ) {
97
+ return JSON.stringify( {
98
+ port: 999,
99
+ } );
100
+ }
101
+
102
+ throw { code: 'ENOENT' };
103
+ } );
104
+
105
+ const config = await loadConfig( path.resolve( '/test/gutenberg' ) );
106
+
107
+ expect( config.env.development.port ).toEqual( 999 );
108
+ expect( config.env.tests.port ).toEqual( 456 );
109
+ expect( config ).toMatchSnapshot();
110
+ } );
111
+
112
+ it( 'should use environment variables over local and override configuration files', async () => {
113
+ process.env.WP_ENV_PORT = 12345;
114
+ process.env.WP_ENV_TESTS_PORT = 61234;
115
+ process.env.WP_ENV_AFTER_SETUP = 'test';
116
+
117
+ readFile.mockImplementation( async ( fileName ) => {
118
+ if ( fileName === '/test/gutenberg/.wp-env.json' ) {
119
+ return JSON.stringify( {
120
+ core: 'WordPress/WordPress#trunk',
121
+ port: 123,
122
+ testsPort: 456,
123
+ afterSetup: 'local',
124
+ } );
125
+ }
126
+
127
+ if ( fileName === '/test/gutenberg/.wp-env.override.json' ) {
128
+ return JSON.stringify( {
129
+ port: 999,
130
+ } );
131
+ }
132
+
133
+ throw { code: 'ENOENT' };
134
+ } );
135
+
136
+ const config = await loadConfig( path.resolve( '/test/gutenberg' ) );
137
+
138
+ expect( config.env.development.port ).toEqual( 12345 );
139
+ expect( config.env.tests.port ).toEqual( 61234 );
140
+ expect( config ).toMatchSnapshot();
141
+ } );
142
+ } );
143
+ /* eslint-enable jest/no-conditional-expect */
@@ -0,0 +1,57 @@
1
+ 'use strict';
2
+ /* eslint-disable jest/no-conditional-expect */
3
+ /**
4
+ * External dependencies
5
+ */
6
+ const { stat } = require( 'fs' ).promises;
7
+ const { homedir } = require( 'os' );
8
+
9
+ /**
10
+ * Internal dependencies
11
+ */
12
+ const getCacheDirectory = require( '../get-cache-directory' );
13
+
14
+ jest.mock( 'fs', () => ( {
15
+ promises: {
16
+ stat: jest.fn(),
17
+ },
18
+ } ) );
19
+ jest.mock( 'os', () => ( {
20
+ homedir: jest.fn(),
21
+ } ) );
22
+
23
+ describe( 'getCacheDirectory', () => {
24
+ afterEach( () => {
25
+ delete process.env.WP_ENV_HOME;
26
+ } );
27
+
28
+ it( 'uses WP_ENV_HOME for cache directory when set', async () => {
29
+ process.env.WP_ENV_HOME = '/test';
30
+
31
+ const parsed = await getCacheDirectory();
32
+
33
+ expect( homedir ).not.toHaveBeenCalled();
34
+ expect( parsed ).toEqual( '/test' );
35
+ } );
36
+
37
+ it( 'uses hidden home directory for cache', async () => {
38
+ stat.mockRejectedValue( false );
39
+ homedir.mockReturnValue( '/home/test' );
40
+
41
+ const parsed = await getCacheDirectory();
42
+
43
+ expect( homedir ).toHaveBeenCalled();
44
+ expect( parsed ).toEqual( '/home/test/.wp-env' );
45
+ } );
46
+
47
+ it( 'uses non-hidden cache directory when using Snap-installed Docker', async () => {
48
+ stat.mockResolvedValue( true );
49
+ homedir.mockReturnValue( '/home/test' );
50
+
51
+ const parsed = await getCacheDirectory();
52
+
53
+ expect( homedir ).toHaveBeenCalled();
54
+ expect( parsed ).toEqual( '/home/test/wp-env' );
55
+ } );
56
+ } );
57
+ /* eslint-enable jest/no-conditional-expect */
@@ -0,0 +1,111 @@
1
+ 'use strict';
2
+ /**
3
+ * Internal dependencies
4
+ */
5
+ const mergeConfigs = require( '../merge-configs' );
6
+
7
+ describe( 'mergeConfigs', () => {
8
+ it( 'should merge configs without environments', () => {
9
+ const merged = mergeConfigs(
10
+ {
11
+ port: 8888,
12
+ coreSource: {
13
+ type: 'local',
14
+ path: '/home/test',
15
+ },
16
+ config: {
17
+ WP_TEST: 'test',
18
+ },
19
+ },
20
+ {
21
+ port: 8889,
22
+ config: {
23
+ WP_TEST_2: 'test-2',
24
+ },
25
+ }
26
+ );
27
+
28
+ expect( merged ).toEqual( {
29
+ port: 8889,
30
+ coreSource: {
31
+ type: 'local',
32
+ path: '/home/test',
33
+ },
34
+ config: {
35
+ WP_TEST: 'test',
36
+ WP_TEST_2: 'test-2',
37
+ },
38
+ } );
39
+ } );
40
+
41
+ it( 'should merge configs with environments', () => {
42
+ const merged = mergeConfigs(
43
+ {
44
+ port: 8888,
45
+ coreSource: {
46
+ type: 'local',
47
+ path: '/home/test',
48
+ },
49
+ config: {
50
+ WP_TEST: 'test',
51
+ },
52
+ env: {
53
+ development: {
54
+ config: {
55
+ WP_TEST_3: 'test-3',
56
+ },
57
+ },
58
+ tests: {
59
+ config: {
60
+ WP_TEST_4: 'test-4',
61
+ },
62
+ },
63
+ },
64
+ },
65
+ {
66
+ port: 8889,
67
+ config: {
68
+ WP_TEST_2: 'test-2',
69
+ },
70
+ env: {
71
+ development: {
72
+ config: {
73
+ WP_TEST_5: 'test-5',
74
+ },
75
+ },
76
+ tests: {
77
+ config: {
78
+ WP_TEST_6: 'test-6',
79
+ },
80
+ },
81
+ },
82
+ }
83
+ );
84
+
85
+ expect( merged ).toEqual( {
86
+ port: 8889,
87
+ coreSource: {
88
+ type: 'local',
89
+ path: '/home/test',
90
+ },
91
+ config: {
92
+ WP_TEST: 'test',
93
+ WP_TEST_2: 'test-2',
94
+ },
95
+ env: {
96
+ development: {
97
+ config: {
98
+ WP_TEST_3: 'test-3',
99
+ WP_TEST_5: 'test-5',
100
+ },
101
+ },
102
+ tests: {
103
+ config: {
104
+ WP_TEST_4: 'test-4',
105
+ WP_TEST_6: 'test-6',
106
+ },
107
+ },
108
+ },
109
+ } );
110
+ } );
111
+ } );