Skip to content

Commit

Permalink
jest-haste-map: watchman crawler: normalize paths (jestjs#3887)
Browse files Browse the repository at this point in the history
* jest-haste-map: watchman crawler: normalize paths

* also normalize path coming from watcher

* simplify, add module + tests

* add posix case
  • Loading branch information
jeanlauliac authored and cpojer committed Jun 27, 2017
1 parent 70d331c commit f459189
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/jest-haste-map/src/crawlers/watchman.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import type {InternalHasteMap} from 'types/HasteMap';
import type {CrawlerOptions} from '../types';

import normalizePathSep from '../lib/normalize_path_sep';
import path from 'path';
import watchman from 'fb-watchman';
import H from '../constants';
Expand Down Expand Up @@ -104,7 +105,7 @@ module.exports = function watchmanCrawl(

clocks[root] = response.clock;
response.files.forEach(fileData => {
const name = root + path.sep + fileData.name;
const name = root + path.sep + normalizePathSep(fileData.name);
if (!fileData.exists) {
delete files[name];
} else if (!ignore(name)) {
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-haste-map/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import HasteModuleMap from './module_map';
import getMockName from './get_mock_name';
import getPlatformExtension from './lib/get_platform_extension';
import nodeCrawl from './crawlers/node';
import normalizePathSep from './lib/normalize_path_sep';
import watchmanCrawl from './crawlers/watchman';
import worker from './worker';

Expand Down Expand Up @@ -616,7 +617,7 @@ class HasteMap extends EventEmitter {
root: Path,
stat: {mtime: Date},
) => {
filePath = path.join(root, filePath);
filePath = path.join(root, normalizePathSep(filePath));
if (
this._ignore(filePath) ||
!extensions.some(extension => filePath.endsWith(extension))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails oncall+jsinfra
*/
'use strict';

describe('normalizePathSep', () => {
it('does nothing on posix', () => {
jest.resetModules();
jest.mock('path', () => require.requireActual('path').posix);
const normalizePathSep = require('../normalize_path_sep');
expect(normalizePathSep('foo/bar/baz.js')).toEqual('foo/bar/baz.js');
});

it('replace slashes on windows', () => {
jest.resetModules();
jest.mock('path', () => require.requireActual('path').win32);
const normalizePathSep = require('../normalize_path_sep');
expect(normalizePathSep('foo/bar/baz.js')).toEqual('foo\\bar\\baz.js');
});
});
20 changes: 20 additions & 0 deletions packages/jest-haste-map/src/lib/normalize_path_sep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/

const path = require('path');

let normalizePathSep;
if (path.sep == '/') {
normalizePathSep = (filePath: string) => filePath;
} else {
normalizePathSep = (filePath: string) => filePath.replace(/\//g, path.sep);
}

module.exports = normalizePathSep;

0 comments on commit f459189

Please sign in to comment.