diff --git a/src/index.ts b/src/index.ts index 0eb4dc1..a9c63da 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import path, { posix } from 'node:path'; import { type Options as FdirOptions, fdir } from 'fdir'; import picomatch from 'picomatch'; -import { isDynamicPattern } from './utils.ts'; +import { escapePath, isDynamicPattern } from './utils.ts'; export interface GlobOptions { absolute?: boolean; @@ -41,7 +41,7 @@ function normalizePattern( } if (path.isAbsolute(result.replace(/\\(?=[()[\]{}!*+?@|])/g, ''))) { - result = posix.relative(cwd, result); + result = posix.relative(escapePath(cwd), result); } else { result = posix.normalize(result); } diff --git a/test/index.test.ts b/test/index.test.ts index 3e29650..bbe30bd 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -14,6 +14,7 @@ const fixture = await createFixture({ 'b.txt': 'b' }, '.a/a/a.txt': 'a', + '.[a]/a.txt': 'a', '.deep/a/a/a.txt': 'a', '.symlink': { file: ({ symlink }) => symlink('../a/a.txt'), @@ -142,6 +143,15 @@ test('fully handle absolute patterns', async () => { assert.deepEqual(files.sort(), ['../b/a.txt', 'a.txt']); }); +test('escaped absolute patterns', async () => { + const files = await glob({ + patterns: [`${cwd.replaceAll('\\', '/')}.\\[a\\]/a.txt`], + absolute: true, + cwd: path.join(cwd, '.[a]') + }); + assert.deepEqual(files.sort(), [`${cwd.replaceAll('\\', '/')}.[a]/a.txt`]); +}); + test('leading ../', async () => { const files = await glob({ patterns: ['../b/*.txt'], cwd: path.join(cwd, 'a') }); assert.deepEqual(files.sort(), ['../b/a.txt', '../b/b.txt']); @@ -221,6 +231,7 @@ test('handle recursive symlinks', async () => { cwd }); assert.deepEqual(files.sort(), [ + '.symlink/.recursive/.[a]/a.txt', '.symlink/.recursive/.symlink/file', '.symlink/.recursive/a/a.txt', '.symlink/.recursive/a/b.txt', @@ -246,6 +257,7 @@ test('handle recursive symlinks (absolute)', async () => { cwd }); assert.deepEqual(files.sort(), [ + `${cwd.replaceAll('\\', '/')}.symlink/.recursive/.[a]/a.txt`, `${cwd.replaceAll('\\', '/')}.symlink/.recursive/.symlink/file`, `${cwd.replaceAll('\\', '/')}.symlink/.recursive/a/a.txt`, `${cwd.replaceAll('\\', '/')}.symlink/.recursive/a/b.txt`,