• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2var fs = require('graceful-fs')
3var path = require('path')
4var test = require('tap').test
5var rimraf = require('rimraf')
6var writeStream = require('../index.js')
7
8var target = path.resolve(__dirname, 'test-chown')
9
10test('slow close', function (t) {
11  t.plan(2)
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 () {
23      reallyClosed = true
24      realEmit.call(this, 'close')
25    }.bind(this), 200)
26  }
27  var stream = writeStream(target)
28  stream.on('finish', function () {
29    t.is(reallyClosed, true, "didn't finish before target was closed")
30  })
31  stream.on('close', function () {
32    t.is(reallyClosed, true, "didn't close before target was closed")
33  })
34  stream.end()
35})
36
37test('cleanup', function (t) {
38  rimraf.sync(target)
39  t.end()
40})
41