Skip to content

Commit 45f960c

Browse files
Aviv Kelleraduh95
Aviv Keller
authored andcommitted
fs: pass correct path to DirentFromStats during glob
PR-URL: #55071 Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
1 parent 736c085 commit 45f960c

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

lib/internal/fs/glob.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const {
1616

1717
const { lstatSync, readdirSync } = require('fs');
1818
const { lstat, readdir } = require('fs/promises');
19-
const { join, resolve, basename, isAbsolute } = require('path');
19+
const { join, resolve, basename, isAbsolute, dirname } = require('path');
2020

2121
const {
2222
kEmptyObject,
@@ -48,7 +48,7 @@ async function getDirent(path) {
4848
} catch {
4949
return null;
5050
}
51-
return new DirentFromStats(basename(path), stat, path);
51+
return new DirentFromStats(basename(path), stat, dirname(path));
5252
}
5353

5454
/**
@@ -60,7 +60,7 @@ function getDirentSync(path) {
6060
if (stat === undefined) {
6161
return null;
6262
}
63-
return new DirentFromStats(basename(path), stat, path);
63+
return new DirentFromStats(basename(path), stat, dirname(path));
6464
}
6565

6666
class Cache {

test/parallel/test-fs-glob.mjs

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as common from '../common/index.mjs';
22
import tmpdir from '../common/tmpdir.js';
3-
import { resolve, dirname, sep, basename } from 'node:path';
3+
import { resolve, dirname, sep, relative, join, isAbsolute } from 'node:path';
44
import { mkdir, writeFile, symlink, glob as asyncGlob } from 'node:fs/promises';
55
import { glob, globSync, Dirent } from 'node:fs';
66
import { test, describe } from 'node:test';
@@ -338,6 +338,11 @@ describe('fsPromises glob', function() {
338338
}
339339
});
340340

341+
const normalizeDirent = (dirent) => relative(fixtureDir, join(dirent.parentPath, dirent.name));
342+
// The call to `join()` with only one argument is important, as
343+
// it ensures that the proper path seperators are applied.
344+
const normalizePath = (path) => (isAbsolute(path) ? relative(fixtureDir, path) : join(path));
345+
341346
describe('glob - withFileTypes', function() {
342347
const promisified = promisify(glob);
343348
for (const [pattern, expected] of Object.entries(patterns)) {
@@ -348,8 +353,7 @@ describe('glob - withFileTypes', function() {
348353
exclude: (dirent) => assert.ok(dirent instanceof Dirent),
349354
});
350355
assertDirents(actual);
351-
const normalized = expected.filter(Boolean).map((item) => basename(item)).sort();
352-
assert.deepStrictEqual(actual.map((dirent) => dirent.name).sort(), normalized.sort());
356+
assert.deepStrictEqual(actual.map(normalizeDirent).sort(), expected.filter(Boolean).map(normalizePath).sort());
353357
});
354358
}
355359
});
@@ -363,8 +367,7 @@ describe('globSync - withFileTypes', function() {
363367
exclude: (dirent) => assert.ok(dirent instanceof Dirent),
364368
});
365369
assertDirents(actual);
366-
const normalized = expected.filter(Boolean).map((item) => basename(item)).sort();
367-
assert.deepStrictEqual(actual.map((dirent) => dirent.name).sort(), normalized.sort());
370+
assert.deepStrictEqual(actual.map(normalizeDirent).sort(), expected.filter(Boolean).map(normalizePath).sort());
368371
});
369372
}
370373
});
@@ -379,8 +382,7 @@ describe('fsPromises glob - withFileTypes', function() {
379382
exclude: (dirent) => assert.ok(dirent instanceof Dirent),
380383
})) actual.push(item);
381384
assertDirents(actual);
382-
const normalized = expected.filter(Boolean).map((item) => basename(item)).sort();
383-
assert.deepStrictEqual(actual.map((dirent) => dirent.name).sort(), normalized.sort());
385+
assert.deepStrictEqual(actual.map(normalizeDirent).sort(), expected.filter(Boolean).map(normalizePath).sort());
384386
});
385387
}
386388
});

0 commit comments

Comments
 (0)