• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.mustSucceed(() => {
35    fs.readdirSync(path);
36  }));
37}
38
39for (const linkTarget of linkTargets) {
40  fs.mkdirSync(path.resolve(tmpdir.path, linkTarget));
41  for (const linkPath of linkPaths) {
42    testSync(linkTarget, `${linkPath}-${path.basename(linkTarget)}-sync`);
43    testAsync(linkTarget, `${linkPath}-${path.basename(linkTarget)}-async`);
44  }
45}
46
47// Test invalid symlink
48{
49  function testSync(target, path) {
50    fs.symlinkSync(target, path);
51    assert(!fs.existsSync(path));
52  }
53
54  function testAsync(target, path) {
55    fs.symlink(target, path, common.mustSucceed(() => {
56      assert(!fs.existsSync(path));
57    }));
58  }
59
60  for (const linkTarget of linkTargets.map((p) => p + '-broken')) {
61    for (const linkPath of linkPaths) {
62      testSync(linkTarget, `${linkPath}-${path.basename(linkTarget)}-sync`);
63      testAsync(linkTarget, `${linkPath}-${path.basename(linkTarget)}-async`);
64    }
65  }
66}
67