Debian Codegraph @

lib/ensure/__tests__/symlink-paths.test.js

raw symbols SCIP
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 /* global afterEach, beforeEach, describe, it, after, before */
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' }], // smart && nodestyle
42 [['foo.txt', 'empty-dir/symlink.txt'], { toCwd: 'foo.txt', toDst: '../foo.txt' }], // smart
43 [['../foo.txt', 'empty-dir/symlink.txt'], { toCwd: 'foo.txt', toDst: '../foo.txt' }], // nodestyle
44 [['foo.txt', 'dir-bar/symlink.txt'], { toCwd: 'foo.txt', toDst: '../foo.txt' }], // smart
45 [['../foo.txt', 'dir-bar/symlink.txt'], { toCwd: 'foo.txt', toDst: '../foo.txt' }], // nodestyle
46 // this is to preserve node's symlink capability these arguments say create
47 // a link to 'dir-foo/foo.txt' this works because it exists this is unlike
48 // the previous example with 'empty-dir' because 'empty-dir/foo.txt' does not exist.
49 [['foo.txt', 'dir-foo/symlink.txt'], { toCwd: 'dir-foo/foo.txt', toDst: 'foo.txt' }], // nodestyle
50 [['foo.txt', 'real-alpha/real-beta/real-gamma/symlink.txt'], { toCwd: 'foo.txt', toDst: '../../../foo.txt' }]
51 ]
52
53 // formats paths to pass on multiple operating systems
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 })