From 357a1198c6b302cd4a99e5909a8690aeaf2f6188 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 7 Dec 2017 10:04:30 +0100 Subject: [PATCH] Use `fs.realpathSync.native` if available (#5031) * Pass in encoding to `realpath` call Fixes #5030 * Use `fs.realpathSync.native` if it exists * update changelog --- CHANGELOG.md | 2 ++ packages/jest-cli/src/cli/index.js | 4 ++-- packages/jest-runtime/src/script_transformer.js | 5 ++--- packages/jest-util/src/index.js | 11 +++++++++++ 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44945b22f8e2..34e0a007cb4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,8 @@ ([#4494](https://github.com/facebook/jest/pull/4494)) * `[jest-cli]` Throw if `maxWorkers` doesn't have a value ([#4591](https://github.com/facebook/jest/pull/4591)) +* `[jest-cli]` Use `fs.realpathSync.native` if available + ([#5031](https://github.com/facebook/jest/pull/5031)) * `[jest-config]` Fix `--passWithNoTests` ([#4639](https://github.com/facebook/jest/pull/4639)) * `[jest-config]` Support `rootDir` tag in testEnvironment diff --git a/packages/jest-cli/src/cli/index.js b/packages/jest-cli/src/cli/index.js index f35011d53b49..750a84494827 100644 --- a/packages/jest-cli/src/cli/index.js +++ b/packages/jest-cli/src/cli/index.js @@ -15,6 +15,7 @@ import { Console, clearLine, createDirectory, + realpath, validateCLIOptions, } from 'jest-util'; import {readConfig} from 'jest-config'; @@ -143,8 +144,7 @@ const getProjectListFromCLIArgs = (argv, project: ?Path) => { if (!projects.length && process.platform === 'win32') { try { - // $FlowFixMe - projects.push(process.binding('fs').realpath(process.cwd())); + projects.push(realpath(process.cwd())); } catch (err) { // do nothing, just catch error // process.binding('fs').realpath can throw, e.g. on mapped drives diff --git a/packages/jest-runtime/src/script_transformer.js b/packages/jest-runtime/src/script_transformer.js index 504af27eb089..248a42a97635 100644 --- a/packages/jest-runtime/src/script_transformer.js +++ b/packages/jest-runtime/src/script_transformer.js @@ -17,7 +17,7 @@ import type { import crypto from 'crypto'; import path from 'path'; import vm from 'vm'; -import {createDirectory} from 'jest-util'; +import {createDirectory, realpath} from 'jest-util'; import fs from 'graceful-fs'; import {transform as babelTransform} from 'babel-core'; import babelPluginIstanbul from 'babel-plugin-istanbul'; @@ -184,8 +184,7 @@ export default class ScriptTransformer { _getRealPath(filepath: Path): Path { try { - // $FlowFixMe - return process.binding('fs').realpath(filepath) || filepath; + return realpath(filepath) || filepath; } catch (err) { return filepath; } diff --git a/packages/jest-util/src/index.js b/packages/jest-util/src/index.js index 859398dd5bc4..528cb81a626b 100644 --- a/packages/jest-util/src/index.js +++ b/packages/jest-util/src/index.js @@ -8,6 +8,7 @@ */ import mkdirp from 'mkdirp'; +import fs from 'fs'; import BufferedConsole from './buffered_console'; import clearLine from './clear_line'; @@ -30,6 +31,15 @@ const createDirectory = (path: string) => { } }; +const realpath = (filepath: string) => { + if (typeof fs.realpathSync.native === 'function') { + return fs.realpathSync.native(filepath); + } + + // $FlowFixMe: This is need for node@<9.2 + return process.binding('fs').realpath(filepath, 'utf8'); +}; + module.exports = { BufferedConsole, Console, @@ -40,6 +50,7 @@ module.exports = { formatTestResults, getConsoleOutput, installCommonGlobals, + realpath, setGlobal, validateCLIOptions, };