From 9c8b93ee324a4514ec1ca2304ea9970f44cdc927 Mon Sep 17 00:00:00 2001 From: William Stein Date: Tue, 25 Oct 2022 18:30:28 -0700 Subject: [PATCH 1/4] fix #863 -- realpathSync('/') returns invalid result - https://github.com/streamich/memfs/issues/863 --- src/__tests__/volume/realpathSync.test.ts | 7 ++++++- src/volume.ts | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/__tests__/volume/realpathSync.test.ts b/src/__tests__/volume/realpathSync.test.ts index 58d77468a..251dfb386 100644 --- a/src/__tests__/volume/realpathSync.test.ts +++ b/src/__tests__/volume/realpathSync.test.ts @@ -1,6 +1,6 @@ import { create } from '../util'; -describe('.realpath(...)', () => { +describe('.realpathSync(...)', () => { it('works with symlinks, #463', () => { const vol = create({}); vol.mkdirSync('/a'); @@ -12,3 +12,8 @@ describe('.realpath(...)', () => { expect(path).toBe('/c/index.js'); }); }); + +describe("edge case -- realpathSync('/') returns '/'", () => { + const vol = create({'./a':'a'}); + expect(vol.realpathSync('/')).toBe('/'); +}); \ No newline at end of file diff --git a/src/volume.ts b/src/volume.ts index 4f635324a..b34860ed5 100644 --- a/src/volume.ts +++ b/src/volume.ts @@ -1493,7 +1493,8 @@ export class Volume { const realLink = this.getResolvedLink(steps); if (!realLink) throw createError(ENOENT, 'realpath', filename); - return strToEncoding(realLink.getPath(), encoding); + const path = realLink.getPath(); + return strToEncoding(path ? path : "/", encoding); } realpathSync(path: PathLike, options?: IRealpathOptions | string): TDataOut { From c94557ee676ab9517a14554b3dcb915fbe2effe2 Mon Sep 17 00:00:00 2001 From: William Stein Date: Tue, 25 Oct 2022 20:21:22 -0700 Subject: [PATCH 2/4] run prettier --- src/__tests__/volume/realpathSync.test.ts | 4 ++-- src/volume.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/__tests__/volume/realpathSync.test.ts b/src/__tests__/volume/realpathSync.test.ts index 251dfb386..b75164b0a 100644 --- a/src/__tests__/volume/realpathSync.test.ts +++ b/src/__tests__/volume/realpathSync.test.ts @@ -14,6 +14,6 @@ describe('.realpathSync(...)', () => { }); describe("edge case -- realpathSync('/') returns '/'", () => { - const vol = create({'./a':'a'}); + const vol = create({ './a': 'a' }); expect(vol.realpathSync('/')).toBe('/'); -}); \ No newline at end of file +}); diff --git a/src/volume.ts b/src/volume.ts index b34860ed5..ce9832719 100644 --- a/src/volume.ts +++ b/src/volume.ts @@ -1494,7 +1494,7 @@ export class Volume { if (!realLink) throw createError(ENOENT, 'realpath', filename); const path = realLink.getPath(); - return strToEncoding(path ? path : "/", encoding); + return strToEncoding(path ? path : '/', encoding); } realpathSync(path: PathLike, options?: IRealpathOptions | string): TDataOut { From dfe01f5f2f5e3e737d949b84c983840b597ba9ad Mon Sep 17 00:00:00 2001 From: William Stein Date: Thu, 3 Nov 2022 09:03:57 -0700 Subject: [PATCH 3/4] style: move a test case --- src/__tests__/volume/realpathSync.test.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/__tests__/volume/realpathSync.test.ts b/src/__tests__/volume/realpathSync.test.ts index b75164b0a..95465c15f 100644 --- a/src/__tests__/volume/realpathSync.test.ts +++ b/src/__tests__/volume/realpathSync.test.ts @@ -11,9 +11,8 @@ describe('.realpathSync(...)', () => { const path = vol.realpathSync('/a/b/index.js'); expect(path).toBe('/c/index.js'); }); -}); - -describe("edge case -- realpathSync('/') returns '/'", () => { - const vol = create({ './a': 'a' }); - expect(vol.realpathSync('/')).toBe('/'); + it('returns the root correctly', () => { + const vol = create({ './a': 'a' }); + expect(vol.realpathSync('/')).toBe('/'); + }); }); From b83585e0c7506fa2c6723fded8425d435d0e1d40 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Mon, 14 Nov 2022 07:32:03 +1300 Subject: [PATCH 4/4] refactor: inline conditional --- src/volume.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/volume.ts b/src/volume.ts index bdbd7623b..01b80504d 100644 --- a/src/volume.ts +++ b/src/volume.ts @@ -1493,8 +1493,7 @@ export class Volume { const realLink = this.getResolvedLink(steps); if (!realLink) throw createError(ENOENT, 'realpath', filename); - const path = realLink.getPath(); - return strToEncoding(path ? path : '/', encoding); + return strToEncoding(realLink.getPath() || '/', encoding); } realpathSync(path: PathLike, options?: IRealpathOptions | string): TDataOut {