From e5afc18cda3ea035d199c7f7652a3a80f86069a3 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 6 Dec 2017 20:24:23 +0100 Subject: [PATCH 1/3] Pass in encoding to `realpath` call Fixes #5030 --- packages/jest-cli/src/cli/index.js | 2 +- packages/jest-runtime/src/script_transformer.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jest-cli/src/cli/index.js b/packages/jest-cli/src/cli/index.js index f35011d53b49..a8262501025a 100644 --- a/packages/jest-cli/src/cli/index.js +++ b/packages/jest-cli/src/cli/index.js @@ -144,7 +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(process.binding('fs').realpath(process.cwd(), 'utf8')); } 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..71c7368deb16 100644 --- a/packages/jest-runtime/src/script_transformer.js +++ b/packages/jest-runtime/src/script_transformer.js @@ -185,7 +185,7 @@ export default class ScriptTransformer { _getRealPath(filepath: Path): Path { try { // $FlowFixMe - return process.binding('fs').realpath(filepath) || filepath; + return process.binding('fs').realpath(filepath, 'utf8') || filepath; } catch (err) { return filepath; } From 69265daff733ad3eb834e5130f2d3e096f960d52 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 6 Dec 2017 20:42:34 +0100 Subject: [PATCH 2/3] Use `fs.realpathSync.native` if it exists --- packages/jest-cli/src/cli/index.js | 4 ++-- packages/jest-runtime/src/script_transformer.js | 5 ++--- packages/jest-util/src/index.js | 11 +++++++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/jest-cli/src/cli/index.js b/packages/jest-cli/src/cli/index.js index a8262501025a..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(), 'utf8')); + 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 71c7368deb16..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, 'utf8') || 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, }; From 227f33452dc7eaedd37348b736fefed55ad88b6b Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 6 Dec 2017 20:47:17 +0100 Subject: [PATCH 3/3] update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) 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