Skip to content

Commit a4820cc

Browse files
committed
escape cwd before adding to pattern
closes #78
1 parent d51e2b9 commit a4820cc

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path, { posix } from 'node:path';
22
import { type Options as FdirOptions, fdir } from 'fdir';
33
import picomatch from 'picomatch';
4-
import { isDynamicPattern } from './utils.ts';
4+
import { escapePath, isDynamicPattern } from './utils.ts';
55

66
export interface GlobOptions {
77
absolute?: boolean;
@@ -41,7 +41,7 @@ function normalizePattern(
4141
}
4242

4343
if (path.isAbsolute(result.replace(/\\(?=[()[\]{}!*+?@|])/g, ''))) {
44-
result = posix.relative(cwd, result);
44+
result = posix.relative(escapePath(cwd), result);
4545
} else {
4646
result = posix.normalize(result);
4747
}

test/index.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const fixture = await createFixture({
1414
'b.txt': 'b'
1515
},
1616
'.a/a/a.txt': 'a',
17+
'.[a]/a.txt': 'a',
1718
'.deep/a/a/a.txt': 'a',
1819
'.symlink': {
1920
file: ({ symlink }) => symlink('../a/a.txt'),
@@ -142,6 +143,15 @@ test('fully handle absolute patterns', async () => {
142143
assert.deepEqual(files.sort(), ['../b/a.txt', 'a.txt']);
143144
});
144145

146+
test('escaped absolute patterns', async () => {
147+
const files = await glob({
148+
patterns: [`${cwd.replaceAll('\\', '/')}.\\[a\\]/a.txt`],
149+
absolute: true,
150+
cwd: path.join(cwd, '.[a]')
151+
});
152+
assert.deepEqual(files.sort(), [`${cwd.replaceAll('\\', '/')}.[a]/a.txt`]);
153+
});
154+
145155
test('leading ../', async () => {
146156
const files = await glob({ patterns: ['../b/*.txt'], cwd: path.join(cwd, 'a') });
147157
assert.deepEqual(files.sort(), ['../b/a.txt', '../b/b.txt']);
@@ -221,6 +231,7 @@ test('handle recursive symlinks', async () => {
221231
cwd
222232
});
223233
assert.deepEqual(files.sort(), [
234+
'.symlink/.recursive/.[a]/a.txt',
224235
'.symlink/.recursive/.symlink/file',
225236
'.symlink/.recursive/a/a.txt',
226237
'.symlink/.recursive/a/b.txt',
@@ -246,6 +257,7 @@ test('handle recursive symlinks (absolute)', async () => {
246257
cwd
247258
});
248259
assert.deepEqual(files.sort(), [
260+
`${cwd.replaceAll('\\', '/')}.symlink/.recursive/.[a]/a.txt`,
249261
`${cwd.replaceAll('\\', '/')}.symlink/.recursive/.symlink/file`,
250262
`${cwd.replaceAll('\\', '/')}.symlink/.recursive/a/a.txt`,
251263
`${cwd.replaceAll('\\', '/')}.symlink/.recursive/a/b.txt`,

0 commit comments

Comments
 (0)