Debian Codegraph @

lib/ensure/__tests__/symlink.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 symlinkPathsSync = _symlinkPaths.symlinkPathsSync
12 const ensureSymlink = fse.ensureSymlink
13 const ensureSymlinkSync = fse.ensureSymlinkSync
14
15 /* global afterEach, beforeEach, describe, it, after, before */
16
17 let TEST_DIR
18
19 describe('fse-ensure-symlink', () => {
20 TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ensure-symlink')
21
22 const tests = [
23 // [[srcpath, dstpath], fs.symlink expect, fse.ensureSymlink expect]
24 [['./foo.txt', './symlink.txt'], 'file-success', 'file-success'],
25 [['../foo.txt', './empty-dir/symlink.txt'], 'file-success', 'file-success'],
26 [['../foo.txt', './empty-dir/symlink.txt'], 'file-success', 'file-success'],
27 [['./foo.txt', './dir-foo/symlink.txt'], 'file-success', 'file-success'],
28 [['./foo.txt', './empty-dir/symlink.txt'], 'file-broken', 'file-success'],
29 [['./foo.txt', './real-alpha/symlink.txt'], 'file-broken', 'file-success'],
30 [['./foo.txt', './real-alpha/real-beta/symlink.txt'], 'file-broken', 'file-success'],
31 [['./foo.txt', './real-alpha/real-beta/real-gamma/symlink.txt'], 'file-broken', 'file-success'],
32 [['./foo.txt', './alpha/symlink.txt'], 'file-error', 'file-success'],
33 [['./foo.txt', './alpha/beta/symlink.txt'], 'file-error', 'file-success'],
34 [['./foo.txt', './alpha/beta/gamma/symlink.txt'], 'file-error', 'file-success'],
35 [['./foo.txt', './real-symlink.txt'], 'file-error', 'file-success'],
36 [['./dir-foo/foo.txt', './real-symlink.txt'], 'file-error', 'file-error'],
37 [['./missing.txt', './symlink.txt'], 'file-broken', 'file-error'],
38 [['./missing.txt', './missing-dir/symlink.txt'], 'file-error', 'file-error'],
39 // error is thrown if destination path exists
40 [['./foo.txt', './dir-foo/foo.txt'], 'file-error', 'file-error'],
41 [['./dir-foo', './symlink-dir-foo'], 'dir-success', 'dir-success'],
42 [['../dir-bar', './dir-foo/symlink-dir-bar'], 'dir-success', 'dir-success'],
43 [['./dir-bar', './dir-foo/symlink-dir-bar'], 'dir-broken', 'dir-success'],
44 [['./dir-bar', './empty-dir/symlink-dir-bar'], 'dir-broken', 'dir-success'],
45 [['./dir-bar', './real-alpha/symlink-dir-bar'], 'dir-broken', 'dir-success'],
46 [['./dir-bar', './real-alpha/real-beta/symlink-dir-bar'], 'dir-broken', 'dir-success'],
47 [['./dir-bar', './real-alpha/real-beta/real-gamma/symlink-dir-bar'], 'dir-broken', 'dir-success'],
48 [['./dir-foo', './alpha/dir-foo'], 'dir-error', 'dir-success'],
49 [['./dir-foo', './alpha/beta/dir-foo'], 'dir-error', 'dir-success'],
50 [['./dir-foo', './alpha/beta/gamma/dir-foo'], 'dir-error', 'dir-success'],
51 [['./dir-foo', './real-symlink-dir-foo'], 'dir-error', 'dir-success'],
52 [['./dir-bar', './real-symlink-dir-foo'], 'dir-error', 'dir-error'],
53 [['./missing', './dir-foo/symlink-dir-missing'], 'dir-broken', 'dir-error'],
54 // error is thrown if destination path exists
55 [['./dir-foo', './real-alpha/real-beta'], 'dir-error', 'dir-error'],
56 [[path.resolve(path.join(TEST_DIR, './foo.txt')), './symlink.txt'], 'file-success', 'file-success'],
57 [[path.resolve(path.join(TEST_DIR, './dir-foo/foo.txt')), './symlink.txt'], 'file-success', 'file-success'],
58 [[path.resolve(path.join(TEST_DIR, './missing.txt')), './symlink.txt'], 'file-broken', 'file-error'],
59 [[path.resolve(path.join(TEST_DIR, '../foo.txt')), './symlink.txt'], 'file-broken', 'file-error'],
60 [[path.resolve(path.join(TEST_DIR, '../dir-foo/foo.txt')), './symlink.txt'], 'file-broken', 'file-error']
61 ]
62
63 before(() => {
64 fse.emptyDirSync(TEST_DIR)
65 process.chdir(TEST_DIR)
66 })
67
68 beforeEach(() => {
69 fs.writeFileSync('./foo.txt', 'foo\n')
70 fse.mkdirsSync('empty-dir')
71 fse.mkdirsSync('dir-foo')
72 fs.writeFileSync('dir-foo/foo.txt', 'dir-foo\n')
73 fse.mkdirsSync('dir-bar')
74 fs.writeFileSync('dir-bar/bar.txt', 'dir-bar\n')
75 fse.mkdirsSync('real-alpha/real-beta/real-gamma')
76 fs.symlinkSync('foo.txt', 'real-symlink.txt')
77 fs.symlinkSync('dir-foo', 'real-symlink-dir-foo')
78 })
79
80 afterEach(done => fse.emptyDir(TEST_DIR, done))
81
82 after(() => {
83 process.chdir(CWD)
84 fse.removeSync(TEST_DIR)
85 })
86
87 function fileSuccess (args, fn) {
88 const srcpath = args[0]
89 const dstpath = args[1]
90 it(`should create symlink file using src ${srcpath} and dst ${dstpath}`, done => {
91 const callback = err => {
92 if (err) return done(err)
93 const relative = symlinkPathsSync(srcpath, dstpath)
94 const srcContent = fs.readFileSync(relative.toCwd, 'utf8')
95 const dstDir = path.dirname(dstpath)
96 const dstBasename = path.basename(dstpath)
97 const isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
98 const dstContent = fs.readFileSync(dstpath, 'utf8')
99 const dstDirContents = fs.readdirSync(dstDir)
100 assert.strictEqual(isSymlink, true)
101 assert.strictEqual(srcContent, dstContent)
102 assert(dstDirContents.indexOf(dstBasename) >= 0)
103 return done()
104 }
105 return fn(...args, callback)
106 })
107 }
108
109 function fileBroken (args, fn) {
110 const srcpath = args[0]
111 const dstpath = args[1]
112 it(`should create broken symlink file using src ${srcpath} and dst ${dstpath}`, done => {
113 const callback = err => {
114 if (err) return done(err)
115 const dstDir = path.dirname(dstpath)
116 const dstBasename = path.basename(dstpath)
117 const isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
118 const dstDirContents = fs.readdirSync(dstDir)
119 assert.strictEqual(isSymlink, true)
120 assert(dstDirContents.indexOf(dstBasename) >= 0)
121 assert.throws(() => fs.readFileSync(dstpath, 'utf8'), Error)
122 return done()
123 }
124 return fn(...args, callback)
125 })
126 }
127
128 function fileError (args, fn) {
129 const srcpath = args[0]
130 const dstpath = args[1]
131 it(`should return error when creating symlink file using src ${srcpath} and dst ${dstpath}`, done => {
132 const dstdirExistsBefore = fs.existsSync(path.dirname(dstpath))
133 const callback = err => {
134 assert.strictEqual(err instanceof Error, true)
135 // ensure that directories aren't created if there's an error
136 const dstdirExistsAfter = fs.existsSync(path.dirname(dstpath))
137 assert.strictEqual(dstdirExistsBefore, dstdirExistsAfter)
138 return done()
139 }
140 return fn(...args, callback)
141 })
142 }
143
144 function dirSuccess (args, fn) {
145 const srcpath = args[0]
146 const dstpath = args[1]
147 it(`should create symlink dir using src ${srcpath} and dst ${dstpath}`, done => {
148 const callback = err => {
149 if (err) return done(err)
150 const relative = symlinkPathsSync(srcpath, dstpath)
151 const srcContents = fs.readdirSync(relative.toCwd)
152 const dstDir = path.dirname(dstpath)
153 const dstBasename = path.basename(dstpath)
154 const isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
155 const dstContents = fs.readdirSync(dstpath)
156 const dstDirContents = fs.readdirSync(dstDir)
157 assert.strictEqual(isSymlink, true)
158 assert.deepStrictEqual(srcContents, dstContents)
159 assert(dstDirContents.indexOf(dstBasename) >= 0)
160 return done()
161 }
162 return fn(...args, callback)
163 })
164 }
165
166 function dirBroken (args, fn) {
167 const srcpath = args[0]
168 const dstpath = args[1]
169 it(`should create broken symlink dir using src ${srcpath} and dst ${dstpath}`, done => {
170 const callback = err => {
171 if (err) return done(err)
172 const dstDir = path.dirname(dstpath)
173 const dstBasename = path.basename(dstpath)
174 const isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
175 const dstDirContents = fs.readdirSync(dstDir)
176 assert.strictEqual(isSymlink, true)
177 assert(dstDirContents.indexOf(dstBasename) >= 0)
178 assert.throws(() => fs.readdirSync(dstpath), Error)
179 return done()
180 }
181 return fn(...args, callback)
182 })
183 }
184
185 function dirError (args, fn) {
186 const srcpath = args[0]
187 const dstpath = args[1]
188 it(`should return error when creating symlink dir using src ${srcpath} and dst ${dstpath}`, done => {
189 const dstdirExistsBefore = fs.existsSync(path.dirname(dstpath))
190 const callback = err => {
191 assert.strictEqual(err instanceof Error, true)
192 // ensure that directories aren't created if there's an error
193 const dstdirExistsAfter = fs.existsSync(path.dirname(dstpath))
194 assert.strictEqual(dstdirExistsBefore, dstdirExistsAfter)
195 return done()
196 }
197 return fn(...args, callback)
198 })
199 }
200
201 function fileSuccessSync (args, fn) {
202 const srcpath = args[0]
203 const dstpath = args[1]
204 it(`should create symlink file using src ${srcpath} and dst ${dstpath}`, () => {
205 fn(...args)
206 const relative = symlinkPathsSync(srcpath, dstpath)
207 const srcContent = fs.readFileSync(relative.toCwd, 'utf8')
208 const dstDir = path.dirname(dstpath)
209 const dstBasename = path.basename(dstpath)
210 const isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
211 const dstContent = fs.readFileSync(dstpath, 'utf8')
212 const dstDirContents = fs.readdirSync(dstDir)
213 assert.strictEqual(isSymlink, true)
214 assert.strictEqual(srcContent, dstContent)
215 assert(dstDirContents.indexOf(dstBasename) >= 0)
216 })
217 }
218
219 function fileBrokenSync (args, fn) {
220 const srcpath = args[0]
221 const dstpath = args[1]
222 it(`should create broken symlink file using src ${srcpath} and dst ${dstpath}`, () => {
223 fn(...args)
224 const dstDir = path.dirname(dstpath)
225 const dstBasename = path.basename(dstpath)
226 const isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
227 const dstDirContents = fs.readdirSync(dstDir)
228 assert.strictEqual(isSymlink, true)
229 assert(dstDirContents.indexOf(dstBasename) >= 0)
230 assert.throws(() => fs.readFileSync(dstpath, 'utf8'), Error)
231 })
232 }
233
234 function fileErrorSync (args, fn) {
235 const srcpath = args[0]
236 const dstpath = args[1]
237 it(`should throw error using src ${srcpath} and dst ${dstpath}`, () => {
238 const dstdirExistsBefore = fs.existsSync(path.dirname(dstpath))
239 let err = null
240 try {
241 fn(...args)
242 } catch (e) {
243 err = e
244 }
245 assert.strictEqual(err instanceof Error, true)
246 const dstdirExistsAfter = fs.existsSync(path.dirname(dstpath))
247 assert.strictEqual(dstdirExistsBefore, dstdirExistsAfter)
248 })
249 }
250
251 function dirSuccessSync (args, fn) {
252 const srcpath = args[0]
253 const dstpath = args[1]
254 it(`should create symlink dir using src ${srcpath} and dst ${dstpath}`, () => {
255 fn(...args)
256 const relative = symlinkPathsSync(srcpath, dstpath)
257 const srcContents = fs.readdirSync(relative.toCwd)
258 const dstDir = path.dirname(dstpath)
259 const dstBasename = path.basename(dstpath)
260 const isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
261 const dstContents = fs.readdirSync(dstpath)
262 const dstDirContents = fs.readdirSync(dstDir)
263 assert.strictEqual(isSymlink, true)
264 assert.deepStrictEqual(srcContents, dstContents)
265 assert(dstDirContents.indexOf(dstBasename) >= 0)
266 })
267 }
268
269 function dirBrokenSync (args, fn) {
270 const srcpath = args[0]
271 const dstpath = args[1]
272 it(`should create broken symlink dir using src ${srcpath} and dst ${dstpath}`, () => {
273 fn(...args)
274 const dstDir = path.dirname(dstpath)
275 const dstBasename = path.basename(dstpath)
276 const isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
277 const dstDirContents = fs.readdirSync(dstDir)
278 assert.strictEqual(isSymlink, true)
279 assert(dstDirContents.indexOf(dstBasename) >= 0)
280 assert.throws(() => fs.readdirSync(dstpath), Error)
281 })
282 }
283
284 function dirErrorSync (args, fn) {
285 const srcpath = args[0]
286 const dstpath = args[1]
287 it(`should throw error when creating symlink dir using src ${srcpath} and dst ${dstpath}`, () => {
288 const dstdirExistsBefore = fs.existsSync(path.dirname(dstpath))
289 let err = null
290 try {
291 fn(...args)
292 } catch (e) {
293 err = e
294 }
295 assert.strictEqual(err instanceof Error, true)
296 const dstdirExistsAfter = fs.existsSync(path.dirname(dstpath))
297 assert.strictEqual(dstdirExistsBefore, dstdirExistsAfter)
298 })
299 }
300
301 describe('ensureSymlink()', () => {
302 const fn = ensureSymlink
303 tests.forEach(test => {
304 const args = test[0]
305 // const nativeBehavior = test[1]
306 const newBehavior = test[2]
307 if (newBehavior === 'file-success') fileSuccess(args, fn)
308 if (newBehavior === 'file-broken') fileBroken(args, fn)
309 if (newBehavior === 'file-error') fileError(args, fn)
310 if (newBehavior === 'dir-success') dirSuccess(args, fn)
311 if (newBehavior === 'dir-broken') dirBroken(args, fn)
312 if (newBehavior === 'dir-error') dirError(args, fn)
313 })
314 })
315
316 describe('ensureSymlink() promise support', () => {
317 tests.filter(test => test[2] === 'file-success').forEach(test => {
318 const args = test[0]
319 const srcpath = args[0]
320 const dstpath = args[1]
321 it(`should create symlink file using src ${srcpath} and dst ${dstpath}`, () => {
322 return ensureSymlink(srcpath, dstpath)
323 .then(() => {
324 const relative = symlinkPathsSync(srcpath, dstpath)
325 const srcContent = fs.readFileSync(relative.toCwd, 'utf8')
326 const dstDir = path.dirname(dstpath)
327 const dstBasename = path.basename(dstpath)
328 const isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
329 const dstContent = fs.readFileSync(dstpath, 'utf8')
330 const dstDirContents = fs.readdirSync(dstDir)
331 assert.strictEqual(isSymlink, true)
332 assert.strictEqual(srcContent, dstContent)
333 assert(dstDirContents.indexOf(dstBasename) >= 0)
334 })
335 })
336 })
337 })
338
339 describe('ensureSymlinkSync()', () => {
340 const fn = ensureSymlinkSync
341 tests.forEach(test => {
342 const args = test[0]
343 // const nativeBehavior = test[1]
344 const newBehavior = test[2]
345 if (newBehavior === 'file-success') fileSuccessSync(args, fn)
346 if (newBehavior === 'file-broken') fileBrokenSync(args, fn)
347 if (newBehavior === 'file-error') fileErrorSync(args, fn)
348 if (newBehavior === 'dir-success') dirSuccessSync(args, fn)
349 if (newBehavior === 'dir-broken') dirBrokenSync(args, fn)
350 if (newBehavior === 'dir-error') dirErrorSync(args, fn)
351 })
352 })
353 })