Skip to content

Commit b0803ec

Browse files
committed
Fix package-imports-test on Windows (#1450)
Summary: Pull Request resolved: #1450 Use the Metro `p` posix => system path convention to make `package-imports-test` platform agnostic and restore it on Windows. Changelog: Internal Reviewed By: vzaidman Differential Revision: D70544964 fbshipit-source-id: 603f6a74b0a6619f181fd84f398df55ca2afbee1
1 parent 55ed2ee commit b0803ec

2 files changed

Lines changed: 46 additions & 37 deletions

File tree

packages/metro-resolver/src/__tests__/package-imports-test.js

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,83 +10,87 @@
1010
*/
1111

1212
import Resolver from '../index';
13-
import {createResolutionContext} from './utils';
14-
import {createPackageAccessors} from './utils';
13+
import {
14+
createPackageAccessors,
15+
createResolutionContext,
16+
posixToSystemPath as p,
17+
} from './utils';
1518

1619
// Implementation of PACKAGE_IMPORTS_RESOLVE described in https://nodejs.org/api/esm.html
1720
describe('subpath imports resolution support', () => {
1821
const baseContext = {
1922
...createResolutionContext({
20-
'/root/src/main.js': '',
21-
'/root/node_modules/test-pkg/package.json': '',
22-
'/root/node_modules/test-pkg/index.js': '',
23-
'/root/node_modules/test-pkg/index-main.js': '',
24-
'/root/node_modules/test-pkg/symlink.js': {
25-
realPath: '/root/node_modules/test-pkg/symlink-target.js',
23+
[p('/root/src/main.js')]: '',
24+
[p('/root/node_modules/test-pkg/package.json')]: '',
25+
[p('/root/node_modules/test-pkg/index.js')]: '',
26+
[p('/root/node_modules/test-pkg/index-main.js')]: '',
27+
// $FlowFixMe[incompatible-type] Flow wants a string for some reason
28+
[p('/root/node_modules/test-pkg/symlink.js')]: {
29+
realPath: p('/root/node_modules/test-pkg/symlink-target.js'),
2630
},
2731
}),
28-
originModulePath: '/root/src/main.js',
32+
originModulePath: p('/root/src/main.js'),
2933
};
3034

3135
test('"imports" subpath that maps directly to a file', () => {
3236
const context = {
3337
...baseContext,
3438
...createPackageAccessors({
35-
'/root/node_modules/test-pkg/package.json': {
39+
[p('/root/node_modules/test-pkg/package.json')]: {
3640
main: 'index-main.js',
3741
imports: {
3842
'#foo': './index.js',
3943
},
4044
},
4145
}),
42-
originModulePath: '/root/node_modules/test-pkg/lib/foo.js',
46+
originModulePath: p('/root/node_modules/test-pkg/lib/foo.js'),
4347
};
4448

4549
expect(Resolver.resolve(context, '#foo', null)).toEqual({
4650
type: 'sourceFile',
47-
filePath: '/root/node_modules/test-pkg/index.js',
51+
filePath: p('/root/node_modules/test-pkg/index.js'),
4852
});
4953
});
5054
});
5155

5256
describe('import subpath patterns resolution support', () => {
5357
const baseContext = {
5458
...createResolutionContext({
55-
'/root/src/main.js': '',
56-
'/root/node_modules/test-pkg/package.json': JSON.stringify({
59+
[p('/root/src/main.js')]: '',
60+
[p('/root/node_modules/test-pkg/package.json')]: JSON.stringify({
5761
name: 'test-pkg',
5862
main: 'index.js',
5963
imports: {
6064
'#features/*': './src/features/*.js',
6165
},
6266
}),
63-
'/root/node_modules/test-pkg/src/index.js': '',
64-
'/root/node_modules/test-pkg/src/features/foo.js': '',
65-
'/root/node_modules/test-pkg/src/features/foo.js.js': '',
66-
'/root/node_modules/test-pkg/src/features/bar/Bar.js': '',
67-
'/root/node_modules/test-pkg/src/features/baz.native.js': '',
67+
[p('/root/node_modules/test-pkg/src/index.js')]: '',
68+
[p('/root/node_modules/test-pkg/src/features/foo.js')]: '',
69+
[p('/root/node_modules/test-pkg/src/features/foo.js.js')]: '',
70+
[p('/root/node_modules/test-pkg/src/features/bar/Bar.js')]: '',
71+
[p('/root/node_modules/test-pkg/src/features/baz.native.js')]: '',
6872
}),
69-
originModulePath: '/root/node_modules/test-pkg/src/index.js',
73+
originModulePath: p('/root/node_modules/test-pkg/src/index.js'),
7074
};
7175

7276
test('resolving subpath patterns in "imports" matching import specifier', () => {
7377
expect(Resolver.resolve(baseContext, '#features/foo', null)).toEqual({
7478
type: 'sourceFile',
75-
filePath: '/root/node_modules/test-pkg/src/features/foo.js',
79+
filePath: p('/root/node_modules/test-pkg/src/features/foo.js'),
7680
});
7781

7882
expect(Resolver.resolve(baseContext, '#features/foo.js', null)).toEqual({
7983
type: 'sourceFile',
80-
filePath: '/root/node_modules/test-pkg/src/features/foo.js.js',
84+
filePath: p('/root/node_modules/test-pkg/src/features/foo.js.js'),
8185
});
8286
});
8387
});
8488

8589
describe('import subpath conditional imports resolution', () => {
8690
const baseContext = {
8791
...createResolutionContext({
88-
'/root/src/main.js': '',
89-
'/root/node_modules/test-pkg/package.json': JSON.stringify({
92+
[p('/root/src/main.js')]: '',
93+
[p('/root/node_modules/test-pkg/package.json')]: JSON.stringify({
9094
name: 'test-pkg',
9195
main: 'index.js',
9296
imports: {
@@ -104,18 +108,18 @@ describe('import subpath conditional imports resolution', () => {
104108
},
105109
},
106110
}),
107-
'/root/node_modules/test-pkg/index.js': '',
108-
'/root/node_modules/test-pkg/lib/foo.js': '',
109-
'/root/node_modules/test-pkg/lib/foo-require.cjs': '',
110-
'/root/node_modules/test-pkg/lib/foo-module.mjs': '',
111-
'/root/node_modules/test-pkg/lib/foo-dev.js': '',
112-
'/root/node_modules/test-pkg/lib/foo-browser.js': '',
113-
'/root/node_modules/test-pkg/lib/foo-react-native.cjs': '',
114-
'/root/node_modules/test-pkg/lib/foo-react-native.mjs': '',
115-
'/root/node_modules/test-pkg/lib/foo-react-native.js': '',
116-
'/root/node_modules/test-pkg/lib/foo.web.js': '',
111+
[p('/root/node_modules/test-pkg/index.js')]: '',
112+
[p('/root/node_modules/test-pkg/lib/foo.js')]: '',
113+
[p('/root/node_modules/test-pkg/lib/foo-require.cjs')]: '',
114+
[p('/root/node_modules/test-pkg/lib/foo-module.mjs')]: '',
115+
[p('/root/node_modules/test-pkg/lib/foo-dev.js')]: '',
116+
[p('/root/node_modules/test-pkg/lib/foo-browser.js')]: '',
117+
[p('/root/node_modules/test-pkg/lib/foo-react-native.cjs')]: '',
118+
[p('/root/node_modules/test-pkg/lib/foo-react-native.mjs')]: '',
119+
[p('/root/node_modules/test-pkg/lib/foo-react-native.js')]: '',
120+
[p('/root/node_modules/test-pkg/lib/foo.web.js')]: '',
117121
}),
118-
originModulePath: '/root/node_modules/test-pkg/src/index.js',
122+
originModulePath: p('/root/node_modules/test-pkg/src/index.js'),
119123
};
120124

121125
test('resolving imports subpath with conditions', () => {
@@ -128,14 +132,14 @@ describe('import subpath conditional imports resolution', () => {
128132
Resolver.resolve({...context, isESMImport: false}, '#foo', null),
129133
).toEqual({
130134
type: 'sourceFile',
131-
filePath: '/root/node_modules/test-pkg/lib/foo-react-native.cjs',
135+
filePath: p('/root/node_modules/test-pkg/lib/foo-react-native.cjs'),
132136
});
133137

134138
expect(
135139
Resolver.resolve({...context, isESMImport: true}, '#foo', null),
136140
).toEqual({
137141
type: 'sourceFile',
138-
filePath: '/root/node_modules/test-pkg/lib/foo-react-native.mjs',
142+
filePath: p('/root/node_modules/test-pkg/lib/foo-react-native.mjs'),
139143
});
140144
});
141145
});

packages/metro-resolver/src/__tests__/utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,8 @@ export function createPackageAccessors(
149149
getPackageForModule,
150150
};
151151
}
152+
153+
export const posixToSystemPath: string => string =
154+
process.platform === 'win32'
155+
? filePath => filePath.replaceAll('/', '\\').replace(/^\\/, 'C:\\')
156+
: filePath => filePath;

0 commit comments

Comments
 (0)