Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

rename-eperm.js 4.1KB

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