Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: pass correct path to DirentFromStats during glob #55071

Merged
merged 5 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/internal/fs/glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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 {
Expand Down
16 changes: 9 additions & 7 deletions test/parallel/test-fs-glob.mjs
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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));
avivkeller marked this conversation as resolved.
Show resolved Hide resolved

describe('glob - withFileTypes', function() {
const promisified = promisify(glob);
for (const [pattern, expected] of Object.entries(patterns)) {
Expand All @@ -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());
});
}
});
Expand All @@ -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());
});
}
});
Expand All @@ -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());
});
}
});
Loading