extract-move.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Set the umask, so that it works the same everywhere.
  2. process.umask(parseInt('22', 8))
  3. var tap = require("tap")
  4. , tar = require("../tar.js")
  5. , fs = require("fs")
  6. , gfs = require("graceful-fs")
  7. , path = require("path")
  8. , file = path.resolve(__dirname, "fixtures/dir.tar")
  9. , target = path.resolve(__dirname, "tmp/extract-test")
  10. , index = 0
  11. , fstream = require("fstream")
  12. , rimraf = require("rimraf")
  13. , mkdirp = require("mkdirp")
  14. , ee = 0
  15. , expectEntries = [
  16. {
  17. "path" : "dir/",
  18. "mode" : "750",
  19. "type" : "5",
  20. "depth" : undefined,
  21. "size" : 0,
  22. "linkpath" : "",
  23. "nlink" : undefined,
  24. "dev" : undefined,
  25. "ino" : undefined
  26. },
  27. {
  28. "path" : "dir/sub/",
  29. "mode" : "750",
  30. "type" : "5",
  31. "depth" : undefined,
  32. "size" : 0,
  33. "linkpath" : "",
  34. "nlink" : undefined,
  35. "dev" : undefined,
  36. "ino" : undefined
  37. } ]
  38. function slow (fs, method, t1, t2) {
  39. var orig = fs[method]
  40. if (!orig) return null
  41. fs[method] = function () {
  42. var args = [].slice.call(arguments)
  43. console.error("slow", method, args[0])
  44. var cb = args.pop()
  45. setTimeout(function () {
  46. orig.apply(fs, args.concat(function(er, data) {
  47. setTimeout(function() {
  48. cb(er, data)
  49. }, t2)
  50. }))
  51. }, t1)
  52. }
  53. }
  54. // Make sure we get the graceful-fs that fstream is using.
  55. var gfs2
  56. try {
  57. gfs2 = require("fstream/node_modules/graceful-fs")
  58. } catch (er) {}
  59. var slowMethods = ["chown", "chmod", "utimes", "lutimes"]
  60. slowMethods.forEach(function (method) {
  61. var t1 = 500
  62. var t2 = 0
  63. slow(fs, method, t1, t2)
  64. slow(gfs, method, t1, t2)
  65. if (gfs2) {
  66. slow(gfs2, method, t1, t2)
  67. }
  68. })
  69. // The extract class basically just pipes the input
  70. // to a Reader, and then to a fstream.DirWriter
  71. // So, this is as much a test of fstream.Reader and fstream.Writer
  72. // as it is of tar.Extract, but it sort of makes sense.
  73. tap.test("preclean", function (t) {
  74. rimraf.sync(target)
  75. /mkdirp.sync(target)
  76. t.pass("cleaned!")
  77. t.end()
  78. })
  79. tap.test("extract test", function (t) {
  80. var extract = tar.Extract(target)
  81. var inp = fs.createReadStream(file)
  82. // give it a weird buffer size to try to break in odd places
  83. inp.bufferSize = 1234
  84. inp.pipe(extract)
  85. extract.on("end", function () {
  86. rimraf.sync(target)
  87. t.equal(ee, expectEntries.length, "should see "+ee+" entries")
  88. // should get no more entries after end
  89. extract.removeAllListeners("entry")
  90. extract.on("entry", function (e) {
  91. t.fail("Should not get entries after end!")
  92. })
  93. t.end()
  94. })
  95. extract.on("entry", function (entry) {
  96. var found =
  97. { path: entry.path
  98. , mode: entry.props.mode.toString(8)
  99. , type: entry.props.type
  100. , depth: entry.props.depth
  101. , size: entry.props.size
  102. , linkpath: entry.props.linkpath
  103. , nlink: entry.props.nlink
  104. , dev: entry.props.dev
  105. , ino: entry.props.ino
  106. }
  107. var wanted = expectEntries[ee ++]
  108. t.equivalent(found, wanted, "tar entry " + ee + " " + wanted.path)
  109. })
  110. })