summaryrefslogtreecommitdiffstats
path: root/node_modules/fs-write-stream-atomic/test
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/fs-write-stream-atomic/test')
-rw-r--r--node_modules/fs-write-stream-atomic/test/basic.js97
-rw-r--r--node_modules/fs-write-stream-atomic/test/chown.js44
-rw-r--r--node_modules/fs-write-stream-atomic/test/rename-eperm.js154
-rw-r--r--node_modules/fs-write-stream-atomic/test/rename-fail.js30
-rw-r--r--node_modules/fs-write-stream-atomic/test/slow-close.js40
-rw-r--r--node_modules/fs-write-stream-atomic/test/toolong.js29
6 files changed, 394 insertions, 0 deletions
diff --git a/node_modules/fs-write-stream-atomic/test/basic.js b/node_modules/fs-write-stream-atomic/test/basic.js
new file mode 100644
index 0000000..d0205e1
--- /dev/null
+++ b/node_modules/fs-write-stream-atomic/test/basic.js
@@ -0,0 +1,97 @@
+var fs = require('graceful-fs')
+var test = require('tap').test
+var path = require('path')
+var writeStream = require('../index.js')
+
+var rename = fs.rename
+fs.rename = function (from, to, cb) {
+ setTimeout(function () {
+ rename(from, to, cb)
+ }, 100)
+}
+
+test('basic', function (t) {
+ // open 10 write streams to the same file.
+ // then write to each of them, and to the target
+ // and verify at the end that each of them does their thing
+ var target = path.resolve(__dirname, 'test.txt')
+ var n = 10
+
+ // We run all of our assertions twice:
+ // once for finish, once for close
+ // There are 6 assertions, two fixed, plus 4 lines in the file.
+ t.plan(n * 2 * 6)
+
+ var streams = []
+ for (var i = 0; i < n; i++) {
+ var s = writeStream(target)
+ s.on('finish', verifier('finish', i))
+ s.on('close', verifier('close', i))
+ streams.push(s)
+ }
+
+ function verifier (ev, num) {
+ return function () {
+ if (ev === 'close') {
+ t.equal(this.__emittedFinish, true, num + '. closed only after finish')
+ } else {
+ this.__emittedFinish = true
+ t.equal(ev, 'finish', num + '. finished')
+ }
+
+ // make sure that one of the atomic streams won.
+ var res = fs.readFileSync(target, 'utf8')
+ var lines = res.trim().split(/\n/)
+ lines.forEach(function (line, lineno) {
+ var first = lines[0].match(/\d+$/)[0]
+ var cur = line.match(/\d+$/)[0]
+ t.equal(cur, first, num + '. line ' + lineno + ' matches')
+ })
+
+ var resExpr = /^first write \d+\nsecond write \d+\nthird write \d+\nfinal write \d+\n$/
+ t.similar(res, resExpr, num + '. content matches')
+ }
+ }
+
+ // now write something to each stream.
+ streams.forEach(function (stream, i) {
+ stream.write('first write ' + i + '\n')
+ })
+
+ // wait a sec for those writes to go out.
+ setTimeout(function () {
+ // write something else to the target.
+ fs.writeFileSync(target, 'brutality!\n')
+
+ // write some more stuff.
+ streams.forEach(function (stream, i) {
+ stream.write('second write ' + i + '\n')
+ })
+
+ setTimeout(function () {
+ // Oops! Deleted the file!
+ fs.unlinkSync(target)
+
+ // write some more stuff.
+ streams.forEach(function (stream, i) {
+ stream.write('third write ' + i + '\n')
+ })
+
+ setTimeout(function () {
+ fs.writeFileSync(target, 'brutality TWO!\n')
+ streams.forEach(function (stream, i) {
+ stream.end('final write ' + i + '\n')
+ })
+ }, 50)
+ }, 50)
+ }, 50)
+})
+
+test('cleanup', function (t) {
+ fs.readdirSync(__dirname).filter(function (f) {
+ return f.match(/^test.txt/)
+ }).forEach(function (file) {
+ fs.unlinkSync(path.resolve(__dirname, file))
+ })
+ t.end()
+})
diff --git a/node_modules/fs-write-stream-atomic/test/chown.js b/node_modules/fs-write-stream-atomic/test/chown.js
new file mode 100644
index 0000000..1733cf2
--- /dev/null
+++ b/node_modules/fs-write-stream-atomic/test/chown.js
@@ -0,0 +1,44 @@
+'use strict'
+var fs = require('graceful-fs')
+var path = require('path')
+var test = require('tap').test
+var rimraf = require('rimraf')
+var writeStream = require('../index.js')
+
+var target = path.resolve(__dirname, 'test-chown')
+
+test('chown works', function (t) {
+ t.plan(1)
+ var stream = writeStream(target, {chown: {uid: process.getuid(), gid: process.getgid()}})
+ var hadError = false
+ stream.on('error', function (er) {
+ hadError = true
+ console.log('#', er)
+ })
+ stream.on('close', function () {
+ t.is(hadError, false, 'no errors before close')
+ })
+ stream.end()
+})
+
+test('chown fails', function (t) {
+ t.plan(1)
+ fs.chown = function (file, uid, gid, cb) {
+ cb(new Error('TEST BREAK'))
+ }
+ var stream = writeStream(target, {chown: {uid: process.getuid(), gid: process.getgid()}})
+ var hadError = false
+ stream.on('error', function (er) {
+ hadError = true
+ console.log('#', er)
+ })
+ stream.on('close', function () {
+ t.is(hadError, true, 'error before close')
+ })
+ stream.end()
+})
+
+test('cleanup', function (t) {
+ rimraf.sync(target)
+ t.end()
+})
diff --git a/node_modules/fs-write-stream-atomic/test/rename-eperm.js b/node_modules/fs-write-stream-atomic/test/rename-eperm.js
new file mode 100644
index 0000000..b1be0f3
--- /dev/null
+++ b/node_modules/fs-write-stream-atomic/test/rename-eperm.js
@@ -0,0 +1,154 @@
+'use strict'
+var fs = require('graceful-fs')
+var path = require('path')
+var test = require('tap').test
+var rimraf = require('rimraf')
+var writeStream = require('../index.js')
+
+var target = path.resolve(__dirname, 'test-rename-eperm1')
+var target2 = path.resolve(__dirname, 'test-rename-eperm2')
+var target3 = path.resolve(__dirname, 'test-rename-eperm3')
+
+test('rename eperm none existing file', function (t) {
+ t.plan(2)
+
+ var _rename = fs.rename
+ fs.existsSync = function (src) {
+ return true
+ }
+ fs.rename = function (src, dest, cb) {
+ // simulate a failure during rename where the file
+ // is renamed successfully but the process encounters
+ // an EPERM error and the target file does not exist
+ _rename(src, dest, function (e) {
+ var err = new Error('TEST BREAK')
+ err.syscall = 'rename'
+ err.code = 'EPERM'
+ cb(err)
+ })
+ }
+
+ var stream = writeStream(target, { isWin: true })
+ var hadError = false
+ var calledFinish = false
+ stream.on('error', function (er) {
+ hadError = true
+ console.log('#', er)
+ })
+ stream.on('finish', function () {
+ calledFinish = true
+ })
+ stream.on('close', function () {
+ t.is(hadError, true, 'error was caught')
+ t.is(calledFinish, false, 'finish was called before close')
+ })
+ stream.end()
+})
+
+// test existing file with diff. content
+test('rename eperm existing file different content', function (t) {
+ t.plan(2)
+
+ var _rename = fs.rename
+ fs.existsSync = function (src) {
+ return true
+ }
+ fs.rename = function (src, dest, cb) {
+ // simulate a failure during rename where the file
+ // is renamed successfully but the process encounters
+ // an EPERM error and the target file that has another content than the
+ // destination
+ _rename(src, dest, function (e) {
+ fs.writeFile(src, 'dest', function (writeErr) {
+ if (writeErr) {
+ return console.log('WRITEERR: ' + writeErr)
+ }
+
+ fs.writeFile(target2, 'target', function (writeErr) {
+ if (writeErr) {
+ return console.log('WRITEERR: ' + writeErr)
+ }
+
+ var err = new Error('TEST BREAK')
+ err.syscall = 'rename'
+ err.code = 'EPERM'
+ cb(err)
+ })
+ })
+ })
+ }
+
+ var stream = writeStream(target2, { isWin: true })
+ var hadError = false
+ var calledFinish = false
+ stream.on('error', function (er) {
+ hadError = true
+ console.log('#', er)
+ })
+ stream.on('finish', function () {
+ calledFinish = true
+ })
+ stream.on('close', function () {
+ t.is(hadError, true, 'error was caught')
+ t.is(calledFinish, false, 'finish was called before close')
+ })
+ stream.end()
+})
+
+// test existing file with the same content
+// test existing file with diff. content
+test('rename eperm existing file different content', function (t) {
+ t.plan(2)
+
+ var _rename = fs.rename
+ fs.existsSync = function (src) {
+ return true
+ }
+ fs.rename = function (src, dest, cb) {
+ // simulate a failure during rename where the file
+ // is renamed successfully but the process encounters
+ // an EPERM error and the target file that has the same content than the
+ // destination
+ _rename(src, dest, function (e) {
+ fs.writeFile(src, 'target2', function (writeErr) {
+ if (writeErr) {
+ return console.log('WRITEERR: ' + writeErr)
+ }
+
+ fs.writeFile(target3, 'target2', function (writeErr) {
+ if (writeErr) {
+ return console.log('WRITEERR: ' + writeErr)
+ }
+
+ var err = new Error('TEST BREAK')
+ err.syscall = 'rename'
+ err.code = 'EPERM'
+ cb(err)
+ })
+ })
+ })
+ }
+
+ var stream = writeStream(target3, { isWin: true })
+ var hadError = false
+ var calledFinish = false
+ stream.on('error', function (er) {
+ hadError = true
+ console.log('#', er)
+ })
+ stream.on('finish', function () {
+ calledFinish = true
+ })
+ stream.on('close', function () {
+ t.is(hadError, false, 'error was caught')
+ t.is(calledFinish, true, 'finish was called before close')
+ })
+ stream.end()
+})
+
+test('cleanup', function (t) {
+ rimraf.sync(target)
+ rimraf.sync(target2)
+ rimraf.sync(target3)
+ t.end()
+})
diff --git a/node_modules/fs-write-stream-atomic/test/rename-fail.js b/node_modules/fs-write-stream-atomic/test/rename-fail.js
new file mode 100644
index 0000000..7e27f0b
--- /dev/null
+++ b/node_modules/fs-write-stream-atomic/test/rename-fail.js
@@ -0,0 +1,30 @@
+'use strict'
+var fs = require('graceful-fs')
+var path = require('path')
+var test = require('tap').test
+var rimraf = require('rimraf')
+var writeStream = require('../index.js')
+
+var target = path.resolve(__dirname, 'test-rename')
+
+test('rename fails', function (t) {
+ t.plan(1)
+ fs.rename = function (src, dest, cb) {
+ cb(new Error('TEST BREAK'))
+ }
+ var stream = writeStream(target)
+ var hadError = false
+ stream.on('error', function (er) {
+ hadError = true
+ console.log('#', er)
+ })
+ stream.on('close', function () {
+ t.is(hadError, true, 'error before close')
+ })
+ stream.end()
+})
+
+test('cleanup', function (t) {
+ rimraf.sync(target)
+ t.end()
+})
diff --git a/node_modules/fs-write-stream-atomic/test/slow-close.js b/node_modules/fs-write-stream-atomic/test/slow-close.js
new file mode 100644
index 0000000..9840a6e
--- /dev/null
+++ b/node_modules/fs-write-stream-atomic/test/slow-close.js
@@ -0,0 +1,40 @@
+'use strict'
+var fs = require('graceful-fs')
+var path = require('path')
+var test = require('tap').test
+var rimraf = require('rimraf')
+var writeStream = require('../index.js')
+
+var target = path.resolve(__dirname, 'test-chown')
+
+test('slow close', function (t) {
+ t.plan(2)
+ // The goal here is to simulate the "file close" step happening so slowly
+ // that the whole close/rename process could finish before the file is
+ // actually closed (and thus buffers truely flushed to the OS). In
+ // previous versions of this module, this would result in the module
+ // emitting finish & close before the file was fully written and in
+ // turn, could break other layers that tried to read the new file.
+ var realEmit = fs.WriteStream.prototype.emit
+ var reallyClosed = false
+ fs.WriteStream.prototype.emit = function (event) {
+ if (event !== 'close') return realEmit.apply(this, arguments)
+ setTimeout(function () {
+ reallyClosed = true
+ realEmit.call(this, 'close')
+ }.bind(this), 200)
+ }
+ var stream = writeStream(target)
+ stream.on('finish', function () {
+ t.is(reallyClosed, true, "didn't finish before target was closed")
+ })
+ stream.on('close', function () {
+ t.is(reallyClosed, true, "didn't close before target was closed")
+ })
+ stream.end()
+})
+
+test('cleanup', function (t) {
+ rimraf.sync(target)
+ t.end()
+})
diff --git a/node_modules/fs-write-stream-atomic/test/toolong.js b/node_modules/fs-write-stream-atomic/test/toolong.js
new file mode 100644
index 0000000..f146cc5
--- /dev/null
+++ b/node_modules/fs-write-stream-atomic/test/toolong.js
@@ -0,0 +1,29 @@
+var path = require('path')
+var test = require('tap').test
+var writeStream = require('../index.js')
+
+function repeat (times, string) {
+ var output = ''
+ for (var ii = 0; ii < times; ++ii) {
+ output += string
+ }
+ return output
+}
+
+var target = path.resolve(__dirname, repeat(1000, 'test'))
+
+test('name too long', function (t) {
+ t.plan(2)
+ var stream = writeStream(target)
+ var hadError = false
+ stream.on('error', function (er) {
+ if (!hadError) {
+ t.is(er.code, 'ENAMETOOLONG', target.length + ' character name results in ENAMETOOLONG')
+ hadError = true
+ }
+ })
+ stream.on('close', function () {
+ t.ok(hadError, 'got error before close')
+ })
+ stream.end()
+})