| 1 |
'use strict' |
| 2 |
|
| 3 |
const CWD = process.cwd() |
| 4 |
|
| 5 |
const fs = require('graceful-fs') |
| 6 |
const os = require('os') |
| 7 |
const fse = require(CWD) |
| 8 |
const path = require('path') |
| 9 |
const assert = require('assert') |
| 10 |
const _symlinkPaths = require('../symlink-paths') |
| 11 |
const symlinkPaths = _symlinkPaths.symlinkPaths |
| 12 |
const symlinkPathsSync = _symlinkPaths.symlinkPathsSync |
| 13 |
const TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ensure-symlink') |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
describe('symlink-type', () => { |
| 18 |
before(() => { |
| 19 |
fse.emptyDirSync(TEST_DIR) |
| 20 |
process.chdir(TEST_DIR) |
| 21 |
}) |
| 22 |
|
| 23 |
beforeEach(() => { |
| 24 |
fs.writeFileSync('./foo.txt', 'foo\n') |
| 25 |
fse.mkdirsSync('./empty-dir') |
| 26 |
fse.mkdirsSync('./dir-foo') |
| 27 |
fs.writeFileSync('./dir-foo/foo.txt', 'dir-foo\n') |
| 28 |
fse.mkdirsSync('./dir-bar') |
| 29 |
fs.writeFileSync('./dir-bar/bar.txt', 'dir-bar\n') |
| 30 |
fse.mkdirsSync('./real-alpha/real-beta/real-gamma') |
| 31 |
}) |
| 32 |
|
| 33 |
afterEach(done => fse.emptyDir(TEST_DIR, done)) |
| 34 |
|
| 35 |
after(() => { |
| 36 |
process.chdir(CWD) |
| 37 |
fse.removeSync(TEST_DIR) |
| 38 |
}) |
| 39 |
|
| 40 |
const tests = [ |
| 41 |
[['foo.txt', 'symlink.txt'], { toCwd: 'foo.txt', toDst: 'foo.txt' }], |
| 42 |
[['foo.txt', 'empty-dir/symlink.txt'], { toCwd: 'foo.txt', toDst: '../foo.txt' }], |
| 43 |
[['../foo.txt', 'empty-dir/symlink.txt'], { toCwd: 'foo.txt', toDst: '../foo.txt' }], |
| 44 |
[['foo.txt', 'dir-bar/symlink.txt'], { toCwd: 'foo.txt', toDst: '../foo.txt' }], |
| 45 |
[['../foo.txt', 'dir-bar/symlink.txt'], { toCwd: 'foo.txt', toDst: '../foo.txt' }], |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
[['foo.txt', 'dir-foo/symlink.txt'], { toCwd: 'dir-foo/foo.txt', toDst: 'foo.txt' }], |
| 50 |
[['foo.txt', 'real-alpha/real-beta/real-gamma/symlink.txt'], { toCwd: 'foo.txt', toDst: '../../../foo.txt' }] |
| 51 |
] |
| 52 |
|
| 53 |
|
| 54 |
tests.forEach(test => { |
| 55 |
test[0][0] = path.join(test[0][0]) |
| 56 |
test[0][1] = path.join(test[0][1]) |
| 57 |
test[1] = { |
| 58 |
toCwd: path.join(test[1].toCwd), |
| 59 |
toDst: path.join(test[1].toDst) |
| 60 |
} |
| 61 |
}) |
| 62 |
|
| 63 |
describe('symlinkPaths()', () => { |
| 64 |
tests.forEach(test => { |
| 65 |
const args = test[0].slice(0) |
| 66 |
const expectedRelativePaths = test[1] |
| 67 |
it(`should return '${JSON.stringify(expectedRelativePaths)}' when src '${args[0]}' and dst is '${args[1]}'`, done => { |
| 68 |
const callback = (err, relativePaths) => { |
| 69 |
if (err) done(err) |
| 70 |
assert.deepStrictEqual(relativePaths, expectedRelativePaths) |
| 71 |
done() |
| 72 |
} |
| 73 |
args.push(callback) |
| 74 |
return symlinkPaths(...args) |
| 75 |
}) |
| 76 |
}) |
| 77 |
}) |
| 78 |
|
| 79 |
describe('symlinkPathsSync()', () => { |
| 80 |
tests.forEach(test => { |
| 81 |
const args = test[0].slice(0) |
| 82 |
const expectedRelativePaths = test[1] |
| 83 |
it(`should return '${JSON.stringify(expectedRelativePaths)}' when src '${args[0]}' and dst is '${args[1]}'`, () => { |
| 84 |
const relativePaths = symlinkPathsSync(...args) |
| 85 |
assert.deepStrictEqual(relativePaths, expectedRelativePaths) |
| 86 |
}) |
| 87 |
}) |
| 88 |
}) |
| 89 |
}) |