From 4a118ee69c6f9a8b8f8a6d14b6f0120a79f294a4 Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Wed, 25 Apr 2018 07:03:24 -0700 Subject: [PATCH] react-native-github: remove old fs mock implementation Reviewed By: rafeca Differential Revision: D7652914 fbshipit-source-id: 5494305750e7616b5120169266c519f460ae7e6d --- .../__snapshots__/index-test.js.snap | 2 ++ .../src/__tests__/index-test.js | 19 ++++++++++++ packages/metro-memory-fs/src/index.js | 30 +++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/packages/metro-memory-fs/src/__tests__/__snapshots__/index-test.js.snap b/packages/metro-memory-fs/src/__tests__/__snapshots__/index-test.js.snap index 89dbcb70..5248ea3d 100644 --- a/packages/metro-memory-fs/src/__tests__/__snapshots__/index-test.js.snap +++ b/packages/metro-memory-fs/src/__tests__/__snapshots__/index-test.js.snap @@ -1,5 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`posix support readlink throws when trying to read a regular file 1`] = `"EINVAL: \`/foo.txt\`: entity is not a symlink"`; + exports[`posix support throws when finding a symlink loop 1`] = `"ELOOP: \`/foo.txt\`: too many levels of symbolic links"`; exports[`posix support throws when trying to create symlink over existing file 1`] = `"EEXIST: \`/foo.txt\`: directory or file already exists"`; diff --git a/packages/metro-memory-fs/src/__tests__/index-test.js b/packages/metro-memory-fs/src/__tests__/index-test.js index c528309b..a9c4873c 100644 --- a/packages/metro-memory-fs/src/__tests__/index-test.js +++ b/packages/metro-memory-fs/src/__tests__/index-test.js @@ -494,6 +494,25 @@ describe('posix support', () => { }); }); + describe('readlink', () => { + it('reads a symlink target', () => { + fs.symlinkSync('foo.txt', '/bar.txt'); + expect(fs.readlinkSync('/bar.txt')).toBe('foo.txt'); + }); + + it('reads a symlink target as buffer', () => { + fs.symlinkSync('foo.txt', '/bar.txt'); + expect(fs.readlinkSync('/bar.txt', 'buffer')).toEqual( + Buffer.from('foo.txt'), + ); + }); + + it('throws when trying to read a regular file', () => { + fs.writeFileSync('/foo.txt', 'test'); + expectFsError('EINVAL', () => fs.readlinkSync('/foo.txt')); + }); + }); + it('throws when trying to read inexistent file', () => { expectFsError('ENOENT', () => fs.readFileSync('/foo.txt')); }); diff --git a/packages/metro-memory-fs/src/index.js b/packages/metro-memory-fs/src/index.js index 62f52546..f07b08fd 100644 --- a/packages/metro-memory-fs/src/index.js +++ b/packages/metro-memory-fs/src/index.js @@ -50,6 +50,7 @@ type Encoding = | 'ascii' | 'base64' | 'binary' + | 'buffer' | 'hex' | 'latin1' | 'ucs2' @@ -99,6 +100,7 @@ const ASYNC_FUNC_NAMES = [ 'read', 'readdir', 'readFile', + 'readlink', 'realpath', 'stat', 'unlink', @@ -317,6 +319,34 @@ class MemoryFs { return result.toString(encoding); }; + readlinkSync = ( + filePath: string | Buffer, + options: ?Encoding | {encoding: ?Encoding}, + ): string | Buffer => { + let encoding; + if (typeof options === 'string') { + encoding = options; + } else if (options != null) { + ({encoding} = options); + } + filePath = pathStr(filePath); + const {node} = this._resolve(filePath, {keepFinalSymlink: true}); + if (node == null) { + throw makeError('ENOENT', filePath, 'no such file or directory'); + } + if (node.type !== 'symbolicLink') { + throw makeError('EINVAL', filePath, 'entity is not a symlink'); + } + if (encoding == null || encoding === 'utf8') { + return node.target; + } + const buf = Buffer.from(node.target); + if (encoding == 'buffer') { + return buf; + } + return buf.toString(encoding); + }; + realpathSync = (filePath: string | Buffer): string => { return this._resolve(pathStr(filePath)).realpath; };