Skip to content

Commit

Permalink
fix root assignment when newCommonPath is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperchupuDev committed Aug 24, 2024
1 parent b5b6ee4 commit 39cf17c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
11 changes: 8 additions & 3 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 39cf17c

Please sign in to comment.