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')
8 var target = path.resolve(__dirname, 'test-chown')
10 test('slow close', function (t) {
12 // The goal here is to simulate the "file close" step happening so slowly
13 // that the whole close/rename process could finish before the file is
14 // actually closed (and thus buffers truely flushed to the OS). In
15 // previous versions of this module, this would result in the module
16 // emitting finish & close before the file was fully written and in
17 // turn, could break other layers that tried to read the new file.
18 var realEmit = fs.WriteStream.prototype.emit
19 var reallyClosed = false
20 fs.WriteStream.prototype.emit = function (event) {
21 if (event !== 'close') return realEmit.apply(this, arguments)
22 setTimeout(function () {
24 realEmit.call(this, 'close')
27 var stream = writeStream(target)
28 stream.on('finish', function () {
29 t.is(reallyClosed, true, "didn't finish before target was closed")
31 stream.on('close', function () {
32 t.is(reallyClosed, true, "didn't close before target was closed")
37 test('cleanup', function (t) {