From e58eff2cf1f052713836d7778e20f540e6f41a81 Mon Sep 17 00:00:00 2001 From: Aviv Keller Date: Tue, 22 Oct 2024 07:38:09 -0400 Subject: [PATCH] fs: pass correct path to `DirentFromStats` during `glob` PR-URL: https://github.com/nodejs/node/pull/55071 Reviewed-By: Yongsheng Zhang Reviewed-By: Moshe Atlow Reviewed-By: James M Snell Reviewed-By: Ethan Arrowood --- lib/internal/fs/glob.js | 6 +++--- test/parallel/test-fs-glob.mjs | 16 +++++++++------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/internal/fs/glob.js b/lib/internal/fs/glob.js index d4b73a0ffca46d..0f4939f7e1ba0d 100644 --- a/lib/internal/fs/glob.js +++ b/lib/internal/fs/glob.js @@ -16,7 +16,7 @@ const { const { lstatSync, readdirSync } = require('fs'); const { lstat, readdir } = require('fs/promises'); -const { join, resolve, basename, isAbsolute } = require('path'); +const { join, resolve, basename, isAbsolute, dirname } = require('path'); const { kEmptyObject, @@ -48,7 +48,7 @@ async function getDirent(path) { } catch { return null; } - return new DirentFromStats(basename(path), stat, path); + return new DirentFromStats(basename(path), stat, dirname(path)); } /** @@ -60,7 +60,7 @@ function getDirentSync(path) { if (stat === undefined) { return null; } - return new DirentFromStats(basename(path), stat, path); + return new DirentFromStats(basename(path), stat, dirname(path)); } class Cache { diff --git a/test/parallel/test-fs-glob.mjs b/test/parallel/test-fs-glob.mjs index 4038c0e165eb69..2321ceb87b8ba1 100644 --- a/test/parallel/test-fs-glob.mjs +++ b/test/parallel/test-fs-glob.mjs @@ -1,6 +1,6 @@ import * as common from '../common/index.mjs'; import tmpdir from '../common/tmpdir.js'; -import { resolve, dirname, sep, basename } from 'node:path'; +import { resolve, dirname, sep, relative, join, isAbsolute } from 'node:path'; import { mkdir, writeFile, symlink, glob as asyncGlob } from 'node:fs/promises'; import { glob, globSync, Dirent } from 'node:fs'; import { test, describe } from 'node:test'; @@ -338,6 +338,11 @@ describe('fsPromises glob', function() { } }); +const normalizeDirent = (dirent) => relative(fixtureDir, join(dirent.parentPath, dirent.name)); +// The call to `join()` with only one argument is important, as +// it ensures that the proper path seperators are applied. +const normalizePath = (path) => (isAbsolute(path) ? relative(fixtureDir, path) : join(path)); + describe('glob - withFileTypes', function() { const promisified = promisify(glob); for (const [pattern, expected] of Object.entries(patterns)) { @@ -348,8 +353,7 @@ describe('glob - withFileTypes', function() { exclude: (dirent) => assert.ok(dirent instanceof Dirent), }); assertDirents(actual); - const normalized = expected.filter(Boolean).map((item) => basename(item)).sort(); - assert.deepStrictEqual(actual.map((dirent) => dirent.name).sort(), normalized.sort()); + assert.deepStrictEqual(actual.map(normalizeDirent).sort(), expected.filter(Boolean).map(normalizePath).sort()); }); } }); @@ -363,8 +367,7 @@ describe('globSync - withFileTypes', function() { exclude: (dirent) => assert.ok(dirent instanceof Dirent), }); assertDirents(actual); - const normalized = expected.filter(Boolean).map((item) => basename(item)).sort(); - assert.deepStrictEqual(actual.map((dirent) => dirent.name).sort(), normalized.sort()); + assert.deepStrictEqual(actual.map(normalizeDirent).sort(), expected.filter(Boolean).map(normalizePath).sort()); }); } }); @@ -379,8 +382,7 @@ describe('fsPromises glob - withFileTypes', function() { exclude: (dirent) => assert.ok(dirent instanceof Dirent), })) actual.push(item); assertDirents(actual); - const normalized = expected.filter(Boolean).map((item) => basename(item)).sort(); - assert.deepStrictEqual(actual.map((dirent) => dirent.name).sort(), normalized.sort()); + assert.deepStrictEqual(actual.map(normalizeDirent).sort(), expected.filter(Boolean).map(normalizePath).sort()); }); } });