1 |
3a515b92
|
cagy
|
'use strict'
|
2 |
|
|
var fs = require('graceful-fs')
|
3 |
|
|
var path = require('path')
|
4 |
|
|
var test = require('tap').test
|
5 |
|
|
var rimraf = require('rimraf')
|
6 |
|
|
var writeStream = require('../index.js')
|
7 |
|
|
|
8 |
|
|
var target = path.resolve(__dirname, 'test-rename-eperm1')
|
9 |
|
|
var target2 = path.resolve(__dirname, 'test-rename-eperm2')
|
10 |
|
|
var target3 = path.resolve(__dirname, 'test-rename-eperm3')
|
11 |
|
|
|
12 |
|
|
test('rename eperm none existing file', function (t) {
|
13 |
|
|
t.plan(2)
|
14 |
|
|
|
15 |
|
|
var _rename = fs.rename
|
16 |
|
|
fs.existsSync = function (src) {
|
17 |
|
|
return true
|
18 |
|
|
}
|
19 |
|
|
fs.rename = function (src, dest, cb) {
|
20 |
|
|
// simulate a failure during rename where the file
|
21 |
|
|
// is renamed successfully but the process encounters
|
22 |
|
|
// an EPERM error and the target file does not exist
|
23 |
|
|
_rename(src, dest, function (e) {
|
24 |
|
|
var err = new Error('TEST BREAK')
|
25 |
|
|
err.syscall = 'rename'
|
26 |
|
|
err.code = 'EPERM'
|
27 |
|
|
cb(err)
|
28 |
|
|
})
|
29 |
|
|
}
|
30 |
|
|
|
31 |
|
|
var stream = writeStream(target, { isWin: true })
|
32 |
|
|
var hadError = false
|
33 |
|
|
var calledFinish = false
|
34 |
|
|
stream.on('error', function (er) {
|
35 |
|
|
hadError = true
|
36 |
|
|
console.log('#', er)
|
37 |
|
|
})
|
38 |
|
|
stream.on('finish', function () {
|
39 |
|
|
calledFinish = true
|
40 |
|
|
})
|
41 |
|
|
stream.on('close', function () {
|
42 |
|
|
t.is(hadError, true, 'error was caught')
|
43 |
|
|
t.is(calledFinish, false, 'finish was called before close')
|
44 |
|
|
})
|
45 |
|
|
stream.end()
|
46 |
|
|
})
|
47 |
|
|
|
48 |
|
|
// test existing file with diff. content
|
49 |
|
|
test('rename eperm existing file different content', function (t) {
|
50 |
|
|
t.plan(2)
|
51 |
|
|
|
52 |
|
|
var _rename = fs.rename
|
53 |
|
|
fs.existsSync = function (src) {
|
54 |
|
|
return true
|
55 |
|
|
}
|
56 |
|
|
fs.rename = function (src, dest, cb) {
|
57 |
|
|
// simulate a failure during rename where the file
|
58 |
|
|
// is renamed successfully but the process encounters
|
59 |
|
|
// an EPERM error and the target file that has another content than the
|
60 |
|
|
// destination
|
61 |
|
|
_rename(src, dest, function (e) {
|
62 |
|
|
fs.writeFile(src, 'dest', function (writeErr) {
|
63 |
|
|
if (writeErr) {
|
64 |
|
|
return console.log('WRITEERR: ' + writeErr)
|
65 |
|
|
}
|
66 |
|
|
|
67 |
|
|
fs.writeFile(target2, 'target', function (writeErr) {
|
68 |
|
|
if (writeErr) {
|
69 |
|
|
return console.log('WRITEERR: ' + writeErr)
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
var err = new Error('TEST BREAK')
|
73 |
|
|
err.syscall = 'rename'
|
74 |
|
|
err.code = 'EPERM'
|
75 |
|
|
cb(err)
|
76 |
|
|
})
|
77 |
|
|
})
|
78 |
|
|
})
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
var stream = writeStream(target2, { isWin: true })
|
82 |
|
|
var hadError = false
|
83 |
|
|
var calledFinish = false
|
84 |
|
|
stream.on('error', function (er) {
|
85 |
|
|
hadError = true
|
86 |
|
|
console.log('#', er)
|
87 |
|
|
})
|
88 |
|
|
stream.on('finish', function () {
|
89 |
|
|
calledFinish = true
|
90 |
|
|
})
|
91 |
|
|
stream.on('close', function () {
|
92 |
|
|
t.is(hadError, true, 'error was caught')
|
93 |
|
|
t.is(calledFinish, false, 'finish was called before close')
|
94 |
|
|
})
|
95 |
|
|
stream.end()
|
96 |
|
|
})
|
97 |
|
|
|
98 |
|
|
// test existing file with the same content
|
99 |
|
|
// test existing file with diff. content
|
100 |
|
|
test('rename eperm existing file different content', function (t) {
|
101 |
|
|
t.plan(2)
|
102 |
|
|
|
103 |
|
|
var _rename = fs.rename
|
104 |
|
|
fs.existsSync = function (src) {
|
105 |
|
|
return true
|
106 |
|
|
}
|
107 |
|
|
fs.rename = function (src, dest, cb) {
|
108 |
|
|
// simulate a failure during rename where the file
|
109 |
|
|
// is renamed successfully but the process encounters
|
110 |
|
|
// an EPERM error and the target file that has the same content than the
|
111 |
|
|
// destination
|
112 |
|
|
_rename(src, dest, function (e) {
|
113 |
|
|
fs.writeFile(src, 'target2', function (writeErr) {
|
114 |
|
|
if (writeErr) {
|
115 |
|
|
return console.log('WRITEERR: ' + writeErr)
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
fs.writeFile(target3, 'target2', function (writeErr) {
|
119 |
|
|
if (writeErr) {
|
120 |
|
|
return console.log('WRITEERR: ' + writeErr)
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
var err = new Error('TEST BREAK')
|
124 |
|
|
err.syscall = 'rename'
|
125 |
|
|
err.code = 'EPERM'
|
126 |
|
|
cb(err)
|
127 |
|
|
})
|
128 |
|
|
})
|
129 |
|
|
})
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
var stream = writeStream(target3, { isWin: true })
|
133 |
|
|
var hadError = false
|
134 |
|
|
var calledFinish = false
|
135 |
|
|
stream.on('error', function (er) {
|
136 |
|
|
hadError = true
|
137 |
|
|
console.log('#', er)
|
138 |
|
|
})
|
139 |
|
|
stream.on('finish', function () {
|
140 |
|
|
calledFinish = true
|
141 |
|
|
})
|
142 |
|
|
stream.on('close', function () {
|
143 |
|
|
t.is(hadError, false, 'error was caught')
|
144 |
|
|
t.is(calledFinish, true, 'finish was called before close')
|
145 |
|
|
})
|
146 |
|
|
stream.end()
|
147 |
|
|
})
|
148 |
|
|
|
149 |
|
|
test('cleanup', function (t) {
|
150 |
|
|
rimraf.sync(target)
|
151 |
|
|
rimraf.sync(target2)
|
152 |
|
|
rimraf.sync(target3)
|
153 |
|
|
t.end()
|
154 |
|
|
})
|