2015-10-16 21:07:21 -07:00
|
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const fs = require('fs');
|
|
|
|
|
2017-12-24 22:38:11 -08:00
|
|
|
const tmpdir = require('../common/tmpdir');
|
|
|
|
tmpdir.refresh();
|
2015-10-16 21:07:21 -07:00
|
|
|
|
2019-03-07 01:03:53 +01:00
|
|
|
// Test creating and reading hard link
|
2023-08-15 22:45:24 +09:00
|
|
|
const srcPath = tmpdir.resolve('hardlink-target.txt');
|
|
|
|
const dstPath = tmpdir.resolve('link1.js');
|
2016-01-25 11:08:30 -08:00
|
|
|
fs.writeFileSync(srcPath, 'hello world');
|
2015-10-16 21:07:21 -07:00
|
|
|
|
2017-04-27 17:27:05 -07:00
|
|
|
function callback(err) {
|
2016-12-30 10:54:01 -05:00
|
|
|
assert.ifError(err);
|
2015-10-16 21:07:21 -07:00
|
|
|
const dstContent = fs.readFileSync(dstPath, 'utf8');
|
2018-11-07 15:21:37 +00:00
|
|
|
assert.strictEqual(dstContent, 'hello world');
|
2017-04-27 17:27:05 -07:00
|
|
|
}
|
2015-10-16 21:07:21 -07:00
|
|
|
|
|
|
|
fs.link(srcPath, dstPath, common.mustCall(callback));
|
2015-11-19 22:24:19 +08:00
|
|
|
|
|
|
|
// test error outputs
|
|
|
|
|
2017-12-13 14:24:34 -08:00
|
|
|
[false, 1, [], {}, null, undefined].forEach((i) => {
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(
|
2017-12-13 14:24:34 -08:00
|
|
|
() => fs.link(i, '', common.mustNotCall()),
|
|
|
|
{
|
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-12-25 18:02:16 +01:00
|
|
|
name: 'TypeError'
|
2017-12-13 14:24:34 -08:00
|
|
|
}
|
|
|
|
);
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(
|
2017-12-13 14:24:34 -08:00
|
|
|
() => fs.link('', i, common.mustNotCall()),
|
|
|
|
{
|
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-12-25 18:02:16 +01:00
|
|
|
name: 'TypeError'
|
2017-12-13 14:24:34 -08:00
|
|
|
}
|
|
|
|
);
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(
|
2017-12-13 14:24:34 -08:00
|
|
|
() => fs.linkSync(i, ''),
|
|
|
|
{
|
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-12-25 18:02:16 +01:00
|
|
|
name: 'TypeError'
|
2017-12-13 14:24:34 -08:00
|
|
|
}
|
|
|
|
);
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(
|
2017-12-13 14:24:34 -08:00
|
|
|
() => fs.linkSync('', i),
|
|
|
|
{
|
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-12-25 18:02:16 +01:00
|
|
|
name: 'TypeError'
|
2017-12-13 14:24:34 -08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|