• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23const common = require('../common');
24const fixtures = require('../common/fixtures');
25if (!common.canCreateSymLink())
26  common.skip('insufficient privileges');
27
28const assert = require('assert');
29const path = require('path');
30const fs = require('fs');
31
32let linkTime;
33let fileTime;
34
35const tmpdir = require('../common/tmpdir');
36tmpdir.refresh();
37
38// Test creating and reading symbolic link
39const linkData = fixtures.path('/cycles/root.js');
40const linkPath = path.join(tmpdir.path, 'symlink1.js');
41
42fs.symlink(linkData, linkPath, common.mustSucceed(() => {
43  fs.lstat(linkPath, common.mustSucceed((stats) => {
44    linkTime = stats.mtime.getTime();
45  }));
46
47  fs.stat(linkPath, common.mustSucceed((stats) => {
48    fileTime = stats.mtime.getTime();
49  }));
50
51  fs.readlink(linkPath, common.mustSucceed((destination) => {
52    assert.strictEqual(destination, linkData);
53  }));
54}));
55
56// Test invalid symlink
57{
58  const linkData = fixtures.path('/not/exists/file');
59  const linkPath = path.join(tmpdir.path, 'symlink2.js');
60
61  fs.symlink(linkData, linkPath, common.mustSucceed(() => {
62    assert(!fs.existsSync(linkPath));
63  }));
64}
65
66[false, 1, {}, [], null, undefined].forEach((input) => {
67  const errObj = {
68    code: 'ERR_INVALID_ARG_TYPE',
69    name: 'TypeError',
70    message: /target|path/
71  };
72  assert.throws(() => fs.symlink(input, '', common.mustNotCall()), errObj);
73  assert.throws(() => fs.symlinkSync(input, ''), errObj);
74
75  assert.throws(() => fs.symlink('', input, common.mustNotCall()), errObj);
76  assert.throws(() => fs.symlinkSync('', input), errObj);
77});
78
79const errObj = {
80  code: 'ERR_FS_INVALID_SYMLINK_TYPE',
81  name: 'Error',
82  message:
83    'Symlink type must be one of "dir", "file", or "junction". Received "��"'
84};
85assert.throws(() => fs.symlink('', '', '��', common.mustNotCall()), errObj);
86assert.throws(() => fs.symlinkSync('', '', '��'), errObj);
87
88process.on('exit', () => {
89  assert.notStrictEqual(linkTime, fileTime);
90});
91