Skip to content

Commit

Permalink
fix(fslib): accept null as second argument for readdir (#3239)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-soporan authored Aug 9, 2021
1 parent 01586a8 commit 857e21f
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 89 deletions.
66 changes: 18 additions & 48 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions .yarn/versions/c82aa0f0.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
releases:
"@yarnpkg/cli": patch
"@yarnpkg/fslib": patch
"@yarnpkg/plugin-pnp": patch
"@yarnpkg/pnp": patch
"@yarnpkg/pnpify": patch

declined:
- "@yarnpkg/esbuild-plugin-pnp"
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-http"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- vscode-zipfs
- "@yarnpkg/builder"
- "@yarnpkg/core"
- "@yarnpkg/doctor"
- "@yarnpkg/json-proxy"
- "@yarnpkg/nm"
- "@yarnpkg/sdks"
- "@yarnpkg/shell"
4 changes: 2 additions & 2 deletions packages/yarnpkg-fslib/sources/FakeFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ export abstract class FakeFS<P extends Path> {
abstract realpathSync(p: P): P;

abstract readdirPromise(p: P): Promise<Array<Filename>>;
abstract readdirPromise(p: P, opts: {withFileTypes: false}): Promise<Array<Filename>>;
abstract readdirPromise(p: P, opts: {withFileTypes: false} | null): Promise<Array<Filename>>;
abstract readdirPromise(p: P, opts: {withFileTypes: true}): Promise<Array<Dirent>>;
abstract readdirPromise(p: P, opts: {withFileTypes: boolean}): Promise<Array<Filename> | Array<Dirent>>;

abstract readdirSync(p: P): Array<Filename>;
abstract readdirSync(p: P, opts: {withFileTypes: false}): Array<Filename>;
abstract readdirSync(p: P, opts: {withFileTypes: false} | null): Array<Filename>;
abstract readdirSync(p: P, opts: {withFileTypes: true}): Array<Dirent>;
abstract readdirSync(p: P, opts: {withFileTypes: boolean}): Array<Filename> | Array<Dirent>;

Expand Down
12 changes: 6 additions & 6 deletions packages/yarnpkg-fslib/sources/NodeFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,12 +412,12 @@ export class NodeFS extends BasePortableFakeFS {
}

async readdirPromise(p: PortablePath): Promise<Array<Filename>>;
async readdirPromise(p: PortablePath, opts: {withFileTypes: false}): Promise<Array<Filename>>;
async readdirPromise(p: PortablePath, opts: {withFileTypes: false} | null): Promise<Array<Filename>>;
async readdirPromise(p: PortablePath, opts: {withFileTypes: true}): Promise<Array<Dirent>>;
async readdirPromise(p: PortablePath, opts: {withFileTypes: boolean}): Promise<Array<Filename> | Array<Dirent>>;
async readdirPromise(p: PortablePath, {withFileTypes}: {withFileTypes?: boolean} = {}): Promise<Array<string> | Array<Dirent>> {
async readdirPromise(p: PortablePath, opts?: {withFileTypes?: boolean} | null): Promise<Array<string> | Array<Dirent>> {
return await new Promise<Array<Filename> | Array<Dirent>>((resolve, reject) => {
if (withFileTypes) {
if (opts?.withFileTypes) {
this.realFs.readdir(npath.fromPortablePath(p), {withFileTypes: true}, this.makeCallback(resolve, reject) as any);
} else {
this.realFs.readdir(npath.fromPortablePath(p), this.makeCallback(value => resolve(value as Array<Filename>), reject));
Expand All @@ -426,11 +426,11 @@ export class NodeFS extends BasePortableFakeFS {
}

readdirSync(p: PortablePath): Array<Filename>;
readdirSync(p: PortablePath, opts: {withFileTypes: false}): Array<Filename>;
readdirSync(p: PortablePath, opts: {withFileTypes: false} | null): Array<Filename>;
readdirSync(p: PortablePath, opts: {withFileTypes: true}): Array<Dirent>;
readdirSync(p: PortablePath, opts: {withFileTypes: boolean}): Array<Filename> | Array<Dirent>;
readdirSync(p: PortablePath, {withFileTypes}: {withFileTypes?: boolean} = {}): Array<string> | Array<Dirent> {
if (withFileTypes) {
readdirSync(p: PortablePath, opts?: {withFileTypes?: boolean} | null): Array<string> | Array<Dirent> {
if (opts?.withFileTypes) {
return this.realFs.readdirSync(npath.fromPortablePath(p), {withFileTypes: true} as any);
} else {
return this.realFs.readdirSync(npath.fromPortablePath(p)) as Array<Filename>;
Expand Down
12 changes: 6 additions & 6 deletions packages/yarnpkg-fslib/sources/ProxiedFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,19 +291,19 @@ export abstract class ProxiedFS<P extends Path, IP extends Path> extends FakeFS<
}

async readdirPromise(p: P): Promise<Array<Filename>>;
async readdirPromise(p: P, opts: {withFileTypes: false}): Promise<Array<Filename>>;
async readdirPromise(p: P, opts: {withFileTypes: false} | null): Promise<Array<Filename>>;
async readdirPromise(p: P, opts: {withFileTypes: true}): Promise<Array<Dirent>>;
async readdirPromise(p: P, opts: {withFileTypes: boolean}): Promise<Array<Filename> | Array<Dirent>>;
async readdirPromise(p: P, {withFileTypes}: {withFileTypes?: boolean} = {}): Promise<Array<string> | Array<Dirent>> {
return this.baseFs.readdirPromise(this.mapToBase(p), {withFileTypes: withFileTypes as any});
async readdirPromise(p: P, opts?: {withFileTypes?: boolean} | null): Promise<Array<string> | Array<Dirent>> {
return this.baseFs.readdirPromise(this.mapToBase(p), opts as any);
}

readdirSync(p: P): Array<Filename>;
readdirSync(p: P, opts: {withFileTypes: false}): Array<Filename>;
readdirSync(p: P, opts: {withFileTypes: false} | null): Array<Filename>;
readdirSync(p: P, opts: {withFileTypes: true}): Array<Dirent>;
readdirSync(p: P, opts: {withFileTypes: boolean}): Array<Filename> | Array<Dirent>;
readdirSync(p: P, {withFileTypes}: {withFileTypes?: boolean} = {}): Array<string> | Array<Dirent> {
return this.baseFs.readdirSync(this.mapToBase(p), {withFileTypes: withFileTypes as any});
readdirSync(p: P, opts?: {withFileTypes?: boolean} | null): Array<string> | Array<Dirent> {
return this.baseFs.readdirSync(this.mapToBase(p), opts as any);
}

async readlinkPromise(p: P) {
Expand Down
12 changes: 6 additions & 6 deletions packages/yarnpkg-fslib/sources/ZipFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1363,18 +1363,18 @@ export class ZipFS extends BasePortableFakeFS {
}

async readdirPromise(p: PortablePath): Promise<Array<Filename>>;
async readdirPromise(p: PortablePath, opts: {withFileTypes: false}): Promise<Array<Filename>>;
async readdirPromise(p: PortablePath, opts: {withFileTypes: false} | null): Promise<Array<Filename>>;
async readdirPromise(p: PortablePath, opts: {withFileTypes: true}): Promise<Array<statUtils.DirEntry>>;
async readdirPromise(p: PortablePath, opts: {withFileTypes: boolean}): Promise<Array<Filename> | Array<statUtils.DirEntry>>;
async readdirPromise(p: PortablePath, {withFileTypes}: {withFileTypes?: boolean} = {}): Promise<Array<string> | Array<statUtils.DirEntry>> {
return this.readdirSync(p, {withFileTypes: withFileTypes as any});
async readdirPromise(p: PortablePath, opts?: {withFileTypes?: boolean} | null): Promise<Array<string> | Array<statUtils.DirEntry>> {
return this.readdirSync(p, opts as any);
}

readdirSync(p: PortablePath): Array<Filename>;
readdirSync(p: PortablePath, opts: {withFileTypes: false}): Array<Filename>;
readdirSync(p: PortablePath, opts: {withFileTypes: false} | null): Array<Filename>;
readdirSync(p: PortablePath, opts: {withFileTypes: true}): Array<statUtils.DirEntry>;
readdirSync(p: PortablePath, opts: {withFileTypes: boolean}): Array<Filename> | Array<statUtils.DirEntry>;
readdirSync(p: PortablePath, {withFileTypes}: {withFileTypes?: boolean} = {}): Array<string> | Array<statUtils.DirEntry> {
readdirSync(p: PortablePath, opts?: {withFileTypes?: boolean} | null): Array<string> | Array<statUtils.DirEntry> {
const resolvedP = this.resolveFilename(`scandir '${p}'`, p);
if (!this.entries.has(resolvedP) && !this.listings.has(resolvedP))
throw errors.ENOENT(`scandir '${p}'`);
Expand All @@ -1384,7 +1384,7 @@ export class ZipFS extends BasePortableFakeFS {
throw errors.ENOTDIR(`scandir '${p}'`);

const entries = [...directoryListing];
if (!withFileTypes)
if (!opts?.withFileTypes)
return entries;

return entries.map(name => {
Expand Down
16 changes: 8 additions & 8 deletions packages/yarnpkg-fslib/sources/ZipOpenFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -750,28 +750,28 @@ export class ZipOpenFS extends BasePortableFakeFS {
}

async readdirPromise(p: PortablePath): Promise<Array<Filename>>;
async readdirPromise(p: PortablePath, opts: {withFileTypes: false}): Promise<Array<Filename>>;
async readdirPromise(p: PortablePath, opts: {withFileTypes: false} | null): Promise<Array<Filename>>;
async readdirPromise(p: PortablePath, opts: {withFileTypes: true}): Promise<Array<Dirent>>;
async readdirPromise(p: PortablePath, opts: {withFileTypes: boolean}): Promise<Array<Filename> | Array<Dirent>>;
async readdirPromise(p: PortablePath, {withFileTypes}: {withFileTypes?: boolean} = {}): Promise<Array<string> | Array<Dirent>> {
async readdirPromise(p: PortablePath, opts?: {withFileTypes?: boolean} | null): Promise<Array<string> | Array<Dirent>> {
return await this.makeCallPromise(p, async () => {
return await this.baseFs.readdirPromise(p, {withFileTypes: withFileTypes as any});
return await this.baseFs.readdirPromise(p, opts as any);
}, async (zipFs, {subPath}) => {
return await zipFs.readdirPromise(subPath, {withFileTypes: withFileTypes as any});
return await zipFs.readdirPromise(subPath, opts as any);
}, {
requireSubpath: false,
});
}

readdirSync(p: PortablePath): Array<Filename>;
readdirSync(p: PortablePath, opts: {withFileTypes: false}): Array<Filename>;
readdirSync(p: PortablePath, opts: {withFileTypes: false} | null): Array<Filename>;
readdirSync(p: PortablePath, opts: {withFileTypes: true}): Array<Dirent>;
readdirSync(p: PortablePath, opts: {withFileTypes: boolean}): Array<Filename> | Array<Dirent>;
readdirSync(p: PortablePath, {withFileTypes}: {withFileTypes?: boolean} = {}): Array<string> | Array<Dirent> {
readdirSync(p: PortablePath, opts?: {withFileTypes?: boolean} | null): Array<string> | Array<Dirent> {
return this.makeCallSync(p, () => {
return this.baseFs.readdirSync(p, {withFileTypes: withFileTypes as any});
return this.baseFs.readdirSync(p, opts as any);
}, (zipFs, {subPath}) => {
return zipFs.readdirSync(subPath, {withFileTypes: withFileTypes as any});
return zipFs.readdirSync(subPath, opts as any);
}, {
requireSubpath: false,
});
Expand Down
6 changes: 3 additions & 3 deletions packages/yarnpkg-fslib/tests/ZipOpenFS.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import {ZipOpenFS} from '../sources';

import {useFakeTime} from './utils';

const ZIP_DIR1 = ppath.join(
export const ZIP_DIR1 = ppath.join(
npath.toPortablePath(__dirname),
`fixtures/foo.zip` as Filename
);
const ZIP_DIR2 = ppath.join(
export const ZIP_DIR2 = ppath.join(
npath.toPortablePath(__dirname),
`fixtures/folder.zip/foo.zip` as Filename
);

export const ZIP_FILE1 = ppath.join(ZIP_DIR1, `foo.txt` as Filename);
const ZIP_FILE2 = ppath.join(ZIP_DIR2, `foo.txt` as Filename);
export const ZIP_FILE2 = ppath.join(ZIP_DIR2, `foo.txt` as Filename);

describe(`getArchivePart`, () => {
const tests = [
Expand Down
24 changes: 23 additions & 1 deletion packages/yarnpkg-fslib/tests/patchedFs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {Filename, npath} from '../sources/path';
import {xfs} from '../sources/xfs';
import {statUtils, ZipOpenFS} from '../sources';

import {ZIP_FILE1} from "./ZipOpenFS.test";
import {ZIP_FILE1, ZIP_DIR1} from "./ZipOpenFS.test";

describe(`patchedFs`, () => {
it(`in case of no error, give null: fs.stat`, done => {
Expand Down Expand Up @@ -121,4 +121,26 @@ describe(`patchedFs`, () => {
patchedFs.closeSync(fd);
}
});

it(`should support passing null as the second argument to readdir`, async () => {
const patchedFs = extendFs(fs, new PosixFS(new ZipOpenFS({libzip: await getLibzipPromise(), baseFs: new NodeFS()})));

const tmpdir = npath.fromPortablePath(xfs.mktempSync());

expect(patchedFs.readdirSync(tmpdir, null)).toHaveLength(0);
await expect(new Promise((resolve, reject) =>
patchedFs.readdir(tmpdir, null, (err, files) =>
err ? reject(err) : resolve(files)
)
)).resolves.toHaveLength(0);
await expect(patchedFs.promises.readdir(tmpdir, null)).resolves.toHaveLength(0);

expect(patchedFs.readdirSync(ZIP_DIR1, null)).toStrictEqual([`foo.txt`]);
await expect(new Promise((resolve, reject) =>
patchedFs.readdir(ZIP_DIR1, null, (err, files) =>
err ? reject(err) : resolve(files)
)
)).resolves.toStrictEqual([`foo.txt`]);
await expect(patchedFs.promises.readdir(ZIP_DIR1, null)).resolves.toStrictEqual([`foo.txt`]);
});
});
2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/hook.js

Large diffs are not rendered by default.

Loading

0 comments on commit 857e21f

Please sign in to comment.