index.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. var test = require('tape')
  2. var rimraf = require('rimraf')
  3. var tar = require('../index')
  4. var tarStream = require('tar-stream')
  5. var path = require('path')
  6. var fs = require('fs')
  7. var os = require('os')
  8. var win32 = os.platform() === 'win32'
  9. var mtime = function (st) {
  10. return Math.floor(st.mtime.getTime() / 1000)
  11. }
  12. test('copy a -> copy/a', function (t) {
  13. t.plan(5)
  14. var a = path.join(__dirname, 'fixtures', 'a')
  15. var b = path.join(__dirname, 'fixtures', 'copy', 'a')
  16. rimraf.sync(b)
  17. tar.pack(a)
  18. .pipe(tar.extract(b))
  19. .on('finish', function () {
  20. var files = fs.readdirSync(b)
  21. t.same(files.length, 1)
  22. t.same(files[0], 'hello.txt')
  23. var fileB = path.join(b, files[0])
  24. var fileA = path.join(a, files[0])
  25. t.same(fs.readFileSync(fileB, 'utf-8'), fs.readFileSync(fileA, 'utf-8'))
  26. t.same(fs.statSync(fileB).mode, fs.statSync(fileA).mode)
  27. t.same(mtime(fs.statSync(fileB)), mtime(fs.statSync(fileA)))
  28. })
  29. })
  30. test('copy b -> copy/b', function (t) {
  31. t.plan(8)
  32. var a = path.join(__dirname, 'fixtures', 'b')
  33. var b = path.join(__dirname, 'fixtures', 'copy', 'b')
  34. rimraf.sync(b)
  35. tar.pack(a)
  36. .pipe(tar.extract(b))
  37. .on('finish', function () {
  38. var files = fs.readdirSync(b)
  39. t.same(files.length, 1)
  40. t.same(files[0], 'a')
  41. var dirB = path.join(b, files[0])
  42. var dirA = path.join(a, files[0])
  43. t.same(fs.statSync(dirB).mode, fs.statSync(dirA).mode)
  44. t.same(mtime(fs.statSync(dirB)), mtime(fs.statSync(dirA)))
  45. t.ok(fs.statSync(dirB).isDirectory())
  46. var fileB = path.join(dirB, 'test.txt')
  47. var fileA = path.join(dirA, 'test.txt')
  48. t.same(fs.readFileSync(fileB, 'utf-8'), fs.readFileSync(fileA, 'utf-8'))
  49. t.same(fs.statSync(fileB).mode, fs.statSync(fileA).mode)
  50. t.same(mtime(fs.statSync(fileB)), mtime(fs.statSync(fileA)))
  51. })
  52. })
  53. test('symlink', function (t) {
  54. if (win32) { // no symlink support on win32 currently. TODO: test if this can be enabled somehow
  55. t.plan(1)
  56. t.ok(true)
  57. return
  58. }
  59. t.plan(5)
  60. var a = path.join(__dirname, 'fixtures', 'c')
  61. rimraf.sync(path.join(a, 'link'))
  62. fs.symlinkSync('.gitignore', path.join(a, 'link'))
  63. var b = path.join(__dirname, 'fixtures', 'copy', 'c')
  64. rimraf.sync(b)
  65. tar.pack(a)
  66. .pipe(tar.extract(b))
  67. .on('finish', function () {
  68. var files = fs.readdirSync(b).sort()
  69. t.same(files.length, 2)
  70. t.same(files[0], '.gitignore')
  71. t.same(files[1], 'link')
  72. var linkA = path.join(a, 'link')
  73. var linkB = path.join(b, 'link')
  74. t.same(mtime(fs.lstatSync(linkB)), mtime(fs.lstatSync(linkA)))
  75. t.same(fs.readlinkSync(linkB), fs.readlinkSync(linkA))
  76. })
  77. })
  78. test('follow symlinks', function (t) {
  79. if (win32) { // no symlink support on win32 currently. TODO: test if this can be enabled somehow
  80. t.plan(1)
  81. t.ok(true)
  82. return
  83. }
  84. t.plan(5)
  85. var a = path.join(__dirname, 'fixtures', 'c')
  86. rimraf.sync(path.join(a, 'link'))
  87. fs.symlinkSync('.gitignore', path.join(a, 'link'))
  88. var b = path.join(__dirname, 'fixtures', 'copy', 'c-dereference')
  89. rimraf.sync(b)
  90. tar.pack(a, { dereference: true })
  91. .pipe(tar.extract(b))
  92. .on('finish', function () {
  93. var files = fs.readdirSync(b).sort()
  94. t.same(files.length, 2)
  95. t.same(files[0], '.gitignore')
  96. t.same(files[1], 'link')
  97. var file1 = path.join(b, '.gitignore')
  98. var file2 = path.join(b, 'link')
  99. t.same(mtime(fs.lstatSync(file1)), mtime(fs.lstatSync(file2)))
  100. t.same(fs.readFileSync(file1), fs.readFileSync(file2))
  101. })
  102. })
  103. test('strip', function (t) {
  104. t.plan(2)
  105. var a = path.join(__dirname, 'fixtures', 'b')
  106. var b = path.join(__dirname, 'fixtures', 'copy', 'b-strip')
  107. rimraf.sync(b)
  108. tar.pack(a)
  109. .pipe(tar.extract(b, { strip: 1 }))
  110. .on('finish', function () {
  111. var files = fs.readdirSync(b).sort()
  112. t.same(files.length, 1)
  113. t.same(files[0], 'test.txt')
  114. })
  115. })
  116. test('strip + map', function (t) {
  117. t.plan(2)
  118. var a = path.join(__dirname, 'fixtures', 'b')
  119. var b = path.join(__dirname, 'fixtures', 'copy', 'b-strip')
  120. rimraf.sync(b)
  121. var uppercase = function (header) {
  122. header.name = header.name.toUpperCase()
  123. return header
  124. }
  125. tar.pack(a)
  126. .pipe(tar.extract(b, { strip: 1, map: uppercase }))
  127. .on('finish', function () {
  128. var files = fs.readdirSync(b).sort()
  129. t.same(files.length, 1)
  130. t.same(files[0], 'TEST.TXT')
  131. })
  132. })
  133. test('map + dir + permissions', function (t) {
  134. t.plan(win32 ? 1 : 2) // skip chmod test, it's not working like unix
  135. var a = path.join(__dirname, 'fixtures', 'b')
  136. var b = path.join(__dirname, 'fixtures', 'copy', 'a-perms')
  137. rimraf.sync(b)
  138. var aWithMode = function (header) {
  139. if (header.name === 'a') {
  140. header.mode = parseInt(700, 8)
  141. }
  142. return header
  143. }
  144. tar.pack(a)
  145. .pipe(tar.extract(b, { map: aWithMode }))
  146. .on('finish', function () {
  147. var files = fs.readdirSync(b).sort()
  148. var stat = fs.statSync(path.join(b, 'a'))
  149. t.same(files.length, 1)
  150. if (!win32) {
  151. t.same(stat.mode & parseInt(777, 8), parseInt(700, 8))
  152. }
  153. })
  154. })
  155. test('specific entries', function (t) {
  156. t.plan(6)
  157. var a = path.join(__dirname, 'fixtures', 'd')
  158. var b = path.join(__dirname, 'fixtures', 'copy', 'd-entries')
  159. var entries = ['file1', 'sub-files/file3', 'sub-dir']
  160. rimraf.sync(b)
  161. tar.pack(a, { entries: entries })
  162. .pipe(tar.extract(b))
  163. .on('finish', function () {
  164. var files = fs.readdirSync(b)
  165. t.same(files.length, 3)
  166. t.notSame(files.indexOf('file1'), -1)
  167. t.notSame(files.indexOf('sub-files'), -1)
  168. t.notSame(files.indexOf('sub-dir'), -1)
  169. var subFiles = fs.readdirSync(path.join(b, 'sub-files'))
  170. t.same(subFiles, ['file3'])
  171. var subDir = fs.readdirSync(path.join(b, 'sub-dir'))
  172. t.same(subDir, ['file5'])
  173. })
  174. })
  175. test('check type while mapping header on packing', function (t) {
  176. t.plan(3)
  177. var e = path.join(__dirname, 'fixtures', 'e')
  178. var checkHeaderType = function (header) {
  179. if (header.name.indexOf('.') === -1) t.same(header.type, header.name)
  180. }
  181. tar.pack(e, { map: checkHeaderType })
  182. })
  183. test('finish callbacks', function (t) {
  184. t.plan(3)
  185. var a = path.join(__dirname, 'fixtures', 'a')
  186. var b = path.join(__dirname, 'fixtures', 'copy', 'a')
  187. rimraf.sync(b)
  188. var packEntries = 0
  189. var extractEntries = 0
  190. var countPackEntry = function (header) { packEntries++ }
  191. var countExtractEntry = function (header) { extractEntries++ }
  192. var pack
  193. var onPackFinish = function (passedPack) {
  194. t.equal(packEntries, 2, 'All entries have been packed') // 2 entries - the file and base directory
  195. t.equal(passedPack, pack, 'The finish hook passes the pack')
  196. }
  197. var onExtractFinish = function () { t.equal(extractEntries, 2) }
  198. pack = tar.pack(a, { map: countPackEntry, finish: onPackFinish })
  199. pack.pipe(tar.extract(b, { map: countExtractEntry, finish: onExtractFinish }))
  200. .on('finish', function () {
  201. t.end()
  202. })
  203. })
  204. test('not finalizing the pack', function (t) {
  205. t.plan(2)
  206. var a = path.join(__dirname, 'fixtures', 'a')
  207. var b = path.join(__dirname, 'fixtures', 'b')
  208. var out = path.join(__dirname, 'fixtures', 'copy', 'merged-packs')
  209. rimraf.sync(out)
  210. var prefixer = function (prefix) {
  211. return function (header) {
  212. header.name = path.join(prefix, header.name)
  213. return header
  214. }
  215. }
  216. tar.pack(a, {
  217. map: prefixer('a-files'),
  218. finalize: false,
  219. finish: packB
  220. })
  221. function packB (pack) {
  222. tar.pack(b, { pack: pack, map: prefixer('b-files') })
  223. .pipe(tar.extract(out))
  224. .on('finish', assertResults)
  225. }
  226. function assertResults () {
  227. var containers = fs.readdirSync(out)
  228. t.deepEqual(containers, ['a-files', 'b-files'])
  229. var aFiles = fs.readdirSync(path.join(out, 'a-files'))
  230. t.deepEqual(aFiles, ['hello.txt'])
  231. }
  232. })
  233. test('do not extract invalid tar', function (t) {
  234. var a = path.join(__dirname, 'fixtures', 'invalid.tar')
  235. var out = path.join(__dirname, 'fixtures', 'invalid')
  236. rimraf.sync(out)
  237. fs.createReadStream(a)
  238. .pipe(tar.extract(out))
  239. .on('error', function (err) {
  240. t.ok(/is not a valid path/i.test(err.message))
  241. fs.stat(path.join(out, '../bar'), function (err) {
  242. t.ok(err)
  243. t.end()
  244. })
  245. })
  246. })
  247. test('no abs hardlink targets', function (t) {
  248. var out = path.join(__dirname, 'fixtures', 'invalid')
  249. var outside = path.join(__dirname, 'fixtures', 'outside')
  250. rimraf.sync(out)
  251. var s = tarStream.pack()
  252. fs.writeFileSync(outside, 'something')
  253. s.entry({
  254. type: 'link',
  255. name: 'link',
  256. linkname: outside
  257. })
  258. s.entry({
  259. name: 'link'
  260. }, 'overwrite')
  261. s.finalize()
  262. s.pipe(tar.extract(out))
  263. .on('error', function (err) {
  264. t.ok(err, 'had error')
  265. fs.readFile(outside, 'utf-8', function (err, str) {
  266. t.error(err, 'no error')
  267. t.same(str, 'something')
  268. t.end()
  269. })
  270. })
  271. })