1'use strict'; 2const common = require('../common'); 3 4// Test creating a symbolic link pointing to a directory. 5// Ref: https://github.com/nodejs/node/pull/23724 6// Ref: https://github.com/nodejs/node/issues/23596 7 8 9if (!common.canCreateSymLink()) 10 common.skip('insufficient privileges'); 11 12const assert = require('assert'); 13const path = require('path'); 14const fs = require('fs'); 15 16const tmpdir = require('../common/tmpdir'); 17tmpdir.refresh(); 18 19const linkTargets = [ 20 'relative-target', 21 path.join(tmpdir.path, 'absolute-target') 22]; 23const linkPaths = [ 24 path.relative(process.cwd(), path.join(tmpdir.path, 'relative-path')), 25 path.join(tmpdir.path, 'absolute-path') 26]; 27 28function testSync(target, path) { 29 fs.symlinkSync(target, path); 30 fs.readdirSync(path); 31} 32 33function testAsync(target, path) { 34 fs.symlink(target, path, common.mustCall((err) => { 35 assert.ifError(err); 36 fs.readdirSync(path); 37 })); 38} 39 40for (const linkTarget of linkTargets) { 41 fs.mkdirSync(path.resolve(tmpdir.path, linkTarget)); 42 for (const linkPath of linkPaths) { 43 testSync(linkTarget, `${linkPath}-${path.basename(linkTarget)}-sync`); 44 testAsync(linkTarget, `${linkPath}-${path.basename(linkTarget)}-async`); 45 } 46} 47 48// Test invalid symlink 49{ 50 function testSync(target, path) { 51 fs.symlinkSync(target, path); 52 assert(!fs.existsSync(path)); 53 } 54 55 function testAsync(target, path) { 56 fs.symlink(target, path, common.mustCall((err) => { 57 assert.ifError(err); 58 assert(!fs.existsSync(path)); 59 })); 60 } 61 62 for (const linkTarget of linkTargets.map((p) => p + '-broken')) { 63 for (const linkPath of linkPaths) { 64 testSync(linkTarget, `${linkPath}-${path.basename(linkTarget)}-sync`); 65 testAsync(linkTarget, `${linkPath}-${path.basename(linkTarget)}-async`); 66 } 67 } 68} 69