rename-eperm.js 4.1 KB
Newer Older
liang ce committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 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()
})