1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const path = require('path'); 5const fs = require('fs'); 6 7const tmpdir = require('../common/tmpdir'); 8tmpdir.refresh(); 9 10// Test creating and reading hard link 11const srcPath = path.join(tmpdir.path, 'hardlink-target.txt'); 12const dstPath = path.join(tmpdir.path, 'link1.js'); 13fs.writeFileSync(srcPath, 'hello world'); 14 15function callback(err) { 16 assert.ifError(err); 17 const dstContent = fs.readFileSync(dstPath, 'utf8'); 18 assert.strictEqual(dstContent, 'hello world'); 19} 20 21fs.link(srcPath, dstPath, common.mustCall(callback)); 22 23// test error outputs 24 25[false, 1, [], {}, null, undefined].forEach((i) => { 26 assert.throws( 27 () => fs.link(i, '', common.mustNotCall()), 28 { 29 code: 'ERR_INVALID_ARG_TYPE', 30 name: 'TypeError' 31 } 32 ); 33 assert.throws( 34 () => fs.link('', i, common.mustNotCall()), 35 { 36 code: 'ERR_INVALID_ARG_TYPE', 37 name: 'TypeError' 38 } 39 ); 40 assert.throws( 41 () => fs.linkSync(i, ''), 42 { 43 code: 'ERR_INVALID_ARG_TYPE', 44 name: 'TypeError' 45 } 46 ); 47 assert.throws( 48 () => fs.linkSync('', i), 49 { 50 code: 'ERR_INVALID_ARG_TYPE', 51 name: 'TypeError' 52 } 53 ); 54}); 55