Skip to content

Commit

Permalink
feat: cp method (#7)
Browse files Browse the repository at this point in the history
Co-authored-by: Hiroki Osame <hiroki.osame@gmail.com>
  • Loading branch information
danielbayley and privatenumber authored Jan 29, 2025
1 parent 50c6de6 commit e3bf3e5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ class FsFixture {
*/
rm(subpath?: string): Promise<void>

/**
Copy a path into the fixture directory.
*/
cp(sourcePath: string, destinationSubpath?: string): Promise<void>

/**
Create a file in the fixture directory.
*/
Expand Down
16 changes: 16 additions & 0 deletions src/fs-fixture.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'node:fs/promises';
import type { CopyOptions } from 'node:fs';
import path from 'node:path';

if (typeof Symbol.asyncDispose !== 'symbol') {
Expand Down Expand Up @@ -50,6 +51,21 @@ export class FsFixture {
});
}

/**
Copy a path into the fixture directory.
*/
cp(
sourcePath: string,
destinationSubpath: string,
options?: CopyOptions,
) {
return fs.cp(

Check warning on line 62 in src/fs-fixture.ts

View workflow job for this annotation

GitHub Actions / Release

The 'fs/promises.cp' is still an experimental feature and is not supported until Node.js 22.3.0. The configured version range is '>=20.12.2'
sourcePath,
this.getPath(destinationSubpath),
options,
);
}

/**
Create a file in the fixture directory.
*/
Expand Down
11 changes: 7 additions & 4 deletions tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ describe('fs-fixture', ({ test }) => {

expect<FsFixture>(fixture);

const filePathA = path.join(fixture.path, 'directory/a');
const filePathB = path.join(fixture.path, 'directory/b');
const filePathA = fixture.getPath('directory/a');
const filePathB = fixture.getPath('directory/b');

// exists
expect(await fixture.exists('directory/a')).toBe(true);
Expand All @@ -77,6 +77,9 @@ describe('fs-fixture', ({ test }) => {
}));
expect(await fixture.readFile('directory/d', 'utf8')).toBe('a');

await fixture.cp(filePathA, 'directory/a-copy');
expect(await fixture.readFile('directory/a-copy', 'utf8')).toBe('a');

// rm file
await fixture.rm('directory/a');

Expand All @@ -95,8 +98,8 @@ describe('fs-fixture', ({ test }) => {

expect<FsFixture>(fixture);

const filePathA = path.join(fixture.path, 'a');
const filePathB = path.join(fixture.path, 'directory/b');
const filePathA = fixture.getPath('a');
const filePathB = fixture.getPath('directory/b');

// exists
expect(await fixture.exists('a')).toBe(true);
Expand Down

0 comments on commit e3bf3e5

Please sign in to comment.