Skip to content

Commit a6a0816

Browse files
committed
test(fslib): Add tests
1 parent 8a61f8e commit a6a0816

File tree

2 files changed

+174
-32
lines changed

2 files changed

+174
-32
lines changed
154 Bytes
Binary file not shown.

packages/yarnpkg-fslib/tests/patchedFs.test.ts

Lines changed: 174 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
import {ZipFS, ZipOpenFS} from '@yarnpkg/libzip';
2-
import fs from 'fs';
3-
import {pathToFileURL} from 'url';
4-
import {promisify} from 'util';
5-
6-
import {ZIP_FILE1, ZIP_DIR1} from '../../yarnpkg-libzip/tests/ZipOpenFS.test';
7-
import {NodeFS} from '../sources/NodeFS';
8-
import {PosixFS} from '../sources/PosixFS';
9-
import {extendFs} from '../sources/patchFs/patchFs';
10-
import {npath, PortablePath} from '../sources/path';
11-
import {xfs} from '../sources/xfs';
12-
import {statUtils} from '../sources';
1+
import {ZipFS, ZipOpenFS} from '@yarnpkg/libzip';
2+
import fs from 'fs';
3+
import {pathToFileURL} from 'url';
4+
import {promisify} from 'util';
5+
6+
import {NodeFS} from '../sources/NodeFS';
7+
import {PosixFS} from '../sources/PosixFS';
8+
import {extendFs} from '../sources/patchFs/patchFs';
9+
import {npath, PortablePath, ppath} from '../sources/path';
10+
import type {Filename} from '../sources/path';
11+
import {xfs} from '../sources/xfs';
12+
import {statUtils} from '../sources';
1313

1414
const ifNotWin32It = process.platform !== `win32` ? it : it.skip;
1515

