Debian Codegraph @

lib/ensure/__tests__/link.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 ensureLink = fse.ensureLink
11 const ensureLinkSync = fse.ensureLinkSync
12
13 /* global afterEach, beforeEach, describe, it, after, before */
14
15 describe('fse-ensure-link', () => {
16 const TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ensure-symlink')
17
18 const tests = [
19 // [[srcpath, dstpath], fs.link expect, ensureLink expect]
20 [['./foo.txt', './link.txt'], 'file-success', 'file-success'],
21 [['./foo.txt', './dir-foo/link.txt'], 'file-success', 'file-success'],
22 [['./foo.txt', './empty-dir/link.txt'], 'file-success', 'file-success'],
23 [['./foo.txt', './real-alpha/link.txt'], 'file-success', 'file-success'],
24 [['./foo.txt', './real-alpha/real-beta/link.txt'], 'file-success', 'file-success'],
25 [['./foo.txt', './real-alpha/real-beta/real-gamma/link.txt'], 'file-success', 'file-success'],
26 [['./foo.txt', './alpha/link.txt'], 'file-error', 'file-success'],
27 [['./foo.txt', './alpha/beta/link.txt'], 'file-error', 'file-success'],
28 [['./foo.txt', './alpha/beta/gamma/link.txt'], 'file-error', 'file-success'],
29 [['./foo.txt', './link-foo.txt'], 'file-error', 'file-success'],
30 [['./dir-foo/foo.txt', './link-foo.txt'], 'file-error', 'file-error'],
31 [['./missing.txt', './link.txt'], 'file-error', 'file-error'],
32 [['./missing.txt', './missing-dir/link.txt'], 'file-error', 'file-error'],
33 [['./foo.txt', './link.txt'], 'file-success', 'file-success'],
34 [['./dir-foo/foo.txt', './link.txt'], 'file-success', 'file-success'],
35 [['./missing.txt', './link.txt'], 'file-error', 'file-error'],
36 [['../foo.txt', './link.txt'], 'file-error', 'file-error'],
37 [['../dir-foo/foo.txt', './link.txt'], 'file-error', 'file-error'],
38 // error is thrown if destination path exists
39 [['./foo.txt', './dir-foo/foo.txt'], 'file-error', 'file-error'],
40 [[path.resolve(path.join(TEST_DIR, './foo.txt')), './link.txt'], 'file-success', 'file-success'],
41 [[path.resolve(path.join(TEST_DIR, './dir-foo/foo.txt')), './link.txt'], 'file-success', 'file-success'],
42 [[path.resolve(path.join(TEST_DIR, './missing.txt')), './link.txt'], 'file-error', 'file-error'],
43 [[path.resolve(path.join(TEST_DIR, '../foo.txt')), './link.txt'], 'file-error', 'file-error'],
44 [[path.resolve(path.join(TEST_DIR, '../dir-foo/foo.txt')), './link.txt'], 'file-error', 'file-error']
45 ]
46
47 before(() => {
48 fse.emptyDirSync(TEST_DIR)
49 process.chdir(TEST_DIR)
50 })
51
52 beforeEach(() => {
53 fs.writeFileSync('./foo.txt', 'foo\n')
54 fse.mkdirsSync('empty-dir')
55 fse.mkdirsSync('dir-foo')
56 fs.writeFileSync('dir-foo/foo.txt', 'dir-foo\n')
57 fse.mkdirsSync('dir-bar')
58 fs.writeFileSync('dir-bar/bar.txt', 'dir-bar\n')
59 fse.mkdirsSync('real-alpha/real-beta/real-gamma')
60 fs.linkSync('foo.txt', 'link-foo.txt')
61 })
62
63 afterEach(done => fse.emptyDir(TEST_DIR, done))
64
65 after(() => {
66 process.chdir(CWD)
67 fse.removeSync(TEST_DIR)
68 })
69
70 function fileSuccess (args, fn) {
71 const srcpath = args[0]
72 const dstpath = args[1]
73
74 it(`should create link file using src ${srcpath} and dst ${dstpath}`, done => {
75 const callback = err => {
76 if (err) return done(err)
77
78 const srcContent = fs.readFileSync(srcpath, 'utf8')
79 const dstDir = path.dirname(dstpath)
80 const dstBasename = path.basename(dstpath)
81 const isSymlink = fs.lstatSync(dstpath).isFile()
82 const dstContent = fs.readFileSync(dstpath, 'utf8')
83 const dstDirContents = fs.readdirSync(dstDir)
84
85 assert.strictEqual(isSymlink, true)
86 assert.strictEqual(srcContent, dstContent)
87 assert(dstDirContents.indexOf(dstBasename) >= 0)
88 return done()
89 }
90 args.push(callback)
91 return fn(...args)
92 })
93 }
94
95 function fileError (args, fn) {
96 const srcpath = args[0]
97 const dstpath = args[1]
98
99 it(`should return error when creating link file using src ${srcpath} and dst ${dstpath}`, done => {
100 const dstdirExistsBefore = fs.existsSync(path.dirname(dstpath))
101 const callback = err => {
102 assert.strictEqual(err instanceof Error, true)
103 // ensure that directories aren't created if there's an error
104 const dstdirExistsAfter = fs.existsSync(path.dirname(dstpath))
105 assert.strictEqual(dstdirExistsBefore, dstdirExistsAfter)
106 return done()
107 }
108 args.push(callback)
109 return fn(...args)
110 })
111 }
112
113 function fileSuccessSync (args, fn) {
114 const srcpath = args[0]
115 const dstpath = args[1]
116
117 it(`should create link file using src ${srcpath} and dst ${dstpath}`, () => {
118 fn(...args)
119 const srcContent = fs.readFileSync(srcpath, 'utf8')
120 const dstDir = path.dirname(dstpath)
121 const dstBasename = path.basename(dstpath)
122 const isSymlink = fs.lstatSync(dstpath).isFile()
123 const dstContent = fs.readFileSync(dstpath, 'utf8')
124 const dstDirContents = fs.readdirSync(dstDir)
125 assert.strictEqual(isSymlink, true)
126 assert.strictEqual(srcContent, dstContent)
127 assert(dstDirContents.indexOf(dstBasename) >= 0)
128 })
129 }
130
131 function fileErrorSync (args, fn) {
132 const srcpath = args[0]
133 const dstpath = args[1]
134
135 it(`should throw error using src ${srcpath} and dst ${dstpath}`, () => {
136 // will fail if dstdir is created and there's an error
137 const dstdirExistsBefore = fs.existsSync(path.dirname(dstpath))
138 let err = null
139 try {
140 fn(...args)
141 } catch (e) {
142 err = e
143 }
144 assert.strictEqual(err instanceof Error, true)
145 const dstdirExistsAfter = fs.existsSync(path.dirname(dstpath))
146 assert.strictEqual(dstdirExistsBefore, dstdirExistsAfter)
147 })
148 }
149
150 describe('fs.link()', () => {
151 const fn = fs.link
152 tests.forEach(test => {
153 const args = test[0].slice(0)
154 const nativeBehavior = test[1]
155 // const newBehavior = test[2]
156 if (nativeBehavior === 'file-success') fileSuccess(args, fn)
157 if (nativeBehavior === 'file-error') fileError(args, fn)
158 })
159 })
160
161 describe('ensureLink()', () => {
162 const fn = ensureLink
163 tests.forEach(test => {
164 const args = test[0].slice(0)
165 // const nativeBehavior = test[1]
166 const newBehavior = test[2]
167 if (newBehavior === 'file-success') fileSuccess(args, fn)
168 if (newBehavior === 'file-error') fileError(args, fn)
169 })
170 })
171
172 describe('ensureLink() promise support', () => {
173 tests.filter(test => test[2] === 'file-success').forEach(test => {
174 const args = test[0].slice(0)
175 const srcpath = args[0]
176 const dstpath = args[1]
177
178 it(`should create link file using src ${srcpath} and dst ${dstpath}`, () => {
179 return ensureLink(srcpath, dstpath)
180 .then(() => {
181 const srcContent = fs.readFileSync(srcpath, 'utf8')
182 const dstDir = path.dirname(dstpath)
183 const dstBasename = path.basename(dstpath)
184 const isSymlink = fs.lstatSync(dstpath).isFile()
185 const dstContent = fs.readFileSync(dstpath, 'utf8')
186 const dstDirContents = fs.readdirSync(dstDir)
187
188 assert.strictEqual(isSymlink, true)
189 assert.strictEqual(srcContent, dstContent)
190 assert(dstDirContents.indexOf(dstBasename) >= 0)
191 })
192 })
193 })
194 })
195
196 describe('fs.linkSync()', () => {
197 const fn = fs.linkSync
198 tests.forEach(test => {
199 const args = test[0].slice(0)
200 const nativeBehavior = test[1]
201 // const newBehavior = test[2]
202 if (nativeBehavior === 'file-success') fileSuccessSync(args, fn)
203 if (nativeBehavior === 'file-error') fileErrorSync(args, fn)
204 })
205 })
206
207 describe('ensureLinkSync()', () => {
208 const fn = ensureLinkSync
209 tests.forEach(test => {
210 const args = test[0].slice(0)
211 // const nativeBehavior = test[1]
212 const newBehavior = test[2]
213 if (newBehavior === 'file-success') fileSuccessSync(args, fn)
214 if (newBehavior === 'file-error') fileErrorSync(args, fn)
215 })
216 })
217 })