@@ -13,6 +13,7 @@ import { NodeHfsImpl } from "../src/node-hfs.js";
1313import assert from "node:assert" ;
1414import fsp from "node:fs/promises" ;
1515import { fileURLToPath } from "node:url" ;
16+ import os from "node:os" ;
1617import path from "node:path" ;
1718import { HfsImplTester } from "@humanfs/test" ;
1819
@@ -256,4 +257,87 @@ describe("NodeHfsImpl Customizations", () => {
256257 ) ;
257258 } ) ;
258259 } ) ;
260+
261+ describe ( "copy()" , ( ) => {
262+ it ( "should recreate a symlink rather than copy the target file contents" , async ( ) => {
263+ const tmpDir = await fsp . mkdtemp (
264+ path . join ( os . tmpdir ( ) , "humanfs-copy-symlink-" ) ,
265+ ) ;
266+ try {
267+ const secret = path . join ( tmpDir , "secret.txt" ) ;
268+ const link = path . join ( tmpDir , "link.txt" ) ;
269+ const dest = path . join ( tmpDir , "dest.txt" ) ;
270+
271+ await fsp . writeFile ( secret , "TOPSECRET" ) ;
272+
273+ try {
274+ await fsp . symlink ( secret , link ) ;
275+ } catch ( err ) {
276+ if ( err . code === "EPERM" ) {
277+ return ; // symlinks require elevated privileges on this OS; skip
278+ }
279+ throw err ;
280+ }
281+
282+ const impl = new NodeHfsImpl ( { fsp } ) ;
283+ await impl . copy ( link , dest ) ;
284+
285+ // destination must be a symlink, not a plain file with secret contents
286+ const destStat = await fsp . lstat ( dest ) ;
287+ assert . ok (
288+ destStat . isSymbolicLink ( ) ,
289+ "destination should be a symlink" ,
290+ ) ;
291+
292+ // the symlink target must point back to the original secret path
293+ const linkTarget = await fsp . readlink ( dest ) ;
294+ assert . strictEqual ( linkTarget , secret ) ;
295+ } finally {
296+ await fsp . rm ( tmpDir , { recursive : true } ) ;
297+ }
298+ } ) ;
299+ } ) ;
300+
301+ describe ( "copyAll()" , ( ) => {
302+ it ( "should recreate symlinks rather than copy target file contents" , async ( ) => {
303+ const tmpDir = await fsp . mkdtemp (
304+ path . join ( os . tmpdir ( ) , "humanfs-copyall-symlink-" ) ,
305+ ) ;
306+ try {
307+ const secret = path . join ( tmpDir , "secret.txt" ) ;
308+ const src = path . join ( tmpDir , "src" ) ;
309+ const dst = path . join ( tmpDir , "dst" ) ;
310+
311+ await fsp . mkdir ( src ) ;
312+ await fsp . writeFile ( secret , "TOPSECRET" ) ;
313+
314+ try {
315+ await fsp . symlink ( secret , path . join ( src , "link.txt" ) ) ;
316+ } catch ( err ) {
317+ if ( err . code === "EPERM" ) {
318+ return ; // symlinks require elevated privileges on this OS; skip
319+ }
320+ throw err ;
321+ }
322+
323+ const impl = new NodeHfsImpl ( { fsp } ) ;
324+ await impl . copyAll ( src , dst ) ;
325+
326+ const copiedLink = path . join ( dst , "link.txt" ) ;
327+ const copiedStat = await fsp . lstat ( copiedLink ) ;
328+
329+ // the copied entry must be a symlink, not a plain file
330+ assert . ok (
331+ copiedStat . isSymbolicLink ( ) ,
332+ "copied entry should be a symlink" ,
333+ ) ;
334+
335+ // the symlink must point to the original target, not contain its data
336+ const linkTarget = await fsp . readlink ( copiedLink ) ;
337+ assert . strictEqual ( linkTarget , secret ) ;
338+ } finally {
339+ await fsp . rm ( tmpDir , { recursive : true } ) ;
340+ }
341+ } ) ;
342+ } ) ;
259343} ) ;
0 commit comments