Skip to content

Commit

Permalink
escape cwd before adding to pattern
Browse files Browse the repository at this point in the history
closes #78
  • Loading branch information
SuperchupuDev committed Dec 23, 2024
1 parent d51e2b9 commit 34d6229
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
10 changes: 10 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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']);
Expand Down

0 comments on commit 34d6229

Please sign in to comment.