16+
const ZIP_DIR = ppath.join(
17+
npath.toPortablePath(__dirname),
18+
`fixtures/foo.zip` as Filename,
19+
);
20+
const ZIP_FILE = ppath.join(ZIP_DIR, `foo.txt`);
21+
1622
describe(`patchedFs`, () => {
1723
it(`in case of no error, give null: fs.stat`, done => {
1824
const file = npath.join(__dirname, `patchedFs.test.ts`);
@@ -159,10 +165,10 @@ describe(`patchedFs`, () => {
159165
patchedFs.closeSync(fd);
160166
}
161167

162-
const zipFd = patchedFs.openSync(ZIP_FILE1, `r`);
168+
const zipFd = patchedFs.openSync(ZIP_FILE, `r`);
163169
try {
164170
const stat = await new Promise<fs.Stats>((resolve, reject) => {
165-
patchedFs.stat(ZIP_FILE1, (err, stats) => {
171+
patchedFs.stat(ZIP_FILE, (err, stats) => {
166172
err ? reject(err) : resolve(stats);
167173
});
168174
});
@@ -192,13 +198,13 @@ describe(`patchedFs`, () => {
192198
)).resolves.toHaveLength(0);
193199
await expect(patchedFs.promises.readdir(tmpdir, null)).resolves.toHaveLength(0);
194200

195-
expect(patchedFs.readdirSync(ZIP_DIR1, null)).toStrictEqual([`foo.txt`]);
201+
expect(patchedFs.readdirSync(ZIP_DIR, null)).toStrictEqual([`foo.txt`]);
196202
await expect(new Promise((resolve, reject) =>
197-
patchedFs.readdir(ZIP_DIR1, null, (err, files) =>
203+
patchedFs.readdir(ZIP_DIR, null, (err, files) =>
198204
err ? reject(err) : resolve(files),
199205
),
200206
)).resolves.toStrictEqual([`foo.txt`]);
201-
await expect(patchedFs.promises.readdir(ZIP_DIR1, null)).resolves.toStrictEqual([`foo.txt`]);
207+
await expect(patchedFs.promises.readdir(ZIP_DIR, null)).resolves.toStrictEqual([`foo.txt`]);
202208
});
203209

204210
it(`should support createReadStream`, async () => {
@@ -340,34 +346,170 @@ describe(`patchedFs`, () => {
340346
expect(statUtils.areStatsEqual(fdStats, syncStats)).toEqual(true);
341347
});
342348

343-
it(`should support FileHandle.read`, async () => {
344-
const patchedFs = extendFs(fs, new PosixFS(new NodeFS()));
349+
describe(`should support FileHandle.read`, () => {
350+
it(`returns a new Buffer of read bytes`, async () => {
351+
const patchedFs = extendFs(fs, new PosixFS(new NodeFS()));
345352

346-
await xfs.mktempPromise(async dir => {
347-
const filepath = npath.join(npath.fromPortablePath(dir), `foo.txt`);
348-
await patchedFs.promises.writeFile(filepath, `foo`);
353+
await xfs.mktempPromise(async dir => {
354+
const filepath = npath.join(npath.fromPortablePath(dir), `foo.txt`);
355+
await patchedFs.promises.writeFile(filepath, `foo`);
349356

350-
{
351357
const fd = await patchedFs.promises.open(filepath, `r`);
352358

353-
const data = Buffer.allocUnsafe(3);
354-
await expect(fd.read(data, 0, 3)).resolves.toMatchObject({
355-
buffer: data,
356-
bytesRead: 3,
357-
});
359+
const {buffer, bytesRead} = await fd.read();
360+
expect(bytesRead).toEqual(3);
361+
expect(buffer.subarray(0, 3)).toEqual(Buffer.from(`foo`));
358362

359363
await fd.close();
360-
}
364+
});
365+
});
366+
it(`reads into a Buffer`, async () => {
367+
const patchedFs = extendFs(fs, new PosixFS(new NodeFS()));
368+
369+
await xfs.mktempPromise(async dir => {
370+
const filepath = npath.join(npath.fromPortablePath(dir), `foo.txt`);
371+
await patchedFs.promises.writeFile(filepath, `foo`);
361372

362-
{
363373
const fd = await patchedFs.promises.open(filepath, `r`);
364374

365-
const {buffer, bytesRead} = await fd.read();
375+
const data = Buffer.allocUnsafe(3);
376+
const {buffer, bytesRead} = await fd.read(data, 0, 3);
366377
expect(bytesRead).toEqual(3);
378+
expect(buffer).toBe(data);
367379
expect(buffer.subarray(0, 3)).toEqual(Buffer.from(`foo`));
368380

369381
await fd.close();
370-
}
382+
});
383+
});
384+
it(`reads into a Buffer with offset via args`, async () => {
385+
const patchedFs = extendFs(fs, new PosixFS(new NodeFS()));
386+
387+
await xfs.mktempPromise(async dir => {
388+
const filepath = npath.join(npath.fromPortablePath(dir), `foo.txt`);
389+
await patchedFs.promises.writeFile(filepath, `foo`);
390+
391+
const fd = await patchedFs.promises.open(filepath, `r`);
392+
393+
const data = Buffer.allocUnsafe(5);
394+
const {buffer, bytesRead} = await fd.read(data, 1, 3);
395+
expect(bytesRead).toEqual(3);
396+
expect(buffer).toBe(data);
397+
expect(buffer.subarray(1, 4)).toEqual(Buffer.from(`foo`));
398+
399+
await fd.close();
400+
});
401+
});
402+
it(`reads into a Buffer with offset via options`, async () => {
403+
const patchedFs = extendFs(fs, new PosixFS(new NodeFS()));
404+
405+
await xfs.mktempPromise(async dir => {
406+
const filepath = npath.join(npath.fromPortablePath(dir), `foo.txt`);
407+
await patchedFs.promises.writeFile(filepath, `foo`);
408+
409+
const fd = await patchedFs.promises.open(filepath, `r`);
410+
411+
const data = Buffer.allocUnsafe(5);
412+
const {buffer, bytesRead} = await fd.read({buffer: data, offset: 1, length: 3});
413+
expect(bytesRead).toEqual(3);
414+
expect(buffer).toBe(data);
415+
expect(buffer.subarray(1, 4)).toEqual(Buffer.from(`foo`));
416+
417+
await fd.close();
418+
});
419+
});
420+
it(`reads into a Buffer with offset via separate options`, async () => {
421+
const patchedFs = extendFs(fs, new PosixFS(new NodeFS()));
422+
423+
await xfs.mktempPromise(async dir => {
424+
const filepath = npath.join(npath.fromPortablePath(dir), `foo.txt`);
425+
await patchedFs.promises.writeFile(filepath, `foo`);
426+
427+
const fd = await patchedFs.promises.open(filepath, `r`);
428+
429+
const data = Buffer.allocUnsafe(5);
430+
// @ts-expect-error @types/node is not up to date, missing read(buffer[, options]) form
431+
const {buffer, bytesRead} = await fd.read(data, {offset: 1, length: 3});
432+
expect(bytesRead).toEqual(3);
433+
expect(buffer).toBe(data);
434+
expect(buffer.subarray(1, 4)).toEqual(Buffer.from(`foo`));
435+
436+
await fd.close();
437+
});
438+
});
439+
it(`reads into a TypedArray`, async () => {
440+
const patchedFs = extendFs(fs, new PosixFS(new NodeFS()));
441+
442+
await xfs.mktempPromise(async dir => {
443+
const filepath = npath.join(npath.fromPortablePath(dir), `foo.txt`);
444+
await patchedFs.promises.writeFile(filepath, `foo`);
445+
446+
const fd = await patchedFs.promises.open(filepath, `r`);
447+
448+
const data = new Uint8Array(3);
449+
const {buffer, bytesRead} = await fd.read(data, 0, 3);
450+
expect(bytesRead).toEqual(3);
451+
expect(buffer).toBe(data);
452+
expect(buffer).toEqual(Uint8Array.from([102, 111, 111]));
453+
454+
await fd.close();
455+
});
456+
});
457+
it(`reads into a DataView`, async () => {
458+
const patchedFs = extendFs(fs, new PosixFS(new NodeFS()));
459+
460+
await xfs.mktempPromise(async dir => {
461+
const filepath = npath.join(npath.fromPortablePath(dir), `foo.txt`);
462+
await patchedFs.promises.writeFile(filepath, `foo`);
463+
464+
const fd = await patchedFs.promises.open(filepath, `r`);
465+
466+
const arrayBuffer = new ArrayBuffer(3);
467+
const data = new DataView(arrayBuffer);
468+
const {buffer, bytesRead} = await fd.read(data, 0, 3);
469+
expect(bytesRead).toEqual(3);
470+
expect(buffer).toBe(data);
471+
expect(arrayBuffer).toEqual(Uint8Array.from([102, 111, 111]).buffer);
472+
473+
await fd.close();
474+
});
475+
});
476+
it(`reads into an offset DataView`, async () => {
477+
const patchedFs = extendFs(fs, new PosixFS(new NodeFS()));
478+
479+
await xfs.mktempPromise(async dir => {
480+
const filepath = npath.join(npath.fromPortablePath(dir), `foo.txt`);
481+
await patchedFs.promises.writeFile(filepath, `foo`);
482+
483+
const fd = await patchedFs.promises.open(filepath, `r`);
484+
485+
const arrayBuffer = new ArrayBuffer(5);
486+
const data = new DataView(arrayBuffer, 1, 3);
487+
const {buffer, bytesRead} = await fd.read(data, 0, 3);
488+
expect(bytesRead).toEqual(3);
489+
expect(buffer).toBe(data);
490+
expect(arrayBuffer).toEqual(Uint8Array.from([0, 102, 111, 111, 0]).buffer);
491+
492+
await fd.close();
493+
});
494+
});
495+
it(`reads into an offset DataView with offset`, async () => {
496+
const patchedFs = extendFs(fs, new PosixFS(new NodeFS()));
497+
498+
await xfs.mktempPromise(async dir => {
499+
const filepath = npath.join(npath.fromPortablePath(dir), `foo.txt`);
500+
await patchedFs.promises.writeFile(filepath, `foo`);
501+
502+
const fd = await patchedFs.promises.open(filepath, `r`);
503+
504+
const arrayBuffer = new ArrayBuffer(8);
505+
const data = new DataView(arrayBuffer, 1, 6);
506+
const {buffer, bytesRead} = await fd.read(data, 2, 3);
507+
expect(bytesRead).toEqual(3);
508+
expect(buffer).toBe(data);
509+
expect(arrayBuffer).toEqual(Uint8Array.from([0, 0, 0, 102, 111, 111, 0, 0]).buffer);
510+
511+
await fd.close();
512+
});
371513
});
372514
});
373515

0 commit comments

Comments
 (0)