From 39cf17c51198657cb99f6651caf239149f4a7d73 Mon Sep 17 00:00:00 2001 From: Superchupu <53496941+SuperchupuDev@users.noreply.github.com> Date: Sat, 24 Aug 2024 14:14:43 +0100 Subject: [PATCH] fix root assignment when `newCommonPath` is empty --- src/index.ts | 4 +++- test/index.test.ts | 11 ++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index a1aefa0..012a803 100644 --- a/src/index.ts +++ b/src/index.ts @@ -69,7 +69,8 @@ function normalizePattern( properties.depthOffset = newCommonPath.length; properties.commonPath = newCommonPath; - properties.root = `${cwd}/${newCommonPath.join('/')}`; + + properties.root = newCommonPath.length > 0 ? `${cwd}/${newCommonPath.join('/')}` : cwd; } return result; @@ -106,6 +107,7 @@ function getRelativePath(path: string, cwd: string, root: string) { function processPath(path: string, cwd: string, root: string, isDirectory: boolean, absolute?: boolean) { const relativePath = absolute ? path.slice(root.length + 1) : path; + //if (absolute) console.log({ path, cwd, root, relativePath }); if (root === cwd) { return isDirectory ? relativePath.slice(0, -1) : relativePath; } diff --git a/test/index.test.ts b/test/index.test.ts index 7dc7727..83ac7ce 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -120,14 +120,19 @@ test('deep with ../', async () => { assert.deepEqual(files3.sort(), ['a.ts']); }); +test('absolute', async () => { + const files = await glob({ patterns: ['a/a.ts'], cwd, absolute: true }); + assert.deepEqual(files.sort(), [`${cwd.replaceAll('\\', '/')}/a/a.ts`]); +}); + test('absolute + dot', async () => { const files = await glob({ patterns: ['a/a.ts'], dot: true, cwd: path.join(cwd, '.a'), absolute: true }); assert.deepEqual(files.sort(), [`${cwd.replaceAll('\\', '/')}/.a/a/a.ts`]); }); -test('absolute', async () => { - const files = await glob({ patterns: ['a/a.ts'], dot: true, cwd: path.join(cwd, '.a'), absolute: true }); - assert.deepEqual(files.sort(), [`${cwd.replaceAll('\\', '/')}/.a/a/a.ts`]); +test('absolute + empty commonPath', async () => { + const files = await glob({ patterns: ['a/**.ts'], cwd, absolute: true, expandDirectories: false }); + assert.deepEqual(files.sort(), [`${cwd.replaceAll('\\', '/')}/a/a.ts`, `${cwd.replaceAll('\\', '/')}/a/b.ts`]); }); test('works with non-absolute cwd', async () => {