Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import type {Options, TransformResultDependency} from '../types';
import CountingSet from '../../lib/CountingSet';
import DeltaCalculator from '../DeltaCalculator';
import {Graph} from '../Graph';
import {createEmitChange} from './test-utils';
import {createEmitChange, createPathNormalizer} from './test-utils';
import path from 'path';

const {EventEmitter} = require('events');

Expand All @@ -38,6 +39,7 @@ describe('DeltaCalculator + require.context', () => {
let deltaCalculator;
let fileWatcher;
let emitChange;
const p = createPathNormalizer();

const options: Options<> = {
unstable_allowRequireContext: true,
Expand All @@ -64,15 +66,15 @@ describe('DeltaCalculator + require.context', () => {

beforeEach(async () => {
fileWatcher = new EventEmitter();
emitChange = createEmitChange(fileWatcher, '/');
emitChange = createEmitChange(fileWatcher, p('/'), path.sep);

markModifiedContextModules.mockImplementation(function <T>(
this: Graph<T>,
filePath,
modifiedContexts,
) {
if (filePath.startsWith('/ctx/')) {
modifiedContexts.add('/ctx?ctx=xxx');
if (filePath.startsWith(p('/ctx/'))) {
modifiedContexts.add(p('/ctx?ctx=xxx'));
}
});

Expand All @@ -86,12 +88,12 @@ describe('DeltaCalculator + require.context', () => {
this: Graph<T>,
options: Options<T>,
): Promise<Result<T>> {
this.dependencies.set('/bundle', {
this.dependencies.set(p('/bundle'), {
dependencies: new Map([
[
'ctx',
{
absolutePath: '/ctx?ctx=xxx',
absolutePath: p('/ctx?ctx=xxx'),
data: {
name: 'ctx',
data: {
Expand All @@ -106,15 +108,15 @@ describe('DeltaCalculator + require.context', () => {
]),
inverseDependencies: new CountingSet([]),
output: [],
path: '/bundle',
path: p('/bundle'),
getSource: () => Buffer.of(),
});
this.dependencies.set('/ctx?ctx=xxx', {
this.dependencies.set(p('/ctx?ctx=xxx'), {
dependencies: new Map([
[
'foo',
{
absolutePath: '/ctx/foo',
absolutePath: p('/ctx/foo'),
data: {
name: 'foo',
data: {
Expand All @@ -127,16 +129,16 @@ describe('DeltaCalculator + require.context', () => {
},
],
]),
inverseDependencies: new CountingSet(['/bundle']),
inverseDependencies: new CountingSet([p('/bundle')]),
output: [],
path: '/ctx?ctx=xxx',
path: p('/ctx?ctx=xxx'),
getSource: () => Buffer.of(),
});
this.dependencies.set('/ctx/foo', {
this.dependencies.set(p('/ctx/foo'), {
dependencies: new Map(),
inverseDependencies: new CountingSet(['/ctx?ctx=xxx']),
inverseDependencies: new CountingSet([p('/ctx?ctx=xxx')]),
output: [],
path: '/ctx/foo',
path: p('/ctx/foo'),
getSource: () => Buffer.of(),
});

Expand All @@ -158,7 +160,7 @@ describe('DeltaCalculator + require.context', () => {

// $FlowFixMe[underconstrained-implicit-instantiation]
deltaCalculator = new DeltaCalculator(
new Set(['/bundle']),
new Set([p('/bundle')]),
fileWatcher,
options,
);
Expand All @@ -184,7 +186,7 @@ describe('DeltaCalculator + require.context', () => {
});

expect(traverseDependencies).toBeCalledWith(
['/ctx?ctx=xxx'],
[p('/ctx?ctx=xxx')],
expect.anything(),
);

Expand All @@ -207,7 +209,7 @@ describe('DeltaCalculator + require.context', () => {
});

expect(traverseDependencies).toBeCalledWith(
['/ctx?ctx=xxx'],
[p('/ctx?ctx=xxx')],
expect.anything(),
);

Expand All @@ -227,7 +229,7 @@ describe('DeltaCalculator + require.context', () => {
});

expect(traverseDependencies).toBeCalledWith(
['/ctx/foo'],
[p('/ctx/foo')],
expect.anything(),
);

Expand Down Expand Up @@ -264,7 +266,7 @@ describe('DeltaCalculator + require.context', () => {
});

expect(traverseDependencies).toBeCalledWith(
['/ctx?ctx=xxx'],
[p('/ctx?ctx=xxx')],
expect.anything(),
);

Expand Down Expand Up @@ -303,7 +305,7 @@ describe('DeltaCalculator + require.context', () => {
});

expect(traverseDependencies).toBeCalledWith(
['/ctx/foo'],
[p('/ctx/foo')],
expect.anything(),
);
});
Expand All @@ -323,7 +325,7 @@ describe('DeltaCalculator + require.context', () => {
});

expect(traverseDependencies).toBeCalledWith(
['/ctx?ctx=xxx'],
[p('/ctx?ctx=xxx')],
expect.anything(),
);

Expand Down
18 changes: 3 additions & 15 deletions packages/metro/src/DeltaBundler/__tests__/DeltaCalculator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ import type {
} from '../types';

import CountingSet from '../../lib/CountingSet';
import {createEmitChange} from './test-utils';
import path from 'path';
import {createEmitChange, createPathNormalizer} from './test-utils';

jest.mock('../../Bundler');

describe.each(['linux', 'win32'])('DeltaCalculator (%s)', osPlatform => {
describe.each(['posix', 'win32'])('DeltaCalculator (%s)', osPlatform => {
let entryModule: Module<$FlowFixMe>;
let fooModule: Module<$FlowFixMe>;
let barModule: Module<$FlowFixMe>;
Expand All @@ -35,6 +34,7 @@ describe.each(['linux', 'win32'])('DeltaCalculator (%s)', osPlatform => {
let traverseDependencies;
let initialTraverseDependencies;
let emitChange;
const p = createPathNormalizer(osPlatform);

const options: Options<> = {
unstable_allowRequireContext: false,
Expand All @@ -59,18 +59,6 @@ describe.each(['linux', 'win32'])('DeltaCalculator (%s)', osPlatform => {
},
};

function p(posixPath: string): string {
if (osPlatform === 'win32') {
if (path.posix.isAbsolute(posixPath)) {
return path.win32.join('C:\\', ...posixPath.split('/'));
} else {
return posixPath.replaceAll('/', '\\');
}
}

return posixPath;
}

beforeEach(async () => {
if (osPlatform === 'win32') {
jest.doMock('path', () => jest.requireActual('path/win32'));
Expand Down
11 changes: 10 additions & 1 deletion packages/metro/src/DeltaBundler/__tests__/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type ChangeEventInput = {
export function createEmitChange(
fileWatcher: EventEmitter,
rootDir: string,
pathSeparator: string = '/',
pathSeparator: string,
): (changes: ChangeEventInput) => void {
return function emitChange(changes: ChangeEventInput): void {
const toEntry = (
Expand Down Expand Up @@ -59,3 +59,12 @@ export function createEmitChange(
});
};
}

export const createPathNormalizer = (
platform: 'win32' | 'posix' = process.platform === 'win32'
? 'win32'
: 'posix',
): (string => string) =>
platform === 'win32'
? posixPath => posixPath.replace(/^\//, 'C:\\').replaceAll('/', '\\')
: posixPath => posixPath;
2 changes: 1 addition & 1 deletion scripts/__tests__/ts-defs-sync-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ test('TypeScript defs are in sync (yarn run build-ts-defs produces no changes)',
expect(error.errors).toEqual([]);
}
expect(error).toBeUndefined();
}, 30000);
}, 60000);
Loading