index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. var tar = require('../')
  2. var path = require('path')
  3. var rfile = require('rfile')
  4. var rimraf = require('rimraf').sync
  5. var mkdir = require('mkdirp').sync
  6. var read = require('fs').createReadStream
  7. var write = require('fs').createWriteStream
  8. var assert = require('assert')
  9. beforeEach(function () {
  10. rimraf(__dirname + '/output')
  11. })
  12. afterEach(function () {
  13. rimraf(__dirname + '/output')
  14. })
  15. describe('tarball.pipe(unpack(directory, callback))', function () {
  16. it('unpacks the tarball into the directory', function (done) {
  17. read(__dirname + '/fixtures/packed.tar').pipe(tar.unpack(__dirname + '/output/unpacked', function (err) {
  18. if (err) return done(err)
  19. assert.equal(rfile('./output/unpacked/bar.txt'), rfile('./fixtures/to-pack/bar.txt'))
  20. assert.equal(rfile('./output/unpacked/foo.txt'), rfile('./fixtures/to-pack/foo.txt'))
  21. done()
  22. }))
  23. })
  24. it('unpacks the tarball into the directory without deleting existing files', function (done) {
  25. read(__dirname + '/fixtures/packed-file.txt').pipe(tar.unpack(__dirname + '/output/unpacked', function (err) {
  26. if (err) return done(err)
  27. read(__dirname + '/fixtures/packed.tar').pipe(tar.unpack(__dirname + '/output/unpacked', {keepFiles: true}, function (err) {
  28. if (err) return done(err)
  29. assert.equal(rfile('./output/unpacked/index.js'), rfile('./fixtures/packed-file.txt'))
  30. done()
  31. }))
  32. }))
  33. })
  34. })
  35. describe('tarball.pipe(unpack(directory, { strip: 0 }, callback))', function () {
  36. it('unpacks the tarball into the directory with subdir package', function (done) {
  37. read(__dirname + '/fixtures/packed.tar').pipe(tar.unpack(__dirname + '/output/unpacked', { strip: 0 } , function (err) {
  38. if (err) return done(err)
  39. assert.equal(rfile('./output/unpacked/package/bar.txt'), rfile('./fixtures/to-pack/bar.txt'))
  40. assert.equal(rfile('./output/unpacked/package/foo.txt'), rfile('./fixtures/to-pack/foo.txt'))
  41. done()
  42. }))
  43. })
  44. })
  45. describe('gziptarball.pipe(unpack(directory, callback))', function () {
  46. it('unpacks the tarball into the directory', function (done) {
  47. read(__dirname + '/fixtures/packed.tar.gz').pipe(tar.unpack(__dirname + '/output/unpacked', function (err) {
  48. if (err) return done(err)
  49. assert.equal(rfile('./output/unpacked/bar.txt'), rfile('./fixtures/to-pack/bar.txt'))
  50. assert.equal(rfile('./output/unpacked/foo.txt'), rfile('./fixtures/to-pack/foo.txt'))
  51. done()
  52. }))
  53. })
  54. })
  55. describe('file.pipe(unpack(directory, callback))', function () {
  56. it('copies the file into the directory', function (done) {
  57. read(__dirname + '/fixtures/packed-file.txt').pipe(tar.unpack(__dirname + '/output/unpacked', function (err) {
  58. if (err) return done(err)
  59. assert.equal(rfile('./output/unpacked/index.js'), rfile('./fixtures/packed-file.txt'))
  60. done()
  61. }))
  62. })
  63. })
  64. describe('pack(directory).pipe(tarball)', function () {
  65. it('packs the directory into the output', function (done) {
  66. var called = false
  67. mkdir(__dirname + '/output/')
  68. tar.pack(__dirname + '/fixtures/to-pack').pipe(write(__dirname + '/output/packed.tar.gz'))
  69. .on('error', function (err) {
  70. if (called) return
  71. called = true
  72. done(err)
  73. })
  74. .on('close', function () {
  75. if (called) return
  76. called = true
  77. read(__dirname + '/output/packed.tar.gz').pipe(tar.unpack(__dirname + '/output/unpacked', function (err) {
  78. if (err) return done(err)
  79. assert.equal(rfile('./output/unpacked/bar.txt'), rfile('./fixtures/to-pack/bar.txt'))
  80. assert.equal(rfile('./output/unpacked/foo.txt'), rfile('./fixtures/to-pack/foo.txt'))
  81. done()
  82. }))
  83. })
  84. })
  85. })
  86. describe('pack(directory, { fromBase: true }).pipe(tarball)', function () {
  87. it('packs the directory with input dir as base dir', function (done) {
  88. var called = false
  89. mkdir(__dirname + '/output/')
  90. tar.pack(__dirname + '/fixtures/to-pack', { fromBase: true }).pipe(write(__dirname + '/output/packed.tar.gz'))
  91. .on('error', function (err) {
  92. if (called) return
  93. called = true
  94. done(err)
  95. })
  96. .on('close', function () {
  97. if (called) return
  98. called = true
  99. read(__dirname + '/output/packed.tar.gz').pipe(tar.unpack(__dirname + '/output/unpacked', { strip: 0 }, function (err) {
  100. if (err) return done(err)
  101. assert.equal(rfile('./output/unpacked/bar.txt'), rfile('./fixtures/to-pack/bar.txt'))
  102. assert.equal(rfile('./output/unpacked/foo.txt'), rfile('./fixtures/to-pack/foo.txt'))
  103. done()
  104. }))
  105. })
  106. })
  107. })