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 a4820cc
Show file tree
Hide file tree
Showing 2 changed files with 14 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
12 changes: 12 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 Expand Up @@ -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',
Expand All @@ -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`,
Expand Down

0 comments on commit a4820cc

Please sign in to comment.