react-native-github: remove old fs mock implementation

Reviewed By: rafeca

Differential Revision: D7652914

fbshipit-source-id: 5494305750e7616b5120169266c519f460ae7e6d
This commit is contained in:
Jean Lauliac 2018-04-25 07:03:24 -07:00 committed by Facebook Github Bot
parent 3bf01f272f
commit 4a118ee69c
3 changed files with 51 additions and 0 deletions

View File

@ -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"`;

View File

@ -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'));
});

View File

@ -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;
};