Skip to content

Commit

Permalink
Fix: env unit test fails on Windows (#41070)
Browse files Browse the repository at this point in the history
* Fix: env config unit test fails on Windows

* remove blank lines

* update readme

* polish codes
  • Loading branch information
t-hamano authored May 17, 2022
1 parent c541147 commit c5ac031
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 40 deletions.
111 changes: 71 additions & 40 deletions packages/env/lib/config/test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
const { readFile, stat } = require( 'fs' ).promises;
const os = require( 'os' );
const { join, resolve } = require( 'path' );

/**
* Internal dependencies
Expand Down Expand Up @@ -101,13 +102,17 @@ describe( 'readConfig', () => {
process.env.WP_ENV_HOME = 'here/is/a/path';
const configWith = await readConfig( '.wp-env.json' );
expect(
configWith.workDirectoryPath.includes( 'here/is/a/path' )
configWith.workDirectoryPath.includes(
join( 'here', 'is', 'a', 'path' )
)
).toBe( true );

process.env.WP_ENV_HOME = undefined;
const configWithout = await readConfig( '.wp-env.json' );
expect(
configWithout.workDirectoryPath.includes( 'here/is/a/path' )
configWithout.workDirectoryPath.includes(
join( 'here', 'is', 'a', 'path' )
)
).toBe( false );

process.env.WP_ENV_HOME = oldEnvHome;
Expand All @@ -126,13 +131,17 @@ describe( 'readConfig', () => {
process.env.WP_ENV_HOME = 'here/is/a/path';
const configWith = await readConfig( '.wp-env.json' );
expect(
configWith.workDirectoryPath.includes( 'here/is/a/path' )
configWith.workDirectoryPath.includes(
join( 'here', 'is', 'a', 'path' )
)
).toBe( true );

process.env.WP_ENV_HOME = undefined;
const configWithout = await readConfig( '.wp-env.json' );
expect(
configWithout.workDirectoryPath.includes( 'here/is/a/path' )
configWithout.workDirectoryPath.includes(
join( 'here', 'is', 'a', 'path' )
)
).toBe( false );

process.env.WP_ENV_HOME = oldEnvHome;
Expand Down Expand Up @@ -242,26 +251,31 @@ describe( 'readConfig', () => {
readFile.mockImplementation( () =>
Promise.resolve(
JSON.stringify( {
plugins: [ './relative', '../parent', '~/home' ],
plugins: [
'./relative',
'../parent',
`${ os.homedir() }/home`,
],
} )
)
);
const config = await readConfig( '.wp-env.json' );

expect( config.env.development ).toMatchObject( {
pluginSources: [
{
type: 'local',
path: expect.stringMatching( /^\/.*relative$/ ),
path: expect.stringMatching( /^(\/||\\).*relative$/ ),
basename: 'relative',
},
{
type: 'local',
path: expect.stringMatching( /^\/.*parent$/ ),
path: expect.stringMatching( /^(\/||\\).*parent$/ ),
basename: 'parent',
},
{
type: 'local',
path: expect.stringMatching( /^\/.*home$/ ),
path: expect.stringMatching( /^(\/||\\).*home$/ ),
basename: 'home',
},
],
Expand All @@ -270,17 +284,17 @@ describe( 'readConfig', () => {
pluginSources: [
{
type: 'local',
path: expect.stringMatching( /^\/.*relative$/ ),
path: expect.stringMatching( /^(\/||\\).*relative$/ ),
basename: 'relative',
},
{
type: 'local',
path: expect.stringMatching( /^\/.*parent$/ ),
path: expect.stringMatching( /^(\/||\\).*parent$/ ),
basename: 'parent',
},
{
type: 'local',
path: expect.stringMatching( /^\/.*home$/ ),
path: expect.stringMatching( /^(\/||\\).*home$/ ),
basename: 'home',
},
],
Expand Down Expand Up @@ -310,28 +324,28 @@ describe( 'readConfig', () => {
expect( config.env.development.pluginSources ).toEqual( [
{
type: 'local',
path: expect.stringMatching( /^\/.*test1a$/ ),
path: expect.stringMatching( /^(\/||\\).*test1a$/ ),
basename: 'test1a',
},
] );
expect( config.env.development.themeSources ).toEqual( [
{
type: 'local',
path: expect.stringMatching( /^\/.*test2a$/ ),
path: expect.stringMatching( /^(\/||\\).*test2a$/ ),
basename: 'test2a',
},
] );
expect( config.env.tests.pluginSources ).toEqual( [
{
type: 'local',
path: expect.stringMatching( /^\/.*test1b$/ ),
path: expect.stringMatching( /^(\/||\\).*test1b$/ ),
basename: 'test1b',
},
] );
expect( config.env.tests.themeSources ).toEqual( [
{
type: 'local',
path: expect.stringMatching( /^\/.*test2b$/ ),
path: expect.stringMatching( /^(\/||\\).*test2b$/ ),
basename: 'test2b',
},
] );
Expand All @@ -345,15 +359,19 @@ describe( 'readConfig', () => {
expect( config.env.development ).toMatchObject( {
coreSource: {
type: 'local',
path: expect.stringMatching( /^\/.*relative$/ ),
testsPath: expect.stringMatching( /^\/.*tests-relative$/ ),
path: expect.stringMatching( /^(\/||\\).*relative$/ ),
testsPath: expect.stringMatching(
/^(\/||\\).*tests-relative$/
),
},
} );
expect( config.env.tests ).toMatchObject( {
coreSource: {
type: 'local',
path: expect.stringMatching( /^\/.*relative$/ ),
testsPath: expect.stringMatching( /^\/.*tests-relative$/ ),
path: expect.stringMatching( /^(\/||\\).*relative$/ ),
testsPath: expect.stringMatching(
/^(\/||\\).*tests-relative$/
),
},
} );
} );
Expand All @@ -378,21 +396,21 @@ describe( 'readConfig', () => {
type: 'git',
url: 'https://github.com/WordPress/gutenberg.git',
ref: 'master',
path: expect.stringMatching( /^\/.*gutenberg$/ ),
path: expect.stringMatching( /^(\/||\\).*gutenberg$/ ),
basename: 'gutenberg',
},
{
type: 'git',
url: 'https://github.com/WordPress/gutenberg.git',
ref: 'trunk',
path: expect.stringMatching( /^\/.*gutenberg$/ ),
path: expect.stringMatching( /^(\/||\\).*gutenberg$/ ),
basename: 'gutenberg',
},
{
type: 'git',
url: 'https://github.com/WordPress/gutenberg.git',
ref: '5.0',
path: expect.stringMatching( /^\/.*gutenberg$/ ),
path: expect.stringMatching( /^(\/||\\).*gutenberg$/ ),
basename: 'gutenberg',
},
{
Expand All @@ -401,7 +419,7 @@ describe( 'readConfig', () => {
'https://github.com/WordPress/theme-experiments.git',
ref: '[email protected]',
path: expect.stringMatching(
/^\/.*theme-experiments\/tt1-blocks$/
/^(\/||\\).*theme-experiments(\/||\\)tt1-blocks$/
),
basename: 'tt1-blocks',
},
Expand Down Expand Up @@ -431,28 +449,32 @@ describe( 'readConfig', () => {
type: 'zip',
url:
'https://downloads.wordpress.org/plugin/gutenberg.zip',
path: expect.stringMatching( /^\/.*gutenberg$/ ),
path: expect.stringMatching( /^(\/||\\).*gutenberg$/ ),
basename: 'gutenberg',
},
{
type: 'zip',
url:
'https://downloads.wordpress.org/plugin/gutenberg.8.1.0.zip',
path: expect.stringMatching( /^\/.*gutenberg$/ ),
path: expect.stringMatching( /^(\/||\\).*gutenberg$/ ),
basename: 'gutenberg',
},
{
type: 'zip',
url:
'https://downloads.wordpress.org/theme/twentytwenty.zip',
path: expect.stringMatching( /^\/.*twentytwenty$/ ),
path: expect.stringMatching(
/^(\/||\\).*twentytwenty$/
),
basename: 'twentytwenty',
},
{
type: 'zip',
url:
'https://downloads.wordpress.org/theme/twentytwenty.1.3.zip',
path: expect.stringMatching( /^\/.*twentytwenty$/ ),
path: expect.stringMatching(
/^(\/||\\).*twentytwenty$/
),
basename: 'twentytwenty',
},
],
Expand Down Expand Up @@ -482,34 +504,42 @@ describe( 'readConfig', () => {
type: 'zip',
url:
'https://www.example.com/test/path/to/gutenberg.zip',
path: expect.stringMatching( /^\/.*gutenberg$/ ),
path: expect.stringMatching( /^(\/||\\).*gutenberg$/ ),
basename: 'gutenberg',
},
{
type: 'zip',
url:
'https://www.example.com/test/path/to/gutenberg.8.1.0.zip',
path: expect.stringMatching( /^\/.*gutenberg.8.1.0$/ ),
path: expect.stringMatching(
/^(\/||\\).*gutenberg.8.1.0$/
),
basename: 'gutenberg.8.1.0',
},
{
type: 'zip',
url:
'https://www.example.com/test/path/to/twentytwenty.zip',
path: expect.stringMatching( /^\/.*twentytwenty$/ ),
path: expect.stringMatching(
/^(\/||\\).*twentytwenty$/
),
basename: 'twentytwenty',
},
{
type: 'zip',
url:
'https://www.example.com/test/path/to/twentytwenty.1.3.zip',
path: expect.stringMatching( /^\/.*twentytwenty.1.3$/ ),
path: expect.stringMatching(
/^(\/||\\).*twentytwenty.1.3$/
),
basename: 'twentytwenty.1.3',
},
{
type: 'zip',
url: 'https://example.com/twentytwenty.1.3.zip',
path: expect.stringMatching( /^\/.*twentytwenty.1.3$/ ),
path: expect.stringMatching(
/^(\/||\\).*twentytwenty.1.3$/
),
basename: 'twentytwenty.1.3',
},
],
Expand Down Expand Up @@ -550,12 +580,12 @@ describe( 'readConfig', () => {
const matchObj = {
test: {
type: 'local',
path: expect.stringMatching( /^\/.*relative$/ ),
path: expect.stringMatching( /^(\/||\\).*relative$/ ),
basename: 'relative',
},
test2: {
type: 'git',
path: expect.stringMatching( /^\/.*gutenberg$/ ),
path: expect.stringMatching( /^(\/||\\).*gutenberg$/ ),
basename: 'gutenberg',
},
};
Expand Down Expand Up @@ -653,24 +683,25 @@ describe( 'readConfig', () => {
expect( config.env.development.mappings ).toEqual( {
test1: {
basename: 'test1',
path: '/test1',
// resolve is required to remove drive letters on Windows.
path: resolve( '/test1' ),
type: 'local',
},
test3: {
basename: 'test3',
path: '/test3',
path: resolve( '/test3' ),
type: 'local',
},
} );
expect( config.env.tests.mappings ).toEqual( {
test1: {
basename: 'test1',
path: '/test1',
path: resolve( '/test1' ),
type: 'local',
},
test2: {
basename: 'test2',
path: '/test2',
path: resolve( '/test2' ),
type: 'local',
},
} );
Expand Down Expand Up @@ -702,14 +733,14 @@ describe( 'readConfig', () => {
expect( config.env.development.mappings ).toEqual( {
test: {
basename: 'test3',
path: '/test3',
path: resolve( '/test3' ),
type: 'local',
},
} );
expect( config.env.tests.mappings ).toEqual( {
test: {
basename: 'test2',
path: '/test2',
path: resolve( '/test2' ),
type: 'local',
},
} );
Expand Down
4 changes: 4 additions & 0 deletions packages/scripts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Enable by default code formatting for JSON files in the `format` command ([#40994](https://github.com/WordPress/gutenberg/pull/40994)). You can opt-out of this behavior by providing a custom file matcher, example: `wp-scripts format src/**/*.js`.

### Bug Fixes

- Fix: env unit test fails on Windows ([#41070](https://github.com/WordPress/gutenberg/pull/41070))

## 23.0.0 (2022-05-04)

### Breaking Changes
Expand Down

0 comments on commit c5ac031

Please sign in to comment.